Show More
@@ -237,7 +237,7 b' class PrefilterManager(Configurable):' | |||||
237 | This must be called after the priority of a transformer is changed. |
|
237 | This must be called after the priority of a transformer is changed. | |
238 | The :meth:`register_transformer` method calls this automatically. |
|
238 | The :meth:`register_transformer` method calls this automatically. | |
239 | """ |
|
239 | """ | |
240 |
self._transformers.sort( |
|
240 | self._transformers.sort(key=lambda x: x.priority) | |
241 |
|
241 | |||
242 | @property |
|
242 | @property | |
243 | def transformers(self): |
|
243 | def transformers(self): | |
@@ -273,7 +273,7 b' class PrefilterManager(Configurable):' | |||||
273 | This must be called after the priority of a checker is changed. |
|
273 | This must be called after the priority of a checker is changed. | |
274 | The :meth:`register_checker` method calls this automatically. |
|
274 | The :meth:`register_checker` method calls this automatically. | |
275 | """ |
|
275 | """ | |
276 |
self._checkers.sort( |
|
276 | self._checkers.sort(key=lambda x: x.priority) | |
277 |
|
277 | |||
278 | @property |
|
278 | @property | |
279 | def checkers(self): |
|
279 | def checkers(self): |
@@ -78,7 +78,6 b" __license__ = 'MIT'" | |||||
78 | import string |
|
78 | import string | |
79 | import sys |
|
79 | import sys | |
80 | from tokenize import tokenprog |
|
80 | from tokenize import tokenprog | |
81 | from types import StringType |
|
|||
82 |
|
81 | |||
83 | class ItplError(ValueError): |
|
82 | class ItplError(ValueError): | |
84 | def __init__(self, text, pos): |
|
83 | def __init__(self, text, pos): | |
@@ -144,7 +143,7 b' class Itpl:' | |||||
144 | pos = 0 |
|
143 | pos = 0 | |
145 |
|
144 | |||
146 | while 1: |
|
145 | while 1: | |
147 |
dollar = |
|
146 | dollar = format.find("$", pos) | |
148 | if dollar < 0: break |
|
147 | if dollar < 0: break | |
149 | nextchar = format[dollar+1] |
|
148 | nextchar = format[dollar+1] | |
150 |
|
149 |
@@ -7,7 +7,7 b" d = path('/home/guido/bin')" | |||||
7 | for f in d.files('*.py'): |
|
7 | for f in d.files('*.py'): | |
8 | f.chmod(0755) |
|
8 | f.chmod(0755) | |
9 |
|
9 | |||
10 |
This module requires Python 2. |
|
10 | This module requires Python 2.5 or later. | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | URL: http://www.jorendorff.com/articles/python/path |
|
13 | URL: http://www.jorendorff.com/articles/python/path | |
@@ -30,9 +30,7 b' Date: 9 Mar 2007' | |||||
30 | from __future__ import generators |
|
30 | from __future__ import generators | |
31 |
|
31 | |||
32 | import sys, warnings, os, fnmatch, glob, shutil, codecs |
|
32 | import sys, warnings, os, fnmatch, glob, shutil, codecs | |
33 | # deprecated in python 2.6 |
|
33 | from hashlib import md5 | |
34 | warnings.filterwarnings('ignore', r'.*md5.*') |
|
|||
35 | import md5 |
|
|||
36 |
|
34 | |||
37 | __version__ = '2.2' |
|
35 | __version__ = '2.2' | |
38 | __all__ = ['path'] |
|
36 | __all__ = ['path'] | |
@@ -49,38 +47,11 b' else:' | |||||
49 | except ImportError: |
|
47 | except ImportError: | |
50 | pwd = None |
|
48 | pwd = None | |
51 |
|
49 | |||
52 | # Pre-2.3 support. Are unicode filenames supported? |
|
|||
53 | _base = str |
|
|||
54 | _getcwd = os.getcwd |
|
|||
55 | try: |
|
|||
56 | if os.path.supports_unicode_filenames: |
|
|||
57 | _base = unicode |
|
|||
58 | _getcwd = os.getcwdu |
|
|||
59 | except AttributeError: |
|
|||
60 | pass |
|
|||
61 |
|
||||
62 | # Pre-2.3 workaround for booleans |
|
|||
63 | try: |
|
|||
64 | True, False |
|
|||
65 | except NameError: |
|
|||
66 | True, False = 1, 0 |
|
|||
67 |
|
||||
68 | # Pre-2.3 workaround for basestring. |
|
|||
69 | try: |
|
|||
70 | basestring |
|
|||
71 | except NameError: |
|
|||
72 | basestring = (str, unicode) |
|
|||
73 |
|
||||
74 | # Universal newline support |
|
|||
75 | _textmode = 'r' |
|
|||
76 | if hasattr(file, 'newlines'): |
|
|||
77 | _textmode = 'U' |
|
|||
78 |
|
||||
79 |
|
50 | |||
80 | class TreeWalkWarning(Warning): |
|
51 | class TreeWalkWarning(Warning): | |
81 | pass |
|
52 | pass | |
82 |
|
53 | |||
83 |
class path( |
|
54 | class path(unicode): | |
84 | """ Represents a filesystem path. |
|
55 | """ Represents a filesystem path. | |
85 |
|
56 | |||
86 | For documentation on individual methods, consult their |
|
57 | For documentation on individual methods, consult their | |
@@ -90,12 +61,12 b' class path(_base):' | |||||
90 | # --- Special Python methods. |
|
61 | # --- Special Python methods. | |
91 |
|
62 | |||
92 | def __repr__(self): |
|
63 | def __repr__(self): | |
93 |
return 'path(%s)' % |
|
64 | return 'path(%s)' % unicode.__repr__(self) | |
94 |
|
65 | |||
95 | # Adding a path and a string yields a path. |
|
66 | # Adding a path and a string yields a path. | |
96 | def __add__(self, more): |
|
67 | def __add__(self, more): | |
97 | try: |
|
68 | try: | |
98 |
resultStr = |
|
69 | resultStr = unicode.__add__(self, more) | |
99 | except TypeError: #Python bug |
|
70 | except TypeError: #Python bug | |
100 | resultStr = NotImplemented |
|
71 | resultStr = NotImplemented | |
101 | if resultStr is NotImplemented: |
|
72 | if resultStr is NotImplemented: | |
@@ -122,7 +93,7 b' class path(_base):' | |||||
122 |
|
93 | |||
123 | def getcwd(cls): |
|
94 | def getcwd(cls): | |
124 | """ Return the current working directory as a path object. """ |
|
95 | """ Return the current working directory as a path object. """ | |
125 |
return cls( |
|
96 | return cls(os.getcwdu()) | |
126 | getcwd = classmethod(getcwd) |
|
97 | getcwd = classmethod(getcwd) | |
127 |
|
98 | |||
128 |
|
99 | |||
@@ -152,7 +123,7 b' class path(_base):' | |||||
152 | return base |
|
123 | return base | |
153 |
|
124 | |||
154 | def _get_ext(self): |
|
125 | def _get_ext(self): | |
155 |
f, ext = os.path.splitext( |
|
126 | f, ext = os.path.splitext(unicode(self)) | |
156 | return ext |
|
127 | return ext | |
157 |
|
128 | |||
158 | def _get_drive(self): |
|
129 | def _get_drive(self): | |
@@ -513,14 +484,14 b' class path(_base):' | |||||
513 | of all the files users have in their bin directories. |
|
484 | of all the files users have in their bin directories. | |
514 | """ |
|
485 | """ | |
515 | cls = self.__class__ |
|
486 | cls = self.__class__ | |
516 |
return [cls(s) for s in glob.glob( |
|
487 | return [cls(s) for s in glob.glob(unicode(self / pattern))] | |
517 |
|
488 | |||
518 |
|
489 | |||
519 | # --- Reading or writing an entire file at once. |
|
490 | # --- Reading or writing an entire file at once. | |
520 |
|
491 | |||
521 | def open(self, mode='r'): |
|
492 | def open(self, mode='r'): | |
522 | """ Open this file. Return a file object. """ |
|
493 | """ Open this file. Return a file object. """ | |
523 |
return |
|
494 | return open(self, mode) | |
524 |
|
495 | |||
525 | def bytes(self): |
|
496 | def bytes(self): | |
526 | """ Open this file, read all bytes, return them as a string. """ |
|
497 | """ Open this file, read all bytes, return them as a string. """ | |
@@ -563,7 +534,7 b' class path(_base):' | |||||
563 | """ |
|
534 | """ | |
564 | if encoding is None: |
|
535 | if encoding is None: | |
565 | # 8-bit |
|
536 | # 8-bit | |
566 |
f = self.open( |
|
537 | f = self.open('U') | |
567 | try: |
|
538 | try: | |
568 | return f.read() |
|
539 | return f.read() | |
569 | finally: |
|
540 | finally: | |
@@ -690,7 +661,7 b' class path(_base):' | |||||
690 | This uses 'U' mode in Python 2.3 and later. |
|
661 | This uses 'U' mode in Python 2.3 and later. | |
691 | """ |
|
662 | """ | |
692 | if encoding is None and retain: |
|
663 | if encoding is None and retain: | |
693 |
f = self.open( |
|
664 | f = self.open('U') | |
694 | try: |
|
665 | try: | |
695 | return f.readlines() |
|
666 | return f.readlines() | |
696 | finally: |
|
667 | finally: | |
@@ -770,7 +741,7 b' class path(_base):' | |||||
770 | """ |
|
741 | """ | |
771 | f = self.open('rb') |
|
742 | f = self.open('rb') | |
772 | try: |
|
743 | try: | |
773 |
m = md5 |
|
744 | m = md5() | |
774 | while True: |
|
745 | while True: | |
775 | d = f.read(8192) |
|
746 | d = f.read(8192) | |
776 | if not d: |
|
747 | if not d: |
@@ -22,10 +22,15 b' def import_item(name):' | |||||
22 | """Import and return bar given the string foo.bar.""" |
|
22 | """Import and return bar given the string foo.bar.""" | |
23 | package = '.'.join(name.split('.')[0:-1]) |
|
23 | package = '.'.join(name.split('.')[0:-1]) | |
24 | obj = name.split('.')[-1] |
|
24 | obj = name.split('.')[-1] | |
25 | execString = 'from %s import %s' % (package, obj) |
|
25 | # execString = 'from %s import %s' % (package, obj) | |
26 | try: |
|
26 | # try: | |
27 | exec execString |
|
27 | # exec execString | |
28 | except SyntaxError: |
|
28 | # except SyntaxError: | |
29 | raise ImportError("Invalid class specification: %s" % name) |
|
29 | # raise ImportError("Invalid class specification: %s" % name) | |
30 | exec 'temp = %s' % obj |
|
30 | # exec 'temp = %s' % obj | |
31 | return temp |
|
31 | # return temp | |
|
32 | if package: | |||
|
33 | module = __import__(package,fromlist=[obj]) | |||
|
34 | return module.__dict__[obj] | |||
|
35 | else: | |||
|
36 | return __import__(obj) |
@@ -56,19 +56,7 b' from types import (' | |||||
56 | InstanceType, ClassType, FunctionType, |
|
56 | InstanceType, ClassType, FunctionType, | |
57 | ListType, TupleType |
|
57 | ListType, TupleType | |
58 | ) |
|
58 | ) | |
59 |
|
59 | from .importstring import import_item | ||
60 | def import_item(name): |
|
|||
61 | """Import and return bar given the string foo.bar.""" |
|
|||
62 | package = '.'.join(name.split('.')[0:-1]) |
|
|||
63 | obj = name.split('.')[-1] |
|
|||
64 | execString = 'from %s import %s' % (package, obj) |
|
|||
65 | try: |
|
|||
66 | exec execString |
|
|||
67 | except SyntaxError: |
|
|||
68 | raise ImportError("Invalid class specification: %s" % name) |
|
|||
69 | exec 'temp = %s' % obj |
|
|||
70 | return temp |
|
|||
71 |
|
||||
72 |
|
60 | |||
73 | ClassTypes = (ClassType, type) |
|
61 | ClassTypes = (ClassType, type) | |
74 |
|
62 |
General Comments 0
You need to be logged in to leave comments.
Login now