From 11920a1e65b7eea2b182cdaa3111bd2f5f616cec Mon Sep 17 00:00:00 2001 From: Morgan Goose Date: Thu, 4 Nov 2010 17:23:12 -0400 Subject: [PATCH 1/4] Addressed the pycco as a module issue --- pycco/main.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pycco/main.py b/pycco/main.py index 79626f3..c853959 100644 --- a/pycco/main.py +++ b/pycco/main.py @@ -196,7 +196,7 @@ def generate_html(source, sections, options): dest = destination(source, options) html = pycco_template({ "title": title, - "stylesheet": path.relpath(path.join(options.dir, "pycco.css"), + "stylesheet": path.relpath(path.join(options['dir'], "pycco.css"), path.split(dest)[0]), "sections": sections, "source": source, @@ -292,14 +292,14 @@ def get_language(source): # Compute the destination HTML path for an input source file path. If the source # is `lib/example.py`, the HTML will be at `docs/example.html` def destination(filepath, options): - preserve_paths = options.paths + preserve_paths = options['paths'] try: name = filepath.replace(filepath[ filepath.rindex("."): ], "") except ValueError: name = filepath if not preserve_paths: name = path.basename(name) - return path.join(options.dir, "%s.html" % name) + return path.join(options['dir'], "%s.html" % name) # Shift items off the front of the `list` until it is empty, then return # `default`. @@ -331,11 +331,11 @@ def template(source): # The bulk of the work is done here # For each source file passed in as an argument, generate the documentation. -def process(sources, options=None): +def process(sources, options): sources.sort() if sources: - ensure_directory(options.dir) - css = open(path.join(options.dir, "pycco.css"), "w") + ensure_directory(options['dir']) + css = open(path.join(options['dir'], "pycco.css"), "w") css.write(pycco_styles) css.close() @@ -357,7 +357,7 @@ def main(): help='The output directory that the rendered files should go to.') opts, sources = parser.parse_args() - process(sources, opts) + process(sources, opts.__dict__) # Run the script. if __name__ == "__main__": From c66d21dca0420b855d52e8639e107af039710a7e Mon Sep 17 00:00:00 2001 From: Morgan Goose Date: Sat, 26 Mar 2016 01:20:52 -0700 Subject: [PATCH 2/4] Feature addition to allow directory arguments This should address the feature request from issue-78, where the request was made to build documentation recursively for all files in a directory and it's subdirectories. https://github.com/pycco-docs/pycco/issues/78 --- pycco/main.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pycco/main.py b/pycco/main.py index e95ad73..a02eee2 100644 --- a/pycco/main.py +++ b/pycco/main.py @@ -456,6 +456,23 @@ def ensure_directory(directory): # The end of each Pygments highlight block. highlight_end = "" +def _flatten_sources(sources): + """ + This function will iterate through the list of sources and if a directory + is encountered it will walk the tree for any files + """ + _sources = [] + + for source in sources: + if os.path.isdir(source): + for dirpath, _, filenames in os.walk(source): + _sources.extend([os.path.join(dirpath,f) for f in filenames]) + else: + _sources.append(source) + + return _sources + + def process(sources, preserve_paths=True, outdir=None, language=None, encoding="utf8", index=False): """For each source file passed as argument, generate the documentation.""" @@ -465,7 +482,7 @@ def process(sources, preserve_paths=True, outdir=None, language=None, encoding=" # Make a copy of sources given on the command line. `main()` needs the # original list when monitoring for changed files. - sources = sorted(sources) + sources = sorted(_flatten_sources(sources)) # Proceed to generating the documentation. if sources: From 1479ee55fa80196d914c1e683f2c379edb9a8a0a Mon Sep 17 00:00:00 2001 From: Morgan Goose Date: Sat, 26 Mar 2016 12:31:04 -0700 Subject: [PATCH 3/4] Including a test for _flatten_sources --- tests/test_pycco.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_pycco.py b/tests/test_pycco.py index 02ef008..ae7d0e4 100644 --- a/tests/test_pycco.py +++ b/tests/test_pycco.py @@ -161,3 +161,25 @@ def test_generate_index(path_lists, outdir_list): file_paths = [os.path.join(*path_list) for path_list in path_lists] outdir = os.path.join(*outdir_list) generate_index.generate_index(file_paths, outdir=outdir) + +def test_flatten_sources(tmpdir): + sources = [str(tmpdir)] + expected_sources = [] + + # Setup the base dir + td = tmpdir.join("test.py") + td.write("#!/bin/env python") + expected_sources.append(str(td)) + + # Make some more directories, each with a file present + for d in ["foo", "bar", "buzz"]: + dd = tmpdir.mkdir(d) + dummy_file = dd.join("test.py") + dummy_file.write("#!/bin/env python") + expected_sources.append(str(dummy_file)) + + # Get the flattened version of the base directory + flattened = p._flatten_sources(sources) + + # Make sure that the lists are the same + assert sorted(expected_sources) == sorted(flattened) From 83ffef5da6f5b2fa919fd1374b05fdc95875dfd5 Mon Sep 17 00:00:00 2001 From: Morgan Goose Date: Mon, 4 Apr 2016 19:10:11 -0700 Subject: [PATCH 4/4] autopep8 results, will squash if acceptable --- pycco/main.py | 16 ++++++++++------ tests/test_pycco.py | 13 +++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pycco/main.py b/pycco/main.py index a02eee2..98d17fe 100644 --- a/pycco/main.py +++ b/pycco/main.py @@ -244,7 +244,8 @@ def highlight(sections, language, preserve_paths=True, outdir=None): output = output.replace(highlight_start, "").replace(highlight_end, "") fragments = re.split(language["divider_html"], output) for i, section in enumerate(sections): - section["code_html"] = highlight_start + shift(fragments, "") + highlight_end + section["code_html"] = highlight_start + \ + shift(fragments, "") + highlight_end try: docs_text = unicode(section["docs_text"]) except UnicodeError: @@ -280,7 +281,8 @@ def generate_html(source, sections, preserve_paths=True, outdir=None): csspath = path.relpath(path.join(outdir, "pycco.css"), path.split(dest)[0]) for sect in sections: - sect["code_html"] = re.sub(r"\{\{", r"__DOUBLE_OPEN_STACHE__", sect["code_html"]) + sect["code_html"] = re.sub( + r"\{\{", r"__DOUBLE_OPEN_STACHE__", sect["code_html"]) rendered = pycco_template({ "title": title, @@ -360,7 +362,8 @@ def generate_html(source, sections, preserve_paths=True, outdir=None): # The mirror of `divider_text` that we expect Pygments to return. We can split # on this to recover the original sections. - l["divider_html"] = re.compile(r'\n*' + l["symbol"] + 'DIVIDER\n*') + l["divider_html"] = re.compile( + r'\n*' + l["symbol"] + 'DIVIDER\n*') # Get the Pygments Lexer for this language. l["lexer"] = lexers.get_lexer_by_name(l["name"]) @@ -434,7 +437,8 @@ def remove_control_chars(s): # Sanitization regexp copied from # http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python from pycco.compat import pycco_unichr - control_chars = ''.join(map(pycco_unichr, list(range(0, 32)) + list(range(127, 160)))) + control_chars = ''.join( + map(pycco_unichr, list(range(0, 32)) + list(range(127, 160)))) control_char_re = re.compile(u'[{}]'.format(re.escape(control_chars))) return control_char_re.sub('', s) @@ -456,6 +460,7 @@ def ensure_directory(directory): # The end of each Pygments highlight block. highlight_end = "" + def _flatten_sources(sources): """ This function will iterate through the list of sources and if a directory @@ -466,14 +471,13 @@ def _flatten_sources(sources): for source in sources: if os.path.isdir(source): for dirpath, _, filenames in os.walk(source): - _sources.extend([os.path.join(dirpath,f) for f in filenames]) + _sources.extend([os.path.join(dirpath, f) for f in filenames]) else: _sources.append(source) return _sources - def process(sources, preserve_paths=True, outdir=None, language=None, encoding="utf8", index=False): """For each source file passed as argument, generate the documentation.""" diff --git a/tests/test_pycco.py b/tests/test_pycco.py index ae7d0e4..6db8265 100644 --- a/tests/test_pycco.py +++ b/tests/test_pycco.py @@ -33,7 +33,8 @@ def test_shift(fragments, default): @given(text(), booleans(), text(min_size=1)) @example("/foo", True, "0") def test_destination(filepath, preserve_paths, outdir): - dest = p.destination(filepath, preserve_paths=preserve_paths, outdir=outdir) + dest = p.destination( + filepath, preserve_paths=preserve_paths, outdir=outdir) assert dest.startswith(outdir) assert dest.endswith(".html") @@ -65,12 +66,14 @@ def test_comment_with_only_cross_ref(): source = '''# ==Link Target==\n\ndef test_link():\n """[[testing.py#link-target]]"""\n pass''' sections = p.parse(source, PYTHON) p.highlight(sections, PYTHON, outdir=tempfile.gettempdir()) - assert sections[1]['docs_html'] == '

testing.py

' + assert sections[1][ + 'docs_html'] == '

testing.py

' @given(text(), text()) def test_get_language_specify_language(source, code): - assert p.get_language(source, code, language="python") == p.languages['.py'] + assert p.get_language( + source, code, language="python") == p.languages['.py'] with pytest.raises(ValueError): p.get_language(source, code, language="non-existent") @@ -99,7 +102,8 @@ def test_get_language_bad_code(code): @given(text(max_size=64)) def test_ensure_directory(dir_name): - tempdir = os.path.join(tempfile.gettempdir(), str(int(time.time())), dir_name) + tempdir = os.path.join(tempfile.gettempdir(), + str(int(time.time())), dir_name) # Use sanitization from function, but only for housekeeping. We # pass in the unsanitized string to the function. @@ -162,6 +166,7 @@ def test_generate_index(path_lists, outdir_list): outdir = os.path.join(*outdir_list) generate_index.generate_index(file_paths, outdir=outdir) + def test_flatten_sources(tmpdir): sources = [str(tmpdir)] expected_sources = []