Skip to content

Commit

Permalink
fixed space indents accidentally removed when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyiscute committed Nov 4, 2021
1 parent 20de360 commit ac96afd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
17 changes: 8 additions & 9 deletions ShitMountainGenerator/select_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,24 @@ def __init__(self, name: str, var_name: str, body: List[str]):
self.var_name = var_name
self.cases = []
self.default = None
block = ""
block = []
for line in body:
if line.startswith("<case"):
block = line
block = [line]
elif line.startswith("<default"):
block = ""
block = []
elif line.startswith("</case"):
self.cases.append(self._parse_case(block))
self.cases.append(self._parse_case(block[:]))
elif line.startswith("</default"):
self.default = block
self.default = "".join(block)
else:
block += re.sub(r"\t|[ ]{4}", "", line)
block.append(re.sub(r"^\t|( {4})", "", line, 1))

def _parse_case(self, block: str) -> SelectCase:
lines = block.splitlines()
def _parse_case(self, lines: List[str]) -> SelectCase:
attrs = parse_attr(lines[0])
if "test" not in attrs:
raise InvalidTemplateDeclerationException("attribute `test` required for case statement")
return SelectCase(attrs["test"], "\n".join(lines[1:]))
return SelectCase(attrs["test"], "".join(lines[1:]))

def select_shit(self, context: Dict[str, Any]) -> Optional[str]:
for case in self.cases:
Expand Down
8 changes: 4 additions & 4 deletions ShitMountainGenerator/shitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def _loop(self, template: str, line: str, field: str, context: Context) -> str:
template_result = self._run_template(template, inner_context)
result_lines.extend(self._add_indent(template_result, indent))

return "\n".join(result_lines)
return "\n".join(result_lines) + "\n"

def _process_var_operation(self, line: str, expression: str, context: Context) -> str:
if expression.startswith("use("):
replacement = self.select_statements[expression.replace("use(", "")[:-1]].select_shit(context)
replacement = self.select_statements[expression.replace("use(", "")[:-1]].select_shit(context).rstrip()
else:
replacement = str(context[expression])

Expand All @@ -76,7 +76,7 @@ def _process_var_operation(self, line: str, expression: str, context: Context) -
def _run_template(self, template: Template, context: Context) -> str:
result_lines: List[str] = []

content = template.raw_content.splitlines()
content = template.raw_content.splitlines(True)
for line in content:
result_line = line
if (match := self._var_pattern.search(line)) is not None:
Expand All @@ -86,7 +86,7 @@ def _run_template(self, template: Template, context: Context) -> str:

result_lines.append(result_line)

return "\n".join(result_lines)
return "".join(result_lines)

def shit(self, context: Context) -> str:
return self._run_template(self.templates["$main$"], context)
23 changes: 11 additions & 12 deletions ShitMountainGenerator/template_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from ShitMountainGenerator.template import Template


def parse_tmpl(block: str) -> Template:
lines = block.splitlines()
def parse_tmpl(block: List[str]) -> Template:
lines = block
attr_dict = parse_attr(lines[0])
lines = lines[1:]
return Template(attr_dict["name"] if "name" in attr_dict else "$main$", "\n".join(lines))
return Template(attr_dict["name"] if "name" in attr_dict else "$main$", "".join(lines))


def parse_select(block: str) -> SelectStatement:
lines = block.splitlines(True)
def parse_select(lines: List[str]) -> SelectStatement:
attrs = parse_attr(lines[0])
if "name" not in attrs or "var" not in attrs:
raise InvalidTemplateDeclerationException(
Expand All @@ -27,19 +26,19 @@ def parse(data: str) -> Tuple[List[Template], List[SelectStatement]]:
lines = data.splitlines(True)
templates: List[Template] = []
select_statements: List[SelectStatement] = []
block = ""
leading_indent = re.compile(r"^\t|[ ]{4}")
block: List[str] = []
leading_indent = re.compile(r"^\t| {4}")
for line in lines:
if (line.startswith("<tmpl")):
block = line
block = [line]
elif line.startswith("</tmpl>"):
templates.append(parse_tmpl(block))
templates.append(parse_tmpl(block[:]))
elif line.startswith("<select"):
block = line
block = [line]
elif line.startswith("</select>"):
select_statements.append(parse_select(block))
select_statements.append(parse_select(block[:]))
else:
# remove leading indent
block += leading_indent.sub("", line)
block.append(leading_indent.sub("", line, 1))

return templates, select_statements
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='shit_mountain_generator',
version='1.0.1',
version='1.0.2',
packages=['ShitMountainGenerator'],
url='https://github.com/guo40020/shit_mountain_generator',
license='MIT',
Expand Down
4 changes: 4 additions & 0 deletions test_case.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</case>
<case test="False">
"{{ json_name }}": self._{{ py_name }}
asdf
sdfg
edfgj
uior
</case>
</select>

Expand Down

0 comments on commit ac96afd

Please sign in to comment.