Commit 7f431bdf authored by Emmanuele Bassi's avatar Emmanuele Bassi 👣
Browse files

gir: Parse more namespace elements

Records, Boxed, Union, and Function are namespace-level object types.
parent 8075c331
......@@ -337,11 +337,13 @@ def run(options):
shlibs = ', '.join(namespace.get_shared_libraries())
aliases = sorted(namespace.get_aliases(), key=lambda alias: alias.name.lower())
boxeds = sorted(namespace.get_boxeds(), key=lambda boxed: boxed.name.lower())
classes = sorted(namespace.get_classes(), key=lambda cls: cls.name.lower())
constants = sorted(namespace.get_constants(), key=lambda const: const.name.lower())
domains = sorted(namespace.get_error_domains(), key=lambda domain: domain.name.lower())
enums = sorted(namespace.get_enumerations(), key=lambda enum: enum.name.lower())
interfaces = sorted(namespace.get_interfaces(), key=lambda interface: interface.name.lower())
records = sorted(namespace.get_records(), key=lambda record: record.name.lower())
title = str(log.color('Namespace', 36))
log.log(f'└── {title}: {namespace.name}, version: {namespace.version}')
......@@ -424,6 +426,37 @@ def run(options):
sections.remove('functions')
_print_class_functions(cls, sections, is_last_iface)
title = str(log.color('Records', 36))
log.log(f' ├── {title}')
if len(records) == 0:
log.log( ' │ └── None')
else:
for i, record in enumerate(records):
is_last_record = i == len(records) - 1
if is_last_record:
log.log(f' │ └── {record.name}')
else:
log.log(f' │ ├── {record.name}')
sections = []
if record.constructors:
sections += ['constructors']
if record.methods:
sections += ['methods']
if record.functions:
sections += ['functions']
if 'constructors' in sections:
sections.remove('constructors')
_print_class_constructors(record, sections, is_last_record)
if 'methods' in sections:
sections.remove('methods')
_print_class_methods(record, sections, is_last_record)
if 'functions' in sections:
sections.remove('functions')
_print_class_functions(record, sections, is_last_record)
title = str(log.color('Aliases', 36))
log.log(f' ├── {title}')
......
......@@ -201,6 +201,59 @@ class Interface(Type):
self.functions.extend(functions)
class Boxed(Type):
def __init__(self, name: str, symbol_prefix: str, gtype: GType):
super().__init__(name, None)
self.symbol_prefix = symbol_prefix
self.gtype = gtype
self.methods = []
self.functions = []
def set_methods(self, methods: T.List[Method]) -> None:
self.methods.extend(methods)
def set_functions(self, functions: T.List[Function]) -> None:
self.functions.extend(functions)
class Record(Type):
def __init__(self, name: str, ctype: str, symbol_prefix: str, gtype: GType):
super().__init__(name, ctype)
self.symbol_prefix = symbol_prefix
self.gtype = gtype
self.constructors = []
self.methods = []
self.functions = []
def set_constructors(self, ctors: T.List[Function]) -> None:
self.constructors.extend(ctors)
def set_methods(self, methods: T.List[Method]) -> None:
self.methods.extend(methods)
def set_functions(self, functions: T.List[Function]) -> None:
self.functions.extend(functions)
class Union(Type):
def __init__(self, name: str, ctype: str, symbol_prefix: str, gtype: GType):
super().__init__(name, ctype)
self.symbol_prefix = symbol_prefix
self.gtype = gtype
self.constructors = []
self.methods = []
self.functions = []
def set_constructors(self, ctors: T.List[Function]) -> None:
self.constructors.extend(ctors)
def set_methods(self, methods: T.List[Method]) -> None:
self.methods.extend(methods)
def set_functions(self, functions: T.List[Function]) -> None:
self.functions.extend(functions)
class EnumerationMember(GIRElement):
def __init__(self, name: str, value: str, identifier: str, nick: str):
super().__init__()
......@@ -245,7 +298,7 @@ class Namespace:
self._shared_libraries = []
self._aliases = []
self._boxed = []
self._boxeds = []
self._classes = []
self._constants = []
self._enumerations = []
......@@ -253,6 +306,7 @@ class Namespace:
self._functions = []
self._interfaces = []
self._records = []
self._unions = []
def __str__(self):
return f"{self.name}-{self.version}"
......@@ -281,6 +335,18 @@ class Namespace:
def add_interface(self, interface: Interface) -> None:
self._interfaces.append(interface)
def add_boxed(self, boxed: Boxed) -> None:
self._boxeds.append(boxed)
def add_record(self, record: Record) -> None:
self._records.append(record)
def add_union(self, union: Union) -> None:
self._unions.append(union)
def add_function(self, function: Function) -> None:
self._functions.append(function)
def get_classes(self) -> T.List[Class]:
return self._classes
......@@ -299,6 +365,18 @@ class Namespace:
def get_interfaces(self) -> T.List[Interface]:
return self._interfaces
def get_boxeds(self) -> T.List[Boxed]:
return self._boxeds
def get_records(self) -> T.List[Record]:
return self._records
def get_unions(self) -> T.List[Union]:
return self._unions
def get_functions(self) -> T.List[Function]:
return self._functions
class Include:
def __init__(self, name: str, version: str):
......
......@@ -83,10 +83,14 @@ class GirParser:
parse_methods = {
_corens('alias'): self._parse_alias,
_glibns('boxed'): self._parse_boxed,
_corens('class'): self._parse_class,
_corens('constant'): self._parse_constant,
_corens('enumeration'): self._parse_enumeration,
_corens('function'): self._parse_function,
_corens('interface'): self._parse_interface,
_corens('record'): self._parse_record,
_corens('union'): self._parse_union,
}
for node in ns:
......@@ -440,3 +444,120 @@ class GirParser:
res.set_signals(signals)
ns.add_interface(res)
def _parse_boxed(self, node: ET.Element, ns: ast.Namespace) -> None:
name = node.attrib.get(_glibns('name'))
symbol_prefix = node.attrib.get(_cns('symbol-prefix'))
type_name = node.attrib.get(_glibns('type-name'))
get_type = node.attrib.get(_glibns('get-type'))
gtype = None
if type_name is not None:
gtype = ast.GType(type_name=type_name, get_type=get_type)
doc = self._maybe_parse_doc(node)
source_pos = self._maybe_parse_source_position(node)
res = ast.Boxed(name=name, symbol_prefix=symbol_prefix, gtype=gtype)
res.set_doc(doc)
res.set_source_position(source_pos)
methods = []
children = node.findall('core:method', GI_NAMESPACES)
for child in children:
methods.append(self._parse_method(child))
res.set_methods(methods)
functions = []
children = node.findall('core:function', GI_NAMESPACES)
for child in children:
functions.append(self._parse_function(child))
res.set_functions(functions)
ns.add_boxed(res)
def _parse_record(self, node: ET.Element, ns: ast.Namespace) -> None:
name = node.attrib.get('name')
symbol_prefix = node.attrib.get(_cns('symbol-prefix'))
ctype = node.attrib.get(_cns('type'))
type_name = node.attrib.get(_glibns('type-name'))
get_type = node.attrib.get(_glibns('get-type'))
type_struct = node.attrib.get(_glibns('type-struct'))
gtype = None
if type_name is not None:
gtype = ast.GType(type_name=type_name, get_type=get_type, type_struct=type_struct)
doc = self._maybe_parse_doc(node)
source_pos = self._maybe_parse_source_position(node)
res = ast.Record(name=name, symbol_prefix=symbol_prefix, ctype=ctype, gtype=gtype)
res.set_doc(doc)
res.set_source_position(source_pos)
ctors = []
children = node.findall('core:constructor', GI_NAMESPACES)
for child in children:
ctors.append(self._parse_function(child))
res.set_constructors(ctors)
methods = []
children = node.findall('core:method', GI_NAMESPACES)
for child in children:
methods.append(self._parse_method(child))
res.set_methods(methods)
functions = []
children = node.findall('core:function', GI_NAMESPACES)
for child in children:
functions.append(self._parse_function(child))
res.set_functions(functions)
ns.add_record(res)
def _parse_union(self, node: ET.Element, ns: ast.Namespace) -> None:
name = node.attrib.get('name')
symbol_prefix = node.attrib.get(_cns('symbol-prefix'))
ctype = node.attrib.get(_cns('type'))
type_name = node.attrib.get(_glibns('type-name'))
get_type = node.attrib.get(_glibns('get-type'))
type_struct = node.attrib.get(_glibns('type-struct'))
gtype = None
if type_name is not None:
gtype = ast.GType(type_name=type_name, get_type=get_type, type_struct=type_struct)
doc = self._maybe_parse_doc(node)
source_pos = self._maybe_parse_source_position(node)
res = ast.Union(name=name, symbol_prefix=symbol_prefix, ctype=ctype, gtype=gtype)
res.set_doc(doc)
res.set_source_position(source_pos)
ctors = []
children = node.findall('core:constructor', GI_NAMESPACES)
for child in children:
ctors.append(self._parse_function(child))
res.set_constructors(ctors)
methods = []
children = node.findall('core:method', GI_NAMESPACES)
for child in children:
methods.append(self._parse_method(child))
res.set_methods(methods)
functions = []
children = node.findall('core:function', GI_NAMESPACES)
for child in children:
functions.append(self._parse_function(child))
res.set_functions(functions)
ns.add_union(res)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment