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

Add a module import view. #16

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
django = "==1.8"
django-environ = "*"
pywatchman = "*"

[packages]
attrs = "*"
pydeps = "*"
django = "~=1.8"

[requires]
python_version = "2.7"
98 changes: 98 additions & 0 deletions Pipfile.lock

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

73 changes: 73 additions & 0 deletions schema_graph/modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import logging

from django.conf import settings
from pydeps import cli, py2depgraph, target

from .schema import Schema

log = logging.getLogger(__name__)


def _get_label(name):
return _split_name(name)[0]


def _split_name(name):
result = name.split('.', 1)
if len(result) == 1:
return [result[0]] * 2
return result


def get_modules_as_schema(base_dir):
cli.verbose = lambda *args: log.debug(*args)
MAX_BACON = 2 # default: 2**65
EXCLUDE_MODULES = [
'django'
]

excludes = []
for name in EXCLUDE_MODULES:
# excludes.append(name)
excludes.append(name+'.*')
kwargs = dict(
T='svg', config=None, debug=False, display=None, exclude=excludes,
exclude_exact=[],
externals=False, format='svg', max_bacon=MAX_BACON, no_config=True,
nodot=False,
noise_level=2**65, noshow=True, output=None, pylib=False, pylib_all=False,
show=False, show_cycles=False, show_deps=False, show_dot=False,
show_raw_deps=False, verbose=0, include_missing=True, start_color=0
)
graph = py2depgraph.py2dep(target.Target(base_dir), **kwargs)

nodes = set()
imports = set()
for a, b in graph:
# b imports a
nodes.add(a.name)
nodes.add(b.name)
new_import = [tuple(_split_name(b.name)), tuple(_split_name(a.name))]
log.debug("adding import %s", new_import)
imports.add(tuple(new_import))

models = {}
for name in nodes:
label, short_name = _split_name(name)
models.setdefault(label, set()).add(short_name)

for app_group in models.keys():
models[app_group] = sorted(models[app_group])

return Schema(
# Vertices
abstract_models={},
models=models,
proxies=[],
# Edges
foreign_keys=sorted(imports),
inheritance=[],
many_to_manys=[],
one_to_ones=[],
)

26 changes: 26 additions & 0 deletions schema_graph/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.views.generic import TemplateView

from schema_graph.schema import get_schema
from schema_graph.modules import get_modules_as_schema


def debug_required(view_function):
Expand Down Expand Up @@ -39,3 +40,28 @@ def get_context_data(self, **kwargs):
}
)
return super(Schema, self).get_context_data(**kwargs)


class Modules(TemplateView):
template_name = "schema_graph/schema.html"
base_dir = None

# Not using `name="dispatch"` here so that we can support Django 1.8.
@method_decorator(debug_required)
def dispatch(self, request, *args, **kwargs):
return super(Modules, self).dispatch(request, *args, **kwargs)

def get_context_data(self, **kwargs):
schema = get_modules_as_schema(base_dir=self.base_dir)
kwargs.update(
{
"abstract_models": json.dumps(schema.abstract_models),
"models": json.dumps(schema.models),
"foreign_keys": json.dumps(schema.foreign_keys),
"many_to_manys": json.dumps(schema.many_to_manys),
"one_to_ones": json.dumps(schema.one_to_ones),
"inheritance": json.dumps(schema.inheritance),
"proxies": json.dumps(schema.proxies),
}
)
return super(Modules, self).get_context_data(**kwargs)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def run(self):
cmdclass={"verify": VerifyVersionCommand},
description="An interactive graph of your Django model structure.",
include_package_data=True,
install_requires=["attrs"],
install_requires=["attrs", "pydeps"],
license="MIT",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
16 changes: 13 additions & 3 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from schema_graph.views import Schema
import os

from schema_graph.views import Modules, Schema


base_dir = os.path.dirname(__file__)

try:
# Django 2+:
from django.urls import path

urlpatterns = [path("", Schema.as_view())]
urlpatterns = [
path("", Schema.as_view()),
path("deps", Schema.as_view()),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Modules ;-)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth creating a urls.py to make it easier to include them together?

]
except ImportError:
# Django < 2:
from django.conf.urls import url

urlpatterns = [url(r"^$", Schema.as_view())]
urlpatterns = [
url(r"^$", Schema.as_view()),
url(r"^deps$", Modules.as_view(base_dir=base_dir)),
]