Skip to content

Commit c422b27

Browse files
committed
improve pydot typing
1 parent e6031ac commit c422b27

File tree

4 files changed

+168
-39
lines changed

4 files changed

+168
-39
lines changed

cwltool/cwlviewer.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
"""Visualize a CWL workflow."""
22
import os
3+
from pathlib import Path
34
from urllib.parse import urlparse
45

5-
import pydot # type: ignore
6+
import pydot
67
import rdflib
78

9+
_queries_dir = (Path(__file__).parent / "rdfqueries").resolve()
10+
_get_inner_edges_query_path = _queries_dir / "get_inner_edges.sparql"
11+
_get_input_edges_query_path = _queries_dir / "get_input_edges.sparql"
12+
_get_output_edges_query_path = _queries_dir / "get_output_edges.sparql"
13+
_get_root_query_path = _queries_dir / "get_root.sparql"
14+
815

916
class CWLViewer:
1017
"""Produce similar images with the https://github.com/common-workflow-language/cwlviewer."""
1118

12-
_queries_dir = os.path.join(
13-
os.path.abspath(os.path.dirname(__file__)), "rdfqueries"
14-
)
15-
_get_inner_edges_query_path = os.path.join(_queries_dir, "get_inner_edges.sparql")
16-
_get_input_edges_query_path = os.path.join(_queries_dir, "get_input_edges.sparql")
17-
_get_output_edges_query_path = os.path.join(_queries_dir, "get_output_edges.sparql")
18-
_get_root_query_path = os.path.join(_queries_dir, "get_root.sparql")
19-
20-
def __init__(
21-
self, rdf_description # type: str
22-
):
19+
def __init__(self, rdf_description: str):
2320
"""Create a viewer object based on the rdf description of the workflow."""
24-
self._dot_graph = CWLViewer._init_dot_graph() # type: pydot.Graph
25-
self._rdf_graph = self._load_cwl_graph(
26-
rdf_description
27-
) # type: rdflib.graph.Graph
28-
self._root_graph_uri = self._get_root_graph_uri() # type: str
21+
self._dot_graph: pydot.Graph = CWLViewer._init_dot_graph()
22+
self._rdf_graph: rdflib.graph.Graph = self._load_cwl_graph(rdf_description)
23+
self._root_graph_uri: str = self._get_root_graph_uri()
2924
self._set_inner_edges()
3025
self._set_input_edges()
3126
self._set_output_edges()
3227

33-
def _load_cwl_graph(
34-
self, rdf_description # type: str
35-
) -> rdflib.graph.Graph:
28+
def _load_cwl_graph(self, rdf_description: str) -> rdflib.graph.Graph:
3629
rdf_graph = rdflib.Graph()
3730
rdf_graph.parse(data=rdf_description, format="n3")
3831
return rdf_graph
3932

4033
def _set_inner_edges(self) -> None:
41-
with open(self._get_inner_edges_query_path) as f:
34+
with open(_get_inner_edges_query_path) as f:
4235
get_inner_edges_query = f.read()
4336
inner_edges = self._rdf_graph.query(
4437
get_inner_edges_query, initBindings={"root_graph": self._root_graph_uri}
@@ -80,14 +73,14 @@ def _set_inner_edges(self) -> None:
8073
)
8174

8275
def _set_input_edges(self) -> None:
83-
with open(self._get_input_edges_query_path) as f:
76+
with open(_get_input_edges_query_path) as f:
8477
get_input_edges_query = f.read()
8578
inputs_subgraph = pydot.Subgraph(graph_name="cluster_inputs")
8679
self._dot_graph.add_subgraph(inputs_subgraph)
87-
inputs_subgraph.set_rank("same")
80+
inputs_subgraph.set("rank", "same")
8881
inputs_subgraph.create_attribute_methods(["style"])
89-
inputs_subgraph.set_style("dashed")
90-
inputs_subgraph.set_label("Workflow Inputs")
82+
inputs_subgraph.set("style", "dashed")
83+
inputs_subgraph.set("label", "Workflow Inputs")
9184

9285
input_edges = self._rdf_graph.query(
9386
get_input_edges_query, initBindings={"root_graph": self._root_graph_uri}
@@ -107,15 +100,15 @@ def _set_input_edges(self) -> None:
107100
)
108101

109102
def _set_output_edges(self) -> None:
110-
with open(self._get_output_edges_query_path) as f:
103+
with open(_get_output_edges_query_path) as f:
111104
get_output_edges = f.read()
112105
outputs_graph = pydot.Subgraph(graph_name="cluster_outputs")
113106
self._dot_graph.add_subgraph(outputs_graph)
114-
outputs_graph.set_rank("same")
107+
outputs_graph.set("rank", "same")
115108
outputs_graph.create_attribute_methods(["style"])
116-
outputs_graph.set_style("dashed")
117-
outputs_graph.set_label("Workflow Outputs")
118-
outputs_graph.set_labelloc("b")
109+
outputs_graph.set("style", "dashed")
110+
outputs_graph.set("label", "Workflow Outputs")
111+
outputs_graph.set("labelloc", "b")
119112
output_edges = self._rdf_graph.query(
120113
get_output_edges, initBindings={"root_graph": self._root_graph_uri}
121114
)
@@ -134,7 +127,7 @@ def _set_output_edges(self) -> None:
134127
)
135128

136129
def _get_root_graph_uri(self) -> rdflib.URIRef:
137-
with open(self._get_root_query_path) as f:
130+
with open(_get_root_query_path) as f:
138131
get_root_query = f.read()
139132
root = list(
140133
self._rdf_graph.query(
@@ -152,11 +145,11 @@ def _get_root_graph_uri(self) -> rdflib.URIRef:
152145
@classmethod
153146
def _init_dot_graph(cls) -> pydot.Graph:
154147
graph = pydot.Graph(graph_type="digraph", simplify=False)
155-
graph.set_bgcolor("#eeeeee")
156-
graph.set_clusterrank("local")
157-
graph.set_labelloc("bottom")
158-
graph.set_labelloc("bottom")
159-
graph.set_labeljust("right")
148+
graph.set("bgcolor", "#eeeeee")
149+
graph.set("clusterrank", "local")
150+
graph.set("labelloc", "bottom")
151+
graph.set("labelloc", "bottom")
152+
graph.set("labeljust", "right")
160153

161154
return graph
162155

tests/test_content_type.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from typing import Any
22

3-
import pydot # type: ignore
3+
import pydot
4+
from pytest import LogCaptureFixture
45

56
from .util import get_main_output
67

78

8-
def test_content_types(caplog: Any) -> None:
9+
def test_content_types(caplog: LogCaptureFixture) -> None:
910
for _ in ("js_output.cwl", "js_output_workflow.cwl"):
1011
commands = [
1112
"https://raw.githubusercontent.com/common-workflow-language/common-workflow-language/main/v1.0/v1.0/test-cwl-out2.cwl",

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any, Dict, List, Union, cast
1212
from urllib.parse import urlparse
1313

14-
import pydot # type: ignore
14+
import pydot
1515
import pytest
1616
from ruamel.yaml.comments import CommentedMap, CommentedSeq
1717
from schema_salad.exceptions import ValidationException

typeshed/pydot.pyi

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from typing import Any, List
2+
3+
PY3: Any
4+
str_type = str
5+
GRAPH_ATTRIBUTES: Any
6+
EDGE_ATTRIBUTES: Any
7+
NODE_ATTRIBUTES: Any
8+
CLUSTER_ATTRIBUTES: Any
9+
DEFAULT_PROGRAMS: Any
10+
11+
def is_windows() -> bool: ...
12+
def is_anaconda() -> bool: ...
13+
def get_executable_extension() -> str: ...
14+
def call_graphviz(program, arguments, working_dir, **kwargs): ...
15+
16+
class frozendict(dict):
17+
__delitem__: Any
18+
__setitem__: Any
19+
clear: Any
20+
pop: Any
21+
popitem: Any
22+
setdefault: Any
23+
update: Any
24+
def __new__(cls, *args, **kw): ...
25+
def __init__(self, *args, **kw) -> None: ...
26+
def __hash__(self): ...
27+
28+
dot_keywords: Any
29+
id_re_alpha_nums: Any
30+
id_re_alpha_nums_with_ports: Any
31+
id_re_num: Any
32+
id_re_with_port: Any
33+
id_re_dbl_quoted: Any
34+
id_re_html: Any
35+
36+
def needs_quotes(s): ...
37+
def quote_if_necessary(s): ...
38+
def graph_from_dot_data(s) -> List['Dot']: ...
39+
def graph_from_dot_file(path, encoding: Any | None = ...): ...
40+
def graph_from_edges(edge_list, node_prefix: str = ..., directed: bool = ...): ...
41+
def graph_from_adjacency_matrix(matrix, node_prefix: str = ..., directed: bool = ...): ...
42+
def graph_from_incidence_matrix(matrix, node_prefix: str = ..., directed: bool = ...): ...
43+
44+
class Common:
45+
def __get_attribute__(self, attr): ...
46+
def set_parent_graph(self, parent_graph) -> None: ...
47+
def get_parent_graph(self): ...
48+
def set(self, name, value) -> None: ...
49+
def get(self, name): ...
50+
def get_attributes(self): ...
51+
def set_sequence(self, seq) -> None: ...
52+
def get_sequence(self): ...
53+
def create_attribute_methods(self, obj_attributes: List[str]): ...
54+
55+
class Error(Exception):
56+
value: Any
57+
def __init__(self, value) -> None: ...
58+
59+
class InvocationException(Exception):
60+
value: Any
61+
def __init__(self, value) -> None: ...
62+
63+
class Node(Common):
64+
obj_dict: Any
65+
def __init__(self, name: str = ..., obj_dict: Any | None = ..., **attrs) -> None: ...
66+
def set_name(self, node_name) -> None: ...
67+
def get_name(self): ...
68+
def get_port(self): ...
69+
def add_style(self, style) -> None: ...
70+
def to_string(self) -> str: ...
71+
72+
class Edge(Common):
73+
obj_dict: Any
74+
def __init__(self, src: str = ..., dst: str = ..., obj_dict: Any | None = ..., **attrs) -> None: ...
75+
def get_source(self): ...
76+
def get_destination(self): ...
77+
def __hash__(self): ...
78+
def __eq__(self, edge): ...
79+
def parse_node_ref(self, node_str): ...
80+
def to_string(self) -> str: ...
81+
82+
class Graph(Common):
83+
obj_dict: Any
84+
def __init__(self, graph_name: str = ..., obj_dict: Any | None = ..., graph_type: str = ..., strict: bool = ..., suppress_disconnected: bool = ..., simplify: bool = ..., **attrs) -> None: ...
85+
def get_graph_type(self): ...
86+
def get_top_graph_type(self): ...
87+
def set_graph_defaults(self, **attrs) -> None: ...
88+
def get_graph_defaults(self, **attrs): ...
89+
def set_node_defaults(self, **attrs) -> None: ...
90+
def get_node_defaults(self, **attrs): ...
91+
def set_edge_defaults(self, **attrs) -> None: ...
92+
def get_edge_defaults(self, **attrs): ...
93+
def set_simplify(self, simplify) -> None: ...
94+
def get_simplify(self): ...
95+
def set_type(self, graph_type) -> None: ...
96+
def get_type(self): ...
97+
def set_name(self, graph_name) -> None: ...
98+
def get_name(self): ...
99+
def set_strict(self, val) -> None: ...
100+
def get_strict(self, val): ...
101+
def set_suppress_disconnected(self, val) -> None: ...
102+
def get_suppress_disconnected(self, val): ...
103+
def get_next_sequence_number(self): ...
104+
def add_node(self, graph_node) -> None: ...
105+
def del_node(self, name, index: Any | None = ...): ...
106+
def get_node(self, name): ...
107+
def get_nodes(self): ...
108+
def get_node_list(self): ...
109+
def add_edge(self, graph_edge) -> None: ...
110+
def del_edge(self, src_or_list, dst: Any | None = ..., index: Any | None = ...): ...
111+
def get_edge(self, src_or_list, dst: Any | None = ...): ...
112+
def get_edges(self): ...
113+
def get_edge_list(self): ...
114+
def add_subgraph(self, sgraph) -> None: ...
115+
def get_subgraph(self, name): ...
116+
def get_subgraphs(self): ...
117+
def get_subgraph_list(self): ...
118+
def set_parent_graph(self, parent_graph) -> None: ...
119+
def to_string(self) -> str: ...
120+
121+
class Subgraph(Graph):
122+
def __init__(self, graph_name: str = ..., obj_dict: Any | None = ..., suppress_disconnected: bool = ..., simplify: bool = ..., **attrs) -> None: ...
123+
124+
class Cluster(Graph):
125+
def __init__(self, graph_name: str = ..., obj_dict: Any | None = ..., suppress_disconnected: bool = ..., simplify: bool = ..., **attrs) -> None: ...
126+
127+
class Dot(Graph):
128+
shape_files: Any
129+
formats: Any
130+
prog: str
131+
def __init__(self, *argsl, **argsd): ...
132+
def set_shape_files(self, file_paths) -> None: ...
133+
def set_prog(self, prog) -> None: ...
134+
def write(self, path, prog: Any | None = ..., format: str = ..., encoding: Any | None = ...): ...
135+
def create(self, prog: Any | None = ..., format: str = ..., encoding: Any | None = ...): ...

0 commit comments

Comments
 (0)