Motr  M0
addb2grammar.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2020 Seagate Technology LLC and/or its Affiliates
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # For any questions about this software or licensing,
17 # please email opensource@seagate.com or cortx-questions@seagate.com.
18 #
19 
20 import sys
21 import fileinput
22 import re
23 import json
24 import copy
25 
26 from pyleri import Choice
27 from pyleri import Optional
28 from pyleri import Grammar
29 from pyleri import Keyword
30 from pyleri import Repeat
31 from pyleri import Sequence
32 from pyleri import Regex
33 from pyleri import List
34 from pyleri import Prio
35 from pyleri import end_of_statement
36 
37 
38 class Addb2Grammar(Grammar):
39 
40  r_start = Regex("^\*")
41  r_meas_time = Regex("[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{9}")
42  r_meas = Regex(".*")
43 
44  r_attr_start = Regex("^\|")
45  r_attr_name = Regex("[A-Za-z0-9]+")
46  r_attr = Optional(Regex("[^|*].*"))
47 
48  r_hist_start = Regex("^\|")
49  r_hist_key = Optional(Regex("[-]{0,1}[0-9]+"))
50  r_hist_sep = Regex(":")
51  r_hist_val = Regex("[0-9]+")
52  r_hist_end = Regex("\|")
53 
54 # --1
55 # * 2015-04-14-15:33:11.998165453 fom-descr service: <7300000000000001:0>, sender: c28baccf27e0001
56 
57 # --2
58 # | : 0 |
59 # | 1 : 0 |
60 # | .... |
61 # | 25 : 0 |
62 
63 # --3
64 # | node <11186d8bf0e34117:ab1897c062a22573>
65 # | ....
66 # | fom @0x7f795008ed20, 'IO fom', transitions: 0, phase: 0
67 # | ....
68 
69  START = Repeat(Sequence(
70  r_start, Sequence(r_meas_time, # 1
71  r_meas),
72  Optional(Repeat(Sequence(r_hist_start, # 2
73  r_hist_key,
74  r_hist_sep,
75  r_hist_val,
76  r_hist_end))),
77  Repeat(Sequence(r_attr_start, # 3
78  r_attr_name,
79  r_attr))))
80 
81 
83  def __init__(self):
84  # [ { 'time': 'XXX', 'measurement': 'YYY', 'params': ['p1','p2'] } ]
85  self.result = []
86  self.meas = {}
87  self.attr_name = None
88 
89  def visit(self, node):
90  name = node.element.name if hasattr(node.element, 'name') else None
91  if name == "r_start":
92  self.meas = copy.copy({"time": None, "measurement": None, "params": []})
93  self.result.append(self.meas)
94  else:
95  if name == "r_meas_time":
96  self.meas["time"] = node.string
97  if name == "r_meas":
98  self.meas["measurement"] = node.string
99  elif name == "r_attr_start":
100  self.meas["params"].append({})
101  elif name == "r_attr_name":
102  self.attr_name = node.string
103  self.meas["params"][-1][self.attr_name] = None
104  elif name == "r_attr":
105  self.meas["params"][-1][self.attr_name] = node.string
106  self.attr_name = None
107 
108 
109 def visit_node(node, children, visitor):
110  visitor.visit(node)
111 
112 def visit_children(children, visitor):
113  for c in children:
114  visit_node(c, visit_children(c.children, visitor), visitor)
115 
116 def visit_tree(res):
117  start = res.tree.children[0] if res.tree.children else res.tree
118  v = Addb2Visitor()
119  visit_node(start, visit_children(start.children, v), v)
120  return v.result
121 
122 def inp():
123  starred = False
124  bunch = []
125  for measurement in fileinput.input():
126  if measurement[0] == '*':
127  if not starred:
128  starred = True
129  else:
130  starred = False
131  mstr = "".join(bunch)
132  res = grammar.parse(mstr)
133  if not res.is_valid:
134  print("Bang!")
135  print(mstr)
136  sys.exit(1)
137  print(repr([x['measurement'] for x in visit_tree(res)]))
138  bunch.clear()
139 
140  bunch.append(measurement)
141  else:
142  bunch.append(measurement)
143 
144 if __name__ == '__main__':
145  grammar = Addb2Grammar()
146  res = grammar.parse(string)
147  print(res.is_valid)
148  print(repr(visit_tree(res)))
149 
static M0_UNUSED void print(struct m0_be_list *list)
Definition: list.c:186
def visit(self, node)
Definition: addb2grammar.py:89
def visit_children(children, visitor)
def visit_tree(res)
def visit_node(node, children, visitor)