Created
July 1, 2021 16:22
-
-
Save gs-niteesh/e62a59b1e0bfcc0a55bdef00d903e308 to your computer and use it in GitHub Desktop.
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
""" | |
Simple visitor | |
""" | |
import sys | |
import pprint | |
from .schema import QAPISchema, QAPISchemaVisitor | |
class Dict(dict): | |
def __add__(self, rhs): | |
if not rhs: | |
return self | |
for (key, val) in rhs.items(): | |
self[key] = val | |
return self | |
class JSONVisitor(QAPISchemaVisitor): | |
""" | |
Not used currently | |
""" | |
def __init__(self): | |
self._cur_doc = None | |
self.visited_items = {} | |
def _jsonize_one_member(self, member): | |
ret = Dict() | |
ret['Optional'] = False | |
if member.type.doc_type(): | |
ret['Type'] = member.type.doc_type() | |
if member.optional: | |
ret['Optional'] = True | |
if member.ifcond: | |
ret['If'] = self._jsonize_ifcond(member.ifcond)['If'] | |
return ret | |
def _jsonize_members(self, what, base=None, variants=None): | |
doc = self._cur_doc | |
if base is not None: | |
print ('Base: ', base) | |
assert False | |
json_val = {} | |
args = {} | |
for section in doc.args.values(): | |
val = self._jsonize_one_member(section.member) | |
if section.text: | |
defn = section.text | |
elif (variants and variants.tag_member == section.member | |
and not section.member.type.doc_type()): | |
values = section.member.type.member_names() | |
print ('Variants:') | |
[print (v) for v in values] | |
else: | |
defn = 'Not Documented' | |
args[section.member.name] = { | |
'Info': defn, **val | |
} | |
if variants: | |
for v in variants.variants: | |
if v.type.is_implicit(): | |
assert not v.type.base and not v.type.variants | |
for m in v.type.local_members: | |
val = self._jsonize_one_member(m) | |
else: | |
pass | |
json_val[what] = args | |
return json_val | |
def _jsonize_arguments(self, boxed_args): | |
doc = self._cur_doc | |
json_val = Dict() | |
if not boxed_args: | |
return self._jsonize_members('Arguments') | |
json_val['Member'] = boxed_args.name | |
return json_val | |
def _jsonize_enum_values(self): | |
json_val = Dict() | |
sections = [] | |
doc = self._cur_doc | |
for section in doc.args.values(): | |
val = {section.member.name: 'ND'} | |
if section.text: | |
text = section.text | |
text = text.replace('\n', ' ') | |
val = {section.member.name: text} | |
if section.member.ifcond: | |
conds = self._jsonize_ifcond(section.member.ifcond) | |
val = {section.member.name: text, 'If': conds['If']} | |
sections.append(val) | |
json_val['values'] = sections | |
return json_val | |
def _jsonize_features(self): | |
json_val = Dict() | |
features = [] | |
doc = self._cur_doc | |
seen_item = False | |
for feature in doc.features.values(): | |
val = {feature.name: feature.text} | |
features.append(val) | |
seen_item = True | |
if not seen_item: | |
return {} | |
json_val['Features'] = features | |
return json_val | |
def _jsonize_sections(self): | |
doc = self._cur_doc | |
json_val = Dict() | |
for section in doc.sections: | |
if section.name and section.text: | |
text = section.text | |
if not section.name == 'Example': | |
text = text.replace('\n', ' ') | |
json_val[section.name] = text | |
else: | |
assert False | |
return json_val | |
def _jsonize_ifcond(self, ifcond): | |
if not ifcond: | |
return {} | |
return {'If': ifcond} | |
def visit_enum_type(self, name, info, ifcond, features, members, prefix): | |
return | |
json_val = Dict() | |
json_val['Type'] = 'ENUM' | |
json_val['Info'] = self._cur_doc.body.text.replace('\n', ' ') | |
json_val += self._jsonize_enum_values() | |
json_val += self._jsonize_features() | |
json_val += self._jsonize_sections() | |
json_val += self._jsonize_ifcond(ifcond) | |
self.visited_items[name] = json_val | |
def visit_command(self, name, info, ifcond, features, arg_type, | |
ret_type, gen, success_response, boxed, allow_oob, | |
allow_preconfig, coroutine): | |
return | |
json_val = Dict() | |
json_val['Type'] = 'COMMAND' | |
json_val['Info'] = self._cur_doc.body.text.replace('\n', ' ') | |
json_val += self._jsonize_arguments(arg_type if boxed else None) | |
json_val += self._jsonize_features() | |
json_val += self._jsonize_sections() | |
json_val += self._jsonize_ifcond(ifcond) | |
self.visited_items[name] = json_val | |
def visit_object_type(self, name, info, ifcond, features, | |
base, members, variants): | |
doc = self._cur_doc | |
if base and base.is_implicit(): | |
base = None | |
json_val = Dict() | |
json_val['Type'] = 'Object' | |
print ('Name: ', name) | |
json_val += self._jsonize_members('Members', base, variants) | |
json_val += self._jsonize_features() | |
json_val += self._jsonize_sections() | |
json_val += self._jsonize_ifcond(ifcond) | |
self.visited_items[name] = json_val | |
def symbol(self, doc, entity): | |
self._cur_doc = doc | |
entity.visit(self) | |
self._cur_doc = None | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print ("Usage: python3 simple_visitor.py <file_name>") | |
sys.exit(0) | |
fname = sys.argv[1] | |
schema = QAPISchema(fname) | |
visitor = JSONVisitor() | |
for doc in schema.docs: | |
if doc.symbol: | |
visitor.symbol(doc, schema.lookup_entity(doc.symbol)) | |
import json | |
print(json.dumps(visitor.visited_items, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment