Add FilesystemRegistry class and fix 'props' bug in constructors

oop-refactor
Thor 4 years ago
parent 700f7b7be6
commit e3738245c3
  1. 43
      src/zasd/filesystem.py

@ -1,4 +1,6 @@
filesystems = {} # Public API
''' Base filesystem classes and registration facilities '''
class Filesystem: class Filesystem:
''' Base filsystem class inherited by each filesystem implementation ''' ''' Base filsystem class inherited by each filesystem implementation '''
@ -8,7 +10,7 @@ class Filesystem:
self._name = name self._name = name
# Process arguments to extract all properties # Process arguments to extract all properties
props = process_args(props, kwprops) props = _process_args(props, kwprops)
# Delegate further initialisation to the subclass # Delegate further initialisation to the subclass
self.initialise(props) self.initialise(props)
@ -45,7 +47,7 @@ class Filesystem:
class Snapshot: class Snapshot:
''' Base snapshot class inherited by each filesystem implementation ''' ''' Base snapshot class inherited by each filesystem implementation '''
def __init__(self, filesystem, tag, *props, **kwprops): def __init__(self, filesystem, tag, props, **kwprops):
# pylint: disable=unused-argument # pylint: disable=unused-argument
# Set common properties # Set common properties
@ -53,7 +55,7 @@ class Snapshot:
self._tag = tag self._tag = tag
# Process arguments to extract all properties # Process arguments to extract all properties
props = process_args(props, kwprops) props = _process_args(props, kwprops)
# Delegate further initialisation to the subclass # Delegate further initialisation to the subclass
self.initialise(props) self.initialise(props)
@ -96,7 +98,30 @@ class Snapshot:
''' Delete this snapshot. No further method calls are made on the ''' Delete this snapshot. No further method calls are made on the
snapshot once it has been destroyed. ''' snapshot once it has been destroyed. '''
def process_args(props, kwprops): class FilesystemRegistry:
''' Class that keeps a global registry of available Filesystem classes '''
_filesystems = {}
@classmethod
def register(cls, name, klass):
''' Register a filesystem. If a filesystem needs to be configured by the
user, it must retrieve its configuration from a dictionary at
config['filesystems'][name]. '''
cls._filesystems[name] = klass
@classmethod
def all(cls):
''' Retrieve a {name: class} dictionary of all registered
filesystems '''
return dict(cls._filesystems)
#
# Private objects
_filesystems = {}
def _process_args(props, kwprops):
''' Merge 'props' and 'kwprops' into single dictionary ''' ''' Merge 'props' and 'kwprops' into single dictionary '''
if props: if props:
if not isinstance(props, dict): if not isinstance(props, dict):
@ -108,10 +133,4 @@ def process_args(props, kwprops):
# Update properties dictionary with keyword arguments # Update properties dictionary with keyword arguments
props.update(kwprops) props.update(kwprops)
return props return props
def register(name, klass):
''' Register a filesystem. If a filesystem needs to be configured by the
user, it must retrieve its configuration from a dictionary at
config['filesystems'][name]. '''
filesystems[name] = klass
Loading…
Cancel
Save