diff options
| author | Yaakov M. Nemoy <loupgaroublond@gmail.com> | 2009-01-15 18:08:22 -0500 |
|---|---|---|
| committer | Yaakov M. Nemoy <loupgaroublond@gmail.com> | 2009-01-15 18:08:22 -0500 |
| commit | 401c14d76893579ce2af6e7018ec4140a3541cf1 (patch) | |
| tree | c957fd77ac4ef2dbc80c2a90e3a2600c8f7b74bb /base | |
| parent | 6f22d71cf8218abff64773545456fa6084ed2096 (diff) | |
| download | fedora-devshell-401c14d76893579ce2af6e7018ec4140a3541cf1.tar.gz fedora-devshell-401c14d76893579ce2af6e7018ec4140a3541cf1.tar.xz fedora-devshell-401c14d76893579ce2af6e7018ec4140a3541cf1.zip | |
Convert Directory to a metaclassed object to handle registration with dirfactory and other administrative details
Diffstat (limited to 'base')
| -rw-r--r-- | base/dirfactory.py | 67 |
1 files changed, 67 insertions, 0 deletions
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 <ynemoy@redhat.com> +# + +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'] |
