-
Notifications
You must be signed in to change notification settings - Fork 13
Add 'ignore' to graph_transition command #56
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,25 +16,31 @@ def all_fsm_fields_data(model): | |||||||
| return [(field, model) for field in model._meta.get_fields() if isinstance(field, FSMFieldMixin)] | ||||||||
|
|
||||||||
|
|
||||||||
| def node_name(field, state): | ||||||||
| def node_name(field, state) -> str: | ||||||||
| opts = field.model._meta | ||||||||
| return "{}.{}.{}.{}".format(opts.app_label, opts.verbose_name.replace(" ", "_"), field.name, state) | ||||||||
|
|
||||||||
|
|
||||||||
| def node_label(field, state): | ||||||||
| if isinstance(state, int) or (isinstance(state, bool) and hasattr(field, "choices")): | ||||||||
| def node_label(field, state) -> str: | ||||||||
| if isinstance(state, int): | ||||||||
| return str(state) | ||||||||
| if isinstance(state, bool) and hasattr(field, "choices"): | ||||||||
| return force_str(dict(field.choices).get(state)) | ||||||||
| return state | ||||||||
|
|
||||||||
|
|
||||||||
| def generate_dot(fields_data): # noqa: C901, PLR0912 | ||||||||
| def generate_dot(fields_data, ignore_transitions: list[str] | None = None): # noqa: C901, PLR0912 | ||||||||
| ignore_transitions = ignore_transitions or [] | ||||||||
| result = graphviz.Digraph() | ||||||||
|
|
||||||||
| for field, model in fields_data: | ||||||||
| sources, targets, edges, any_targets, any_except_targets = set(), set(), set(), set(), set() | ||||||||
|
|
||||||||
| # dump nodes and edges | ||||||||
| for transition in field.get_all_transitions(model): | ||||||||
| if transition.name in ignore_transitions: | ||||||||
| continue | ||||||||
|
|
||||||||
| _targets = list( | ||||||||
| (state for state in transition.target.allowed_states) | ||||||||
| if isinstance(transition.target, (GET_STATE, RETURN_VALUE)) | ||||||||
|
|
@@ -127,7 +133,7 @@ def add_arguments(self, parser): | |||||||
| "-o", | ||||||||
| action="store", | ||||||||
| dest="outputfile", | ||||||||
| help=("Render output file. Type of output dependent on file extensions. Use png or jpg to render graph to image."), | ||||||||
| help="Render output file. Type of output dependent on file extensions. Use png or jpg to render graph to image.", | ||||||||
| ) | ||||||||
| parser.add_argument( | ||||||||
| "--layout", | ||||||||
|
|
@@ -137,6 +143,14 @@ def add_arguments(self, parser): | |||||||
| default="dot", | ||||||||
| help=f"Layout to be used by GraphViz for visualization. Layouts: {get_graphviz_layouts()}.", | ||||||||
| ) | ||||||||
| parser.add_argument( | ||||||||
| "--exclude", | ||||||||
| "-e", | ||||||||
| action="store", | ||||||||
| dest="exclude", | ||||||||
| default="", | ||||||||
| help="Ignore transitions with this name.", | ||||||||
| ) | ||||||||
| parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]")) | ||||||||
|
|
||||||||
| def render_output(self, graph, **options): | ||||||||
|
|
@@ -153,9 +167,8 @@ def handle(self, *args, **options): | |||||||
| field_spec = arg.split(".") | ||||||||
|
|
||||||||
| if len(field_spec) == 1: | ||||||||
| app = apps.get_app(field_spec[0]) | ||||||||
| models = apps.get_models(app) | ||||||||
| for model in models: | ||||||||
| app = apps.get_app_config(field_spec[0]) | ||||||||
| for model in apps.get_models(app): | ||||||||
| fields_data += all_fsm_fields_data(model) | ||||||||
| if len(field_spec) == 2: # noqa: PLR2004 | ||||||||
| model = apps.get_model(field_spec[0], field_spec[1]) | ||||||||
|
|
@@ -166,7 +179,8 @@ def handle(self, *args, **options): | |||||||
| else: | ||||||||
| for model in apps.get_models(): | ||||||||
| fields_data += all_fsm_fields_data(model) | ||||||||
| dotdata = generate_dot(fields_data) | ||||||||
|
|
||||||||
| dotdata = generate_dot(fields_data, ignore_transitions=options["exclude"].split(",")) | ||||||||
|
||||||||
| dotdata = generate_dot(fields_data, ignore_transitions=options["exclude"].split(",")) | |
| exclude_transitions = [name.strip() for name in options["exclude"].split(",") if name.strip()] if options["exclude"] else [] | |
| dotdata = generate_dot(fields_data, ignore_transitions=exclude_transitions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apps.get_app was renamed decades ago 😅