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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to code formatter Black #267
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5af44e0
Switch to code formatter Black: Preparations
peternowee 65988af
Switch to code formatter Black: Config and 1st run
peternowee cdbfc3e
Switch to code formatter Black: Quotes, 2nd run
peternowee 9377e3b
Switch to code formatter Black: Dev dependencies
peternowee 479bdca
Switch to code formatter Black: Travis integration
peternowee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,25 @@ | |
import sys | ||
|
||
from pyparsing import ( | ||
nestedExpr, Literal, CaselessLiteral, | ||
Word, OneOrMore, | ||
nestedExpr, | ||
Literal, | ||
CaselessLiteral, | ||
Word, | ||
OneOrMore, | ||
Forward, | ||
Group, Optional, Combine, | ||
restOfLine, cStyleComment, nums, alphanums, | ||
Group, | ||
Optional, | ||
Combine, | ||
restOfLine, | ||
cStyleComment, | ||
nums, | ||
alphanums, | ||
printables, | ||
ParseException, ParseResults, CharsNotIn, | ||
QuotedString) | ||
ParseException, | ||
ParseResults, | ||
CharsNotIn, | ||
QuotedString, | ||
) | ||
|
||
import pydot | ||
|
||
|
@@ -35,15 +46,14 @@ | |
|
||
|
||
class P_AttrList(object): | ||
|
||
def __init__(self, toks): | ||
self.attrs = {} | ||
i = 0 | ||
|
||
while i < len(toks): | ||
attrname = toks[i] | ||
if i+2 < len(toks) and toks[i+1] == '=': | ||
attrvalue = toks[i+2] | ||
if i + 2 < len(toks) and toks[i + 1] == '=': | ||
attrvalue = toks[i + 2] | ||
i += 3 | ||
else: | ||
attrvalue = None | ||
|
@@ -56,14 +66,16 @@ def __repr__(self): | |
|
||
|
||
class DefaultStatement(P_AttrList): | ||
|
||
def __init__(self, default_type, attrs): | ||
self.default_type = default_type | ||
self.attrs = attrs | ||
|
||
def __repr__(self): | ||
return "%s(%s, %r)" % (self.__class__.__name__, | ||
self.default_type, self.attrs) | ||
return "%s(%s, %r)" % ( | ||
self.__class__.__name__, | ||
self.default_type, | ||
self.attrs, | ||
) | ||
|
||
|
||
top_graphs = list() | ||
|
@@ -75,9 +87,11 @@ def push_top_graph_stmt(str, loc, toks): | |
|
||
for element in toks: | ||
|
||
if (isinstance(element, (ParseResults, tuple, list)) and | ||
len(element) == 1 and | ||
isinstance(element[0], str_type)): | ||
if ( | ||
isinstance(element, (ParseResults, tuple, list)) | ||
and len(element) == 1 | ||
and isinstance(element[0], str_type) | ||
): | ||
|
||
element = element[0] | ||
|
||
|
@@ -113,7 +127,8 @@ def push_top_graph_stmt(str, loc, toks): | |
|
||
else: | ||
raise ValueError( | ||
'Unknown element statement: {s}'.format(s=element)) | ||
'Unknown element statement: {s}'.format(s=element) | ||
) | ||
|
||
for g in top_graphs: | ||
update_parent_graph_hierarchy(g) | ||
|
@@ -140,25 +155,29 @@ def update_parent_graph_hierarchy(g, parent_graph=None, level=0): | |
|
||
for key, objs in item_dict[key_name].items(): | ||
for obj in objs: | ||
if ('parent_graph' in obj and | ||
obj['parent_graph'].get_parent_graph() == g): | ||
if ( | ||
'parent_graph' in obj | ||
and obj['parent_graph'].get_parent_graph() == g | ||
): | ||
if obj['parent_graph'] is g: | ||
pass | ||
else: | ||
obj['parent_graph'].set_parent_graph(parent_graph) | ||
|
||
if key_name == 'edges' and len(key) == 2: | ||
for idx, vertex in enumerate(obj['points']): | ||
if isinstance(vertex, | ||
(pydot.Graph, | ||
pydot.Subgraph, pydot.Cluster)): | ||
if isinstance( | ||
vertex, | ||
(pydot.Graph, pydot.Subgraph, pydot.Cluster), | ||
): | ||
vertex.set_parent_graph(parent_graph) | ||
if isinstance(vertex, pydot.frozendict): | ||
if vertex['parent_graph'] is g: | ||
pass | ||
else: | ||
vertex['parent_graph'].set_parent_graph( | ||
parent_graph) | ||
parent_graph | ||
) | ||
|
||
|
||
def add_defaults(element, defaults): | ||
|
@@ -168,8 +187,9 @@ def add_defaults(element, defaults): | |
d[key] = value | ||
|
||
|
||
def add_elements(g, toks, defaults_graph=None, | ||
defaults_node=None, defaults_edge=None): | ||
def add_elements( | ||
g, toks, defaults_graph=None, defaults_node=None, defaults_edge=None | ||
): | ||
if defaults_graph is None: | ||
defaults_graph = {} | ||
if defaults_node is None: | ||
|
@@ -197,8 +217,9 @@ def add_elements(g, toks, defaults_graph=None, | |
elif isinstance(element, ParseResults): | ||
|
||
for e in element: | ||
add_elements(g, [e], defaults_graph, | ||
defaults_node, defaults_edge) | ||
add_elements( | ||
g, [e], defaults_graph, defaults_node, defaults_edge | ||
) | ||
|
||
elif isinstance(element, DefaultStatement): | ||
|
||
|
@@ -221,15 +242,18 @@ def add_elements(g, toks, defaults_graph=None, | |
else: | ||
raise ValueError( | ||
'Unknown DefaultStatement: {s}'.format( | ||
s=element.default_type)) | ||
s=element.default_type | ||
) | ||
) | ||
|
||
elif isinstance(element, P_AttrList): | ||
|
||
g.obj_dict['attributes'].update(element.attrs) | ||
|
||
else: | ||
raise ValueError( | ||
'Unknown element statement: {s}'.format(s=element)) | ||
'Unknown element statement: {s}'.format(s=element) | ||
) | ||
|
||
|
||
def push_graph_stmt(str, loc, toks): | ||
|
@@ -268,8 +292,7 @@ def push_default_stmt(str, loc, toks): | |
if default_type in ['graph', 'node', 'edge']: | ||
return DefaultStatement(default_type, attrs) | ||
else: | ||
raise ValueError( | ||
'Unknown default statement: {s}'.format(s=toks)) | ||
raise ValueError('Unknown default statement: {s}'.format(s=toks)) | ||
|
||
|
||
def push_attr_list(str, loc, toks): | ||
|
@@ -290,7 +313,7 @@ def get_port(node): | |
def do_node_ports(node): | ||
node_port = '' | ||
if len(node) > 1: | ||
node_port = ''.join([str(a)+str(b) for a, b in node[1]]) | ||
node_port = ''.join([str(a) + str(b) for a, b in node[1]]) | ||
|
||
return node_port | ||
|
||
|
@@ -311,16 +334,16 @@ def push_edge_stmt(str, loc, toks): | |
|
||
if isinstance(toks[2][0], ParseResults): | ||
|
||
n_next_list = [[n.get_name(), ] for n in toks[2][0]] | ||
n_next_list = [[n.get_name()] for n in toks[2][0]] | ||
for n_next in [n for n in n_next_list]: | ||
n_next_port = do_node_ports(n_next) | ||
e.append(pydot.Edge(n_prev, n_next[0]+n_next_port, **attrs)) | ||
e.append(pydot.Edge(n_prev, n_next[0] + n_next_port, **attrs)) | ||
|
||
elif isinstance(toks[2][0], pydot.Graph): | ||
|
||
e.append(pydot.Edge(n_prev, | ||
pydot.frozendict(toks[2][0].obj_dict), | ||
**attrs)) | ||
e.append( | ||
pydot.Edge(n_prev, pydot.frozendict(toks[2][0].obj_dict), **attrs) | ||
) | ||
|
||
elif isinstance(toks[2][0], pydot.Node): | ||
|
||
|
@@ -338,18 +361,21 @@ def push_edge_stmt(str, loc, toks): | |
|
||
for n_next in [n for n in tuple(toks)[2::2]]: | ||
|
||
if (isinstance(n_next, P_AttrList) or | ||
not isinstance(n_next[0], str_type)): | ||
if isinstance(n_next, P_AttrList) or not isinstance( | ||
n_next[0], str_type | ||
): | ||
continue | ||
|
||
n_next_port = do_node_ports(n_next) | ||
e.append(pydot.Edge(n_prev, n_next[0]+n_next_port, **attrs)) | ||
e.append(pydot.Edge(n_prev, n_next[0] + n_next_port, **attrs)) | ||
|
||
n_prev = n_next[0]+n_next_port | ||
n_prev = n_next[0] + n_next_port | ||
else: | ||
raise Exception( | ||
'Edge target {r} with type {s} unsupported.'.format( | ||
r=toks[2][0], s=type(toks[2][0]))) | ||
r=toks[2][0], s=type(toks[2][0]) | ||
) | ||
) | ||
|
||
return e | ||
|
||
|
@@ -417,84 +443,95 @@ def parse_html(s, loc, toks): | |
|
||
opener = '<' | ||
closer = '>' | ||
html_text = nestedExpr( | ||
opener, closer, (CharsNotIn(opener + closer)) | ||
).setParseAction(parse_html).leaveWhitespace() | ||
html_text = ( | ||
nestedExpr(opener, closer, (CharsNotIn(opener + closer))) | ||
.setParseAction(parse_html) | ||
.leaveWhitespace() | ||
) | ||
|
||
ID = (identifier | html_text | double_quoted_string | | ||
alphastring_).setName("ID") | ||
ID = ( | ||
identifier | html_text | double_quoted_string | alphastring_ | ||
).setName("ID") | ||
|
||
float_number = Combine( | ||
Optional(minus) + | ||
OneOrMore(Word(nums + ".")) | ||
Optional(minus) + OneOrMore(Word(nums + ".")) | ||
).setName("float_number") | ||
|
||
righthand_id = (float_number | ID).setName("righthand_id") | ||
|
||
port_angle = (at + ID).setName("port_angle") | ||
|
||
port_location = ( | ||
OneOrMore(Group(colon + ID)) | | ||
Group(colon + lparen + ID + comma + ID + rparen) | ||
OneOrMore(Group(colon + ID)) | ||
| Group(colon + lparen + ID + comma + ID + rparen) | ||
).setName("port_location") | ||
|
||
port = ( | ||
Group(port_location + Optional(port_angle)) | | ||
Group(port_angle + Optional(port_location)) | ||
Group(port_location + Optional(port_angle)) | ||
| Group(port_angle + Optional(port_location)) | ||
).setName("port") | ||
|
||
node_id = (ID + Optional(port)) | ||
node_id = ID + Optional(port) | ||
a_list = OneOrMore( | ||
ID + | ||
Optional(equals + righthand_id) + | ||
Optional(comma.suppress()) | ||
ID + Optional(equals + righthand_id) + Optional(comma.suppress()) | ||
).setName("a_list") | ||
|
||
attr_list = OneOrMore( | ||
lbrack.suppress() + | ||
Optional(a_list) + | ||
rbrack.suppress() | ||
lbrack.suppress() + Optional(a_list) + rbrack.suppress() | ||
).setName("attr_list") | ||
|
||
attr_stmt = (Group(graph_ | node_ | edge_) + | ||
attr_list).setName("attr_stmt") | ||
attr_stmt = (Group(graph_ | node_ | edge_) + attr_list).setName( | ||
"attr_stmt" | ||
) | ||
|
||
edgeop = (Literal("--") | Literal("->")).setName("edgeop") | ||
|
||
stmt_list = Forward() | ||
graph_stmt = Group( | ||
lbrace.suppress() + | ||
Optional(stmt_list) + | ||
rbrace.suppress() + | ||
Optional(semi.suppress()) | ||
lbrace.suppress() | ||
+ Optional(stmt_list) | ||
+ rbrace.suppress() | ||
+ Optional(semi.suppress()) | ||
).setName("graph_stmt") | ||
|
||
edge_point = Forward() | ||
|
||
edgeRHS = OneOrMore(edgeop + edge_point) | ||
edge_stmt = edge_point + edgeRHS + Optional(attr_list) | ||
|
||
subgraph = Group( | ||
subgraph_ + Optional(ID) + graph_stmt).setName("subgraph") | ||
subgraph = Group(subgraph_ + Optional(ID) + graph_stmt).setName( | ||
"subgraph" | ||
) | ||
|
||
edge_point << Group( | ||
subgraph | graph_stmt | node_id).setName('edge_point') | ||
edge_point << Group(subgraph | graph_stmt | node_id).setName( | ||
'edge_point' | ||
) | ||
|
||
node_stmt = ( | ||
node_id + Optional(attr_list) + | ||
Optional(semi.suppress())).setName("node_stmt") | ||
node_id + Optional(attr_list) + Optional(semi.suppress()) | ||
).setName("node_stmt") | ||
|
||
assignment = (ID + equals + righthand_id).setName("assignment") | ||
stmt = (assignment | edge_stmt | attr_stmt | | ||
subgraph | graph_stmt | node_stmt).setName("stmt") | ||
stmt = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar concern, but these cases might be more limited than imports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Also not big enough to warrant an exception, though. |
||
assignment | ||
| edge_stmt | ||
| attr_stmt | ||
| subgraph | ||
| graph_stmt | ||
| node_stmt | ||
).setName("stmt") | ||
stmt_list << OneOrMore(stmt + Optional(semi.suppress())) | ||
|
||
graphparser = OneOrMore( | ||
(Optional(strict_) + Group((graph_ | digraph_)) + | ||
Optional(ID) + graph_stmt).setResultsName("graph")) | ||
( | ||
Optional(strict_) | ||
+ Group((graph_ | digraph_)) | ||
+ Optional(ID) | ||
+ graph_stmt | ||
).setResultsName("graph") | ||
) | ||
|
||
singleLineComment = Group( | ||
"//" + restOfLine) | Group("#" + restOfLine) | ||
singleLineComment = Group("//" + restOfLine) | Group("#" + restOfLine) | ||
|
||
# actions | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One change that according to me is not optimal, if we think about "better readability".
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.
Agreed. Black does this to minimize diffs, but when it results in a lot of lines it becomes disturbing. The alternative would be to wrap it in
# fmt: off
and# fmt: on
pragmas, just like I did for the*_ATTRIBUTES
constants at the top ofpydot.py
. However, I also wanted to keep the number of such exceptions low and thought the problem here (from 8 lines to 19 lines) was not big enough to warrant another exception. Just one of the things we have to accept as the price for switching toblack
, was my conclusion.