Skip to content
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

[DNM] Compare format-black vs. format-yapf-facebook #7

Open
wants to merge 5 commits into
base: format-black
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .style.yapf
@@ -0,0 +1,4 @@
[style]
based_on_style=facebook
blank_line_before_nested_class_or_def=True
column_limit=79
119 changes: 43 additions & 76 deletions dot_parser.py
Expand Up @@ -12,32 +12,16 @@
import sys

from pyparsing import (
nestedExpr,
Literal,
CaselessLiteral,
Word,
OneOrMore,
Forward,
Group,
Optional,
Combine,
restOfLine,
cStyleComment,
nums,
alphanums,
printables,
ParseException,
ParseResults,
CharsNotIn,
QuotedString,
nestedExpr, Literal, CaselessLiteral, Word, OneOrMore, Forward, Group,
Optional, Combine, restOfLine, cStyleComment, nums, alphanums, printables,
ParseException, ParseResults, CharsNotIn, QuotedString
)

import pydot

__author__ = ['Michael Krause', 'Ero Carrera']
__license__ = 'MIT'


PY3 = sys.version_info >= (3, 0, 0)
if PY3:
str_type = str
Expand All @@ -46,6 +30,7 @@


class P_AttrList(object):

def __init__(self, toks):
self.attrs = {}
i = 0
Expand All @@ -66,15 +51,14 @@ 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,
self.__class__.__name__, self.default_type, self.attrs
)


Expand All @@ -88,9 +72,8 @@ 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)
isinstance(element, (ParseResults, tuple, list)) and
len(element) == 1 and isinstance(element[0], str_type)
):

element = element[0]
Expand Down Expand Up @@ -143,7 +126,7 @@ def update_parent_graph_hierarchy(g, parent_graph=None, level=0):
if parent_graph is None:
parent_graph = g

for key_name in ('edges',):
for key_name in ('edges', ):

if isinstance(g, pydot.frozendict):
item_dict = g
Expand All @@ -156,8 +139,8 @@ 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
'parent_graph' in obj and
obj['parent_graph'].get_parent_graph() == g
):
if obj['parent_graph'] is g:
pass
Expand All @@ -168,7 +151,7 @@ def update_parent_graph_hierarchy(g, parent_graph=None, level=0):
for idx, vertex in enumerate(obj['points']):
if isinstance(
vertex,
(pydot.Graph, pydot.Subgraph, pydot.Cluster),
(pydot.Graph, pydot.Subgraph, pydot.Cluster)
):
vertex.set_parent_graph(parent_graph)
if isinstance(vertex, pydot.frozendict):
Expand Down Expand Up @@ -334,12 +317,9 @@ 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))
Expand All @@ -366,8 +346,9 @@ 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

Expand Down Expand Up @@ -448,35 +429,32 @@ 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 + "."))
).setName("float_number")
float_number = Combine(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())
).setName("a_list")
Expand All @@ -485,54 +463,43 @@ def parse_html(s, loc, toks):
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")

assignment = (ID + equals + righthand_id).setName("assignment")
stmt = (
assignment
| edge_stmt
| attr_stmt
| subgraph
| graph_stmt
| node_stmt
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
Optional(strict_) + Group((graph_ | digraph_)) + Optional(ID) +
graph_stmt
).setResultsName("graph")
)

Expand Down