From e3312e80eae00fa94941fc91d064ef4fcdf69899 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 18 Jul 2010 15:02:36 -0400 Subject: [PATCH] Add workarounds for Python 2.5. Also sort list in doctests since listdir()'s output is not sorted by default. --- dirtag/__init__.py | 56 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/dirtag/__init__.py b/dirtag/__init__.py index fb24f31..7ad9645 100644 --- a/dirtag/__init__.py +++ b/dirtag/__init__.py @@ -18,6 +18,60 @@ import os import os.path +import sys + +if sys.version_info < (2, 5, 0, 'alpha', 0): + raise Exception('Requires at least Python 2.5, not %s' + % ('.'.join([str(x) for x in sys.version_info]))) +elif sys.version_info < (2, 6, 0, 'alpha', 0): # Workarounds for 2.5 + def walk(top, topdown=True, onerror=None, followlinks=False): + """From 2.6's os.py.""" + from os import listdir, error + from os.path import join, isdir, islink + try: + names = listdir(top) + except error, err: + if onerror is not None: + onerror(err) + return + + dirs, nondirs = [], [] + for name in names: + if isdir(join(top, name)): + dirs.append(name) + else: + nondirs.append(name) + + if topdown: + yield top, dirs, nondirs + for name in dirs: + path = join(top, name) + if followlinks or not islink(path): + for x in walk(path, topdown, onerror, followlinks): + yield x + if not topdown: + yield top, dirs, nondirs + + os.walk = walk + + from os.path import curdir, abspath, commonprefix, sep, pardir, join + + def relpath(path, start=curdir): + """From 2.6's posixpath.py.""" + if not path: + raise ValueError("no path specified") + + start_list = abspath(start).split(sep) + path_list = abspath(path).split(sep) + + i = len(commonprefix([start_list, path_list])) + + rel_list = [pardir] * (len(start_list)-i) + path_list[i:] + if not rel_list: + return curdir + return join(*rel_list) + + os.path.relpath = relpath class Node (list): @@ -148,7 +202,7 @@ class Dirtag (object): >>> os.listdir('test/tag/x/y') ['b1.html'] >>> d.add_tag(['a', 'a3.html'], ['x', 'y']) - >>> os.listdir('test/tag/x/y') + >>> sorted(os.listdir('test/tag/x/y')) ['a3.html', 'b1.html'] >>> print 'Z'+os.path.realpath('test/tag/x/y/a3.html') # doctest: +ELLIPSIS Z.../test/raw/a/a3.html -- 2.26.2