Commit 9c705474 by Steven Bird

* code to detect python version in util.py

  (py25, py26, py27)
* call this in __init__.py (deciding whether to import align, which requires 2.6+)
* call this in corpus/reader/util.py (deciding whether to use followlinks flag)
parent f77d83bd
...@@ -113,8 +113,7 @@ from util import * ...@@ -113,8 +113,7 @@ from util import *
from yamltags import * from yamltags import *
# Modules that require Python 2.6 # Modules that require Python 2.6
from sys import version_info as vi if py26() or py27():
if vi[0] == 2 and vi[1] >= 6:
from align import * from align import *
# don't import contents into top-level namespace: # don't import contents into top-level namespace:
......
...@@ -24,7 +24,7 @@ from nltk.internals import slice_bounds ...@@ -24,7 +24,7 @@ from nltk.internals import slice_bounds
from nltk.data import PathPointer, FileSystemPathPointer, ZipFilePathPointer from nltk.data import PathPointer, FileSystemPathPointer, ZipFilePathPointer
from nltk.data import SeekableUnicodeStreamReader from nltk.data import SeekableUnicodeStreamReader
from nltk.sourcedstring import SourcedStringStream from nltk.sourcedstring import SourcedStringStream
from nltk.util import AbstractLazySequence, LazySubsequence, LazyConcatenation from nltk.util import AbstractLazySequence, LazySubsequence, LazyConcatenation, py25
###################################################################### ######################################################################
#{ Corpus View #{ Corpus View
...@@ -760,7 +760,11 @@ def find_corpus_fileids(root, regexp): ...@@ -760,7 +760,11 @@ def find_corpus_fileids(root, regexp):
# or symlinked) subdirectories, and match paths against the regexp. # or symlinked) subdirectories, and match paths against the regexp.
elif isinstance(root, FileSystemPathPointer): elif isinstance(root, FileSystemPathPointer):
items = [] items = []
for dirname, subdirs, fileids in os.walk(root.path, followlinks=True): # workaround for py25 which doesn't support followlinks
kwargs = {}
if not py25():
kwargs = {'followlinks': True}
for dirname, subdirs, fileids in os.walk(root.path, **kwargs):
prefix = ''.join('%s/' % p for p in _path_from(root.path, dirname)) prefix = ''.join('%s/' % p for p in _path_from(root.path, dirname))
items += [prefix+fileid for fileid in fileids items += [prefix+fileid for fileid in fileids
if re.match(regexp, prefix+fileid)] if re.match(regexp, prefix+fileid)]
......
...@@ -16,6 +16,7 @@ import os ...@@ -16,6 +16,7 @@ import os
from itertools import islice, chain from itertools import islice, chain
from pprint import pprint from pprint import pprint
from collections import defaultdict, deque from collections import defaultdict, deque
from sys import version_info
from nltk.internals import slice_bounds from nltk.internals import slice_bounds
...@@ -108,6 +109,18 @@ def tokenwrap(tokens, separator=" ", width=70): ...@@ -108,6 +109,18 @@ def tokenwrap(tokens, separator=" ", width=70):
########################################################################## ##########################################################################
# Python version
##########################################################################
def py25():
return version_info[0] == 2 and version_info[1] == 5
def py26():
return version_info[0] == 2 and version_info[1] == 6
def py27():
return version_info[0] == 2 and version_info[1] == 7
##########################################################################
# Indexing # Indexing
########################################################################## ##########################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment