本文整理汇总了Python中spyne.util.odict.odict函数的典型用法代码示例。如果您正苦于以下问题:Python odict函数的具体用法?Python odict怎么用?Python odict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了odict函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, interface):
InterfaceDocumentBase.__init__(self, interface)
self.schema_dict = {}
self.validation_schema = None
self.namespaces = odict()
self.complex_types = set()
示例2: map
def map(self, django_model, exclude=None, **kwargs):
"""Prepare dict of model fields mapped to spyne models.
:param django_model: Django model class.
:param exclude: list of fields excluded from mapping.
:param kwargs: extra kwargs are passed to all field mappers
:returns: dict mapping attribute names to spyne models
:raises: :exc:`UnknownFieldMapperException`
"""
field_map = odict()
for field in self._get_fields(django_model, exclude):
field_type = field.get_internal_type()
try:
field_mapper = self._registry[field_type]
except KeyError:
# mapper for this field is not registered
if not (field.has_default() or field.null):
# field is required
raise self.UnknownFieldMapperException(
'No mapper for field type {0}'.format(field_type))
else:
# skip this field
logger.info('Field {0} is skipped from mapping.')
continue
attr_name, spyne_model = field_mapper.map(field, **kwargs)
field_map[attr_name] = spyne_model
return field_map
示例3: _parse_qs
def _parse_qs(qs):
pairs = (s2 for s1 in qs.split('&') for s2 in s1.split(';'))
retval = odict()
for name_value in pairs:
if name_value is None or len(name_value) == 0:
continue
nv = name_value.split('=', 1)
if len(nv) != 2:
# Handle case of a control-name with no equal sign
nv.append(None)
name = unquote(nv[0].replace('+', ' '))
value = None
if nv[1] is not None:
value = unquote(nv[1].replace('+', ' '))
l = retval.get(name, None)
if l is None:
l = retval[name] = []
l.append(value)
return retval
示例4: _s_customize
def _s_customize(cls, **kwargs):
"""This function duplicates and customizes the class it belongs to. The
original class remains unchanged.
Not meant to be overridden.
"""
cls_dict = odict({'__module__': cls.__module__})
if getattr(cls, '__orig__', None) is None:
cls_dict['__orig__'] = cls
class Attributes(cls.Attributes):
pass
if cls.Attributes.translations is None:
Attributes.translations = {}
if cls.Attributes.sqla_column_args is None:
Attributes.sqla_column_args = (), {}
cls_dict['Attributes'] = Attributes
# as nillable is a property, it gets reset everytime a new class is
# defined. So we need to reinitialize it explicitly.
Attributes.nillable = cls.Attributes.nillable
class Annotations(cls.Annotations):
pass
cls_dict['Annotations'] = Annotations
for k, v in kwargs.items():
if k.startswith('_'):
continue
elif k in ("doc", "appinfo"):
setattr(Annotations, k, v)
elif k in ('primary_key', 'pk'):
Attributes.sqla_column_args[-1]['primary_key'] = v
elif k in ('prot_attrs', 'pa'):
setattr(Attributes, 'prot_attrs', _decode_pa_dict(v))
elif k in ('foreign_key', 'fk'):
from sqlalchemy.schema import ForeignKey
t, d = Attributes.sqla_column_args
fkt = (ForeignKey(v),)
Attributes.sqla_column_args = (t + fkt, d)
elif k in ('autoincrement', 'onupdate', 'server_default'):
Attributes.sqla_column_args[-1][k] = v
elif k == 'max_occurs' and v in ('unbounded', 'inf', float('inf')):
setattr(Attributes, k, Decimal('inf'))
else:
setattr(Attributes, k, v)
return (cls.__name__, (cls,), cls_dict)
示例5: produce
def produce(namespace, type_name, members):
"""Lets you create a class programmatically."""
return ComplexModelMeta(type_name, (ComplexModel,), odict({
'__namespace__': namespace,
'__type_name__': type_name,
'_type_info': TypeInfo(members),
}))
示例6: produce
def produce(namespace, type_name, members):
"""Lets you create a class programmatically."""
return ComplexModelMeta(
type_name,
(ComplexModel,),
odict({"__namespace__": namespace, "__type_name__": type_name, "_type_info": TypeInfo(members)}),
)
示例7: __init__
def __init__(self, interface):
InterfaceDocumentBase.__init__(self, interface)
self.schema_dict = {}
self.validation_schema = None
pref = self.interface.prefmap[self.interface.app.tns]
self.namespaces = odict({pref: SchemaInfo()})
self.complex_types = set()
示例8: __init__
def __init__(self, interface):
super(XmlSchema, self).__init__(interface)
self.schema_dict = {}
self.validation_schema = None
pref = self.interface.prefmap[self.interface.app.tns]
self.namespaces = odict({pref: SchemaInfo()})
self.complex_types = set()
示例9: etree_to_dict
def etree_to_dict(element, iterable=(list, list.append)):
"""Takes an xml root element and returns the corresponding dict. The second
argument is a pair of iterable type and the function used to add elements to
the iterable. The xml attributes are ignored.
"""
if (element.text is None) or element.text.isspace():
retval = odict()
for elt in element:
if not (elt.tag in retval):
retval[elt.tag] = iterable[0]()
iterable[1](retval[elt.tag], etree_to_dict(elt, iterable))
else:
retval = element.text
return retval
示例10: parse_schema
def parse_schema(ctx, elt):
ctx.nsmap = nsmap = elt.nsmap
ctx.prefmap = prefmap = dict([(v,k) for k,v in ctx.nsmap.items()])
ctx.schema = schema = _prot.from_element(ctx, XmlSchema10, elt)
ctx.pending_types = {}
ctx.pending_elements = {}
ctx.tns = tns = schema.target_namespace
if tns in ctx.retval:
return
ctx.retval[tns] = _Schema()
ctx.debug0("1 %s processing includes", M(tns))
if schema.includes:
for include in schema.includes:
process_includes(ctx, include)
if schema.elements:
schema.elements = odict([(e.name, e) for e in schema.elements])
if schema.complex_types:
schema.complex_types = odict([(c.name, c) for c in schema.complex_types])
if schema.simple_types:
schema.simple_types = odict([(s.name, s) for s in schema.simple_types])
if schema.attributes:
schema.attributes = odict([(a.name, a) for a in schema.attributes])
ctx.debug0("2 %s processing imports", R(tns))
if schema.imports:
for imp in schema.imports:
if not imp.namespace in ctx.retval:
ctx.debug1("%s importing %s", tns, imp.namespace)
file_name = ctx.files[imp.namespace]
parse_schema_file(ctx.clone(2, dirname(file_name)), file_name)
ctx.retval[tns].imports.add(imp.namespace)
ctx.debug0("3 %s processing attributes", G(tns))
if schema.attributes:
for s in schema.attributes.values():
n, t= process_attribute(ctx, s)
ctx.retval[ctx.tns].types[n] = t
ctx.debug0("4 %s processing simple_types", G(tns))
if schema.simple_types:
for s in schema.simple_types.values():
st = process_simple_type(ctx, s)
ctx.retval[ctx.tns].types[s.name] = st
ctx.debug0("5 %s processing complex_types", B(tns))
if schema.complex_types:
for c in schema.complex_types.values():
process_complex_type(ctx, c)
ctx.debug0("6 %s processing elements", Y(tns))
if schema.elements:
for e in schema.elements.values():
process_schema_element(ctx, e)
process_pending(ctx)
if ctx.parent is None: # for the top-most schema
if ctx.children is not None: # if it uses <include> or <import>
# This is needed for schemas with circular imports
for c in chain([ctx], ctx.children):
print_pending(c)
ctx.debug0('')
for c in chain([ctx], ctx.children):
process_pending(c)
for c in chain([ctx], ctx.children):
process_pending(c)
ctx.debug0('')
for c in chain([ctx], ctx.children):
print_pending(c, fail=True)
return ctx.retval
示例11: parse_schema
def parse_schema(self, elt):
self.nsmap = nsmap = elt.nsmap
self.prefmap = prefmap = dict([(v,k) for k,v in self.nsmap.items()])
self.schema = schema = _prot.from_element(self, XmlSchema10, elt)
self.pending_types = {}
self.pending_elements = {}
self.tns = tns = schema.target_namespace
if self.tns is None:
self.tns = tns = '__no_ns__'
if tns in self.retval:
return
self.retval[tns] = _Schema()
self.debug0("1 %s processing includes", M(tns))
if schema.includes:
for include in schema.includes:
self.process_includes(include)
if schema.elements:
schema.elements = odict([(e.name, e) for e in schema.elements])
if schema.complex_types:
schema.complex_types = odict([(c.name, c) for c in schema.complex_types])
if schema.simple_types:
schema.simple_types = odict([(s.name, s) for s in schema.simple_types])
if schema.attributes:
schema.attributes = odict([(a.name, a) for a in schema.attributes])
self.debug0("2 %s processing imports", R(tns))
if schema.imports:
for imp in schema.imports:
if not imp.namespace in self.retval:
self.debug1("%s importing %s", tns, imp.namespace)
file_name = self.files[imp.namespace]
self.clone(2, dirname(file_name)).parse_schema_file(file_name)
self.retval[tns].imports.add(imp.namespace)
self.debug0("3 %s processing attributes", G(tns))
if schema.attributes:
for s in schema.attributes.values():
n, t = self.process_attribute(s)
self.retval[self.tns].types[n] = t
self.debug0("4 %s processing simple_types", G(tns))
if schema.simple_types:
for s in schema.simple_types.values():
st = self.process_simple_type(s)
self.retval[self.tns].types[s.name] = st
self.debug0("5 %s processing complex_types", B(tns))
if schema.complex_types:
for c in schema.complex_types.values():
self.process_complex_type(c)
self.debug0("6 %s processing elements", Y(tns))
if schema.elements:
for e in schema.elements.values():
self.process_schema_element(e)
self.process_pending()
if self.parent is None: # for the top-most schema
if self.children is not None: # if it uses <include> or <import>
# This is needed for schemas with circular imports
for c in chain([self], self.children):
c.print_pending()
self.debug0('')
# FIXME: This has no guarantee of working yet covers all the
# schema files found in the wild so far.
for c in chain([self], self.children):
c.process_pending()
for c in chain([self], self.children):
c.process_pending()
self.debug0('')
for c in chain([self], self.children):
c.print_pending(fail=True)
return self.retval
示例12: _s_customize
def _s_customize(cls, **kwargs):
"""This function duplicates and customizes the class it belongs to. The
original class remains unchanged.
Not meant to be overridden.
"""
cls_dict = odict({'__module__': cls.__module__, '__doc__': cls.__doc__})
if getattr(cls, '__orig__', None) is None:
cls_dict['__orig__'] = cls
class Attributes(cls.Attributes):
pass
if cls.Attributes.translations is None:
Attributes.translations = {}
if cls.Attributes.sqla_column_args is None:
Attributes.sqla_column_args = (), {}
cls_dict['Attributes'] = Attributes
# properties get reset everytime a new class is defined. So we need
# to reinitialize them explicitly.
for k in ('nillable', '_xml_cloth', '_xml_root_cloth', '_html_cloth',
'_html_root_cloth'):
v = getattr(cls.Attributes, k)
if v is not None:
setattr(Attributes, k, v)
class Annotations(cls.Annotations):
pass
cls_dict['Annotations'] = Annotations
# get protocol attrs
prot = kwargs.get('protocol', None)
if prot is None:
prot = kwargs.get('prot', None)
if prot is None:
prot = kwargs.get('p', None)
if prot is not None and len(prot.type_attrs) > 0:
# if there is a class customization from protocol, do it
type_attrs = prot.type_attrs.copy()
type_attrs.update(kwargs)
logger.debug("%r: kwargs %r => %r from prot typeattr %r",
cls, kwargs, type_attrs, prot.type_attrs)
kwargs = type_attrs
for k, v in kwargs.items():
if k.startswith('_'):
continue
if k in ('protocol', 'prot', 'p'):
setattr(Attributes, 'prot', v)
if k in ("doc", "appinfo"):
setattr(Annotations, k, v)
elif k in ('primary_key', 'pk'):
setattr(Attributes, 'primary_key', v)
Attributes.sqla_column_args[-1]['primary_key'] = v
elif k in ('protocol_attrs', 'prot_attrs', 'pa'):
setattr(Attributes, 'prot_attrs', _decode_pa_dict(v))
elif k in ('foreign_key', 'fk'):
from sqlalchemy.schema import ForeignKey
t, d = Attributes.sqla_column_args
fkt = (ForeignKey(v),)
Attributes.sqla_column_args = (t + fkt, d)
elif k in ('autoincrement', 'onupdate', 'server_default'):
Attributes.sqla_column_args[-1][k] = v
elif k == 'values_dict':
assert not 'values' in v, "`values` and `values_dict` can't be" \
"specified at the same time"
Attributes.values = v.keys()
Attributes.values_dict = v
elif k == 'max_occurs' and v in ('unbounded', 'inf', float('inf')):
setattr(Attributes, k, Decimal('inf'))
else:
setattr(Attributes, k, v)
return (cls.__name__, (cls,), cls_dict)
示例13: parse_schema
def parse_schema(self, elt):
self.nsmap = dict(elt.nsmap.items())
self.prefmap = dict([(v, k) for k, v in self.nsmap.items()])
self.schema = schema = _prot.from_element(self, XmlSchema10, elt)
self.pending_types = {}
self.pending_elements = {}
self.tns = tns = schema.target_namespace
if self.tns is None:
self.tns = tns = '__no_ns__'
if tns in self.retval:
return
self.retval[tns] = _Schema()
self.debug0("1 %s processing includes", MAG(tns))
if schema.includes:
for include in schema.includes:
self.process_includes(include)
if schema.elements:
schema.elements = odict([(e.name, e) for e in schema.elements])
if schema.complex_types:
schema.complex_types = odict([(c.name, c)
for c in schema.complex_types])
if schema.simple_types:
schema.simple_types = odict([(s.name, s)
for s in schema.simple_types])
if schema.attributes:
schema.attributes = odict([(a.name, a) for a in schema.attributes])
self.debug0("2 %s processing imports", R(tns))
if schema.imports:
for imp in schema.imports:
if not imp.namespace in self.retval:
self.debug1("%s importing %s", tns, imp.namespace)
fname = self.files[imp.namespace]
self.clone(2, dirname(fname)).parse_schema_file(fname)
self.retval[tns].imports.add(imp.namespace)
self.debug0("3 %s processing simple_types", G(tns))
if schema.simple_types:
for s in schema.simple_types.values():
self.process_simple_type(s)
# no simple types should have been left behind.
assert sum((len(v) for v in self.pending_simple_types.values())) == 0, \
self.pending_simple_types.values()
self.debug0("4 %s processing attributes", G(tns))
if schema.attributes:
for s in schema.attributes.values():
n, t = self.process_attribute(s)
self.retval[self.tns].types[n] = t
self.debug0("5 %s processing complex_types", B(tns))
if schema.complex_types:
for c in schema.complex_types.values():
self.process_complex_type(c)
self.debug0("6 %s processing elements", YEL(tns))
if schema.elements:
for e in schema.elements.values():
self.process_schema_element(e)
self.process_pending()
if self.parent is None: # for the top-most schema
if self.children is not None: # if it uses <include> or <import>
# This is needed for schemas with circular imports
for c in chain([self], self.children):
c.print_pending()
self.debug0('')
# FIXME: should put this in a while loop that loops until no
# changes occur
for c in chain([self], self.children):
c.process_pending()
for c in chain([self], self.children):
c.process_pending()
self.debug0('')
for c in chain([self], self.children):
c.print_pending(fail=(not self.skip_errors))
return self.retval
示例14: process_complex_type
#.........这里部分代码省略.........
def process_element(e):
if e.ref is not None:
tn = e.ref
name = e.ref.split(":", 1)[-1]
elif e.name is not None:
tn = e.type
name = e.name
if tn is None:
# According to http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#element-element
# this means this element is now considered to be a
# http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#ur-type-itself
self.debug2(" skipped: %s ur-type", e.name)
return
else:
raise Exception("dunno")
process_type(tn, name, element=e)
ti = []
base = ComplexModelBase
if c.name in self.retval[self.tns].types:
self.debug1("modifying existing %r", c.name)
else:
self.debug1("adding complex type: %s", c.name)
if c.sequence is not None:
if c.sequence.elements is not None:
for e in c.sequence.elements:
process_element(e)
if c.sequence.choices is not None:
for ch in c.sequence.choices:
if ch.elements is not None:
for e in ch.elements:
process_element(e)
if c.choice is not None:
if c.choice.elements is not None:
for e in c.choice.elements:
process_element(e)
if c.attributes is not None:
for a in c.attributes:
if a.name is None:
continue
if a.type is None:
continue
process_type(a.type, a.name, XmlAttribute, attribute=a)
if c.simple_content is not None:
sc = c.simple_content
ext = sc.extension
restr = sc.restriction
if ext is not None:
base_name = ext.base
b = self.get_type(ext.base)
if ext.attributes is not None:
for a in ext.attributes:
ti.append(self.process_attribute(a))
elif restr is not None:
base_name = restr.base
b = self.get_type(restr.base)
if restr.attributes is not None:
for a in restr.attributes:
ti.append(self.process_attribute(a))
else:
raise Exception("Invalid simpleContent tag: %r", sc)
if issubclass(b, ComplexModelBase):
base = b
else:
process_type(base_name, "_data", XmlData)
if c.name in self.retval[self.tns].types:
r = self.retval[self.tns].types[c.name]
r._type_info.update(ti)
else:
cls_dict = odict({
'__type_name__': c.name,
'__namespace__': self.tns,
'_type_info': ti,
})
if self.repr is not None:
cls_dict['__repr__'] = self.repr
r = ComplexModelMeta(str(c.name), (base,), cls_dict)
self.retval[self.tns].types[c.name] = r
return r
示例15: process_complex_type
#.........这里部分代码省略.........
if attribute is not None:
if attribute.default is not None:
kwargs['default'] = _prot.from_string(t, a.default)
if len(kwargs) > 0:
t = t.customize(**kwargs)
ti.append( (name, wrapper(t)) )
self.debug2(" found: %r(%s), c: %r", key, tn, kwargs)
def process_element(e):
if e.ref is not None:
tn = e.ref
name = e.ref.split(":", 1)[-1]
elif e.name is not None:
tn = e.type
name = e.name
else:
raise Exception("dunno")
process_type(tn, name, element=e)
class L(list):
def append(self, a):
k, v = a
assert isinstance(k, six.string_types), k
super(L, self).append(a)
ti = L()
base = ComplexModelBase
if c.name in self.retval[self.tns].types:
self.debug1("modifying existing %r", c.name)
else:
self.debug1("adding complex type: %s", c.name)
if c.sequence is not None:
if c.sequence.elements is not None:
for e in c.sequence.elements:
process_element(e)
if c.sequence.choices is not None:
for ch in c.sequence.choices:
if ch.elements is not None:
for e in ch.elements:
process_element(e)
if c.choice is not None:
if c.choice.elements is not None:
for e in c.choice.elements:
process_element(e)
if c.attributes is not None:
for a in c.attributes:
if a.name is None:
continue
if a.type is None:
continue
process_type(a.type, a.name, XmlAttribute, attribute=a)
if c.simple_content is not None:
ext = c.simple_content.extension
base_name = None
if ext is not None:
base_name = ext.base
b = self.get_type(ext.base)
if ext.attributes is not None:
for a in ext.attributes:
ti.append(self.process_attribute(a))
restr = c.simple_content.restriction
if restr is not None:
base_name = restr.base
b = self.get_type(restr.base)
if restr.attributes is not None:
for a in restr.attributes:
ti.append(self.process_attribute(a))
if issubclass(b, ComplexModelBase):
base = b
else:
process_type(base_name, "_data", XmlData)
if c.name in self.retval[self.tns].types:
self.retval[self.tns].types[c.name]._type_info.update(ti)
else:
cls_dict = odict({
'__type_name__': c.name,
'__namespace__': self.tns,
'_type_info': ti,
})
if self.repr is not None:
cls_dict['__repr__'] = self.repr
r = ComplexModelMeta(str(c.name), (base,), cls_dict)
self.retval[self.tns].types[c.name] = r