Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@generic
def typeinfo_for(context):
"""Return the relevant ``sharing.TypeInfo`` for `context`
`context` may be a URI string, a ``sharing.TypeInfo``, ``sharing.Field``,
or ``schema.Descriptor`` (e.g. ``schema.One``, etc.). object. It can also
be any object registered as a type alias using ``sharing.typedef()``.
The return value is a ``sharing.TypeInfo``. If no type information is
available for `context`, raise ``sharing.UnknownType``
"""
raise UnknownType(context)
class ItemPaintHovered(object):
"""
Paints on top of all items, just for the hovered item (see painter.HoveredItemPainter)
"""
def __init__(self, item, view):
self.item = item
self.view = view
def paint(self, context, selected):
pass
PaintHovered = generic(ItemPaintHovered)
def add_converter(context, from_type, converter):
"""Register `converter` for converting `from_type` in `context`"""
# XXX if converters cached by record types, add hooks here for updating
gf = get_converter(context)
if not get_converter.has_object(context):
gf = generic(gf) # extend base function
get_converter.when_object(context)(lambda context: gf)
gf.when_type(from_type)(converter)
@generic
def get_converter(context):
"""Return a conversion function for encoding in the `context` type context
"""
ctx = typeinfo_for(context)
if isinstance(context,type) and issubclass(context, TypeInfo):
# prevent infinite recursion for unregistered primitive types
# if this error occurs, somebody added a new primitive type without
# creating a base conversion function for it
raise AssertionError("Unregistered metatype", ctx)
return get_converter(ctx)
@generic
def format_field(context, value):
"""Format a value based on a field or typeinfo"""
return value
def subtype(typeinfo, *args, **kw):
"""XXX"""
newti = typeinfo_for(typeinfo).clone(*args, **kw)
gf = generic(get_converter(typeinfo))
get_converter.when_object(newti)(lambda ctx:gf)
return newti
@generic
def default_converter(value):
raise TypeError(
"No converter registered for values of type %r" % type(value)
)
def create_default_converter(t):
converter = generic(default_converter)
converter.when_object(NoChange)(lambda val: val)
converter.when_object(Inherit)(lambda val: val)
converter.when_object(None)(lambda val: val)
get_converter.when_object(t)(lambda ctx: converter)