def DataFactory(path, mimetype=None, **kwargs): path = Path(path) default_content = kwargs['default_content'] if 'default_content' in kwargs else None dtype = kwargs.pop('dtype', None) if path.isfile(): if default_content is not None: raise ValueError( "got multiple values for content (parameter and '%s')" % path.name) else: path, mimetype = arrange_data_args(path, mimetype, dtype) klass = DataClass(mimetype) return klass(path=path, mimetype=mimetype) elif path.exists(): raise ValueError("'%s' exists but is not a file" % path) elif not path.exists(): if default_content is None: default_content = b'' try: f = path.open('wb') except IOError: content = default_content else: f.write(default_content) f.close() content = None path, mimetype = arrange_data_args(path, mimetype, dtype) klass = DataClass(mimetype) return klass(path=path, mimetype=mimetype, content=content)
def DataFactory(path, mimetype=None, **kwargs): path = Path(path) default_content = kwargs[ 'default_content'] if 'default_content' in kwargs else None dtype = kwargs.pop('dtype', None) if path.isfile(): if default_content is not None: raise ValueError( "got multiple values for content (parameter and '%s')" % path.name) else: path, mimetype = arrange_data_args(path, mimetype, dtype) klass = DataClass(mimetype) return klass(path=path, mimetype=mimetype) elif path.exists(): raise ValueError("'%s' exists but is not a file" % path) elif not path.exists(): if default_content is None: default_content = b'' try: f = path.open('wb') except IOError: content = default_content else: f.write(default_content) f.close() content = None path, mimetype = arrange_data_args(path, mimetype, dtype) klass = DataClass(mimetype) return klass(path=path, mimetype=mimetype, content=content)
def qicon(filename): if filename is None: return QtGui.QIcon(get_shared_data('icons/oxygen_application-x-desktop.png')) if filename.startswith(':/'): return QtGui.QIcon(filename) else: path = Path(filename) if not path.isfile(): path = get_shared_data(filename) if path is None: path = get_shared_data('icons/%s' % filename) if path: return QtGui.QIcon(path) else: return QtGui.QIcon(":/images/resources/%s" % filename)
def arrange_path(path, path_class=Path): """ Return a Path, FilePath or DirPath dependings on path nature. Path is used for special path like device "files" or path not existing on disk. If path is empty, returns None. If path do not exists on disk or is not file nor directory (like /dev/xyz on linux),it return a path_class. """ if not path: return None path = Path(str(path)) if path.isfile(): return FilePath(path) elif path.isdir(): return DirPath(path) else: return path_class(path)
def arrange_path(path, path_class=Path): u""" Return a Path, FilePath or DirPath dependings on path nature. Path is used for special path like device "files" or path not existing on disk. If path is empty, returns None. If path do not exists on disk or is not file nor directory (like /dev/xyz on linux),it return a path_class. """ if not path: return None path = Path(unicode(path)) if path.isfile(): return FilePath(path) elif path.isdir(): return DirPath(path) else: return path_class(path)
class Data(object): mimetype = None default_name = 'Data' default_file_name = "filename.ext" pattern = "*.ext" extension = "ext" icon = "Crystal_Clear_app_kcmdf.png" def __init__(self, **kwargs): """ Classical use : *path* exists. Nothing is loaded in memory. Use :meth:`~Data.read` to get content """ # TODO: document args self.path = Path(kwargs.pop('path')) if 'path' in kwargs else None self._filename = Path(kwargs.pop('filename')).name if 'filename' in kwargs else None if self._filename is None and self.path is None: raise ValueError('path or filename required') if self._filename and self.path and self.path.name != self._filename: raise ValueError("path '%s' and filename '%s' are not compatible" % (self.path, self._filename)) self.dtype = kwargs.pop('dtype', None) self._content = kwargs.pop('content', None) self.mimetype = kwargs.pop('mimetype', None) def get_documentation(self): return "No documentation for %s" % self.filename def is_same_data(self, other): if self.exists() and other.exists(): return self.path == other.path elif not self.exists() and not other.exists(): return self._content == other._content else: return False def save(self): if self.path is None: raise ValueError('You must specify a path to be able to save data') if self._content is not None: with open(self.path, 'wb') as f: f.write(self._content) self._content = None def read(self): if self.exists(): with open(self.path, 'rb') as f: return f.read() else: return self._content def rename(self, new): pnew = Path(new) if pnew.isabs() or pnew.name != new: raise ValueError('You must give filename only, not path') new_path = self.path.parent / new self.move(new_path) def move(self, new_path): new_path = Path(new_path) if self._filename is not None: self._filename = new_path.name if self.path.isfile(): self.path.move(new_path) self.path = new_path def exists(self): if self.path: return self.path.exists() else: return False @property def filename(self): if self._filename is None: return self.path.name else: return self._filename @property def name(self): return self.filename @filename.setter def filename(self, value): self._filename = value def _set_content(self, content): self._content = content def _get_content(self): if self._content is None: return self.read() else: return self._content content = property(fget=_get_content, fset=_set_content) code = property()