Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion treesource/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand Down
10 changes: 5 additions & 5 deletions treesource/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down