From 401c14d76893579ce2af6e7018ec4140a3541cf1 Mon Sep 17 00:00:00 2001 From: "Yaakov M. Nemoy" Date: Thu, 15 Jan 2009 18:08:22 -0500 Subject: Convert Directory to a metaclassed object to handle registration with dirfactory and other administrative details --- base/dirfactory.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ modules/directory.py | 11 ++++++-- modules/dirfactory.py | 72 --------------------------------------------------- 3 files changed, 76 insertions(+), 74 deletions(-) create mode 100644 base/dirfactory.py delete mode 100644 modules/dirfactory.py diff --git a/base/dirfactory.py b/base/dirfactory.py new file mode 100644 index 0000000..e099197 --- /dev/null +++ b/base/dirfactory.py @@ -0,0 +1,67 @@ +# Fedora Developer Shell +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Library General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# Authors: Yaakov M. Nemoy +# + +from __future__ import with_statement + +from configobj import ConfigObj +from os import getcwd +from os.path import abspath, exists + +from base.util import pwd, log +from base.exceptions import ExecutionException + +directory_type = dict() + +def register(cls, name): + global directory_type + directory_type[name] = cls + +class DirFactory(object): + '''creates a new object of type defined by a directory's .devshell file''' + def __new__(cls, name=None): + if not name: + log.debug('no name with dirfactory') + dir = getcwd() + type = whatis_sysdir(dir) + else: + log.debug('dirfactory.new with name ' + name) + dir = abspath(name) + if not exists(dir): + type = 'directory' + else: + type = whatis_sysdir(dir) + try: + new_cls = directory_type[type] + except KeyError, e: + raise ExecutionException(e, 'the directory type %s is not supported by this installation of fedora-deveshell' % type.capitalize()) + foo = new_cls.__new__(new_cls, name) + foo.__init__(name) + return foo + +def whatis_sysdir(dir): + ''' given a dir, determine it's type''' + with pwd(dir): + cfg = ConfigObj('.devshell') + try: + type = cfg['type'] + log.debug('is type ' + type) + return type + except KeyError, e: + return 'directory' + +__all__ = ['DirFactory', 'register'] diff --git a/modules/directory.py b/modules/directory.py index 22cdec5..31c864c 100644 --- a/modules/directory.py +++ b/modules/directory.py @@ -19,6 +19,8 @@ from __future__ import with_statement +import base.dirfactory as dirfactory + from configobj import ConfigObj from os import makedirs, getcwd, listdir from os.path import abspath, join, split, splitext, basename, exists, dirname @@ -27,12 +29,17 @@ from base.base import log from base.module import Module from base.util import pwd, copytree -from modules.dirfactory import DirFactory +class MetaDirectory(type): + def __init__(cls, name, bases, attrs): + t = name.lower() + cls._type = t + dirfactory.register(cls, t) + class Directory(Module): '''a generic base class for any module that has to maintain state on the file system in a directory ''' - _type = 'directory' + __metaclass__ = MetaDirectory def __init__(self, name=None): ''' initializer diff --git a/modules/dirfactory.py b/modules/dirfactory.py deleted file mode 100644 index 1c2e5a2..0000000 --- a/modules/dirfactory.py +++ /dev/null @@ -1,72 +0,0 @@ -# Fedora Developer Shell -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Library General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# -# Authors: Yaakov M. Nemoy -# - -from __future__ import with_statement - -from configobj import ConfigObj -from inspect import isclass -from os import getcwd -from os.path import abspath, exists - -from base.util import pwd, log -from base.module import Module -from modules.package import Package -from modules.directory import Directory -from modules.revisioncontrol import RevisionControl -from modules.sourceball import SourceBall -from modules.darcs import Darcs - -# TODO: i'm jumping through bureaucrautic hoops right now. -# Don't program at 3am kids, it's harmful to your sanity -foo = [] -for x,y in globals().copy().iteritems(): - foo.append([x,y]) -foo = dict(foo) -dirs = dict([[key.lower(), val] for key, val in foo.iteritems() if isclass(val) and issubclass(val, Directory)]) - -class DirFactory(Module): - '''creates a new object of type defined by a directory's .devshell file''' - def __new__(cls, name=None): - if not name: - log.debug('no name with dirfactory') - dir = getcwd() - type = whatis_sysdir(dir) - else: - log.debug('dirfactory.new with name ' + name) - dir = abspath(name) - if not exists(dir): - type = 'directory' - else: - type = whatis_sysdir(dir) - new_cls = dirs[type] - foo = new_cls.__new__(new_cls, name) - foo.__init__(name) - return foo - -def whatis_sysdir(dir): - ''' given a dir, determine it's type''' - with pwd(dir): - cfg = ConfigObj('.devshell') - try: - type = cfg['type'] - log.debug('is type ' + type) - return type - except KeyError, e: - return 'directory' - -__all__ = ['DirFactory'] -- cgit