Turn your Python and Javascript code into DOT flowcharts

Notes from 2017

  • This is an older project which I am no longer working on. It was built before ES6 existed and before Python 3 had much usage. While it was always experimental and will probably still give you insights into your code, it will increasingly show signs of age. I am also unable to addess any issues, bugs, or pull requests.
  • Like anyone who has been an engineer for more than 6 months, I am downright embarrassed by code I wrote when I was younger and this is no exception. Tabs!?!? What was I thinking????
  • The domain, code2flow.com is unrelated to this project and as far as I can tell through the internet archive, they launched their service after this repository was created. I've never heard anything from them and it doesn't appear like they use anything from here

code2flow

Turn your Python and Javascript source code into DOT flowcharts

Code2flow will sweep through your project source code looking for function definitions. Then it will do another sweep looking for where those functions are called. Code2flow connects the dots and presents you with a flowchart estimating the functional structure of your program.

In other words, code2flow generates callgraphs

Code2flow is especially useful for untangling spaghetti code and getting new developers up to speed.

Code2flow is EXPERIMENTAL and meant to provide a rough overview of the structure of simpler projects. There are many known limitations (see below). Expect MOST aspects of this application to change in future releases.

Here is what happens when you run it on jquery Alt text

On the python calendar module Alt text

On code2flow/languages/python.py Alt text

Installation

Download, navigate to the directory, and run:

sudo python setup.py install

If you don't have it already, you will also have to install graphviz

Using apt-get:

sudo apt-get install graphviz

Using port (for macs):

sudo port install graphviz

Usage

To generate a DOT file run something like:

code2flow mypythonfile.py

Or, for javascript

code2flow myjavascriptfile.js

By default, code2flow will render a DOT file, out.gv and a PNG file, out.png.

You can also render the flowchart in any of the formats that graphviz supports: bmp canon cgimage cmap cmapx cmapx_np dot eps exr fig gif gv imap imap_np ismap jp2 jpe jpeg jpg pct pdf pic pict plain plain-ext png pov ps ps2 psd sgi svg svgz tga tif tiff tk vml vmlz x11 xdot xlib

For example:

code2flow mypythonfile.py -o myflow.jpeg

Specify multiple files, import directories, and even use *

code2flow project/directory/*.js
code2flow project/directory --language js

Limitations

Code2flow is meant to provide a reasonable conjecture of the structure of simple projects and has many known limitations.

  • Arrays of functions are not handled
  • The logic for whether or not a function returns is simply looking for 'return' in that function
  • Functions not declared in the initial class/object definitions (e.g. attached later) are mostly not handled
  • Dynamically generated and lambda functions are mostly not handled
  • In python, functions inherited from a parent class are not handled
  • In python, import ... as ... is not handled correctly
  • In javascript, prototypes will result in unpredictable results
  • And many more

Basically, code2flow may not diagram your sourcecode exactly as you might expect it to

Feedback / Bugs / Contact

Please do email! [email protected]

How to contribute

  1. You can contribute code! The project is open source and is new so any reasonably useful feature would probably be helpful and accepted. New languages are especially appreciated!

  2. You can spread the word! A simple way to help is to share this project with others. If you have a blog, mention code2flow! Linking from relevant questions on StackOverflow or other programming forums also helps quite a bit. I would do it myself but it is unfortunately against the community guidelines. The more exposure this project gets, the more I can devote my time to building it!

Feature / Language Requests

There is a lot in the pipeline already but email me! Those requests which keep coming up repeatedly will get priority.

To get the feature you want more quickly there are two options:

A. The project is open source so it is easy to contribute.

B. I am available for hire on contract and will happily build your request or just do headstands for you all day for the correct amount of money. For more about me, visit http://scottrogowski.com/about

Owner
Scott Rogowski
https://ffer.io && https://fastmap.io
Scott Rogowski
Comments
  • UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 4895: character maps to <undefined>

    UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 4895: character maps to

    C:\Users\sterg\Documents\GitHub\sparks-baird\CrabNet\crabnet>code2flow model.py
    Code2Flow: Found 1 files from sources argument.
    Code2Flow: Implicitly detected language as 'py'.
    Code2Flow: Processing 1 source file(s).
    Code2Flow:   model.py
    Traceback (most recent call last):
      File "C:\Users\sterg\AppData\Local\Programs\Python\Python39\Scripts\code2flow-script.py", line 33, in <module>
        sys.exit(load_entry_point('code2flow==2.2.0', 'console_scripts', 'code2flow')())
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\site-packages\code2flow\engine.py", line 625, in main
        code2flow(
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\site-packages\code2flow\engine.py", line 531, in code2flow
        file_groups, all_nodes, edges = map_it(sources, language, no_trimming,
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\site-packages\code2flow\engine.py", line 317, in map_it
        raise ex
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\site-packages\code2flow\engine.py", line 312, in map_it
        file_ast_trees.append((source, language.get_tree(source, lang_params)))
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\site-packages\code2flow\python.py", line 155, in get_tree
        tree = ast.parse(f.read())
      File "c:\users\sterg\appdata\local\programs\python\python39\lib\encodings\cp1252.py", line 23, in decode
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 4895: character maps to <undefined>
    
  • Request: Output all call sites which refer to a given function

    Request: Output all call sites which refer to a given function

    Scenario: I have a method (ruby) defined in a model somewhere. I'd like to identify all call sites which call this method and call sites that refer to those call sites up to some given limit. This sounds like a subset of the graph which is generated.

  • Code2flow installs improperly, is added to PATH, and opens

    Code2flow installs improperly, is added to PATH, and opens "How do you want to open this file?" menu

    Following the instructions in the README, using python install setup.py, code2flow is added to PATH, but when trying to run, whether with a file or by itself, it instead opens a "How do you want to open this file?" menu.

    If you select a text editor it will bring up this:

    #!"C:\Users\[User]\AppData\Local\Programs\Python\Python39\python.exe"
    # EASY-INSTALL-SCRIPT: 'code2flow==2.0.1','code2flow'
    __requires__ = 'code2flow==2.0.1'
    __import__('pkg_resources').run_script('code2flow==2.0.1', 'code2flow')
    

    Uninstalling and reinstalling it does nothing.

    Using Python 3.9.1 on Windows 10.

  • [Feature request] Graphing jQuery calls

    [Feature request] Graphing jQuery calls

    This might be a tough feature, yet it would be exceptionally useful if available. jQuery calls start with a dollar sign, or with (jQuery) with the parenthesis if used in non-conflict mode (iirc). That's where a lot of drawing is done.

    I could give it a shot when I find some time.

  • Request for PHP support

    Request for PHP support

    From my experience I have seen the messiest code written in PHP. Hence I'd like to see support for PHP.

    What do you think is required to implement a new language in code2flow?

  • Thoughts on making JS prototypes work

    Thoughts on making JS prototypes work

    It'd be nice if prototypes worked correctly. But there's the question of how they'd be represented on the graph.

    Just to think out loud, this would possibly require code2flow to be able to differentiate between object methods (e.g. toString) and normal functions. This would then allow the graph to represent which object method is being called (e.g. if a class overrode the Object.toString method, that could be represented.

    One thing I'm curious about is your decision to 'home-grow' the regular expressions etc - have you deliberately decided not to use existing tools (say uglifyjs) to generate an AST and work from there? On a related note, have you seen https://github.com/abort/javascript-call-graph?

    Just pondering on the most appropriate tool for the job at the moment.

  • Re exception: bogus escape: '\\2' (Javascript)

    Re exception: bogus escape: '\\2' (Javascript)

    Input file is called "main.js" (so nothing spectacular).

    Here's the traceback (I tried to correct the output from Powershell):

    PS C:\Users\xxx> ..\..\Python\2.7\python.exe .\Apps\code2flow-master\code2fl
    ow .\Apps\code2flow-master\2graph\main.js
    Mapping .\Apps\code2flow-master\2graph\main
    Removing comments and strings...
    Generating function nodes...
    Traceback (most recent call last):
      File ".\Apps\code2flow-master\code2flow", line 151, in <module>
        groups,nodes,edges = mapper.map()
      File "C:\Users\xxx\Apps\code2flow-master\code2flowlib\engine.py", line 814, in map
        fileGroup = self.generateFileGroup(name=filename,source=source)
      File "C:\Users\xxx\Apps\code2flow-master\code2flowlib\languages\javascript.py", line 329, in generateFileGroup
        return Group(name=name,source=source,fullSource=source,isFunction=True)
      File "C:\Users\xxx\Apps\code2flow-master\code2flowlib\languages\javascript.py", line 79, in __init__
        super(Group,self).__init__(**kwargs)
      File "C:\Users\xxx\Apps\code2flow-master\code2flowlib\engine.py", line 215, in __init__
        self.newObjectPattern = self.generateNewObjectPattern()
      File "C:\Users\xxx\Apps\code2flow-master\code2flowlib\languages\javascript.py", line 170, in generateNewObjectPattern
        return re.compile(r'new\s+%s\s*\('%self.name)
      File "C:\Python\2.7\lib\re.py", line 190, in compile
        return _compile(pattern, flags)
      File "C:\Python\2.7\lib\re.py", line 244, in _compile
        raise error, v # invalid expression
    sre_constants.error: bogus escape: '\\2'
    
    
  • [Feature Request] A select Namespace argument

    [Feature Request] A select Namespace argument

    Currently, in my project, I just care about how a couple of classes interact with each other. Using code2flow has been an amazing way to see just how they are connected and show it to others. The issue is I've had to write a script that gets every other namespace to ignore with the exceptions of the ones I want. The ability to say, I want a graph with just these classes would be amazing.

  • Python: Implicit decorator calls breaks `process_assign` with AssertionError

    Python: Implicit decorator calls breaks `process_assign` with AssertionError

    Sample code

    from typing import Callable
    
    def trace(fn: Callable) -> Callable:
        def wrapper(*args, **kwargs):
            print('traced call')
            return fn(*args, **kwargs)
        return wrapper
    
    def do_something(msg):
        return msg + ' world'
    
    message = 'hello'
    new_message = trace(do_something)(message)
    

    when fed into code2flow would produce an AssertionError

    File "%/bin/code2flow", line 33, in <module>
    sys.exit(load_entry_point('code2flow', 'console_scripts', 'code2flow')())
    File "%/code2flow/engine.py", line 630, in main
    code2flow(
    File "%/code2flow/engine.py", line 536, in code2flow
    file_groups, all_nodes, edges = map_it(sources, language, no_trimming,
    File "%/code2flow/engine.py", line 324, in map_it
    file_group = make_file_group(file_ast_tree, source, extension)
    File "%/code2flow/engine.py", line 208, in make_file_group
    file_group.add_node(language.make_root_node(body_trees, parent=file_group), is_root=True)
    File "%/code2flow/python.py", line 229, in make_root_node
    variables = make_local_variables(lines, parent)
    File "%/code2flow/python.py", line 120, in make_local_variables
    variables += process_assign(element)
    File "%/code2flow/python.py", line 79, in process_assign
    ret.append(Variable(token, call, element.lineno))
    File "%/code2flow/model.py", line 159, in __init__
    assert points_to
    
  • Request: New Shape

    Request: New Shape

    As per Russian standard for block-schemes, there is a shape for functions/methods defined elsewhere. It looks like this in plain style: изображение

    I'll try to take a look at the code on my free time, but will promise nothing

  • Performance

    Performance

    I am running this on a ~1.2mb 38kloc browserified source file, and it's been running for > 10 mins at 100% cpu on a core i7. no disk output, and no output from strace so it appears internally cpu bound. a note of advisement as to the applicability to project size would probably be helpful.

    Update: Traceback when I Ctrl-C it is:

    Traceback (most recent call last):
      File "/home/user/.pyenv/versions/2.7.9/bin/code2flow", line 4, in <module>
        __import__('pkg_resources').run_script('code2flow==0.2', 'code2flow')
      File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 723, in run_script
      File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 1643, in run_script
      File "/home/user/.pyenv/versions/2.7.9/lib/python2.7/site-packages/code2flow-0.2-py2.7.egg/EGG-INFO/scripts/code2flow", line 151, in <module>
    
      File "build/bdist.linux-x86_64/egg/code2flowlib/engine.py", line 810, in map
      File "build/bdist.linux-x86_64/egg/code2flowlib/engine.py", line 369, in __init__
      File "build/bdist.linux-x86_64/egg/code2flowlib/engine.py", line 721, in _removeCommentsAndStrings
    

    Update 2: the original source file had (believe it or not) >2mb of sourceMap (a single massive line) appended to it. Once I removed it, code2flow did complete after several minutes, so perhaps there is a pathological case in extremely long comment.

  • Request: distinguish functions with the same name but in different files

    Request: distinguish functions with the same name but in different files

    Hello! I tried to use code2flow on some python projects, And found it cannot distinguish files with the same name but in different directories, or functions with the same name but in different files. Also it becomes a mess if the two cases happen in the same time.

    I also looked into the source code. The first problem can be fixed by changing line 348 in engine.py to include the full path into group name. But the second one seems much more difficult. I guess what you get from an ast Call node is only the function name, not enough to tell which function it is if there are more than one function with the same name in the project. But maybe we can use import statement to do this. But to do this, we need to relate the module name in import statement with the corresponding file full path.

    Maybe you can check this problem and see if there's another solution? I think this is not a rare case so a solution is needed.

  • Request: Allow depth/downstream-depth without `--target-function`

    Request: Allow depth/downstream-depth without `--target-function`

    I'm trying to use code2flow on a very large codebase and the resulting out.png file keeps getting scaled to the point where it is unreadable.

    I would like to be able to set a graph depth without having to pick a particular function.

    code2flow my_package --downstream-depth 3
    

    Or maybe just a generic --depth.

    Scaled out.png file

    image

    ~I realize there's probably a way to get dot/graphwhiz to reduce the scaling, but I haven't figured that out yet.~ Also I only really care about the first few nodes in the graph.

    Edit: changing the output type to .svg solved the issue with low res .png files. But I still think being able to set a depth without specifying a target function would be a good feature.

    Very cool project BTW.

A very minimalistic python module that lets you track the time your code snippets take to run.

Clock Keeper A very minimalistic python module that lets you track the time your code snippets take to run. This package is available on PyPI! Run the

Jan 19, 2022
coala provides a unified command-line interface for linting and fixing all your code, regardless of the programming languages you use.

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." ― John F. Woods coala provides a

Dec 5, 2022
Robocop is a tool that performs static code analysis of Robot Framework code.
Robocop is a tool that performs static code analysis of Robot Framework code.

Robocop Introduction Documentation Values Requirements Installation Usage Example Robotidy FAQ Watch our talk from RoboCon 2021 about Robocop and Robo

Nov 23, 2022
CodeAnalysis - Static Code Analysis: a code comprehensive analysis platform
CodeAnalysis - Static Code Analysis: a code comprehensive analysis platform

TCA, Tencent Cloud Code Analysis English | 简体中文 What is TCA Tencent Cloud Code A

Dec 5, 2022
Alarmer is a tool focus on error reporting for your application.

alarmer Alarmer is a tool focus on error reporting for your application. Installation pip install alarmer Usage It's simple to integrate alarmer in yo

Jul 3, 2022
Python package to parse and generate C/C++ code as context aware preprocessor.

Devana Devana is a python tool that make it easy to parsing, format, transform and generate C++ (or C) code. This tool uses libclang to parse the code

Apr 3, 2022
Find dead Python code

Vulture - Find dead code Vulture finds unused code in Python programs. This is useful for cleaning up and finding errors in large code bases. If you r

Nov 30, 2022
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna; pydocstyle (form

Nov 30, 2022
The uncompromising Python code formatter
The uncompromising Python code formatter

The Uncompromising Code Formatter “Any color you like.” Black is the uncompromising Python code formatter. By using it, you agree to cede control over

Nov 27, 2022
A static type analyzer for Python code

pytype - ? ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Nov 30, 2022
Find usage statistics (imports, function calls, attribute access) for Python code-bases

Python Library stats This is a small library that allows you to query some useful statistics for Python code-bases. We currently report library import

May 2, 2022
This is a Python program to get the source lines of code (SLOC) count for a given GitHub repository.

This is a Python program to get the source lines of code (SLOC) count for a given GitHub repository.

Mar 10, 2022
Guesslang detects the programming language of a given source code
Guesslang detects the programming language of a given source code

Detect the programming language of a source code

Nov 29, 2022
Learning source code review, spot vulnerability, find some ways how to fix it.

Learn Source Code Review Learning source code review, spot vulnerability, find some ways how to fix it. WordPress Plugin Authenticated Stored XSS on C

Nov 29, 2022
An app to show the total number of lines of code written by an user.

Lines of code Have you ever wondered how many lines of code you wrote in github? This tool will calculate it for you! To calculate the total number of

Jan 26, 2022
Metrinome is an all-purpose tool for working with code complexity metrics.
Metrinome is an all-purpose tool for working with code complexity metrics.

Overview Metrinome is an all-purpose tool for working with code complexity metrics. It can be used as both a REPL and API, and includes: Converters to

Sep 27, 2022
A simple stopwatch for measuring code performance with static typing.

A simple stopwatch for measuring code performance. This is a fork from python-stopwatch, which adds static typing and a few other things.

Feb 18, 2022
Inspects Python source files and provides information about type and location of classes, methods etc

prospector About Prospector is a tool to analyse Python code and output information about errors, potential problems, convention violations and comple

Nov 26, 2022
pycallgraph is a Python module that creates call graphs for Python programs.
pycallgraph is a Python module that creates call graphs for Python programs.

Project Abandoned Many apologies. I've stopped maintaining this project due to personal time constraints. Blog post with more information. I'm happy t

Nov 29, 2022