Package pybaz :: Module pathname
[frames] | no frames]

Source Code for Module pybaz.pathname

 1  # arch-tag: 75709428-970d-4bba-ac1c-7e1e7428345f 
 2  # Copyright (C) 2004  David Allouche <david@allouche.net> 
 3  #                     Canonical Ltd. 
 4  # 
 5  #    This program is free software; you can redistribute it and/or modify 
 6  #    it under the terms of the GNU General Public License as published by 
 7  #    the Free Software Foundation; either version 2 of the License, or 
 8  #    (at your option) any later version. 
 9  # 
10  #    This program is distributed in the hope that it will be useful, 
11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  #    GNU General Public License for more details. 
14  # 
15  #    You should have received a copy of the GNU General Public License 
16  #    along with this program; if not, write to the Free Software 
17  #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
18   
19  """File name manipulation.""" 
20   
21  __all__ = ['PathName', 'DirName', 'FileName'] 
22   
23  import os 
24   
25   
26 -class PathName(str):
27 28 """String with extra methods for filename operations.""" 29
30 - def __new__(cls, path='.'):
31 return str.__new__(cls, os.path.normpath(path))
32
33 - def __div__(self, path):
34 if isinstance(path, (DirName, FileName)): 35 ctor = path.__class__ 36 else: 37 ctor = PathName 38 return ctor(os.path.join(self, path))
39
40 - def __repr__(self):
41 return '%s(%s)' % (type(self).__name__, str.__repr__(self))
42
43 - def abspath(self):
44 return self.__class__(os.path.abspath(self))
45 - def dirname(self):
46 return DirName(os.path.dirname(self))
47 - def basename(self):
48 return self.__class__(os.path.basename(self))
49
50 - def realpath(self):
51 if 'realpath' in dir(os.path): 52 return self.__class__(os.path.realpath(self)) 53 else: 54 return self.abspath()
55
56 - def splitname(self):
57 dir_, base = os.path.split(self) 58 return DirName(dir_), self.__class__(base)
59 60
61 -class DirName(PathName):
62 63 """PathName, further characterized as a directory name."""
64 65
66 -class FileName(PathName):
67 68 """PathName further characterized as a file name."""
69