thebjorn/pydeps

Programmatic use

Closed this issue ยท 5 comments

I'm trying to use pydeps as a library, to get the SVG as text, and not output it in a file.

The examples in #50 do not allow to do that.

What I'm doing instead is this:

from pydeps import py2depgraph, dot
from pydeps.pydeps import depgraph_to_dotsrc
from pydeps.target import Target

target = Target("src/mkdocstrings")
with target.chdir_work():
    dep_graph = py2depgraph.py2dep(target)
dot_src = depgraph_to_dotsrc(target, dep_graph)
svg = dot.call_graphviz_dot(dot_src, "svg")

The problem is that this code reaches cli.verbose(...) while cli.verbose is not initialized, and is still None, resulting in a not callable exception. This forces me to do this:

from pydeps import cli

cli.verbose = cli._not_verbose

...which is a bit cumbersome ๐Ÿ˜…

Then I ran into other errors because of missing arguments, and it's very hard to know which one I need since they are hidden along a good part of the call chain in **kwargs dicts, and otherwise are not set to default values. I had to run back and forth debugging inspection to get the first options:

options = {"exclude_exact": [], "exclude": [], "only": ["mkdocstrings"], "show_cycles": False, "show_raw_deps": False}

...then stopped and decided to actually use the argument parser:

options = cli.parse_args(["src/mkdocstrings", "--noshow", "--only", "mkdocstrings"])

target = Target(options["fname"])
with target.chdir_work():
    dep_graph = py2depgraph.py2dep(target, **options)
dot_src = depgraph_to_dotsrc(target, dep_graph, **options)

Anyway, I got it to work, so it's fine, this is just some feedback on using pydeps as a library ๐Ÿ˜…

Hope this can help others ๐Ÿ™‚

To use --start-color, we must also do this manually:

from pydeps import colors
colors.START_COLOR = options["start_color"]

The next version of pydeps will have a call_pydeps(file_or_dir, **kwargs) function where you only have to specify the options you want to override.

Amazing, thanks @thebjorn!

v1.12.5 is now available on PyPI.

I looked at the code and it is still not as programmatic as I would like it to be ๐Ÿ˜„
call_pydeps returns either a list of externals, or _pydeps(inp, **ctx) which actually returns nothing.
So, it's better than before as you can use call_pydeps from Python to generate SVG or else, but you still cannot get the SVG contents without writing it to a file.

Don't bother too much about my use case though, I have working code already ๐Ÿ™‚