-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to generate a graph from a model
- Loading branch information
Patrick Valsecchi
committed
Mar 16, 2017
1 parent
22ba5ff
commit 1407587
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python3 | ||
from c2cwsgiutils.models_graph import generate_model_graph | ||
|
||
from c2cwsgiutils_app import models | ||
|
||
|
||
def main(): | ||
generate_model_graph(models) | ||
|
||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import inspect | ||
import sqlalchemy as sa | ||
import sys | ||
|
||
|
||
def generate_model_graph(module): | ||
if len(sys.argv) == 1: | ||
base_name = 'Base' | ||
elif len(sys.argv) == 2: | ||
base_name = sys.argv[1] | ||
else: | ||
print("Invalid parameters\nUsage: %s [base_class]" % sys.argv[0]) | ||
exit(1) | ||
|
||
_generate_model_graph(module, getattr(module, base_name)) | ||
|
||
|
||
def _generate_model_graph(module, base): | ||
print(""" | ||
digraph { | ||
rankdir=BT; | ||
""") | ||
|
||
interesting = { | ||
getattr(module, symbol_name) | ||
for symbol_name in dir(module) | ||
if _is_interesting(getattr(module, symbol_name), base) | ||
} | ||
|
||
for symbol in list(interesting): | ||
symbol = getattr(module, symbol.__name__) | ||
if _is_interesting(symbol, base): | ||
_print_node(symbol, interesting) | ||
|
||
print("}") | ||
|
||
|
||
def _print_node(symbol, interesting): | ||
print('%s [label="%s", shape=box];' % (symbol.__name__, _get_table_desc(symbol))) | ||
for parent in symbol.__bases__: | ||
if parent != object: | ||
if parent not in interesting: | ||
_print_node(parent, interesting) | ||
interesting.add(parent) | ||
print("%s -> %s;" % (symbol.__name__, parent.__name__)) | ||
|
||
|
||
def _is_interesting(what, base): | ||
return inspect.isclass(what) and issubclass(what, base) | ||
|
||
|
||
def _get_table_desc(symbol): | ||
cols = [symbol.__name__, ""] + _get_local_cols(symbol) | ||
|
||
return "\\n".join(cols) | ||
|
||
|
||
def _get_all_cols(symbol): | ||
cols = [] | ||
|
||
for member_name in symbol.__dict__: | ||
member = getattr(symbol, member_name) | ||
if member_name in ('__table__', 'metadata'): | ||
pass | ||
elif isinstance(member, sa.sql.schema.SchemaItem): | ||
cols.append(member_name + ('[null]' if member.nullable else '')) | ||
elif isinstance(member, sa.orm.attributes.InstrumentedAttribute): | ||
nullable = member.property.columns[0].nullable \ | ||
if isinstance(member.property, sa.orm.ColumnProperty) \ | ||
else False | ||
link = not isinstance(member.property, sa.orm.ColumnProperty) | ||
cols.append(member_name + (' [null]' if nullable else '') + (' ->' if link else '')) | ||
|
||
return cols | ||
|
||
|
||
def _get_local_cols(symbol): | ||
result = set(_get_all_cols(symbol)) | ||
for parent in symbol.__bases__: | ||
result -= set(_get_all_cols(parent)) | ||
|
||
return sorted(list(result)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from setuptools import setup, find_packages | ||
|
||
|
||
version = '0.4.0' | ||
version = '0.5.0' | ||
|
||
setup( | ||
name='c2cwsgiutils', | ||
|