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