Skip to content

Commit a9eceb3

Browse files
committed
wip: working, but requires testing
Got this to an initial working state, but now requires more eyes on it and additional tests.
0 parents  commit a9eceb3

12 files changed

+992
-0
lines changed

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.7-alpine
2+
3+
RUN apk add --no-cache gcc libc-dev unixodbc-dev
4+
5+
RUN mkdir /opt/representer
6+
COPY . /opt/representer
7+
WORKDIR /opt/representer
8+
9+
RUN pip install -r requirements.txt
10+
11+
ENTRYPOINT ["sh", "/opt/representer/bin/generate.sh"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Michael Morehouse
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Exercism Python Representer
2+
3+
An [automated anaylsis](analyis) tool that parses a Python file to create a normalized `representation.txt` file per the [representer interface][interface].
4+
5+
[analysis]: https://github.com/exercism/automated-analysis
6+
[interface]: https://github.com/exercism/automated-analysis/blob/master/docs/representers/interface.md
7+
8+

bin/generate.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/env python3
2+
"""
3+
CLI for the representer for the Python track on Exercism.io.
4+
"""
5+
from argparse import ArgumentParser, ArgumentTypeError
6+
7+
import representer
8+
9+
10+
def _slug(arg):
11+
try:
12+
return representer.utils.slug(arg)
13+
except ValueError as err:
14+
raise ArgumentTypeError(str(err))
15+
16+
17+
def _directory(arg):
18+
try:
19+
return representer.utils.directory(arg)
20+
except (FileNotFoundError, PermissionError) as err:
21+
raise ArgumentTypeError(str(err))
22+
23+
24+
def main():
25+
"""
26+
Parse CLI arguments and create a representation.txt.
27+
"""
28+
parser = ArgumentParser(
29+
description="Produce a normalized representation of a Python exercise."
30+
)
31+
32+
parser.add_argument(
33+
"slug", metavar="SLUG", type=_slug, help="name of the exercise to process",
34+
)
35+
36+
parser.add_argument(
37+
"directory",
38+
metavar="DIR",
39+
type=_directory,
40+
help="directory where the [EXERCISE.py] file is located",
41+
)
42+
43+
args = parser.parse_args()
44+
representer.represent(args.slug, args.directory)
45+
46+
47+
if __name__ == "__main__":
48+
main()

bin/generate.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/sh
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
3+
SRC="$( dirname "$DIR" )/src"
4+
export PYTHONPATH="$SRC:$PYTHONPATH"
5+
python bin/generate.py "$@"

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
astor==0.8.0
2+
black==19.10b0

src/representer/__init__.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Representer for Python.
3+
"""
4+
from typing import Dict
5+
6+
from . import utils
7+
from .normalizer import Normalizer
8+
9+
10+
class Representer:
11+
"""
12+
Represent Python code in normalized form.
13+
"""
14+
15+
def __init__(self, source: str) -> None:
16+
self._tree = utils.parse(source)
17+
self._normalizer = Normalizer()
18+
19+
def normalize(self) -> None:
20+
"""
21+
Normalize the tree.
22+
"""
23+
self._tree = self._normalizer.visit(self._tree)
24+
25+
def dump_tree(self) -> str:
26+
"""
27+
Dump the current state of the tree for printing.
28+
"""
29+
return utils.dump_tree(self._tree)
30+
31+
def dump_code(self, reformat=True) -> str:
32+
"""
33+
Dump the current tree as generate code.
34+
"""
35+
code = utils.to_source(self._tree)
36+
if reformat:
37+
return utils.reformat(code)
38+
return code
39+
40+
@property
41+
def mapping(self) -> Dict[str, str]:
42+
"""
43+
Get the placeholder assignments after normalize.
44+
"""
45+
return self._normalizer.get_placeholders()
46+
47+
def dump_map(self) -> str:
48+
"""
49+
Dump the tree's mapping of placeholders.
50+
"""
51+
return utils.to_json(self.mapping)
52+
53+
54+
def represent(slug: utils.Slug, directory: utils.Directory) -> None:
55+
"""
56+
Normalize the `directory/slug.py` file representation.
57+
"""
58+
src = directory.joinpath(slug.replace("-", "_") + ".py")
59+
out_dst = directory.joinpath("representation.out")
60+
txt_dst = directory.joinpath("representation.txt")
61+
map_dst = directory.joinpath("mapping.json")
62+
63+
# parse the tree from the file contents
64+
representation = Representer(src.read_text())
65+
66+
# save dump of the initial tree for debug
67+
out = ["# BEGIN TREE BEFORE", representation.dump_tree(), ""]
68+
69+
# normalize the tree
70+
representation.normalize()
71+
72+
# save dump of the normalized tree for debug
73+
out.extend(["# BEGIN TREE AFTER", representation.dump_tree()])
74+
75+
# dump the representation files
76+
out_dst.write_text("\n".join(out))
77+
txt_dst.write_text(representation.dump_code())
78+
map_dst.write_text(representation.dump_map())

0 commit comments

Comments
 (0)