root/branches/add_stat_caching/ftpsync-0.1/casepath.py

Revision 545, 3.2 kB (checked in by schwa, 2 years ago)
Code contributed by Martin Wilck. (Thank you!) I might use some of
this code to add caching of stat results to ftputil.
  • Property svn:eol-style set to native
Line 
1 import posixpath
2 from ftputil import ftp_path, ftp_stat
3 from casestr import CaseInsStr
4
5 class BasePath(ftp_path._Path):
6     """
7     A reimplementation of ftputil.ftp_path._Path which is better suited
8     as base class than the original _Path.
9     """
10     # ftputil.ftp_path._Path can't be inherited well because of the
11     # direct posixpath assignments in ftputil.ftp_path._Path.__init__(),
12     # which subclasses can't overload.
13
14     # Here, dirname(), basename(), etc. are defined as methods which
15     # can be overloaded in derived classes.
16
17     # This class inherits _Path in order to avoid duplicating
18     # code. Note that _Path.__init__() isn't called.
19
20     def __init__(self, host, pp=posixpath):
21         self.pp = pp
22         self._host = host
23
24     def dirname(self, *args):
25         return self.pp.dirname(*args)
26
27     def basename(self, *args):
28         return self.pp.basename(*args)
29
30     def isabs(self, *args):
31         return self.pp.isabs(*args)
32
33     def commonprefix(self, *args):
34         return self.pp.commonprefix(*args)
35
36     def join(self, *args):
37         return self.pp.join(*args)
38
39     def split(self, *args):
40         return self.pp.split(*args)
41
42     def splitdrive(self, *args):
43         return self.pp.splitdrive(*args)
44
45     def splitext(self, *args):
46         return self.pp.splitext(*args)
47
48     def normcase(self, *args):
49         return self.pp.normcase(*args)
50
51     def normpath(self, *args):
52         return self.pp.normpath(*args)
53
54
55 class CaseInsPath(BasePath):
56     """
57     A "path" implementation that treats paths in a case-insensitive manner.
58     The only difference to BasePath (and _Path) is that all
59     returned strings are 'CaseInsStr' objects.
60     """
61    
62     def dirname(self, path):
63         return CaseInsStr(BasePath.dirname(self, path))
64
65     def basename(self, path):
66         return CaseInsStr(BasePath.basename(self, path))
67
68     def abspath(self, path):
69         return CaseInsStr(BasePath.abspath(self, path))
70
71     def normpath(self, path):
72         return CaseInsStr(BasePath.normpath(self, path))
73
74     def normcase(self, path):
75         return CaseInsStr(path.lower())
76
77     def join(self, *args):
78         return CaseInsStr(BasePath.join(self, *args))
79
80     def split(self, *args):
81         return [CaseInsStr(x)
82                 for x in BasePath.split(self, *args)]
83
84     def splitext(self, *args):
85         return [CaseInsStr(x)
86                 for x in BasePath.splitext(self, *args)]
87
88     def splitdrive(self, *args):
89         return [CaseInsStr(x)
90                 for x in BasePath.splitdrive(self, *args)]
91
92
93 class CaseInsStat(ftp_stat._Stat):
94     """
95     A class derived from _Stat that treats file names in a case insensitive
96     manner.
97
98     E.g. "Spam" will be found and stat'd in a directory listing "spam, eggs".
99     """
100
101     def _stat_candidates(self, lines, wanted_name):
102         """Return candidate lines for further analysis."""
103         ret =  [line
104                 for line in lines
105                 if CaseInsStr(line).find(wanted_name) != -1]
106         return ret
107        
108     def _real_lstat(self, path,  _exception_for_missing_path=True):
109
110         path = CaseInsStr(path)
111         ret = ftp_stat._Stat._real_lstat(self, path,
112                                          _exception_for_missing_path)
113         return ret
Note: See TracBrowser for help on using the browser.