Skip to content

Commit 05e7a5c

Browse files
committed
Merge pull request #96 from goosemo/feature/Allow-recursing-directories
Feature/allow recursing directories
2 parents e472761 + 83ffef5 commit 05e7a5c

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

pycco/main.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ def highlight(sections, language, preserve_paths=True, outdir=None):
244244
output = output.replace(highlight_start, "").replace(highlight_end, "")
245245
fragments = re.split(language["divider_html"], output)
246246
for i, section in enumerate(sections):
247-
section["code_html"] = highlight_start + shift(fragments, "") + highlight_end
247+
section["code_html"] = highlight_start + \
248+
shift(fragments, "") + highlight_end
248249
try:
249250
docs_text = unicode(section["docs_text"])
250251
except UnicodeError:
@@ -280,7 +281,8 @@ def generate_html(source, sections, preserve_paths=True, outdir=None):
280281
csspath = path.relpath(path.join(outdir, "pycco.css"), path.split(dest)[0])
281282

282283
for sect in sections:
283-
sect["code_html"] = re.sub(r"\{\{", r"__DOUBLE_OPEN_STACHE__", sect["code_html"])
284+
sect["code_html"] = re.sub(
285+
r"\{\{", r"__DOUBLE_OPEN_STACHE__", sect["code_html"])
284286

285287
rendered = pycco_template({
286288
"title": title,
@@ -364,7 +366,8 @@ def generate_html(source, sections, preserve_paths=True, outdir=None):
364366

365367
# The mirror of `divider_text` that we expect Pygments to return. We can split
366368
# on this to recover the original sections.
367-
l["divider_html"] = re.compile(r'\n*<span class="c[1]?">' + l["symbol"] + 'DIVIDER</span>\n*')
369+
l["divider_html"] = re.compile(
370+
r'\n*<span class="c[1]?">' + l["symbol"] + 'DIVIDER</span>\n*')
368371

369372
# Get the Pygments Lexer for this language.
370373
l["lexer"] = lexers.get_lexer_by_name(l["name"])
@@ -438,7 +441,8 @@ def remove_control_chars(s):
438441
# Sanitization regexp copied from
439442
# http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python
440443
from pycco.compat import pycco_unichr
441-
control_chars = ''.join(map(pycco_unichr, list(range(0, 32)) + list(range(127, 160))))
444+
control_chars = ''.join(
445+
map(pycco_unichr, list(range(0, 32)) + list(range(127, 160))))
442446
control_char_re = re.compile(u'[{}]'.format(re.escape(control_chars)))
443447
return control_char_re.sub('', s)
444448

@@ -461,6 +465,23 @@ def ensure_directory(directory):
461465
highlight_end = "</pre></div>"
462466

463467

468+
def _flatten_sources(sources):
469+
"""
470+
This function will iterate through the list of sources and if a directory
471+
is encountered it will walk the tree for any files
472+
"""
473+
_sources = []
474+
475+
for source in sources:
476+
if os.path.isdir(source):
477+
for dirpath, _, filenames in os.walk(source):
478+
_sources.extend([os.path.join(dirpath, f) for f in filenames])
479+
else:
480+
_sources.append(source)
481+
482+
return _sources
483+
484+
464485
def process(sources, preserve_paths=True, outdir=None, language=None, encoding="utf8", index=False):
465486
"""For each source file passed as argument, generate the documentation."""
466487

@@ -469,7 +490,7 @@ def process(sources, preserve_paths=True, outdir=None, language=None, encoding="
469490

470491
# Make a copy of sources given on the command line. `main()` needs the
471492
# original list when monitoring for changed files.
472-
sources = sorted(sources)
493+
sources = sorted(_flatten_sources(sources))
473494

474495
# Proceed to generating the documentation.
475496
if sources:

tests/test_pycco.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_shift(fragments, default):
3333
@given(text(), booleans(), text(min_size=1))
3434
@example("/foo", True, "0")
3535
def test_destination(filepath, preserve_paths, outdir):
36-
dest = p.destination(filepath, preserve_paths=preserve_paths, outdir=outdir)
36+
dest = p.destination(
37+
filepath, preserve_paths=preserve_paths, outdir=outdir)
3738
assert dest.startswith(outdir)
3839
assert dest.endswith(".html")
3940

@@ -65,12 +66,14 @@ def test_comment_with_only_cross_ref():
6566
source = '''# ==Link Target==\n\ndef test_link():\n """[[testing.py#link-target]]"""\n pass'''
6667
sections = p.parse(source, PYTHON)
6768
p.highlight(sections, PYTHON, outdir=tempfile.gettempdir())
68-
assert sections[1]['docs_html'] == '<p><a href="testing.html#link-target">testing.py</a></p>'
69+
assert sections[1][
70+
'docs_html'] == '<p><a href="testing.html#link-target">testing.py</a></p>'
6971

7072

7173
@given(text(), text())
7274
def test_get_language_specify_language(source, code):
73-
assert p.get_language(source, code, language="python") == p.languages['.py']
75+
assert p.get_language(
76+
source, code, language="python") == p.languages['.py']
7477

7578
with pytest.raises(ValueError):
7679
p.get_language(source, code, language="non-existent")
@@ -99,7 +102,8 @@ def test_get_language_bad_code(code):
99102

100103
@given(text(max_size=64))
101104
def test_ensure_directory(dir_name):
102-
tempdir = os.path.join(tempfile.gettempdir(), str(int(time.time())), dir_name)
105+
tempdir = os.path.join(tempfile.gettempdir(),
106+
str(int(time.time())), dir_name)
103107

104108
# Use sanitization from function, but only for housekeeping. We
105109
# pass in the unsanitized string to the function.
@@ -161,3 +165,26 @@ def test_generate_index(path_lists, outdir_list):
161165
file_paths = [os.path.join(*path_list) for path_list in path_lists]
162166
outdir = os.path.join(*outdir_list)
163167
generate_index.generate_index(file_paths, outdir=outdir)
168+
169+
170+
def test_flatten_sources(tmpdir):
171+
sources = [str(tmpdir)]
172+
expected_sources = []
173+
174+
# Setup the base dir
175+
td = tmpdir.join("test.py")
176+
td.write("#!/bin/env python")
177+
expected_sources.append(str(td))
178+
179+
# Make some more directories, each with a file present
180+
for d in ["foo", "bar", "buzz"]:
181+
dd = tmpdir.mkdir(d)
182+
dummy_file = dd.join("test.py")
183+
dummy_file.write("#!/bin/env python")
184+
expected_sources.append(str(dummy_file))
185+
186+
# Get the flattened version of the base directory
187+
flattened = p._flatten_sources(sources)
188+
189+
# Make sure that the lists are the same
190+
assert sorted(expected_sources) == sorted(flattened)

0 commit comments

Comments
 (0)