diff options
| author | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2008-02-12 19:31:30 +0100 |
|---|---|---|
| committer | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2008-02-12 19:31:30 +0100 |
| commit | 22e977e95ca5d82b8984cde3aea3bc7662381a98 (patch) | |
| tree | e09c37991bdbaf90c0de1f63aa885f4a5b1ce35e | |
| parent | 3537fcb0b24e30c3621c94783b249005e9a8fa99 (diff) | |
Working plugin architecture
Even if the plugins don't work yet :)
| -rw-r--r-- | nbb/nbb_lib.in | 118 |
1 files changed, 89 insertions, 29 deletions
diff --git a/nbb/nbb_lib.in b/nbb/nbb_lib.in index d226fa0..4b5710d 100644 --- a/nbb/nbb_lib.in +++ b/nbb/nbb_lib.in @@ -3,65 +3,124 @@ import os from subprocess import Popen import subprocess -vc_srctree_classes = [] - -class FooMeta(type): - def __init__(self, name, bases, attrs): - print ("FooMeta.__init__: name=%s, bases=%s, attrs=%s" - % (name, bases, attrs)) - cls = super(FooMeta, self).__init__(name, bases, attrs) - if attrs['platform'] is not None: - # register_foo_implementation(cls.platform, cls) - vc_srctree_classes.append(cls) - return cls + +######################################################################## +# Utility functions +######################################################################## + + +def prog_stdout(call_list): + p = Popen(call_list, stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + return stdout.strip() + + +######################################################################## +# VCS Source Tree plugin system +######################################################################## + +class NotASourceTree(Exception): + pass + +class VCSourceTreeMeta(type): + vc_srctree_classes = [] + def __init__(cls, name, bases, attrs): + if not hasattr(cls, 'plugins'): + # This branch only executes when processing the mount point itself. + # So, since this is a new plugin type, not an implementation, this + # class shouldn't be registered as a plugin. Instead, it sets up a + # list where plugins can be registered later. + cls.plugins = [] + else: + # This must be a plugin implementation, which should be registered. + # Simply appending it to the list is all that's needed to keep + # track of it later. + cls.plugins.append(cls) + class VCSourceTree(object): - __metaclass__ = FooMeta - platform = None - def __init__(self, srcdir): - def check_class(cls): + """ + Mount point for plugins which refer to actions that can be performed. + + Plugins implementing this reference should provide the following + interface: + + vcs_name attribute + The text to be displayed, describing the version control system + __init__ function + Must raise NotASourceTree() if it is not a VCS source tree + """ + __metaclass__ = VCSourceTreeMeta + + def detect(cls, srcdir): + def check_class(klass): try: - return cls(srcdir) - except NotASourceTree(): + return klass(srcdir) + except NotASourceTree: return None - matches = filter(check_class, vc_srctree_classes) + if len(VCSourceTree.plugins) < 1: + raise "No VC source tree classes registered" + matches = filter(check_class, VCSourceTree.plugins) if len(matches) > 1: raise ("More than one source tree type detected for '%s'" % (srcdir,)) elif len(matches) < 1: raise "Source tree type for '%s' not detected" % (srcdir,) return matches[0] + detect = classmethod(detect) -def prog_stdout(call_list): - p = Popen(call_list, stdout=subprocess.PIPE) - stdout, stderr = p.communicate() - return stdout.strip() -class NotASourceTree(Exception): - pass +######################################################################## +# VCS Source Tree plugins +######################################################################## class GitSourceTree(VCSourceTree): - platform = 'git' + + vcs_name = 'git' + def __init__(self, srcdir): if "true" != prog_stdout(["git", "rev-parse", "--is-inside-work-tree"]): - raise "Not a git sourcetree" - self.top_srcdir = os.path.join(srcdir, FIXME) + raise NotASourceTree() + self.top_srcdir = os.path.join(srcdir, '.') # FIXME) + + def __str__(self): + return "%s(%s)" % (self.__class__.__name__, self.top_srcdir) + class BzrSourceTree(VCSourceTree): - platform = 'bzr' + + vcs_name = 'bzr' + def __init__(self, srcdir): try: import bzrlib.workingtree wt,b = bzrlib.workingtree.WorkingTree.open_containing(".") + except bzrlib.errors.NotBranchError: + raise NotASourceTree() except ImportError: raise NotASourceTree() self.wt = wt + print "wt:", wt + print "b:", b + print "wt:", dir(wt) + print "b:", dir(b) + + def __str__(self): + return "%s(%s)" % (self.__class__.__name__, self.top_srcdir) + + +######################################################################## +# Main program +######################################################################## class NBB(object): def __init__(self): srcdir = os.getcwd() - self.sourcetree = VCSourceTree(srcdir) + self.sourcetree = VCSourceTree.detect(srcdir) + print "sourcetree: %s" % self.sourcetree + print "sourcetree:", dir(self.sourcetree) + def main(argv): prog = argv[0] @@ -79,6 +138,7 @@ def main(argv): pass nbb = NBB() + if __name__ == '__main__': raise "This is not a library" |
