degal/filesystem.py
changeset 77 2a53c5ade434
parent 76 e22d9f699081
child 79 e5400304a3d3
equal deleted inserted replaced
76:e22d9f699081 77:2a53c5ade434
   203                 return None
   203                 return None
   204 
   204 
   205             else :
   205             else :
   206                 raise
   206                 raise
   207     
   207     
   208     # alias str/unicode
   208     def __str__ (self) :
   209     __str__ = path
   209         return self.path
   210     __unicode__ = unicodepath
   210 
       
   211     def __unicode__ (self) :
       
   212         return self.unicodepath
   211     
   213     
   212     def __repr__ (self) :
   214     def __repr__ (self) :
   213         """
   215         """
   214             Returns a str representing this dir
   216             Returns a str representing this dir
   215         """
   217         """
   312             Returns the file extension part of the file's name, without any leading dot
   314             Returns the file extension part of the file's name, without any leading dot
   313         """
   315         """
   314 
   316 
   315         _, fileext = os.path.splitext(self.name)
   317         _, fileext = os.path.splitext(self.name)
   316 
   318 
   317         return fileext.rstrip('.')
   319         # strip leading .
       
   320         return fileext[1:]
   318 
   321 
   319     def matchext (self, ext_list) :
   322     def matchext (self, ext_list) :
   320         """
   323         """
   321             Tests if this file's extension is part of the recognized list of extensions
   324             Tests if this file's extension is part of the recognized list of extensions
   322         """
   325         """
   345 
   348 
   346         if encoding :
   349         if encoding :
   347             return codecs.open(self.path, mode, encoding, errors, bufsize)
   350             return codecs.open(self.path, mode, encoding, errors, bufsize)
   348 
   351 
   349         else :
   352         else :
   350             return open(self.path, mode, bufsize)
   353             return open(self.path, mode, *(arg for arg in (bufsize, ) if arg is not None))
   351 
   354 
   352     def open_write (self, encoding=None, errors=None, bufsize=None) :
   355     def open_write (self, *args, **kwargs) :
   353         """
   356         """
   354             Open for write using open('w').
   357             Open for write using open('w').
   355         """
   358         """
   356 
   359 
   357         return self.open('w', encoding, errors, bufsize)
   360         return self.open('w', *args, **kwargs)
   358 
   361 
   359     def copy_from (self, file) :
   362     def copy_from (self, file) :
   360         """
   363         """
   361             Replace this file with a copy of the given file with default permissions.
   364             Replace this file with a copy of the given file with default permissions.
   362 
   365 
   391 
   394 
   392         if create and not subdir.is_dir() :
   395         if create and not subdir.is_dir() :
   393             # create it!
   396             # create it!
   394             subdir.mkdir()
   397             subdir.mkdir()
   395 
   398 
   396         return dir
   399         return subdir
   397     
   400     
   398     def test_subdir (self, name, create=False) :
   401     def test_subdir (self, name, create=False) :
   399         """
   402         """
   400             Test for the presence of a subdir with the given name, and possibly return it, or None.
   403             Test for the presence of a subdir with the given name, and possibly return it, or None.
   401 
   404 
   427     def subfile (self, name) :
   430     def subfile (self, name) :
   428         """
   431         """
   429             Returns a File object representing the name underneath this dir
   432             Returns a File object representing the name underneath this dir
   430         """
   433         """
   431 
   434 
   432         return Directory(self, name=name)
   435         return File(self, name=name)
   433 
   436 
   434     def test (self) :
   437     def test (self) :
   435         """
   438         """
   436             Tests that this dir exists as a dir. Raises an error it not, otherwise, returns itself
   439             Tests that this dir exists as a dir. Raises an error it not, otherwise, returns itself
   437         """
   440         """