Allow registering hooks.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
This commit is contained in:
parent
944c76d7b1
commit
20a5e43043
1 changed files with 20 additions and 12 deletions
|
|
@ -554,6 +554,13 @@ class FileStorage:
|
||||||
"yaml": YAMLFile,
|
"yaml": YAMLFile,
|
||||||
"raw": RawFile
|
"raw": RawFile
|
||||||
}
|
}
|
||||||
|
HOOKS = {
|
||||||
|
"git": GitHook,
|
||||||
|
"sha256": lambda *a, **k: HashHook("sha256", *a, **k),
|
||||||
|
"sha384": lambda *a, **k: HashHook("sha384", *a, **k),
|
||||||
|
"sha512": lambda *a, **k: HashHook("sha512", *a, **k),
|
||||||
|
"hash": lambda *a, **k: HashHook(None, *a, **k),
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self, fmt, filename, txnformat=None, options=None):
|
def __init__(self, fmt, filename, txnformat=None, options=None):
|
||||||
txnformat = self._canonicalize_transaction_types(txnformat)
|
txnformat = self._canonicalize_transaction_types(txnformat)
|
||||||
|
|
@ -574,6 +581,17 @@ class FileStorage:
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def register_hook(klass, name, obj):
|
||||||
|
klass.HOOKS[name] = obj
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def unregister_hook(klass, name):
|
||||||
|
try:
|
||||||
|
del klass.HOOKS[name]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _canonicalize_transaction_types(types):
|
def _canonicalize_transaction_types(types):
|
||||||
if types is None:
|
if types is None:
|
||||||
|
|
@ -586,23 +604,13 @@ class FileStorage:
|
||||||
stypes.discard("hash")
|
stypes.discard("hash")
|
||||||
return list(filter(lambda x: x in stypes, types))
|
return list(filter(lambda x: x in stypes, types))
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_transaction_types():
|
|
||||||
return {
|
|
||||||
"git": GitHook,
|
|
||||||
"sha256": lambda *a, **k: HashHook("sha256", *a, **k),
|
|
||||||
"sha384": lambda *a, **k: HashHook("sha384", *a, **k),
|
|
||||||
"sha512": lambda *a, **k: HashHook("sha512", *a, **k),
|
|
||||||
"hash": lambda *a, **k: HashHook(None, *a, **k),
|
|
||||||
}
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def transaction_types():
|
def transaction_types():
|
||||||
return list(sorted(FileStorage._get_transaction_types().keys()))
|
return list(sorted(FileStorage.HOOKS.keys()))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _make_transaction_store(items, options):
|
def _make_transaction_store(items, options):
|
||||||
txntypes = FileStorage._get_transaction_types()
|
txntypes = FileStorage.HOOKS
|
||||||
if items is None or items == "":
|
if items is None or items == "":
|
||||||
return Hook()
|
return Hook()
|
||||||
elif isinstance(items, str):
|
elif isinstance(items, str):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue