def _format_type_tokens(self, symbol, type_tokens): language = symbol.get_extension_attribute( self.extension.extension_name, 'language') if language != 'c': type_desc = self.extension.get_attr(symbol, 'type_desc') assert (type_desc) gi_name = type_desc.gi_name new_tokens = [] link = None if gi_name in FUNDAMENTALS[language]: fund_link = FUNDAMENTALS[language][gi_name] link = Link(fund_link.ref, fund_link._title, gi_name) elif gi_name in ALL_GI_TYPES: ctype_name = ALL_GI_TYPES[gi_name] link = self.extension.app.link_resolver.get_named_link( ctype_name) if type_desc.nesting_depth: new_tokens.append('[' * type_desc.nesting_depth + ' ') if link: new_tokens.append(link) else: # Should not happen but let's be conservative new_tokens.append(type_desc.gi_name) if type_desc.nesting_depth: new_tokens.append(']' * type_desc.nesting_depth) return Formatter._format_type_tokens(self, symbol, new_tokens) return Formatter._format_type_tokens(self, symbol, type_tokens)
def _format_prototype (self, function, is_pointer, title): language = function.get_extension_attribute(self.extension.extension_name, 'language') if language == 'c': return Formatter._format_prototype (self, function, is_pointer, title) params = function.get_extension_attribute ('gi-extension', 'parameters') if params is None: return Formatter._format_prototype (self, function, is_pointer, title) c_name = function.make_name() template = self.engine.get_template(language + '_prototype.html') if type (function) == SignalSymbol: comment = "%s callback for the '%s' signal" % (language, c_name) elif type (function) == VFunctionSymbol: comment = "%s implementation of the '%s' virtual method" % \ (language, c_name) else: comment = "%s wrapper for '%s'" % (language, c_name) res = template.render ({'return_value': function.return_value, 'parent_name': function.parent_name, 'function_name': title, 'parameters': params, 'comment': comment, 'throws': function.throws, 'out_params': [], 'is_method': isinstance(function, MethodSymbol)}) return res
def __init__(self, gi_extension): Formatter.__init__(self, gi_extension) self._order_by_parent = True self._symbol_formatters.update( { GIClassSymbol: self._format_class_symbol, GIStructSymbol: self._format_class_symbol, }) self._ordering.insert(self._ordering.index(ClassSymbol) + 1, GIClassSymbol) self._ordering.insert(self._ordering.index(GIClassSymbol) + 1, GIStructSymbol) self.__annotation_parser = GIAnnotationParser()
def __init__(self, extension): self.__tmpdir = TemporaryDirectory() with open(os.path.join(self.__tmpdir.name, "padtemplate.html"), "w") as _: _.write(GstPadTemplateSymbol.TEMPLATE) with open(os.path.join(self.__tmpdir.name, "enumtemplate.html"), "w") as _: _.write(GstNamedConstantValue.TEMPLATE) with open(os.path.join(self.__tmpdir.name, "plugins.html"), "w") as _: _.write(GstPluginsSymbol.TEMPLATE) with open(os.path.join(self.__tmpdir.name, "plugin.html"), "w") as _: _.write(GstPluginSymbol.TEMPLATE) with open(os.path.join(self.__tmpdir.name, "element.html"), "w") as _: _.write(GstElementSymbol.TEMPLATE) Formatter.__init__(self, extension) self._order_by_parent = True self._ordering.insert(0, GstPluginSymbol) self._ordering.insert(1, GstElementSymbol) self._ordering.insert( self._ordering.index(ClassSymbol) + 1, GIClassSymbol) self._ordering.insert( self._ordering.index(GIClassSymbol) + 1, GstPadTemplateSymbol) self._ordering.insert( self._ordering.index(GstPadTemplateSymbol) + 1, GstPluginsSymbol) self._ordering.insert( self._ordering.index(InterfaceSymbol) + 1, GIInterfaceSymbol) self._ordering.append(GstNamedConstantsSymbols) self._symbol_formatters.update({ GstPluginsSymbol: self._format_plugins_symbol, GstPluginSymbol: self._format_plugin_symbol, GstPadTemplateSymbol: self._format_pad_template_symbol, GstElementSymbol: self._format_element_symbol, GstNamedConstantsSymbols: self._format_enum, GIClassSymbol: self._format_class_symbol, GIInterfaceSymbol: self._format_interface_symbol, GstNamedConstantValue: self._format_name_constant_value, }) self.gi_languages = []
def _format_parameter_symbol(self, parameter): self.__add_annotations(parameter) language = parameter.get_extension_attribute( self.extension.extension_name, 'language') if language != 'c': direction = parameter.get_extension_attribute( 'gi-extension', 'direction') if direction == 'out': return None is_destroy = parameter.get_extension_attribute( 'gi-extension', 'is_destroy') if is_destroy: return None is_closure = parameter.get_extension_attribute( 'gi-extension', 'is_closure') if is_closure and language == 'python': parameter.extension_contents['type-link'] = self._format_link( PYTHON_VARIADIC_LINK, None, 'variadic') else: parameter.extension_contents[ 'type-link'] = self._format_linked_symbol(parameter) else: parameter.extension_contents.pop('type-link', None) res = Formatter._format_parameter_symbol(self, parameter) return res
def _format_prototype(self, function, is_pointer, title): c_proto = Formatter._format_prototype(self, function, is_pointer, title) template = self.get_template('python_prototype.html') python_proto = template.render({ 'function_name': title, 'parameters': function.parameters, 'throws': False, 'comment': "python callback for the '%s' signal" % function.make_name(), 'is_method': False }) template = self.get_template('javascript_prototype.html') for param in function.parameters: param.extension_contents['type-link'] = self._format_linked_symbol( param) js_proto = template.render({ 'function_name': title, 'parameters': function.parameters, 'throws': False, 'comment': "javascript callback for the '%s' signal" % function.make_name(), 'is_method': False }) for param in function.parameters: param.extension_contents.pop('type-link', None) return '%s%s%s' % (c_proto, python_proto, js_proto)
def _format_property_prototype(self, prop, title, type_link): language = prop.get_extension_attribute(self.extension.extension_name, 'language') if language == 'python': title = 'self.props.%s' % title return Formatter._format_property_prototype(self, prop, title, type_link)
def _format_return_value_symbol(self, *args): retval = args[0] is_void = retval[0] is None or \ retval[0].get_extension_attribute('gi-extension', 'gi_name') == 'none' if not is_void: language = retval[0].get_extension_attribute( self.extension.extension_name, 'language') else: language = 'c' if language == 'c': if is_void: retval = [None] else: retval = retval[:1] for item in retval: if item: item.formatted_link = '' elif is_void: retval = retval[1:] or [None] args = list(args) args[0] = retval return Formatter._format_return_value_symbol(self, *args)
def _format_symbol (self, symbol): if isinstance(symbol, (QualifiedSymbol, FieldSymbol, EnumMemberSymbol)): return Formatter._format_symbol(self, symbol) langs_docs = {} previous_lang = None for lang in self.extension.get_languages(): self.extension.setup_language(lang, previous_lang) lang_name = lang.language_name if lang_name == 'c' or is_introspectable(symbol.unique_name, lang): self.__add_attrs(symbol, language=lang_name) langs_docs[lang_name] = Formatter._format_symbol(self, symbol) else: langs_docs[lang_name] = None previous_lang = lang self.extension.setup_language(None, previous_lang) return self.__wrap_in_language(symbol, langs_docs)
def _format_linked_symbol (self, symbol): if not symbol: return Formatter._format_linked_symbol (self, symbol) language = symbol.get_extension_attribute(self.extension.extension_name, 'language') if language == 'c': res = Formatter._format_linked_symbol (self, symbol) if symbol == None: res = 'void' return res if not isinstance (symbol, QualifiedSymbol): return Formatter._format_linked_symbol (self, symbol) type_desc = symbol.get_extension_attribute ('gi-extension', 'type_desc') if type_desc: return self._format_type_tokens (symbol, symbol.type_tokens) return Formatter._format_linked_symbol (self, symbol)
def _format_constant(self, constant): language = constant.get_extension_attribute(self.extension.extension_name, 'language') if language == 'c': return Formatter._format_constant (self, constant) template = self.engine.get_template('constant.html') out = template.render ({'symbol': constant, 'definition': None, 'constant': constant}) return out
def _format_struct (self, struct): language = struct.get_extension_attribute(self.extension.extension_name, 'language') if language == 'c': return Formatter._format_struct (self, struct) members_list = self._format_members_list (struct.members, 'Attributes', struct) template = self.engine.get_template ("python_compound.html") out = template.render ({"symbol": struct, "members_list": members_list}) return out
def _format_class_symbol (self, klass): saved_raw_text = klass.raw_text if klass.get_extension_attribute(self.extension.extension_name, 'language') != 'c': klass.raw_text = None out = Formatter._format_class_symbol(self, klass) if klass.get_extension_attribute(self.extension.extension_name, 'language') == 'c': # Render class structure if available. if klass.class_struct_symbol: out += '<h3>Class structure</h3>' out += klass.class_struct_symbol.detailed_description klass.raw_text = saved_raw_text return out
def _format_symbol(self, symbol): if isinstance(symbol, (QualifiedSymbol, FieldSymbol, EnumMemberSymbol)): return Formatter._format_symbol(self, symbol) self.extension.setup_language('c', None) self.__add_attrs(symbol, language='c') c_out = Formatter._format_symbol(self, symbol) python_out = None js_out = None self.extension.setup_language('python', 'c') if is_introspectable(symbol.unique_name, 'python'): self.__add_attrs(symbol, language='python') python_out = Formatter._format_symbol(self, symbol) self.extension.setup_language('javascript', 'python') if is_introspectable(symbol.unique_name, 'javascript'): self.__add_attrs(symbol, language='javascript') js_out = Formatter._format_symbol(self, symbol) self.extension.setup_language(None, 'javascript') return self.__wrap_in_language(symbol, c_out, python_out, js_out)
def _format_parameter_symbol(self, parameter): self.__add_annotations(parameter) language = parameter.get_extension_attribute( self.extension.extension_name, 'language') if language != 'c': direction = parameter.get_extension_attribute( 'gi-extension', 'direction') if direction == 'out': return None parameter.extension_contents[ 'type-link'] = self._format_linked_symbol(parameter) else: parameter.extension_contents.pop('type-link', None) res = Formatter._format_parameter_symbol(self, parameter) return res
def _format_parameter_symbol(self, parameter): if self.extension.get_attr(parameter, 'action'): parameter.extension_contents[ 'type-link'] = self._format_type_tokens( parameter, parameter.type_tokens) return Formatter._format_parameter_symbol(self, parameter)
def _make_formatter(self): return Formatter(self)
def _format_prototype(self, function, is_pointer, title): gi_extension = self.extension.project.extensions.get('gi-extension') if 'c' in self.extension.gi_languages: c_proto = Formatter._format_prototype(self, function, is_pointer, title) else: c_proto = '' if 'python' in self.extension.gi_languages: if type(function) == ActionSignalSymbol: template = self.get_template('python_action_prototype.html') python_proto = template.render({ 'name': title, 'parameters': function.parameters, 'return_value': function.return_value }) else: template = self.get_template('python_prototype.html') python_proto = template.render({ 'function_name': title, 'parameters': function.parameters, 'throws': False, 'comment': "python callback for the '%s' signal" % function.make_name(), 'is_method': False }) else: python_proto = '' if 'javascript' in self.extension.gi_languages: if type(function) == ActionSignalSymbol: template = self.get_template( 'javascript_action_prototype.html') js_proto = template.render({ 'name': title, 'parameters': function.parameters, 'return_value': function.return_value }) else: template = self.get_template('javascript_prototype.html') for param in function.parameters: param.extension_contents[ 'type-link'] = self._format_linked_symbol(param) js_proto = template.render({ 'function_name': title, 'parameters': function.parameters, 'throws': False, 'comment': "javascript callback for the '%s' signal" % function.make_name(), 'is_method': False }) for param in function.parameters: param.extension_contents.pop('type-link', None) else: js_proto = '' return '%s%s%s' % (c_proto, python_proto, js_proto)
def _format_return_item_symbol(self, symbol): self.__add_annotations(symbol) return Formatter._format_return_item_symbol(self, symbol)