Skip to main content

Generate UML diagrams from source code

Project description

code2uml

将代码转为UML图

输入目录或者文件,输出UML图

根据antlr4完成目前的代码,实现功能:

  1. 支持目录或者单个文件的输入,或者代码段(代码段时必须指定语言类型)
  2. 根据文件名自动识别为C++或者java语言,或者可以通过参数指定语言类型
  3. 根据文件内容自动识别为类、函数、变量、常量、方法等,将其转换成中间语言
  4. 根据中间语言生成plantuml或者mermaid的UML类图
  5. 对于代码段,必须在调用时指定语言类型

编译

pip install .

使用方法

code2uml --input path

Features

  1. add packages in java
  2. add dot ouput

problems

问题1 plantuml无法生成完整的图片

plantuml can't generate complete image

use command: java -jar ~/下载/plantuml-mit-1.2025.0.jar code2uml_java.puml -v

java -jar ~/下载/plantuml-mit-1.2025.0.jar code2uml_java.puml -v
(0.060 - 508 Mo) 499 Mo - SecurityProfile LEGACY
(0.066 - 508 Mo) 499 Mo - PlantUML Version 1.2025.0
(0.066 - 508 Mo) 499 Mo - GraphicsEnvironment.isHeadless() false
(0.066 - 508 Mo) 499 Mo - Forcing resource load on OpenJdk
(0.109 - 508 Mo) 497 Mo - Found 1 files
(0.110 - 508 Mo) 497 Mo - Working on code2uml_java.puml
(0.125 - 508 Mo) 496 Mo - Using default charset
(0.130 - 508 Mo) 496 Mo - Reading from code2uml_java.puml
(0.165 - 508 Mo) 480 Mo - ...text loaded...
(0.237 - 508 Mo) 497 Mo - Setting current dir: frameworks/base/services/core/java/com/android/server/am/.
(0.237 - 508 Mo) 497 Mo - Setting current dir: frameworks/base/services/core/java/com/android/server/am
(0.237 - 508 Mo) 497 Mo - Reading file: code2uml_java.puml
(0.257 - 508 Mo) 496 Mo - ..compiling diagram...
(0.389 - 508 Mo) 464 Mo - Trying to load style plantuml.skin
(0.389 - 508 Mo) 464 Mo - Current dir is frameworks/base/services/core/java/com/android/server/am so trying frameworks/base/services/core/java/com/android/server/am/plantuml.skin
(0.389 - 508 Mo) 464 Mo - File not found : 
(0.391 - 508 Mo) 464 Mo - ... but plantuml.skin found inside the .jar
(0.416 - 508 Mo) 454 Mo - ...parsing ok...
(0.416 - 508 Mo) 454 Mo - Compilation duration 159
(0.417 - 508 Mo) 454 Mo - Creating file: frameworks/base/services/core/java/com/android/server/am/code2uml_java.png
(0.424 - 508 Mo) 454 Mo - Using style root.element.classdiagram.package.title false
(0.519 - 508 Mo) 420 Mo - Using style root.element.statediagram.state.header false
(0.534 - 508 Mo) 416 Mo - Using style root.element.classdiagram.class false
(0.536 - 508 Mo) 416 Mo - Using style root.element.classdiagram.class.header false
(0.538 - 508 Mo) 416 Mo - Using style root.element.spot.spotclass false
(0.715 - 508 Mo) 284 Mo - Using style root.element.classdiagram.arrow false
(0.715 - 508 Mo) 284 Mo - Using style root.element.classdiagram.arrow.cardinality false
(0.754 - 508 Mo) 258 Mo - Using style root.element.classdiagram.group.package false
(0.954 - 508 Mo) 349 Mo - Starting Graphviz process [/usr/bin/dot, -Tsvg]
(0.954 - 508 Mo) 349 Mo - DotString size: 45002
(0.985 - 508 Mo) 347 Mo - Ending process ok
(0.985 - 508 Mo) 347 Mo - Ending Graphviz process
(1.554 - 612 Mo) 419 Mo - Using style root.document.classdiagram false
(1.554 - 612 Mo) 419 Mo - Using style root.document false
(2.004 - 612 Mo) 311 Mo - ...image drawing...
(2.004 - 612 Mo) 309 Mo - Width too large 21790. You should set PLANTUML_LIMIT_SIZE
(2.004 - 612 Mo) 309 Mo - Height too large 22766. You should set PLANTUML_LIMIT_SIZE
(2.005 - 612 Mo) 309 Mo - Creating image 4096x4096
(3.035 - 736 Mo) 253 Mo - Number of image(s): 1

important note:

(2.004 - 612 Mo) 311 Mo - ...image drawing...
(2.004 - 612 Mo) 309 Mo - Width too large 21790. You should set PLANTUML_LIMIT_SIZE
(2.004 - 612 Mo) 309 Mo - Height too large 22766. You should set PLANTUML_LIMIT_SIZE
(2.005 - 612 Mo) 309 Mo - Creating image 4096x4096

使用命令进行替换 java -jar ~/下载/plantuml-mit-1.2025.0.jar code2uml_java.puml -v -DPLANTUML_LIMIT_SIZE=22766

或者使用svg格式

alt text

问题2 dot无法生成图片

使用命令:

dot code2uml_java.dot -v

问题点:

dot - graphviz version 2.43.0 (0)
libdir = "/usr/lib/x86_64-linux-gnu/graphviz"
Activated plugin library: libgvplugin_dot_layout.so.6
Using layout: dot:dot_layout
Activated plugin library: libgvplugin_core.so.6
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
        /usr/lib/x86_64-linux-gnu/graphviz/config6a
                was successfully loaded.
    render      :  cairo dot dot_json fig gd json json0 map mp pic pov ps svg tk visio vml vrml xdot xdot_json
    layout      :  circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
    textlayout  :  textlayout
    device      :  canon cmap cmapx cmapx_np dot dot_json eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg json json0 mp pdf pic plain plain-ext png pov ps ps2 svg svgz tk vdx vml vmlz vrml wbmp webp x11 xdot xdot1.2 xdot1.4 xdot_json xlib
    loadimage   :  (lib) eps gd gd2 gif jpe jpeg jpg png ps svg webp xbm
Error: code2uml_java.dot: syntax error in line 1281 scanning a quoted string (missing endquote? longer than 16384?)
String starting:"{ ActivityManagerService|+ mInstaller : Installer|+ mTaskSupervisor : ActivityTa

dot语言中,label的标签最大长度为16K, 超过16K就会报错,目前官方的版本暂时没有解决这个问题,需要自己修改源码。

问题3 cpp中类图存在问题

cpp目前在antlr4中暂时没有办法解决该问题,属于工具引入的bug,目前无法解决。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code2uml-0.2.tar.gz (250.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code2uml-0.2-py3-none-any.whl (241.9 kB view details)

Uploaded Python 3

File details

Details for the file code2uml-0.2.tar.gz.

File metadata

  • Download URL: code2uml-0.2.tar.gz
  • Upload date:
  • Size: 250.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for code2uml-0.2.tar.gz
Algorithm Hash digest
SHA256 7171fa8084b6f05587071aac7f66a88ed340a1f5880a7b2e01e09dee556cf28e
MD5 1aaf1b5d80b3f6931a879cca17b6226f
BLAKE2b-256 947e9ee629b0194835e206daddb1a3baaac14fc1698d5a819cb2df582a12f02d

See more details on using hashes here.

File details

Details for the file code2uml-0.2-py3-none-any.whl.

File metadata

  • Download URL: code2uml-0.2-py3-none-any.whl
  • Upload date:
  • Size: 241.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for code2uml-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 84eb4473fcc3beadd845676f88f1720d47df63e049c9711fbf8ea289bbdb476b
MD5 de73d78f33045b0482d0e0fda8209889
BLAKE2b-256 c99091f5b641be68b7348ad0588833fab830847e6a70d031f1de5dcecf0a94fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page