def test_simpledict(): """Test simpledict() class creation and usage. """ SD = simpledict('SD') check_class(SD, 'SD') check_frozenclass(SD.frozen, 'SD') check_structclass(SD.struct, 'SD') SD = simpledict('SD', frozenbase=None) assert not hasattr(SD, 'frozen') check_class(SD, 'SD') check_structclass(SD.struct, 'SD') SD = simpledict('SD', structbase=None) assert not hasattr(SD, 'struct') check_class(SD, 'SD') check_frozenclass(SD.frozen, 'SD') SD = simpledict('SD', frozenbase=None, structbase=None) assert not hasattr(SD, 'frozen') assert not hasattr(SD, 'struct') check_class(SD, 'SD')
def simpledict(typename, **kwargs): basedict = moretools.simpledict(typename, **kwargs) class Type(mdict.type, type(basedict)): @cached def __getitem__(cls, mtypes): class Type(type(cls)): mtype = mtuple[mtypes] mkeytype = mtype.mtypes[0] mvaluetype = mtype.mtypes[1] Type.__module__ = cls.__module__ Type.__name__ = '%s.type[%s, %s]' % ( cls.__name__, Type.mkeytype.__name__, Type.mvaluetype.__name__) class typedcls(with_metaclass(Type, cls)): pass typedcls.__module__ = cls.__module__ typedcls.__name__ = '%s[%s, %s]' % ( cls.__name__, Type.mkeytype.__name__, Type.mvaluetype.__name__) return typedcls class simpledict(with_metaclass(Type, typed.base, basedict)): def __setitem__(self, key, value): cls = type(self) if not isinstance(key, cls.mkeytype): key = cls.mkeytype(key) if not isinstance(value, cls.mvaluetype): value = cls.mvaluetype(value) basedict.__setitem__(self, key, value) try: # Taken from collections.py: simpledict.__module__ = Type.__module__ = ( sys._getframe(1).f_globals.get('__name__', '__main__')) except (AttributeError, ValueError): pass Type.__name__ = typename + '.type' simpledict.__name__ = typename return simpledict
"""Custom `simpledict.structbase` type to create PropertiesDict.struct class. """ def __init__(self, model, properties): def bases(): for cls in model.__bases__: if cls is not modelbase: yield cls.properties # Delegates properties to SimpleDictType.__init__() simpledict.structbase.__init__( # First arg is struct __name__ self, '%s.properties' % repr(model), bases(), properties) PropertiesDict = simpledict('PropertiesDict', structbase=PropertiesDictStructBase, dicttype=MembersDict.dicttype) class PropertyError(MemberError): __module__ = 'modeled' class Type(member.type): __module__ = 'modeled' error = PropertyError @cached def __getitem__(cls, mtype, typedcls=None): if not typedcls:
class ArgsDictStructType(SimpleDictStructType): """`basestructtype` for `simpledict()` to create ArgsDict.struct class. """ def __init__(self, model, args): def bases(): for cls in model.__bases__: if cls is not mobject.model: yield cls.args # Delegates args to SimpleDictType.__init__() SimpleDictStructType.__init__( # First arg is struct __name__ self, '%s.args' % repr(model), bases(), args) ArgsDict = simpledict( 'ArgsDict', basestructtype=ArgsDictStructType, dicttype=OrderedDict) class CFuncArgError(MemberError): __module__ = 'modeled' DEFAULT_MTYPES = { 'i': int, 'I': int, 'l': int, 'L': int, 'f': float, 'd': float, 'P': int,
"""`basestructtype` for `simpledict()` to create MembersDict.struct class. """ def __init__(self, model, widgets): def bases(): for cls in model.__bases__: if cls is not object: try: yield cls.widgets except AttributeError: pass # Delegates widgets to SimpleDictType.__init__() simpledict.structbase.__init__( # First arg is struct __name__ self, '%s.widgets' % repr(model), bases(), widgets) WidgetsDict = simpledict( 'WidgetsDict', structbase=WidgetsDictStructBase, dicttype=OrderedDict) class Widget(object): def __init__(self, Q, name, *args, **kwargs): self.Q = Q self.name = name self.args = args self.kwargs = kwargs self.id = None def __call__(self): Q = self.Q if self.id: Q = Q.id[self.id] return getattr(Q, self.name)(*self.args, **self.kwargs)
# from .keywords 'KeywordDecoratorType', 'InvalidKeywordOption', ] from inspect import isclass from collections import OrderedDict from moretools import simpledict from .base import TestLibraryType from .keywords import ( KeywordsDict, KeywordDecoratorType, InvalidKeywordOption) # Ordered name-mapped storage of user-defined session/context handlers HandlersDict = simpledict('HandlersDict', dicttype=OrderedDict) def testlibrary( register_keyword_options=[], default_keyword_options=[], context_handlers=[], session_handlers=[] ): """Creates the actual base type for a user-defined Robot Test Library derived from :class:`TestLibraryType`. - Generates a Keyword decorator class from `.keywords.KeywordDecoratorType`, adding the decorators from `register_keyword_options`. - Adds the `keyword` decorator to the Test Library class
options.setdefault(optgroup, Options())[opt] = value SimpleDictType.__init__( self, {opt: value for opt, value in dict(options).items() if opt and not opt.startswith('_')}) class OptionsStructType(SimpleDictStructType): """`basestructtype` for `simpledict()` to create Options.struct class. """ def __init__(self, model, options=()): """Initialize an Options.struct instance with the associated <modeled.object>.`model` instance and an `options` mapping resulting from user-defined `class model` of a :class:`modeled.object` based class definition. """ def bases(): for cls in model.__bases__: if cls is not modelbase: yield cls.options # Delegates options to OptionsType.__init__() SimpleDictStructType.__init__( # First arg is struct __name__ self, '%s.options' % repr(model), bases(), options) Options = simpledict( 'Options', basetype=OptionsType, basestructtype=OptionsStructType) from modeled.model import modelbase
return str.__new__(cls, value) def __init__(self, value, ref=None): try: self.ref = ref or value.ref except AttributeError: self.ref = value class Type(object): def __init__(self, name, typename): self.name = name self.typename = Typename(typename) class TypesDictType(SimpleDictType): def __init__(self, *types): SimpleDictType.__init__(self) for t in types: if type(t) is not Type: t = Type(*t) self[t.name] = t TypesDict = simpledict("TypesDict", basetype=TypesDictType, dicttype=OrderedDict, iterate="values") def manager(typename): return Typename(typename, typename + "::manager_type")
simpledict.structbase.__init__( # First arg is struct __name__ self, '%s.members' % repr(model), bases(), members) def __call__(self, properties=True): if properties: return iter(self) def members(): for name, member in self: if not modeled.ismodeledproperty(member): yield name, member return members() MembersDict = simpledict( 'MembersDict', structbase=MembersDictStructBase, dicttype=OrderedDict) class MemberError(AttributeError): __module__ = 'modeled' ## MemberError.__name__ = 'modeled.MemberError' # To assign a unique id the every new member instance, # to make them orderable (incremented in member.__init__): _memberid = 0 class Type(typed.base.type): """Metaclass for :class:`member`.
class PropertiesDictStructBase(simpledict.structbase): """Custom `simpledict.structbase` type to create PropertiesDict.struct class. """ def __init__(self, model, properties): def bases(): for cls in model.__bases__: if cls is not modelbase: yield cls.properties # Delegates properties to SimpleDictType.__init__() simpledict.structbase.__init__( # First arg is struct __name__ self, '%s.properties' % repr(model), bases(), properties) PropertiesDict = simpledict( 'PropertiesDict', structbase=PropertiesDictStructBase, dicttype=MembersDict.dicttype) class PropertyError(MemberError): __module__ = 'modeled' class Type(member.type): __module__ = 'modeled' error = PropertyError @cached def __getitem__(cls, mtype, typedcls=None): if not typedcls:
class ArgsDictStructType(SimpleDictStructType): """`basestructtype` for `simpledict()` to create ArgsDict.struct class. """ def __init__(self, model, args): def bases(): for cls in model.__bases__: if cls is not mobject.model: yield cls.args # Delegates args to SimpleDictType.__init__() SimpleDictStructType.__init__( # First arg is struct __name__ self, '%s.args' % repr(model), bases(), args) ArgsDict = simpledict('ArgsDict', basestructtype=ArgsDictStructType, dicttype=OrderedDict) class CFuncArgError(MemberError): __module__ = 'modeled' DEFAULT_MTYPES = { 'i': int, 'I': int, 'l': int, 'L': int, 'f': float, 'd': float, 'P': int,
"""robottools.library.inspector.multi .. moduleauthor:: Stefan Zimmermann <*****@*****.**> """ __all__ = ["MultiTestLibraryInspector"] from collections import OrderedDict from itertools import chain from moretools import camelize, simpledict from . import TestLibraryInspector from .keyword import KeywordInspector TestLibrariesDict = simpledict("TestLibrariesDict", dicttype=OrderedDict) class MultiTestLibraryInspector(object): def __init__(self, *libraries): libslist = [] for lib in libraries: if type(lib) is not TestLibraryInspector: lib = TestLibraryInspector(str(lib)) libslist.append(lib) self.libraries = TestLibrariesDict.frozen((lib.name, lib) for lib in libslist) @property def __doc__(self): return "%s\n\n%s" % ( repr(self),
"""robottools.library.inspector.multi .. moduleauthor:: Stefan Zimmermann <*****@*****.**> """ __all__ = ['MultiTestLibraryInspector'] from collections import OrderedDict from itertools import chain from moretools import camelize, simpledict from . import TestLibraryInspector from .keyword import KeywordInspector TestLibrariesDict = simpledict('TestLibrariesDict', dicttype=OrderedDict) class MultiTestLibraryInspector(object): def __init__(self, *libraries): libslist = [] for lib in libraries: if type(lib) is not TestLibraryInspector: lib = TestLibraryInspector(str(lib)) libslist.append(lib) self.libraries = TestLibrariesDict.frozen( (lib.name, lib) for lib in libslist) @property def __doc__(self): return '%s\n\n%s' % (repr(self), '\n\n'.join(sorted(
try: self.ref = ref or value.ref except AttributeError: self.ref = value class Type(object): def __init__(self, name, typename): self.name = name self.typename = Typename(typename) class TypesDictType(SimpleDictType): def __init__(self, *types): SimpleDictType.__init__(self) for t in types: if type(t) is not Type: t = Type(*t) self[t.name] = t TypesDict = simpledict('TypesDict', basetype=TypesDictType, dicttype=OrderedDict, iterate='values') def manager(typename): return Typename(typename, typename + '::manager_type')
# from .keywords 'KeywordDecoratorType', 'InvalidKeywordOption', ] from inspect import isclass from collections import OrderedDict from moretools import simpledict from .base import TestLibraryType from .keywords import (KeywordsDict, KeywordDecoratorType, InvalidKeywordOption) # Ordered name-mapped storage of user-defined session/context handlers HandlersDict = simpledict('HandlersDict', dicttype=OrderedDict) def testlibrary(register_keyword_options=[], default_keyword_options=[], context_handlers=[], session_handlers=[]): """Creates the actual base type for a user-defined Robot Test Library derived from :class:`TestLibraryType`. - Generates a Keyword decorator class from `.keywords.KeywordDecoratorType`, adding the decorators from `register_keyword_options`. - Adds the `keyword` decorator to the Test Library class by instantiating the decorator class with the `default_keyword_options`. - For every handler in `session_handlers`,
class IntTypes(simpledict('IntTypes', iterate='values')): def __getitem__(self, name): for int_ in self: if name in (int_, int_.cfo): return int_ raise KeyError(name)