def __init__(self, thrift): """ Schema Converter for Thrift to JADN :param thrift: str or dict of the JADN schema :type thrift: str """ self._thrift = toStr(thrift).replace( '\r', '') # replace windows line terminators with unix style self.indent = ' ' self._fieldMap = { 'enum': 'Enumerated', 'struct': 'Record', # primitives 'string': 'String' } self._structs = [ 'Record', 'Choice', 'Map', 'Enumerated', 'Array', 'ArrayOf' ] self._fieldRegex = { 'enum': re.compile( r'(?P<name>.*?)\s+=\s+(?P<id>\d+);(\s+//\s+(?P<comment>.*))?\n?' ), 'struct': re.compile( r'(?P<id>\d+):\s+(?P<option>.*?)\s+(?P<type>.*?)\s+(?P<name>.*?);(\s+//\s+(?P<comment>.*))?\n?' ), 'array': re.compile( r'(?P<id>\d+):\s+(?P<option>.*?)\s+list\<(?P<type>.*?)\>\s+(?P<name>.*?);(\s+//\s+(?P<comment>.*))?\n?' ) } self._fieldRegex['arrayof'] = self._fieldRegex['array']
def makeCustom(self): customFields = re.search( r'/\* JADN Custom Fields\n(?P<custom>[\w\W]+?)\n\*/', toStr(self._thrift)) fields = [] if customFields: try: fields = Utils.defaultDecode( json.loads( customFields.group('custom').replace('\'', '\"'))) except Exception as e: print('Custom Fields Load Error: {}'.format(e)) return fields
def cddl2jadn_dumps(cddl): """ Produce JADN schema from cddl schema :param cddl: CUDDL schema to convert :return: CUDDL schema :rtype str """ try: parser = ParserPython(CddlRules) parse_tree = parser.parse(toStr(cddl)) result = visit_parse_tree(parse_tree, CddlVisitor()) return Utils.jadnFormat(result, indent=2) except Exception as e: raise Exception('Proto parsing error has occurred: {}'.format(e))
def makeMeta(self): """ Create the header for the schema :return: header for schema :rtype dict """ tmp = {} meta = re.search(r'\/\*\s*?meta(.*|\n)*?\*\/', toStr(self._thrift)) if meta: for meta_line in meta.group().split('\n')[1:-1]: line = re.sub(r'^\s+\*\s+', '', meta_line).split(' - ') try: tmp[line[0]] = json.loads(' - '.join(line[1:])) except Exception as e: tmp[line[0]] = ' - '.join(line[1:]) return tmp
def __init__(self, proto): """ Schema Converter for ProtoBuf3 to JADN :param proto: str or dict of the JADN schema :type proto: str """ self._proto = toStr(proto).replace( '\r', '') # replace windows line terminators with unix style self.indent = ' ' self._fieldMap = { 'enum': 'Enumerated', 'message': 'Record', 'oneof': 'Choice', # primitives 'string': 'String' } self._structs = [ 'Record', 'Choice', 'Map', 'Enumerated', 'Array', 'ArrayOf' ] self._fieldRegex = { 'enum': re.compile( r'(?P<name>.*?)\s+=\s+(?P<id>\d+);(\s+//\s+(?P<comment>.*))?\n?' ), 'message': re.compile( r'(?P<type>.*?)\s+(?P<name>.*?)\s+=\s+(?P<id>\d+);(\s+//\s+(?P<comment>.*))?\n?' ), 'array': re.compile( r'\s+(?P<repeat>.*?)\s+(?P<type>.*?)\s+(?P<name>.*?)\s+=\s+(?P<id>\d+);(\s+//\s+(?P<comment>.*))?\n?' ) } self._fieldRegex['oneof'] = self._fieldRegex['message'] self._fieldRegex['arrayof'] = self._fieldRegex['array']
opts = json.loads(opts.group('opts')) except Exception as e: opts = {} else: opts = {} return com, opts if __name__ == '__main__': schemaFiles = {'proto': '', 'thrift': ''} for schema in schemaFiles: schemaFiles[schema] = toStr( open( os.path.join('.', 'schema_gen_test', 'openc2-wd06.{}'.format(schema)), 'rb').read()) testResults = { 'proto': { 'arpeggio': [], 'original': [] }, 'thrift': { 'arpeggio': [], 'original': [] } } iterations = 250 for i in range(1, iterations + 1):