Skip to content

Commit

Permalink
New: support debuggingbook fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-zeller committed Nov 16, 2020
1 parent 606331f commit 4b66a78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,10 @@ endif

## Dependencies as graph
NBDEPEND = $(SHARED)utils/nbdepend.py
SITEMAP_OPTIONS = --graph --transitive-reduction # --cluster-by-parts
SITEMAP_OPTIONS = --graph --transitive-reduction --project $(PROJECT) # --cluster-by-parts

sitemap: $(SITEMAP_SVG)
$(SITEMAP_SVG): $(CHAPTER_SOURCES) $(NBDEPEND)
$(SITEMAP_SVG): $(CHAPTER_SOURCES) $(NBDEPEND) $(CHAPTERS_MAKEFILE)
$(PYTHON) $(NBDEPEND) $(SITEMAP_OPTIONS) $(CHAPTER_SOURCES) > $@~ && mv $@~ $@
@$(OPEN) $@

Expand Down
60 changes: 39 additions & 21 deletions notebooks/ClassDiagram.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"metadata": {},
"outputs": [],
"source": [
"class A:\n",
"class A_Class:\n",
" def foo(self):\n",
" pass"
]
Expand All @@ -135,7 +135,7 @@
"metadata": {},
"outputs": [],
"source": [
"class B(A):\n",
"class B_Class(A_Class):\n",
" def foo(self):\n",
" pass\n",
" def bar(self):\n",
Expand All @@ -148,7 +148,7 @@
"metadata": {},
"outputs": [],
"source": [
"class C:\n",
"class C_Class:\n",
" def qux(self):\n",
" pass"
]
Expand All @@ -159,9 +159,9 @@
"metadata": {},
"outputs": [],
"source": [
"class D(B, C):\n",
"class D_Class(B_Class, C_Class):\n",
" def foo(self):\n",
" B.foo(self)\n"
" B_Class.foo(self)\n"
]
},
{
Expand All @@ -170,7 +170,7 @@
"metadata": {},
"outputs": [],
"source": [
"class_hierarchy(A)"
"class_hierarchy(A_Class)"
]
},
{
Expand All @@ -193,7 +193,7 @@
"metadata": {},
"outputs": [],
"source": [
"D.__bases__"
"D_Class.__bases__"
]
},
{
Expand Down Expand Up @@ -237,7 +237,7 @@
"metadata": {},
"outputs": [],
"source": [
"class_tree(D)"
"class_tree(D_Class)"
]
},
{
Expand Down Expand Up @@ -298,7 +298,7 @@
"metadata": {},
"outputs": [],
"source": [
"class_methods(D)"
"class_methods(D_Class)"
]
},
{
Expand Down Expand Up @@ -337,7 +337,7 @@
"metadata": {},
"outputs": [],
"source": [
"public_class_methods(D)"
"public_class_methods(D_Class)"
]
},
{
Expand All @@ -346,7 +346,7 @@
"metadata": {},
"outputs": [],
"source": [
"doc_class_methods(D)"
"doc_class_methods(D_Class)"
]
},
{
Expand All @@ -362,11 +362,19 @@
"metadata": {},
"outputs": [],
"source": [
"def display_class_hierarchy(classes, include_methods=True):\n",
"def display_class_hierarchy(classes, include_methods=True,\n",
" project='fuzzingbook'):\n",
" from graphviz import Digraph\n",
" \n",
" CLASS_FONT = 'Patua One, Helvetica, sans-serif'\n",
" METHOD_FONT = 'monospace'\n",
"\n",
" if project == 'debuggingbook':\n",
" CLASS_FONT = 'Raleway, Helvetica, Arial, sans-serif'\n",
" CLASS_COLOR = 'purple'\n",
" else:\n",
" CLASS_FONT = 'Patua One, Helvetica, sans-serif'\n",
" CLASS_COLOR = '#B03A2E'\n",
"\n",
" METHOD_FONT = \"'Fira Mono', 'Source Code Pro', monospace\"\n",
" METHOD_COLOR = 'black'\n",
"\n",
" if isinstance(classes, list):\n",
" starting_class = classes[0]\n",
Expand All @@ -380,12 +388,12 @@
" dot.attr('edge', arrowhead='empty')\n",
" edges = set()\n",
"\n",
" def method_string(name, f):\n",
" method_string = '<font face=\"' + METHOD_FONT + '\" point-size=\"10\">'\n",
" def method_string(method_name, f):\n",
" method_string = f'<font face=\"{METHOD_FONT}\" point-size=\"10\">'\n",
" if f.__doc__ is not None:\n",
" method_string += '<b>' + name + '()</b>'\n",
" else:\n",
" method_string += '<font color=\"#808080\">' + name + '()</font>'\n",
" method_string += f'<font color=\"{METHOD_COLOR}\">{method_name}()</font>'\n",
" method_string += '</font>'\n",
" return method_string\n",
"\n",
Expand All @@ -408,7 +416,8 @@
" url = cls.__module__ + '.ipynb'\n",
" if include_methods:\n",
" methods = class_methods_string(cls)\n",
" spec = '<{<b><font color=\"#B03A2E\">' + cls.__name__ + '</font></b>|' + methods + '}>'\n",
" spec = '<{<b><font color=\"' + CLASS_COLOR + '\">' + \\\n",
" cls.__name__ + '</font></b>|' + methods + '}>'\n",
" else:\n",
" spec = '<' + cls.__name__ + '>'\n",
" dot.node(name, spec, href=url)\n",
Expand Down Expand Up @@ -437,7 +446,16 @@
"metadata": {},
"outputs": [],
"source": [
"display_class_hierarchy([D, A])"
"display_class_hierarchy([D_Class, A_Class], project='debuggingbook')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display_class_hierarchy([D_Class, A_Class], project='fuzzingbook')"
]
},
{
Expand All @@ -460,7 +478,7 @@
"metadata": {},
"outputs": [],
"source": [
"display_class_hierarchy(D)"
"display_class_hierarchy(D_Class)"
]
},
{
Expand Down
20 changes: 14 additions & 6 deletions utils/nbdepend.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,30 @@ def get_text_contents(notebook):


def draw_notebook_dependencies(notebooks,
format='svg', transitive_reduction=True, clusters=True):
format='svg', transitive_reduction=True, clusters=True, project='fuzzingbook'):
dot = Digraph(comment="Notebook dependencies")
# dot.attr(size='20,30', rank='max')

if project == 'debuggingbook':
fontname = 'Raleway'
fontcolor = 'purple'
else:
fontname = 'Patua One'
fontcolor = '#B03A2E'

node_attrs = {
'shape': 'note', # note, plain, none
'style': 'filled',
'fontname': 'Patua One',
'fontcolor': '#B03A2E',
'fontname': fontname,
'fontcolor': fontcolor,
'fillcolor': 'white'
}
cluster = None

cluster_attrs = {
'shape': 'plain', # note, plain, none
'style': 'filled',
'fontname': 'Patua One',
'fontname': fontname,
'fontcolor': 'black',
'color': '#F0F0F0',
}
Expand All @@ -110,7 +117,7 @@ def draw_notebook_dependencies(notebooks,

cluster = Digraph(name='cluster_' + basename)
cluster.node(basename, label=title, URL='%s.ipynb' % basename,
tooltip=basename, shape='plain', fontname='Patua One')
tooltip=basename, shape='plain', fontname=fontname)

elif cluster is not None:
cluster.node(basename)
Expand Down Expand Up @@ -148,12 +155,13 @@ def draw_notebook_dependencies(notebooks,
parser = argparse.ArgumentParser()
parser.add_argument("--graph", action='store_true', help="Produce graph")
parser.add_argument("--graph-format", action='store', default='svg', help="Graph format (gv, pdf, svg, ...)")
parser.add_argument("--project", action='store', help="Project name")
parser.add_argument("--transitive-reduction", action='store_true', help="Use transitive reduction")
parser.add_argument("--cluster-by-parts", action='store_true', help="Cluster by parts")
parser.add_argument("notebooks", nargs='*', help="notebooks to determine dependencies from")
args = parser.parse_args()

if args.graph:
draw_notebook_dependencies(args.notebooks, args.graph_format, args.transitive_reduction, args.cluster_by_parts)
draw_notebook_dependencies(args.notebooks, args.graph_format, args.transitive_reduction, args.cluster_by_parts, args.project)
else:
print_notebook_dependencies(args.notebooks)

0 comments on commit 4b66a78

Please sign in to comment.