Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
fix(composer): fix styling according to codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Jul 16, 2019
1 parent 08aa30f commit 64aef41
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 25 additions & 17 deletions gnes/composer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Layer:
'income': 'pull'
}

def __init__(self, id: int = 0):
self.id = id
def __init__(self, layer_id: int = 0):
self.layer_id = layer_id
self.components = []

@staticmethod
Expand Down Expand Up @@ -84,7 +84,8 @@ def __init__(self, args):
for comp in tmp['component']:
self.add_layer()
if isinstance(comp, list):
[self.add_comp(c) for c in comp if self.add_comp(c)]
for c in comp:
self.add_comp(c)
elif self.check_fields(comp):
self.add_comp(comp)
else:
Expand All @@ -108,7 +109,7 @@ def check_fields(self, comp: Dict) -> bool:
return True

def add_layer(self, layer: 'Layer' = None) -> None:
self._layers.append(copy.deepcopy(layer) or self.Layer(id=self._num_layer))
self._layers.append(copy.deepcopy(layer) or self.Layer(layer_id=self._num_layer))
self._num_layer += 1

def add_comp(self, comp: Dict) -> None:
Expand All @@ -127,19 +128,21 @@ def build_layers(self) -> List['YamlGraph.Layer']:
return all_layers

@staticmethod
def build_shell(all_layers: List['YamlGraph.Layer']):
def build_shell(all_layers: List['YamlGraph.Layer'], log_fn: str = None, job_background: bool = False):
shell_lines = []
taboo = {'name', 'replicas'}
for layer in all_layers:
for c in layer.components:
rep_c = YamlGraph.Layer.get_value(c, 'replicas')
shell_lines.append('printf "starting service %s with %s replicas...\\n"' % (
colored(c['name'], 'green'), colored(rep_c, 'yellow')))
for j in range(rep_c):
for _ in range(rep_c):
cmd = YamlGraph.comp2file[c['name']]
print(c)
args = ' '.join(['--%s %s' % (a, v if ' ' not in v else ('"%s"' % v)) for a, v in c.items() if a not in taboo and v])
shell_lines.append('gnes %s %s &' % (cmd, args))
args = ' '.join(['--%s %s' % (a, v if ' ' not in v else ('"%s"' % v)) for a, v in c.items() if
a not in taboo and v])
shell_lines.append('gnes %s %s %s %s' % (
cmd, args, '>> %s 2>&1' % log_fn if log_fn else '', '&' if job_background else ''))

r = pkg_resources.resource_stream('gnes', '/'.join(('resources', 'compose', 'gnes-shell.sh')))
with r:
Expand All @@ -160,7 +163,7 @@ def build_mermaid(all_layers: List['YamlGraph.Layer'], show_topdown: bool = True
p = '((%s%s))' if c['name'] == 'Router' else '[%s%s]'
p1 = '((%s%s))' if c1['name'] == 'Router' else '[%s%s]'
for j1 in range(YamlGraph.Layer.get_value(c1, 'replicas')):
id, id1 = '%s%s%s' % (last_layer.id, c_idx, j), '%s%s%s' % (layer.id, c1_idx, j1)
id, id1 = '%s%s%s' % (last_layer.layer_id, c_idx, j), '%s%s%s' % (layer.layer_id, c1_idx, j1)
conn_type = (
c['socket_out'].split('_')[0] + '/' + c1['socket_in'].split('_')[0]).lower()
s_id = '%s%s' % (c_idx if len(last_layer.components) > 1 else '',
Expand All @@ -174,8 +177,13 @@ def build_mermaid(all_layers: List['YamlGraph.Layer'], show_topdown: bool = True
# if len(last_layer.components) > 1:
# self.mermaid_graph.append('\tend')

style = 'style Frontend000 fill:#ffd889ff,stroke:#f66,stroke-width:2px,stroke-dasharray:5'
mermaid_str = '\n'.join(['graph TD' if show_topdown else 'graph LR'] + [style] + mermaid_graph)
style = ['classDef FrontendCLS fill:#ffd889ff,stroke:#f66,stroke-width:2px,stroke-dasharray:5;',
'classDef EncoderCLS fill:#ffd889ff,stroke:#f66,stroke-width:2px;',
'classDef IndexerCLS fill:#ffd889ff,stroke:#f66,stroke-width:2px;',
'classDef RouterCLS fill:#ffd889ff,stroke:#f66,stroke-width:2px;',
'classDef PreprocessorCLS fill:#ffd889ff,stroke:#f66,stroke-width:2px;']

mermaid_str = '\n'.join(['graph TD' if show_topdown else 'graph LR'] + style + mermaid_graph)
return mermaid_str

def build_html(self, mermaid_str: str):
Expand Down Expand Up @@ -204,7 +212,7 @@ def rule2():

def rule3():
# a shortcut fn: (N)-2-(N) with push pull connection
router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
last_layer.components[0]['socket_out'] = str(SocketType.PUSH_CONNECT)
r = CommentedMap({'name': 'Router',
Expand Down Expand Up @@ -233,7 +241,7 @@ def rule5():

def rule6():
last_layer.components[0]['socket_out'] = str(SocketType.PUB_BIND)
router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
for c in layer.components:
r = CommentedMap({'name': 'Router',
Expand All @@ -250,7 +258,7 @@ def rule6():
def rule7():
last_layer.components[0]['socket_out'] = str(SocketType.PUSH_CONNECT)

router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
r0 = CommentedMap({'name': 'Router',
'yaml_path': None,
Expand All @@ -262,7 +270,7 @@ def rule7():
router_layers.append(router_layer)
last_layer.components[0]['port_out'] = r0['port_in']

router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
for c in layer.components:
r = CommentedMap({'name': 'Router',
Expand All @@ -278,7 +286,7 @@ def rule7():

def rule8():
last_layer.components[0]['socket_out'] = str(SocketType.PUSH_CONNECT)
router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
r = CommentedMap({'name': 'Router',
'yaml_path': None,
Expand All @@ -304,7 +312,7 @@ def rule8():
c['port_in'] = r['port_out']
router_layers.append(router_layer)

router_layer = YamlGraph.Layer(id=self._num_layer)
router_layer = YamlGraph.Layer(layer_id=self._num_layer)
self._num_layer += 1
router_layer.append(r)
router_layers.append(router_layer)
Expand Down
4 changes: 3 additions & 1 deletion gnes/resources/compose/gnes-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ set -e

trap 'kill $(jobs -p)' EXIT

{{gnes-template}}
{{gnes-template}}

wait

0 comments on commit 64aef41

Please sign in to comment.