def __call__(self): if IDirectory.providedBy(self): dir_path = os.path.join(*self.fs_path) if os.path.exists(dir_path) and not os.path.isdir(dir_path): raise KeyError( 'Attempt to create a directory with name which already ' 'exists as file') try: os.mkdir(dir_path) except OSError as e: # Ignore ``already exists``. if e.errno != 17: raise e # pragma no cover # Change file system mode if set fs_mode = self.fs_mode if fs_mode is not None: os.chmod(dir_path, fs_mode) while self._deleted: name = self._deleted.pop() abs_path = os.path.join(*self.fs_path + [name]) if os.path.exists(abs_path): if os.path.isdir(abs_path): shutil.rmtree(abs_path) else: os.remove(abs_path) for name, target in self.items(): if IDirectory.providedBy(target): target() elif IFile.providedBy(target): target()
def __setitem__(self, name, value): if not name: raise KeyError('Empty key not allowed in directories') name = self._encode_name(name) if IFile.providedBy(value) or IDirectory.providedBy(value): if IDirectory.providedBy(value): value.backup = self.backup self.storage[name] = value objectEventNotify(FileAddedEvent(value)) return raise ValueError('Unknown child node.')
def _get_child_dirs(self): """Get the directly contained directory handlers. Returns (name, handler) tuples. """ return [(name, handler) for (name, handler) in self.items() if IDirectory.providedBy(handler)]
def __call__(self, source): super(PackageSyncer, self).__call__(source) if (IClass.providedBy(source) or IInterface.providedBy(source)) \ and IDirectory.providedBy(self.anchor) \ and (source.parent.stereotype('pyegg:pypackage') is not None \ or source.parent.stereotype('pyegg:pyegg') is not None): modulename = '%s.py' % IModuleNameChooser(source)() self.anchor[modulename] = Module() self.anchor = self.anchor[modulename] elif (IClass.providedBy(source) or IInterface.providedBy(source)) \ and IModule.providedBy(self.anchor) \ and (source.parent.stereotype('pyegg:pypackage') is not None \ or source.parent.stereotype('pyegg:pyegg') is not None): modulename = '%s.py' % IModuleNameChooser(source)() container = self.anchor.parent container[modulename] = Module() self.anchor = container[modulename] else: if source.parent is None: return target_node = read_target_node(source.parent, self.target) if not target_node: super(PackageSyncer, self).__call__(source) return if len(target_node.path) < len(self.anchor.path): self.anchor = target_node
def __setitem__(self, name, value): if name in self.keys(): msg = u"Node already exists: %s" % ('/'.join(self.path + [name])) raise ValueError(msg) if IFile.providedBy(value) \ or IDirectory.providedBy(value): super(Directory, self).__setitem__(name, value) objectEventNotify(FileAddedEvent(value))
def filepath(self): path = self.__name__ if path is None: raise Incomplete, u"Could not verify file path." if path.find(os.path.sep) != -1: return path if self.__parent__ is None or not IDirectory.providedBy(self.__parent__): raise Incomplete, u"Could not verify file path." return os.path.join(*self.path)
def __call__(self): if self.__parent__: if hasattr(self, '_from_root'): if not self._from_root: raise RuntimeError(u"Directory called but not on " "virtual root.") if IRoot.providedBy(self): setattr(self, '_from_root', True) if IDirectory.providedBy(self): self._mkdir() for name, target in self.items(): if IDirectory.providedBy(target): target() elif IFile.providedBy(target): if self.backup and os.path.exists(target.abspath): shutil.copyfile(target.abspath, target.abspath + '.bak') target() if IRoot.providedBy(self): setattr(self, '_from_root', False)
def filepath(self): path = self.__name__ if path is None: raise Incomplete, u"Could not verify file path." if path.find(os.path.sep) != -1: return path if self.__parent__ is None \ or not IDirectory.providedBy(self.__parent__): raise Incomplete, u"Could not verify file path." return os.path.join(*self.path)
def autoimport(self, source,target): """Takes classes with 'api' stereotype and imports them into the pyegg's __init__.py. """ targetob = read_target_node(source, target.target) if IModule.providedBy(targetob) or IDirectory.providedBy(targetob): init = targetob.parent['__init__.py'] else: init = targetob.parent.parent['__init__.py'] imps = Imports(init) imps.set(None, [[source.name, None]])
def __call__(self): if IDirectory.providedBy(self): dir_path = os.path.join(*self.fs_path) try: os.mkdir(dir_path) except OSError, e: # Ignore ``already exists``. if e.errno != 17: raise e # Change file system mode if set if self.fs_mode is not None: os.chmod(dir_path, self.fs_mode)
def __call__(self): if IDirectory.providedBy(self): dir_path = os.path.join(*self.fs_path) if os.path.exists(dir_path) and not os.path.isdir(dir_path): raise KeyError('Attempt to create a directory with name which ' 'already exists as file') try: os.mkdir(dir_path) except OSError, e: # Ignore ``already exists``. if e.errno != 17: raise e #pragma NO COVER # Change file system mode if set if self.fs_mode is not None: os.chmod(dir_path, self.fs_mode)
def __setitem__(self, name, value): if not name: raise KeyError('Empty key not allowed in directories') name = self._encode_name(name) if IFile.providedBy(value) or IDirectory.providedBy(value): self.storage[name] = value # XXX: This event is currently used in node.ext.zcml and # node.ext.python to trigger parsing. But this behavior # requires the event to be triggered on __getitem__ which is # actually not how life cycle events shall behave. Fix in # node.ext.zcml and node.ext.python, remove event notification # here, use node.behaviors.Lifecycle and suppress event # notification in self._create_child_by_factory objectEventNotify(FileAddedEvent(value)) return raise ValueError('Unknown child node.')
ignores = modtok.modules except ComponentLookupError, e: # no modules created with <<pymodule>> packages ignores = set() for name in directory.keys(): try: module = directory[name] except TypeError: #happens if the factory cannot be called without args (e.g. .pt) #in this case its no python file and doesnt have to be removed continue if not module.name.endswith('.py'): # XXX: thats perhaps not the perfect solution to sk continue if IDirectory.providedBy(module) \ or module.name == '__init__.py' \ or module in ignores: continue if len(module) > 1: continue if len(module): bl = module[module.keys()[0]] if IFunction.providedBy(bl): continue if bl.lines != as_comment(get_copyright(source)): continue # dont throw away templates if ITemplate.providedBy(module) and module.template: continue del module.parent[module.name]
class DirectoryStorage(DictStorage): fs_encoding = default('utf-8') fs_mode = default(None) backup = default(True) ignores = default(list()) default_file_factory = default(File) # XXX: rename later to file_factories, keep now as is for b/c reasons factories = default(dict()) @default @property def file_factories(self): # temporary, see above return self.factories @default @property def child_directory_factory(self): return Directory @default @property def fs_path(self): return self.path @finalize def __init__(self, name=None, parent=None, backup=False, factories=dict()): self.__name__ = name self.__parent__ = parent self.backup = backup # override file factories if given if factories: self.factories = factories self._deleted = list() @finalize @locktree def __call__(self): if IDirectory.providedBy(self): dir_path = os.path.join(*self.fs_path) try: os.mkdir(dir_path) except OSError, e: # Ignore ``already exists``. if e.errno != 17: raise e # Change file system mode if set if self.fs_mode is not None: os.chmod(dir_path, self.fs_mode) while self._deleted: name = self._deleted.pop() abspath = os.path.join(*self.fs_path + [name]) if os.path.exists(abspath): if os.path.isdir(abspath): shutil.rmtree(abspath) else: os.remove(abspath) bakpath = os.path.join(*self.fs_path + ['.%s.bak' % name]) if os.path.exists(bakpath): os.remove(bakpath) continue for name, target in self.items(): if IDirectory.providedBy(target): target() elif IFile.providedBy(target): target() # Use fs_path if provided by child, otherwise fallback to path # XXX: deprecate the fallback use of path if hasattr(target, 'fs_path'): fs_path = target.fs_path else: fs_path = target.path abspath = os.path.join(*fs_path) if self.backup and os.path.exists(abspath): bakpath = os.path.join(*target.fs_path[:-1] + ['.%s.bak' % target.name]) shutil.copyfile(abspath, bakpath)