Skip to content

Commit fa86e89

Browse files
authored
Merge pull request #7 from lennertvandevelde/fix-code-args-code-selection
2 parents 22826fc + dd5889d commit fa86e89

2 files changed

Lines changed: 19 additions & 41 deletions

File tree

argmark/argmark.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import re
55
from typing import List
6-
6+
from inspect import cleandoc
77
from mdutils.mdutils import MdUtils
88

99

@@ -23,35 +23,10 @@ def inline_code(code: str) -> str:
2323
return f"`{code}`"
2424

2525

26-
def get_indent(line: str) -> int:
27-
"""
28-
29-
Get the indent of the file.
30-
31-
Args:
32-
33-
line (str) : A string whose indent needs to be found
34-
35-
Returns:
36-
37-
int: Indent
38-
39-
"""
40-
indent = 0
41-
for c in line:
42-
if c == " ":
43-
indent += 1
44-
elif c == "\t":
45-
indent += 8
46-
else:
47-
# break on first word / non-white char
48-
break
49-
return indent
50-
5126

5227
def gen_help(lines: List) -> None:
5328
"""
54-
Generate the help given the source code as list of lines
29+
Generate lines of code containing the argument parser and pass it to md_help.
5530
5631
Args:
5732
@@ -62,27 +37,35 @@ def gen_help(lines: List) -> None:
6237
None
6338
6439
"""
65-
indent = 0
40+
lines_string = ""
41+
lines_string += "import argparse"
42+
lines_string += "\n"
43+
lines_string += "import argmark"
44+
lines_string += "\n"
45+
6646
parser_expr = re.compile(r"(\w+)\.parse_args\(")
6747
for i, line in enumerate(lines):
48+
if "ArgumentParser(" in line:
49+
firstline = i
6850
if ".parse_args(" in line:
6951
parser = re.search(parser_expr, line)
7052
if parser is not None:
7153
lastline = i
7254
parser = parser.group(1)
73-
indent = get_indent(line)
7455
break
75-
lines = lines[:lastline]
76-
lines.append("\n")
77-
lines.append(" " * indent + "import argmark")
78-
lines.append(" " * indent + f"argmark.md_help({parser})")
79-
logging.debug("\n".join(lines))
80-
exec("\n".join(lines), {"__name__": "__main__"})
56+
57+
lines = lines[firstline:lastline]
58+
59+
lines_string += cleandoc("\n".join(lines))
60+
lines_string += "\n"
61+
lines_string += f"argmark.md_help({parser})"
62+
logging.debug(lines_string)
63+
exec(lines_string, {"__name__": "__main__"})
8164

8265

8366
def md_help(parser: _argparse.ArgumentParser) -> None:
8467
"""
85-
68+
Generate a mardown file from the given argument parser.
8669
Args:
8770
parser: parser object
8871

tests/test_argmark.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ def test_inline_code():
1010
assert inline_code("f") == "`f`"
1111

1212

13-
def test_get_indent():
14-
assert get_indent(" foo") == 2
15-
assert get_indent("\tfoo") == 8
16-
17-
1813
def test_gen_help():
1914
py_file = os.path.join(_install_dir, "sample_argparse.py")
2015
md_file = "sample_argparse.md"

0 commit comments

Comments
 (0)