Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Feature/add autogenerated samples #9

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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: 4 additions & 0 deletions .circleci/circleci_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export GIT_BRANCH=$CIRCLE_BRANCH
export GIT_COMMIT_HASH=$CIRCLE_SHA1
export CI_USERNAME=$CIRCLE_USERNAME
export CI_BUILD_NUM=$CIRCLE_BUILD_NUM
44 changes: 44 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#user.email: devdoomari@gmail.com
#user.name: devdoomari-circlei
jobs:
push_dist:
docker:
- image: circleci/python:3.7.3
steps:
- checkout
- run:
name: Set git username/email
command: mkdir -p ~/.git
&& git config --global user.email "devdoomari@gmail.com"
&& git config --global user.name "devdoomari.circleci"
- restore_cache:
name: Restore pip package cache
keys:
- pip-packages-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum "requirements_docgen.txt" }}
- run:
name: link pip package folder
command: (ln -s `python -m site --user-site` user-site-packages) || echo "link pip package folder"
- run:
name: Install Dependencies
command: |
pip install --user -r requirements.txt &&
pip install --user -r requirements_docgen.txt &&
echo "python deps installed"
- save_cache:
name: Save pip Package Cache
key: pip-packages-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum "requirements_docgen.txt" }}
paths:
- user-site-packages
- run:
name: build_doc
command: python docgen/docgen.py
- run:
name: pull-merge-build-forcepush
command: |
source .circleci/circleci_env.sh && ./scripts/git_build_and_push_dist.sh
workflows:
version: 2
build_and_push:
jobs:
- push_dist

12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
graphviz = "*"

[packages]

[requires]
python_version = "3.7"
29 changes: 29 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 13 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
Python Packaging User Guide
(continued from previous page)
], )
author_email="author@example.com",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
# Example Package
This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.
# PyCodegen: Generate code from python types!

# Steps involved:

1. convert python code -> intermediary-format: "type-extractor"
- implementation [py_codegen/type_extractor](py_codegen/type_extractor)
- [intermediary-format implementations](py_codegen/type_extractor/nodes/README.md)

2. convert intermediate-format -> custom generated code
- 100% customizable as "plugins"

# Examples of python -> intermediary-format:
-
8 changes: 8 additions & 0 deletions doc/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test
digraph {
A [label="King Arthur"]
B [label="Sir Bedevere the Wise"]
L [label="Sir Lancelot the Brave"]
A -> B
A -> L
}
43 changes: 43 additions & 0 deletions doc/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docgen/docgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from graphviz import Source
11 changes: 11 additions & 0 deletions docgen/graphviz/test.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
digraph G {
splines="FALSE";

/* Entities */
shortName [label="shortName", shape="|square,rectangle,circle,ellipse,triangle,plaintext,point,diamond,pentagon,hexagon,septagon,octagon,egg,trapezium,parallelogram,house,doublecircle,doubleoctagon,tripleoctagon,invtriangle,invtrapezium,invhouse,Mdiamond,Msquare,Mcircle,none,note,tab,folder,box3d|", URL="http://en.wikipedia.org/wiki/John de Fries"]

/* Relationships */
F1 -> shortName[label=".63"]

/* Ranks */
}
Empty file added docgen/overview.py
Empty file.
9 changes: 9 additions & 0 deletions docgen/python_to_jpg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import JpgImageFormatter


def python_code_to_jpg(python_code: str, dest_file_path: str):
result = highlight(python_code, PythonLexer(), JpgImageFormatter())
with open(dest_file_path, 'wb') as file:
file.write(result)
15 changes: 15 additions & 0 deletions docgen/test.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
digraph G {
size ="4,4";
main [shape=box];
main -> parse [weight=8];
parse -> execute;
main -> init [style=dotted];
main -> cleanup;
execute -> { make_string; printf};
init -> make_string;
edge [color=red];
main -> printf [style=bold,label="100 times"];
make_string [label="make a string"];
node [shape=box,style=filled,color=".7 .3 1.0"];
execute -> compare;
}
1 change: 0 additions & 1 deletion py_codegen/test_fixtures/func_with_typed_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from mypy_extensions import TypedDict



SimpleTypedDict1 = TypedDict('SimpleTypedDict1', {
'a': str,
})
35 changes: 35 additions & 0 deletions py_codegen/type_extractor/__generate_samples__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Callable
import inspect

from py_codegen.test_fixtures.func_return_none import func_return_nullable
from py_codegen.test_fixtures.func_not_annotated import func_not_annotated
from py_codegen.test_fixtures.func_with_typed_dict import func_with_typed_dict
from py_codegen.type_extractor.__tests__.test_func_with_typed_dict import test_func_with_typed_dict


def gen_sample_func_doc(func: Callable, test_func: Callable):
src = ''.join(inspect.getsourcelines(func)[0])
test_src = ''.join(inspect.getsourcelines(test_func)[0])


to_write = f'''
# Example for {func.__qualname__} :

## Original Source:
```python
{src}
```

## Test:
```
{test_src}
'''
return to_write


def save_sample_func_doc(func: Callable, test_func: Callable):
func_doc = gen_sample_func_doc(func, test_func)
func_name = func.__qualname__
open(f'./__samples__/{func_name}.md')

test = save_sample_func(func_with_typed_dict, test_func_with_typed_dict)
11 changes: 11 additions & 0 deletions py_codegen/type_extractor/__tests__/test_class_with_union_field.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@

from py_codegen.test_fixtures.union_type_class import ClassWithUnionField
from py_codegen.type_extractor.nodes.BaseNodeType import BaseNodeType
from py_codegen.type_extractor.nodes.ClassFound import ClassFound
from py_codegen.type_extractor.nodes.TypeOR import TypeOR
from py_codegen.type_extractor.__tests__.utils import traverse, cleanup
from py_codegen.type_extractor.type_extractor import TypeExtractor

import textwrap
import pprint

def format_cls(cls: ClassFound):
raw = pprint.pformat(cls)



def test_class_with_union_field():
type_collector = TypeExtractor()
@@ -26,4 +35,6 @@ def test_class_with_union_field():
)
}
functions = type_collector.functions
clsfound = classes['ClassWithUnionField']
assert functions == {}
import pdb;pdb.set_trace()
10 changes: 5 additions & 5 deletions py_codegen/type_extractor/__tests__/test_func_with_typed_dict.py
Original file line number Diff line number Diff line change
@@ -25,9 +25,9 @@ def test_func_with_typed_dict():
annotations={
'a': str,
},
)
),
},
)
),
},
return_type=TypedDictFound(
annotations={
@@ -36,10 +36,10 @@ def test_func_with_typed_dict():
name='SomeClass',
fields={
'a': int,
}
)
},
),
},
name='OutputType'
name='OutputType',
)
),
cleanup,
1 change: 1 addition & 0 deletions py_codegen/type_extractor/nodes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# type_extractor nodes
1 change: 1 addition & 0 deletions requirements_docgen.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
graphviz>=0.10.1
23 changes: 23 additions & 0 deletions scripts/git_build_and_push_dist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if [ "${GIT_BRANCH}" == "master" ] || [[ "${GIT_BRANCH}" == dist* ]]
then
exit
fi
if [ "${GIT_BRANCH}" == "develop" ]
then
git checkout master
else
git checkout -b $'dist-'"$GIT_BRANCH"
fi
git checkout master
mkdir -p ~/.git && git config user.email "devdoomari@gmail.com" && git config user.name "devdoomari.circleci"
git merge -X theirs --no-edit $GIT_BRANCH

git add -f lib docs
git commit --allow-empty -m $'generated from:'"$GIT_COMMIT_HASH"$'\ntriggered by:'"$CI_USERNAME"$'\n[ci skip]'
git tag -a $'dist_'"$GIT_BRANCH"'_'"$CI_BUILD_NUM" -m "."
if [ "${GIT_BRANCH}" == "develop" ]
then
git push --set-upstream origin master --force --follow-tags
else
git push --set-upstream origin $'dist-'"$GIT_BRANCH" --force --follow-tags
fi