Skip to content

Commit

Permalink
Escape prefix/suffix before forming glob pattern
Browse files Browse the repository at this point in the history
 - Output in unicode when invoked from command line
 - Add a test
  • Loading branch information
musically-ut committed May 6, 2015
1 parent 03ea2aa commit 55809f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
7 changes: 4 additions & 3 deletions seqfile/seqfile.py
Expand Up @@ -64,7 +64,8 @@ def _findNextFile(folder, prefix, suffix, fnameGen, base, maxattempts, loop):
prefix = prefix if prefix is not None else u''
suffix = suffix if suffix is not None else u''

pattern = prefix + u'(?P<seqNumber>[0-9]+)' + suffix + u'$'
pattern = (_re.escape(prefix) + u'(?P<seqNumber>[0-9]+)' +
_re.escape(suffix) + u'$')

allFiles = _unicodeGlob(pattern, folder)
sortedFiles = _natsort.natsorted(allFiles,
Expand Down Expand Up @@ -208,7 +209,7 @@ def _run(passedArgs=None, stderr=None, stdout=None, exitFn=None):
# The newline will be converted to the correct platform specific line
# ending as `sys.stdout` is opened in non-binary mode.
# Hence, we do not need to explicitly print out `\r\n` for Windows.
stdout.write(nextFile + '\n')
stdout.write(nextFile + u'\n')
except OSError as e:
stderr.write(_os.strerror(e.errno) + '\n')
stderr.write(_os.strerror(e.errno) + u'\n')
_sys.exit(e.errno)
21 changes: 11 additions & 10 deletions seqfile/tests.py
Expand Up @@ -2,20 +2,14 @@
import tempfile as _T
import shutil as _shutil
import os as _os
import glob
import glob as _glob
from io import StringIO as _StringIO

from nose.tools import raises
from . import seqfile as _S
from contextlib import contextmanager
import pep8

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

join = _os.path.join


@contextmanager
def tempDir(prefix=None):
Expand All @@ -31,12 +25,13 @@ def fnameGen(x): return prefix + str(x) + suffix


def execRun(*args):
stderr, stdout = StringIO(), StringIO()
stderr, stdout = _StringIO(), _StringIO()
_S._run(args, stderr, stdout)
return stderr.getvalue().strip(), stdout.getvalue().strip()


prefix, suffix = 'mdl.', '.cPickle'
join = _os.path.join


###############################################################################
Expand All @@ -46,7 +41,7 @@ def execRun(*args):
def test_pep8_conformance():
"""Test that we conform to PEP8."""
pep8style = pep8.StyleGuide()
result = pep8style.check_files(glob.glob('./seqfile/*.py'))
result = pep8style.check_files(_glob.glob('./seqfile/*.py'))
assert result.total_errors == 0


Expand Down Expand Up @@ -131,6 +126,12 @@ def test_findNextFile_with_noisy_files():
assert _S.findNextFile(d, prefix, suffix) == join(d, fnameGen(2))


def test_findNextFile_with_regex_compat_files():
with tempDir() as d:
_S._doAtomicFileCreation(join(d, fnameGen(0).replace('.', 'X')))
assert _S.findNextFile(d, prefix, suffix) == join(d, fnameGen(0))


def test_findNextFile_with_files_fnamegen():
with tempDir() as d:
_S._doAtomicFileCreation(join(d, fnameGen(0)))
Expand Down

0 comments on commit 55809f3

Please sign in to comment.