summaryrefslogtreecommitdiffstats
path: root/protocol.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-08-29 21:59:01 +0200
committerJan Pokorný <jpokorny@redhat.com>2014-08-29 23:39:12 +0200
commit8383fb1342bf639234f5fc1ee6534f055a558bee (patch)
tree47667fdd68db3d511969d579cc1803a6159cc7ff /protocol.py
parent7534c5f0c5a7da2d4f684129639de41fb0772a18 (diff)
downloadclufter-8383fb1342bf639234f5fc1ee6534f055a558bee.tar.gz
clufter-8383fb1342bf639234f5fc1ee6534f055a558bee.tar.xz
clufter-8383fb1342bf639234f5fc1ee6534f055a558bee.zip
(Optionally) promote protocols to first-class entity
+ reflect that on the formats' level (also bring a convention for that) + make plugin_registry.probe fnc more tolerant towards non-classes as we want to differentiate between protocol on instance basis rather than based on dedicated classes (because we want to pragmatically maintain interchangeability with plain strings!) + make extra steps in protocol.py so the mentioned interchangeability actually works Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'protocol.py')
-rw-r--r--protocol.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/protocol.py b/protocol.py
new file mode 100644
index 0000000..54c81d4
--- /dev/null
+++ b/protocol.py
@@ -0,0 +1,41 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Base protocol stuff (metaclass, etc.)"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+import logging
+
+from .plugin_registry import PluginRegistry
+from .utils import tuplist
+from .utils_prog import cli_undecor
+
+log = logging.getLogger(__name__)
+
+
+class protocols(PluginRegistry):
+ """Protocol registry (to be used as a metaclass for filters)
+
+ To be noted, this harness is solely optional, and only good to allow
+ early discovery of the typos in the protocols and to maintain some
+ in-code documentation of their usage as opposed to much further
+ in the processing with some files already successfully produced, etc.
+ """
+ @classmethod
+ def register(registry, pr):
+ # undecor to pass the checks in probe
+ return registry.probe(cli_undecor(pr),
+ pr if isinstance(pr, Protocol) else Protocol(pr))
+
+
+class Protocol(str):
+ """Class intended to be (exceptionally) instantioned (enhanced string)"""
+ __metaclass__ = protocols
+
+ def __new__(cls, *args, **kwargs):
+ ret = super(Protocol, cls).__new__(cls, *args, **kwargs)
+ return protocols.register(ret)
+
+ def ensure_proto(self, value):
+ return value if tuplist(value) else (self, value)