def open_tempfile(mode="w", buffering=-1, suffix="", prefix="", dir="", auto_remove=True, create_dir=False): """Context manager that opens a temporary file.""" if create_dir and not sos.path.isdir(dir): sos.mkdir(dir) fd, temp_name = tempfile.mkstemp \ (suffix = suffix, prefix = prefix, dir = dir, text = "t" in mode) try: file = sos.fdopen(fd, mode, buffering) except: sos.close(fd) sos.remove(temp_name) raise else: try: try: yield (file, temp_name) except: if sos.path.exists(temp_name): sos.remove(temp_name) raise finally: if not file.closed: file.close() if auto_remove and sos.path.exists(temp_name): sos.remove(temp_name)
def __init__ (self, path, prefix = None, root = None) : if not sos.path.isdir (path) : sos.mkdir (path) self.parser = Lib.Parser () self.__super.__init__ (path, prefix = prefix, root = root) for s in self._subdirs (path) : self._new_subbox (s)
def open_tempfile (mode = "w", buffering = -1, suffix = "", prefix = "", dir = "", auto_remove = True, create_dir = False) : """Context manager that opens a temporary file.""" if create_dir and not sos.path.isdir (dir) : sos.mkdir (dir) fd, temp_name = tempfile.mkstemp \ (suffix = suffix, prefix = prefix, dir = dir, text = "t" in mode) try : file = sos.fdopen (fd, mode, buffering) except : sos.close (fd) sos.remove (temp_name) raise else : try : try : yield (file, temp_name) except : if sos.path.exists (temp_name) : sos.remove (temp_name) raise finally : if not file.closed : file.close () if auto_remove and sos.path.exists (temp_name) : sos.remove (temp_name)
def create (self) : assert not sos.path.exists (self.db_uri.name), self.db_uri.name assert not sos.path.exists (self.x_uri.name), self.x_uri.name x_name = self.x_uri.name with TFL.lock_file (x_name) : sos.mkdir (x_name) sos.system ("chmod go= %s" % x_name) self._create_info ()
def create (self) : assert not sos.path.exists (self.db_uri), self.db_uri.name assert not sos.path.exists (self.x_uri), self.x_uri.name x_uri = self.x_uri with TFL.lock_file (x_uri) : sos.mkdir (x_uri) sos.system ("chmod go= %s" % x_uri.name) self._create_info ()
def mkdir (self, dir, name) : dir_name = os.path.join (self.root_dir, name) if not os.path.isdir (dir_name) : os.makedirs (dir_name) for d in "th", "im" : os.mkdir (os.path.join (dir_name, d)) gallery = self.GETM (name, directory = dir_name) self._mkdir (gallery) return self.directory_entry (gallery)
def create_diary (Y, path) : for m in Y.months : mp = sos.path.join (path, "%2.2d" % (m.number, )) if not sos.path.isdir (mp) : sos.mkdir (mp) for d in m.days : f = sos.path.join (mp, "%2.2d.%s" % (d.day, "diary")) if not sos.path.isfile (f) : open (f, "w").close ()
def mkdir(self, dir, name): dir_name = os.path.join(self.root_dir, name) if not os.path.isdir(dir_name): os.makedirs(dir_name) for d in "th", "im": os.mkdir(os.path.join(dir_name, d)) gallery = self.GETM(name, directory=dir_name) self._mkdir(gallery) return self.directory_entry(gallery)
def load_info (self) : assert sos.path.exists (self.db_uri), self.db_uri.name x_uri = self.x_uri with TFL.lock_file (x_uri) : if not sos.path.exists (x_uri) : sos.mkdir (x_uri) sos.system ("chmod go= %s" % x_uri.name) with contextlib.closing \ (self.ZF.ZipFile (self.db_uri, "r")) as zf : zf.extractall (x_uri) self.info = self._load_info ()
def load_info (self) : assert sos.path.exists (self.db_uri.name), self.db_uri.name x_name = self.x_uri.name with TFL.lock_file (x_name) : if not sos.path.exists (x_name) : sos.mkdir (x_name) sos.system ("chmod go= %s" % x_name) with contextlib.closing \ (self.ZF.ZipFile (self.db_uri.name, "r")) as zf : zf.extractall (x_name) self.info = self._load_info ()
def __init__ (self, name, prefix = None, root = None) : path = sos.path.join \ (PMA.Office.virtual_mailbox_path (), name) if not sos.path.isdir (path) : sos.mkdir (path) self.__super.__init__ \ ( name = name , path = path , prefix = prefix , root = root ) for mb in self.mailboxes : mb.register_change_observer (self._mailbox_changed)
def __init__(self, name, prefix=None, root=None): path = sos.path.join \ (PMA.Office.virtual_mailbox_path (), name) if not sos.path.isdir(path): sos.mkdir(path) self.__super.__init__ \ ( name = name , path = path , prefix = prefix , root = root ) for mb in self.mailboxes: mb.register_change_observer(self._mailbox_changed)
def _main (cmd) : year = cmd.year path = sos.path.join (sos.expanded_path (cmd.path), "%4.4d" % year) Y = Year (year) pfil = sos.path.join (path, cmd.Plan) vfil = sos.path.join (path, cmd.View) if cmd.create or cmd.diary : if not sos.path.isdir (path) : sos.mkdir (path) if cmd.diary : create_diary (Y, path) if cmd.create : if cmd.Plan : write_year (Y.as_plan, pfil, cmd.force) if cmd.View : write_year (Y.as_cal, vfil, cmd.force)
def __init__ (self, sync_dir, mode, bufsize, bak_name) : self.__Ancestor.__init__ (self, sync_dir, mode, "w", bufsize, bak_name) name = self.sync_dir.file_name if sos.path.exists (name) : if bak_name : name = sos.tempfile_name \ (Filename (name).directory or sos.curdir) sos.mkdir (name) else : sos.rmdir (name, deletefiles = 1) if sos.path.exists (name) : raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) try : sos.mkdir (name) except (IOError, OSError) : raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) else : sos.mkdir (name) self.name = name self._write_stamp_file_header (name)
def __init__(self, sync_dir, mode, bufsize, bak_name): self.__Ancestor.__init__(self, sync_dir, mode, "w", bufsize, bak_name) name = self.sync_dir.file_name if sos.path.exists(name): if bak_name: name = sos.tempfile_name \ (Filename (name).directory or sos.curdir) sos.mkdir(name) else: sos.rmdir(name, deletefiles=1) if sos.path.exists(name): raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) try: sos.mkdir(name) except (IOError, OSError): raise Could_Not_Delete_Old_DB \ ( "Deleting %s failed without error message from " "the OS. Try saving to a different database." % (name, ) ) else: sos.mkdir(name) self.name = name self._write_stamp_file_header(name)
def _path(cls, root, stem): result = sos.path.join(root, stem) if not sos.path.isdir(result): sos.mkdir(result) return result