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,
|
||||
"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):
|
||||
txnformat = self._canonicalize_transaction_types(txnformat)
|
||||
|
|
@ -574,6 +581,17 @@ class FileStorage:
|
|||
except KeyError:
|
||||
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
|
||||
def _canonicalize_transaction_types(types):
|
||||
if types is None:
|
||||
|
|
@ -586,23 +604,13 @@ class FileStorage:
|
|||
stypes.discard("hash")
|
||||
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
|
||||
def transaction_types():
|
||||
return list(sorted(FileStorage._get_transaction_types().keys()))
|
||||
return list(sorted(FileStorage.HOOKS.keys()))
|
||||
|
||||
@staticmethod
|
||||
def _make_transaction_store(items, options):
|
||||
txntypes = FileStorage._get_transaction_types()
|
||||
txntypes = FileStorage.HOOKS
|
||||
if items is None or items == "":
|
||||
return Hook()
|
||||
elif isinstance(items, str):
|
||||
|
|
|
|||
Loading…
Reference in a new issue