def __init__(self, items=None, **options): if items is None: items = [] try: assert(issubclass(self.mtype, modeled.list)) except AttributeError: items = modeled.list(items) self.__class__ = type(self)[items.mtype] member.__init__(self, items, **options) self.indexname = options.get('indexname', 'index') try: self.itemname = options['itemname'] except KeyError: if modeled.ismodeledclass(self.itemtype): self.itemname = decamelize(self.itemtype.__name__) else: self.itemname = 'item'
def __init__(self, items=None, **options): if items is None: items = [] try: assert (issubclass(self.mtype, modeled.list)) except AttributeError: items = modeled.list(items) self.__class__ = type(self)[items.mtype] member.__init__(self, items, **options) self.indexname = options.get('indexname', 'index') try: self.itemname = options['itemname'] except KeyError: if modeled.ismodeledclass(self.itemtype): self.itemname = decamelize(self.itemtype.__name__) else: self.itemname = 'item'
def __init__(self, items=None, **options): if items is None: items = {} try: assert(issubclass(self.mtype, modeled.dict)) except AttributeError: items = modeled.dict(items) self.__class__ = type(self)[items.mtype.mtypes] member.__init__(self, items, **options) self.keyname = options.get('keyname', 'key') try: self.valuename = options['valuename'] except KeyError: if modeled.ismodeledclass(self.valuetype): self.valuename = decamelize(self.valuetype.__name__) else: self.valuename = 'value'
def to_statement(cls, namespace=None, prefix=None, revision=None): """Get YANG module as :class:`pyang.statement.Statement` instance, ready for feeding to a pyang output plugin. """ # adapted modeled class is used as YANG module and main container module = Statement(None, None, None, 'module', cls.yangname) if not prefix: prefix = decamelize(cls.__name__, joiner='-') if namespace: namespace = Statement(None, module, None, 'namespace', namespace) module.substmts.append(namespace) prefix = Statement(None, module, None, 'prefix', prefix) module.substmts.append(prefix) if not revision: revision = str(date.today()) revision = Statement(None, module, None, 'revision', revision) module.substmts.append(revision) container = super(YANGModuleMeta, cls).to_statement(parent=module) module.substmts.append(container) # add defined @rpc methods for name, obj in getmembers(cls): if getattr(obj, '__isnetconfrpc__', False): method = obj yangname = name.replace('_', '-') rpc = Statement(None, module, None, 'rpc', yangname) module.substmts.append(rpc) if method.__doc__: description = Statement(None, rpc, None, 'description', method.__doc__.strip()) rpc.substmts.append(description) # if the rpc method has args then add an input statement, # containing a leaf statement for every arg argspec = getargspec(method) if len(argspec.args) > 1: # ignore 'self' input_ = Statement(None, rpc, None, 'input', None) rpc.substmts.append(input_) for name in argspec.args[1:]: yangname = name.replace('_', '-') mtype = method.mtypes[name] statement = cls.member_to_statement(mtype, yangname=yangname, parent=input_) input_.substmts.append(statement) return module
def to_statement(cls, namespace=None, prefix=None, revision=None): """Get YANG module as :class:`pyang.statement.Statement` instance, ready for feeding to a pyang output plugin. """ # adapted modeled class is used as YANG module and main container module = Statement(None, None, None, 'module', cls.yangname) if not prefix: prefix = decamelize(cls.__name__, joiner='-') if namespace: namespace = Statement(None, module, None, 'namespace', namespace) module.substmts.append(namespace) prefix = Statement(None, module, None, 'prefix', prefix) module.substmts.append(prefix) if not revision: revision = str(date.today()) revision = Statement(None, module, None, 'revision', revision) module.substmts.append(revision) container = super(YANGModuleMeta, cls).to_statement(parent=module) module.substmts.append(container) # add defined @rpc methods for name, obj in getmembers(cls): if getattr(obj, '__isnetconfrpc__', False): method = obj yangname = name.replace('_', '-') rpc = Statement(None, module, None, 'rpc', yangname) module.substmts.append(rpc) if method.__doc__: description = Statement( None, rpc, None, 'description', method.__doc__.strip()) rpc.substmts.append(description) # if the rpc method has args then add an input statement, # containing a leaf statement for every arg argspec = getargspec(method) if len(argspec.args) > 1: # ignore 'self' input_ = Statement(None, rpc, None, 'input', None) rpc.substmts.append(input_) for name in argspec.args[1:]: yangname = name.replace('_', '-') mtype = method.mtypes[name] statement = cls.member_to_statement( mtype, yangname=yangname, parent=input_) input_.substmts.append(statement) return module
def test_decamelize(): """Test the string decamelize() function. """ for lower_case, CamelCase in CAMELIZED: assert decamelize(CamelCase) == lower_case
def __init__(self, handlerclsname=None, options=None): """Generate several variants of a session handler name for use in identifiers and message strings. - Based on the `handlerclsname` and/or the attributes of the optional `Handler.Meta` class in `options`, which can define name (variant) prefixes/suffixes and/or explicit name variants. """ # Check all prefix definitions and generate actual prefix strings prefixes = {} def gen_prefix(key, default, append=''): """Check the prefix definition for name variant identified by `key`. - Set to `default` if not defined. - Always `append` the given extra string. """ try: prefix = getattr( options, (key and key + '_') + 'name_prefix') except AttributeError: prefix = default else: prefix = prefix and str(prefix) or '' if prefix and not prefix.endswith(append): prefix += append # Finally add to the prefixes dictionary prefixes[key] = prefix def gen_plural_prefix(key, append=''): """Check the prefix definition for plural name variant identified by plural_`key`. - Set to singular `key` prefix if not defined. - Always `append` the given extra string. """ plural_key = 'plural' + (key and '_' + key) default = prefixes[key] gen_prefix(plural_key, default, append) # Base name prefixes gen_prefix('', '', '_') gen_plural_prefix('', '_') gen_prefix('upper', camelize(prefixes[''])) gen_plural_prefix('upper') # Identifier name prefixes gen_prefix('identifier', '', '_') gen_plural_prefix('identifier', '_') gen_prefix('upper_identifier', camelize(prefixes['identifier'])) # Verbose name prefixes gen_prefix('verbose', '', ' ') gen_plural_prefix('verbose', ' ') # Check all suffix definitions and generate actual suffix strings suffixes = {} def gen_suffix(key, default, prepend=''): """Check the suffix definition for name variant identified by `key`. - Set to `default` if not defined. - Always `prepend` the given extra string. """ try: suffix = getattr(options, key + '_name_suffix') except AttributeError: suffix = default else: suffix = suffix and str(suffix) or '' if suffix and not suffix.startswith(prepend): suffix = prepend + suffix # Finally add to the suffixes dictionary suffixes[key] = suffix def gen_plural_suffix(key, prepend=''): """Check the suffix definition for plural name variant identified by plural_`key`. - Set to singular `key` suffix + 's' if not defined. - Always `prepend` the given extra string. """ plural_key = 'plural' + (key and '_' + key) default = suffixes[key] and suffixes[key] + 's' gen_suffix(plural_key, default, prepend) # Identifier name suffixes gen_suffix('', '', '_') gen_plural_suffix('', '_') gen_suffix('upper', camelize(suffixes[''])) gen_plural_suffix('upper') # Identifier name suffixes ## gen_suffix('identifier', 'session', '_') gen_suffix('identifier', '', '_') gen_plural_suffix('identifier', '_') gen_suffix('upper_identifier', camelize(suffixes['identifier'])) # Verbose name suffixes ## gen_suffix('verbose', 'Session', ' ') gen_suffix('verbose', '', ' ') gen_plural_suffix('verbose', ' ') # Check explicit name variant definitions variants = {} for variantkey in ( '', 'plural', 'upper', 'plural_upper', 'identifier', 'plural_identifier', 'upper_identifier', 'verbose', 'plural_verbose' ): defname = (variantkey and variantkey + '_') + 'name' variant = getattr(options, defname, None) # Non-empty string or None variant = variant and (str(variant) or None) or None variants[variantkey] = variant # Create final base name (helper) variants # (NOT stored in final meta object (self)) key = '' name = ( variants[key] or prefixes[key] + decamelize(handlerclsname) + suffixes[key]) key = 'plural' plural_name = ( variants[key] and prefixes[key] + variants[key] + suffixes[key] or None) key = 'upper' upper_name = ( variants[key] or variants[''] and camelize(variants['']) or prefixes[key] + handlerclsname + suffixes[key]) key = 'plural_upper' plural_upper_name = ( variants[key] or variants['plural'] and prefixes[key] + camelize(variants['plural']) + ( suffixes[key] or (not variants['plural'] and 's' or '')) or None) # Create final identifier/verbose name variants # (stored in final meta object (self)) key = 'identifier' self.identifier_name = ( variants[key] or prefixes[key] + name + suffixes[key]) key = 'plural_identifier' self.plural_identifier_name = ( variants[key] or prefixes[key] + (plural_name or name) + ( suffixes[key] or (not plural_name and 's' or ''))) key = 'upper_identifier' self.upper_identifier_name = ( variants[key] or prefixes[key] + upper_name + suffixes[key]) key = 'verbose' self.verbose_name = ( variants[key] or prefixes[key] + upper_name + suffixes[key]) key = 'plural_verbose' self.plural_verbose_name = ( variants[key] or prefixes[key] + (plural_upper_name or upper_name) + ( suffixes[key] or (not plural_upper_name and 's' or '')))
def __init__(self, handlerclsname=None, options=None): """Generate several variants of the session handler name for use in identifiers and message strings and store additional general `options` given as ``Handler.Meta`` class object. - Name generation is based on the `handlerclsname` and/or related `options` attributes, which can define name (variant) prefixes/suffixes and/or explicit name variants. """ self.auto_explicit = bool(getattr(options, 'auto_explicit', False)) # Check all prefix definitions and generate actual prefix strings prefixes = {} def gen_prefix(key, default, append=''): """Check the prefix definition for name variant identified by `key`. - Set to `default` if not defined. - Always `append` the given extra string. """ try: prefix = getattr(options, (key and key + '_') + 'name_prefix') except AttributeError: prefix = default else: prefix = prefix and str(prefix) or '' if prefix and not prefix.endswith(append): prefix += append # Finally add to the prefixes dictionary prefixes[key] = prefix def gen_plural_prefix(key, append=''): """Check the prefix definition for plural name variant identified by plural_`key`. - Set to singular `key` prefix if not defined. - Always `append` the given extra string. """ plural_key = 'plural' + (key and '_' + key) default = prefixes[key] gen_prefix(plural_key, default, append) # Base name prefixes gen_prefix('', '', '_') gen_plural_prefix('', '_') gen_prefix('upper', camelize(prefixes[''])) gen_plural_prefix('upper') # Identifier name prefixes gen_prefix('identifier', '', '_') gen_plural_prefix('identifier', '_') gen_prefix('upper_identifier', camelize(prefixes['identifier'])) # Verbose name prefixes gen_prefix('verbose', '', ' ') gen_plural_prefix('verbose', ' ') # Check all suffix definitions and generate actual suffix strings suffixes = {} def gen_suffix(key, default, prepend=''): """Check the suffix definition for name variant identified by `key`. - Set to `default` if not defined. - Always `prepend` the given extra string. """ try: suffix = getattr(options, key + '_name_suffix') except AttributeError: suffix = default else: suffix = suffix and str(suffix) or '' if suffix and not suffix.startswith(prepend): suffix = prepend + suffix # Finally add to the suffixes dictionary suffixes[key] = suffix def gen_plural_suffix(key, prepend=''): """Check the suffix definition for plural name variant identified by plural_`key`. - Set to singular `key` suffix + 's' if not defined. - Always `prepend` the given extra string. """ plural_key = 'plural' + (key and '_' + key) default = suffixes[key] and suffixes[key] + 's' gen_suffix(plural_key, default, prepend) # Identifier name suffixes gen_suffix('', '', '_') gen_plural_suffix('', '_') gen_suffix('upper', camelize(suffixes[''])) gen_plural_suffix('upper') # Identifier name suffixes ## gen_suffix('identifier', 'session', '_') gen_suffix('identifier', '', '_') gen_plural_suffix('identifier', '_') gen_suffix('upper_identifier', camelize(suffixes['identifier'])) # Verbose name suffixes ## gen_suffix('verbose', 'Session', ' ') gen_suffix('verbose', '', ' ') gen_plural_suffix('verbose', ' ') # Check explicit name variant definitions variants = {} for variantkey in ('', 'plural', 'upper', 'plural_upper', 'identifier', 'plural_identifier', 'upper_identifier', 'verbose', 'plural_verbose'): defname = (variantkey and variantkey + '_') + 'name' variant = getattr(options, defname, None) # Non-empty string or None variant = variant and (str(variant) or None) or None variants[variantkey] = variant # Create final base name (helper) variants # (NOT stored in final meta object (self)) key = '' name = (variants[key] or prefixes[key] + decamelize(handlerclsname) + suffixes[key]) key = 'plural' plural_name = (variants[key] and prefixes[key] + variants[key] + suffixes[key] or None) key = 'upper' upper_name = (variants[key] or variants[''] and camelize(variants['']) or prefixes[key] + handlerclsname + suffixes[key]) key = 'plural_upper' plural_upper_name = (variants[key] or variants['plural'] and prefixes[key] + camelize(variants['plural']) + (suffixes[key] or (not variants['plural'] and 's' or '')) or None) # Create final identifier/verbose name variants # (stored in final meta object (self)) key = 'identifier' self.identifier_name = (variants[key] or prefixes[key] + name + suffixes[key]) key = 'plural_identifier' self.plural_identifier_name = (variants[key] or prefixes[key] + (plural_name or name) + (suffixes[key] or (not plural_name and 's' or ''))) key = 'upper_identifier' self.upper_identifier_name = (variants[key] or prefixes[key] + upper_name + suffixes[key]) key = 'verbose' self.verbose_name = (variants[key] or prefixes[key] + upper_name + suffixes[key]) key = 'plural_verbose' self.plural_verbose_name = (variants[key] or prefixes[key] + (plural_upper_name or upper_name) + (suffixes[key] or (not plural_upper_name and 's' or '')))
def yangname(cls): """Generate YANG container name by decamelizing modeled class name with hyphen separators. """ return decamelize(cls.mclass.model.name, joiner='-')