diff --git a/README.md b/README.md index 2713ef2..1683ea6 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ with pip: `pip install treesource`. ## Usage Execute the python module from the root folder: `python -m treesource` ``` -usage: python -m treesource [-h] [-u] [-a] [-r PATH] [-f FORMAT] +usage: python -m treesource [-h] [-u] [-a] [-r PATH] [-f FORMAT] [-i ENCODING] Source file-trees generator. @@ -46,6 +46,8 @@ optional arguments: -r PATH, --root PATH the root directory of the tree -f FORMAT, --format FORMAT the rendering format [txt|md|ascii] + -i ENCODING, --input_encoding ENCODING + the encoding that shall be assumed for the treesource.txt files ``` ## Output formats diff --git a/treesource/__main__.py b/treesource/__main__.py index 872e2e3..7995d36 100644 --- a/treesource/__main__.py +++ b/treesource/__main__.py @@ -21,10 +21,12 @@ parser.add_argument('-f', '--format', metavar='FORMAT', type=str, default='txt', help='the rendering format [txt|md|ascii]') + parser.add_argument('-i', '--input_encoding', type=str, + default=None, help='the encoding assumed for the treesource.txt e.g. utf-8') args = parser.parse_args() # generate the tree - tree = generate_tree(args.root, keep_undocumented=args.show_all) + tree = generate_tree(args.root, keep_undocumented=args.show_all, input_encoding=args.input_encoding) # render the tree if args.format.upper() in ['MD', 'MARKDOWN']: diff --git a/treesource/scan.py b/treesource/scan.py index 7e920ed..dac6c94 100644 --- a/treesource/scan.py +++ b/treesource/scan.py @@ -3,13 +3,13 @@ from anytree import AnyNode, PostOrderIter -def get_doc_string(path, first_lines=30): +def get_doc_string(path, first_lines=30, input_encoding=None): """look for the doctree docstring in the first lines of the file in path""" docstring = None try: - f = open(path, 'r') + f = open(path, 'r', encoding=input_encoding) except Exception: return None @@ -81,7 +81,7 @@ def walk2tree(walk_lsit, prune_undocumented): return root -def generate_tree(startpath, keep_undocumented=False): +def generate_tree(startpath, keep_undocumented=False, input_encoding=None): """Generate a file-tree object from the current working directory. keep_undocumented: keep undocumented files and directories in the tree @@ -102,7 +102,7 @@ def generate_tree(startpath, keep_undocumented=False): documented_files = [] for filename in files: # look for the docstring in the file - docstring = get_doc_string(os.path.join(root, filename)) + docstring = get_doc_string(os.path.join(root, filename),input_encoding=input_encoding) # speed up by not inserting undocumented diles if keep_undocumented or (docstring is not None): documented_files.append(dict( @@ -114,7 +114,7 @@ def generate_tree(startpath, keep_undocumented=False): dirdoc = None docfile = os.path.join(root, 'treesource.txt') if os.path.exists(docfile): - with open(docfile, 'r') as f: + with open(docfile, 'r', encoding=input_encoding) as f: dirdoc = f.readline() walked.append(dict(