def _make_method(self, method, interface, service_catalog=None): if service_catalog is None: class_name = interface['shortname'] else: class_name = service_catalog method_name = method['name'] method_sig = '' if not (('OsidManager' in interface['inherit_shortnames'] or 'OsidProxyManager' in interface['inherit_shortnames']) and '_session' in method_name): if method_name == 'get_id': method_sig = ' .. autoattribute:: ' + class_name + '.ident' elif method_name == 'get_identifier_namespace': method_sig = ' .. autoattribute:: ' + class_name + '.namespace' elif method_name.startswith('get_') and method['args'] == []: method_sig = ' .. autoattribute:: ' + class_name + '.' + fix_reserved_word( method_name[4:]) elif method_name.startswith('set_') and len(method['args']) == 1: method_sig = ' .. autoattribute:: ' + class_name + '.' + fix_reserved_word( method_name[4:]) elif method_name.startswith('clear_') and method['args'] == []: method_sig = ' .. autoattribute:: ' + class_name + '.' + fix_reserved_word( method_name[6:]) # And finally all the methods: else: method_sig = ' .. automethod:: {0}.{1}'.format( class_name, method_name) return method_sig
def write_modules(self, modules): summary = u'{0}\"\"\"{1}\n{2} version {3}\n\n{4}\n\n\"\"\"\n'.format( self._utf_code, self.package['title'], self.package['name'], self.package['version'], self.package['summary']).encode('utf-8') ## # Finally, iterate through the completed package module structure and # write out both the import statements and class definitions to the # appropriate module for this package. for module in modules: if modules[module]['body'] is not None and modules[module][ 'body'].strip() != '': with open(self._abc_module(module), 'w') as write_file: write_file.write('{0}\n\n\n{1}'.format( '\n'.join(modules[module]['imports']), modules[module] ['body']).decode('utf-8').encode('utf-8')) with open( '{0}/services/{1}.py'.format( self._app_name(), self.replace( fix_reserved_word(self.package['name'], is_module=True))), 'w') as write_file: write_file.write('{0}{1}\n\n\n{2}'.format( summary, '\n'.join(modules[self.package['name']]['imports']), modules[self.package['name']]['body']).decode( 'utf-8').encode('utf-8'))
def _update_module_imports(self, modules, interface): package = self.replace( fix_reserved_word(self.package['name'], is_module=True)) if any(sn in interface['inherit_shortnames'] for sn in ['OsidManager', 'OsidProxyManager', 'OsidProfile']): module_name = 'service_managers' currentmodule_str = '.. currentmodule:: dlkit.services.{0}'.format( package) automodule_str = '.. automodule:: dlkit.services.{0}'.format( package) elif interface['shortname'] == self.patterns['package_catalog_caps']: module_name = self.patterns['package_catalog_under'] currentmodule_str = '.. currentmodule:: dlkit.services.{0}'.format( package) automodule_str = '' else: module_name = interface['category'] currentmodule_str = '.. currentmodule:: dlkit.{0}.{1}'.format( package, module_name) automodule_str = '.. automodule:: dlkit.{0}.{1}'.format( package, module_name) module_title = '\n{0}\n{1}'.format( ' '.join(module_name.split('_')).title(), '=' * len(module_name)) self.append(modules[module_name]['imports'], currentmodule_str) self.append(modules[module_name]['imports'], automodule_str) self.append(modules[module_name]['imports'], module_title) self._module_name = module_name
def set_and_del_property(method, service_catalog=None): method_name = strip_prefixes(method['name']) prop = ' .. py:attribute:: {0}\n'.format(fix_reserved_word(method_name)) if service_catalog is None: prop += '{0}:noindex:\n'.format(8 * ' ') return prop
def write_license_file(self): with open(self._abc_module('summary', extension='rst'), 'w') as write_file: pkg_name = self.replace( fix_reserved_word(self.package['name'], is_module=True)) write_file.write(('Summary\n=======\n\n' + '.. currentmodule:: dlkit.services.' + pkg_name + '\n' + '.. automodule:: dlkit.services.' + pkg_name + '\n').encode('utf-8'))
def set_and_del_property(method): prop = ' ' method_name = strip_prefixes(method['name']) prop += fix_reserved_word(method_name) clear_method = 'clear_' + method_name set_method = 'set_' + method_name prop += ' = property(fset={}, fdel={})'.format(set_method, clear_method) return prop
def _abc_pkg_name(self, abc=True, package_name=None, reserved_word=True): if package_name is None: package_name = self.package['name'] if reserved_word: pkg = fix_reserved_word(self.replace(package_name), is_module=True) else: pkg = self.replace(package_name) if abc: return self._abc_prefix + pkg + self._abc_suffix else: return self._pkg_prefix + pkg + self._pkg_suffix
def write_modules(self, modules): # Finally, iterate through the completed package module structure and # write out both the import statements and class definitions to the # appropriate module for this package. for module in modules: if modules[module]['body'].strip() != '': with open( '{0}/{1}.py'.format( self._app_name(), fix_reserved_word(self.first(self.package['name']), is_module=True)), 'w') as write_file: self._write_module_string(write_file, modules[module])
def simple_property(prop_type, method, property_name=None, service_catalog=None): method_name = strip_prefixes(method['name']) if property_name is None: attr_name = fix_reserved_word(method_name) else: attr_name = property_name prop = ' .. py:attribute:: {0}\n'.format(attr_name) if service_catalog is None: prop += '{0}:noindex:\n'.format(8 * ' ') return prop
def simple_property(prop_type, method, property_name=None): prop = ' ' method_name = strip_prefixes(method['name']) if property_name is None: prop += fix_reserved_word(method_name) else: prop += property_name if prop_type == 'get': method_name = 'get_' + method_name elif prop_type == 'del': method_name = 'clear_' + method_name elif prop_type == 'set': method_name = 'set_' + method_name else: raise ValueError() prop += ' = property(f{}={})'.format(prop_type, method_name) return prop
def method_iterator(root): from binder_helpers import fix_reserved_word, camel_to_under, fix_bad_name method = OrderedDict() method['name'] = fix_bad_name(camel_to_under(root.get(XOSID_NS + 'name'))) # Initialize the various dictionary elements so as to assure that there # will always be a return value, even if it is empty, since not all of # these will be caught in the for loop. This also helps ensure that the # ordering matches the example package, not that it matters. method['doc'] = {'headline': '', 'body': ''} method['arg_doc'] = '' method['return_doc'] = '' method['error_doc'] = '' method['sphinx_param_doc'] = '' method['sphinx_return_doc'] = '' method['sphinx_error_doc'] = '' method['compliance_doc'] = '' method['impl_notes_doc'] = '' method['args'] = [] method['arg_types'] = [] method['return_type'] = '' method['errors'] = OrderedDict() for child in root: # Process main method documentation: if child.tag == (XOSID_NS + 'description'): method['doc'] = parse_docstring(process_text(child, ' '), ' ') body = method['doc']['body'] method['doc']['body'] = '\n\n'.join(body.split('\n \n')) # Process parameter info into args dictionary and doc: if child.tag == (XOSID_NS + 'parameter'): param = fix_reserved_word( camel_to_under(child.get(XOSID_NS + 'name'))) array = False for grand_child in child: if grand_child.tag == (XOSID_NS + 'interfaceType'): param_type = grand_child.get(XOSID_NS + 'type') if grand_child.get(XOSID_NS + 'array') == 'true': array = True if grand_child.tag == (XOSID_NS + 'primitiveType'): param_type = grand_child.get(XOSID_NS + 'type') if grand_child.get(XOSID_NS + 'array') == 'true': array = True if array is True: param_type += '[]' method['args'].append({ 'var_name': param, 'arg_type': param_type, 'array': array }) method['arg_types'].append(param_type) method['arg_doc'] = method['arg_doc'] + make_param_doc(child) method['sphinx_param_doc'] += make_sphinx_param_doc(child) # Process return info into return type and doc: if child.tag == (XOSID_NS + 'return'): for grand_child in child: if grand_child.tag == (XOSID_NS + 'interfaceType'): return_type = grand_child.get(XOSID_NS + 'type') if grand_child.tag == (XOSID_NS + 'primitiveType'): return_type = grand_child.get(XOSID_NS + 'type') method['return_type'] = fix_bad_name(return_type, method['name']) method['return_doc'] = make_return_doc(child) method['sphinx_return_doc'] = make_sphinx_return_doc(child) # Process error info into error doc. Note that at this time I # am not parsing the exceptions. I have not found a need to do # this as of yet. if child.tag == (XOSID_NS + 'error'): method['errors'][child.get(XOSID_NS + 'type')] = child.get(XOSID_NS + 'category') if method['error_doc']: method['error_doc'] += '\n' if method['sphinx_error_doc']: method['sphinx_error_doc'] += '\n' method['error_doc'] = method['error_doc'] + make_error_doc(child) method['sphinx_error_doc'] += make_sphinx_error_doc(child) # Process compliance info into compliance doc. if child.tag == (XOSID_NS + 'compliance'): method['compliance_doc'] = make_compliance_doc(child) # Process implementation notes into impl notes doc. if child.tag == (XOSID_NS + 'implNotes'): method['impl_notes_doc'] = make_implnotes_doc(child) return method
def _get_method_context(self, method, interface): """Get the method context vars, to be used in the template""" def construct_arg_context(arg_number, arg_type_full): arg_type = self.last(arg_type_full).strip('[]') arg_context = { arg_number + '_type': arg_type, arg_number + '_type_under': camel_to_under(arg_type), arg_number + '_type_mixed': camel_to_mixed(arg_type), arg_number + '_abcapp_name': self._app_name(package_name=get_pkg_name( arg_type_full.strip('[]')), abstract=True), arg_number + '_abcpkg_name': self._abc_pkg_name(package_name=get_pkg_name( arg_type_full.strip('[]')), abc=False), arg_number + '_module': self.get_interface_module( self._abc_pkg_name( package_name=get_pkg_name(arg_type_full), abc=False), arg_type) } return arg_context context = {} if interface['shortname'] + '.' + method['name'] in self.patterns: context = self.patterns[interface['shortname'] + '.' + method['name']]['kwargs'] arg_list = [] for arg in method['args']: arg_list.append(arg['var_name']) context['arg_list'] = ', '.join(arg_list) if 'package_name' in context: # Add keyword arguments to template kwargs that are particular # to the json implementation fixed_package_name = fix_reserved_word(self.package['name'], is_module=True) context['app_name'] = self._app_name() context['implpkg_name'] = self._abc_pkg_name(abc=False, reserved_word=False) context['abcapp_name'] = self._app_name() context['abcpkg_name'] = self._abc_pkg_name(abc=False) context['interface_name_under'] = camel_to_under( context['interface_name']) context['interface_name_dot'] = '.'.join( context['interface_name_under'].split('_')[:-1]) context['package_name_caps'] = self.replace( self.package['name'].title(), desired='') context['package_name_upper'] = self.package['name'].upper() context['package_name_replace'] = self.replace( self.package['name']) context['package_name_replace_reserved'] = self.replace( fixed_package_name) context['package_name_replace_upper'] = self.replace( self.package['name']).upper() if method['args']: context['args_kwargs_or_nothing'] = '*args, **kwargs' else: context['args_kwargs_or_nothing'] = '' if context['interface_name_under'].endswith('_session'): context['session_shortname_dot'] = '.'.join( context['interface_name_under'].split('_')[:-1]) if 'arg0_type_full' in context: context.update( construct_arg_context('arg0', context['arg0_type_full'])) if 'arg1_type_full' in context: context.update( construct_arg_context('arg1', context['arg1_type_full'])) if 'arg2_type_full' in context: context.update( construct_arg_context('arg2', context['arg2_type_full'])) if 'arg3_type_full' in context: context.update( construct_arg_context('arg3', context['arg3_type_full'])) if 'arg0_object' in context: context['arg0_object_under'] = camel_to_under( context['arg0_object']) context['arg0_object_mixed'] = camel_to_mixed( context['arg0_object']) if 'return_type_full' in context: context['return_type'] = context['return_type_full'].split( '.')[-1] context['return_pkg'] = get_pkg_name( context['return_type_full']) context['return_module'] = self.get_interface_module( self._abc_pkg_name( package_name=get_pkg_name(context['return_type_full']), abc=False), context['return_type_full'].split('.')[-1]) if 'return_pkg' in context: context['return_app_name'] = self._app_name( package_name=context['return_pkg']) context['return_implpkg_name'] = self._abc_pkg_name( package_name=context['return_pkg'], abc=False) context['return_pkg_title'] = context['return_pkg'].title() context['return_pkg_caps'] = context['return_pkg'].upper() context['return_pkg_replace_caps'] = self.replace( context['return_pkg'].upper()) context['return_pkg_replace_title'] = self.replace( context['return_pkg'].title()) if 'return_cat_name' in context: context['return_cat_name_under'] = camel_to_under( context['return_cat_name']) if 'object_name_under' in context: context['object_name_upper'] = context[ 'object_name_under'].upper() context['object_name_mixed'] = under_to_mixed( context['object_name_under']) # Might want to add creating kwargs['object_name' from this as well] if 'object_name' in context and 'package_name' in context: context['object_app_name'] = self._app_name() context['object_implpkg_name'] = self._abc_pkg_name(abc=False) context['object_module'] = self.get_interface_module( 'package_name', 'object_name') if 'var_name' in context: context['var_name_upper'] = context['var_name'].upper() context['var_name_mixed'] = under_to_mixed(context['var_name']) context['var_name_plural'] = make_plural(context['var_name']) context['var_name_plural_mixed'] = under_to_mixed( context['var_name_plural']) context['var_name_singular'] = remove_plural( context['var_name']) context['var_name_singular_mixed'] = under_to_mixed( context['var_name_singular']) if 'return_type' in context: context['return_type_under'] = camel_to_under( context['return_type']) if 'return_type' in context and context['return_type'].endswith( 'List'): context['return_type_list_object'] = context[ 'return_type'][:-4] context['return_type_list_object_under'] = camel_to_under( context['return_type_list_object']) context['return_type_list_object_plural_under'] = make_plural( context['return_type_list_object_under']) if ('containable_object_name' in context and 'containable_object_name_under' not in context and 'containable_object_name_upper' not in context): context['containable_object_name_under'] = camel_to_under( context['containable_object_name']) context['containable_object_name_plural'] = make_plural( context['containable_object_name']) context[ 'containable_object_name_plural_under'] = camel_to_under( context['containable_object_name_plural']) context['containable_object_name_mixed'] = camel_to_mixed( context['containable_object_name']) context['containable_object_name_upper'] = camel_to_under( context['containable_object_name']).upper() if 'object_namespace' in context: context['object_package_name'] = '.'.join( context['object_namespace'].split('.')[:-1]) context['object_package_name_replace'] = '_'.join( context['object_package_name'].split('.')) context['object_package_name_replace_upper'] = context[ 'object_package_name_replace'].upper() context['object_namespace_replace'] = '_'.join( context['object_namespace'].split('.')) if ('object_name' in context and 'object_name_under' not in context and 'object_name_upper' not in context): context['object_name_plural'] = make_plural( context['object_name']) context['object_name_under'] = camel_to_under( context['object_name']) context['object_name_mixed'] = camel_to_mixed( context['object_name']) context['object_name_upper'] = camel_to_under( context['object_name']).upper() if 'object_name_under' in context: context['object_name_plural_under'] = make_plural( context['object_name_under']) if 'aggregated_object_name' in context: context['aggregated_object_name_under'] = camel_to_under( context['aggregated_object_name']) context['aggregated_object_name_mixed'] = camel_to_mixed( context['aggregated_object_name']) context['aggregated_objects_name_under'] = camel_to_under( make_plural(context['aggregated_object_name'])) context['aggregated_objects_name_mixed'] = camel_to_mixed( make_plural(context['aggregated_object_name'])) if 'source_name' in context: context['source_name_mixed'] = under_to_mixed( context['source_name']) if 'destination_name' in context: context['destination_name_mixed'] = under_to_mixed( context['destination_name']) if 'cat_name' in context: context['cat_name_under'] = camel_to_under(context['cat_name']) context['cat_name_lower'] = context['cat_name'].lower() context['cat_name_mixed'] = camel_to_mixed(context['cat_name']) context['cat_name_plural'] = make_plural(context['cat_name']) context['cat_name_plural_under'] = camel_to_under( context['cat_name_plural']) context['cat_name_plural_lower'] = context[ 'cat_name_plural'].lower() context['cat_name_plural_mixed'] = camel_to_mixed( context['cat_name_plural']) if 'return_cat_name' in context: context['return_cat_name_under'] = camel_to_under( context['return_cat_name']) context['return_cat_name_lower'] = context[ 'return_cat_name'].lower() context['return_cat_name_mixed'] = camel_to_mixed( context['return_cat_name']) if 'Proxy' in context['interface_name']: context['non_proxy_interface_name'] = ''.join( context['interface_name'].split('Proxy')) if ('return_pkg' in context and 'return_module' in context and context['package_name'] == context['return_pkg'] and context['module_name'] == context['return_module']): context['import_str'] = '' elif ('package_name' in context and 'return_pkg' in context and 'return_type' in context and 'return_module' in context): context[ 'import_str'] = '{0}from ..{1}.{2} import {3}\n'.format( self._dind, context['return_implpkg_name'], context['return_module'], context['return_type']) if 'method_name' in context and context['method_name'].startswith( 'can_'): context['func_name'] = context['method_name'].split('_')[1] if 'method_name' in context: context['method_session_name'] = context[ 'method_name'].replace('get_', '') if 'syntax' in context: context['syntax_under'] = syntax_to_under(context['syntax']) # Special one for services test builder: if self._is_manager_session(interface, self.package['name']): context['svc_mgr_or_catalog'] = 'svc_mgr' else: context['svc_mgr_or_catalog'] = 'catalog' return context