<commit_before>#
# This file is part of python-rhev. python-rhev is free software that is
# made available under the MIT license. Consult the file "LICENSE" that
# is distributed together with this file for the exact licensing terms.
#
# python-rhev is copyright (c) 2010 by the python-rhev authors. See the
# file "AUTHORS" for a complete overview.

import time

from rhev import _schema, inflect
from rhev.error import Error
from rhev._schema import BaseResource, BaseResources

from pyxb.binding.basis import complexTypeDefinition as ComplexType
from pyxb.binding.basis import simpleTypeDefinition as SimpleType
from pyxb.exceptions_ import PyXBException

# Maintaining this table is a (small) price we pay for slight naming
# inconsistencies in the API

_mapping_data = \
[
    # (resource, collection, relationship)
    (_schema.API, None, '/'),
    (_schema.DataCenter, _schema.DataCenters, 'datacenters'),
    (_schema.Cluster, _schema.Clusters, 'clusters'),
    (_schema.StorageDomain, _schema.StorageDomains, 'storagedomains'),
    (_schema.Network, _schema.Networks, 'networks'),
    (_schema.Host, _schema.Hosts, 'hosts'),
    (_schema.HostNIC, _schema.HostNics, 'nics'),
    (_schema.HostStorage, _schema.Storage, 'storage'),
    (_schema.VM, _schema.VMs, 'vms'),
    (_schema.NIC, _schema.Nics, 'nics'),
    (_schema.Disk, _schema.Disks, 'disks'),
    (_schema.CdRom, _schema.CdRoms, 'cdroms'),
    (_schema.Template, _schema.Templates, 'templates'),
    (_schema.VmPool, _schema.VmPools, 'vmpools'),
    (_schema.User, _schema.Users, 'users'),
    (_schema.Event, _schema.Events, 'events'),
    (_schema.Tag, _schema.Tags, 'tags'),
    (_schema.Action, _schema.Actions, None),
    (_schema.Capabilities, None, 'capabilities')
]

# This is a mapping of binding class -> element constructor. Element
# constructors are generated for all top-level elements.

_element_constructors = {}

for key in _schema.Namespace.elementBindings():
    cls = _schema.Namespace.elementBindings()[key].typeDefinition()
    _element_constructors[cls] = getattr(_schema, key)


def _type_info(typ_or_rel):
    """Return information for a mapping type or relationship."""
    for info in _mapping_data:
        if isinstance(typ_or_rel, basestring):
            if info[2] == typ_or_rel:
                break
        elif isinstance(typ_or_rel, type):
            if issubclass(typ_or_rel, info[0]) or \
                    info[1] is not None and issubclass(typ_or_rel, info[1]):
                break
    else:
        return
    info += (_element_constructors[info[0]].name().localName(),)
    if info[1] and info[1] in _element_constructors:
        info += (_element_constructors[info[1]].name().localName(),)
    else:
        info += (None,)
    info += (_element_constructors[info[0]],)
    if info[1] and info[1] in _element_constructors:
        info += (_element_constructors[info[1]],)
    else:
        info += (None,)
    return info

def new(cls, *args, **kwargs):
    """Create a new object."""
    info = _type_info(cls)
    if info is not None:
        if issubclass(cls, info[0]):
            factory = info[5]
        elif issubclass(cls, info[1]):
            factory = info[6]
    elif issubclass(cls, ComplexType):
        factory = cls
    else:
        raise TypeError, 'Do not know how to construct %s' % cls
    obj = factory(*args, **kwargs)
    return obj

def ref(obj):
    """Return a reference to an object. This is a copy of the object, with
    only the "id"  parameter set."""
    newobj = new(type(obj))
    newobj.id = obj.id
    return newobj

def href(obj):
    """Return a reference to an object. This is a copy of the object, with
    only the "id" and "href" parameters set."""
    newobj = new(type(obj))<commit_msg>Add new function subtype().<commit_after>#
# This file is part of python-rhev. python-rhev is free software that is
# made available under the MIT license. Consult the file "LICENSE" that
# is distributed together with this file for the exact licensing terms.
#
# python-rhev is copyright (c) 2010 by the python-rhev authors. See the
# file "AUTHORS" for a complete overview.

import time

from rhev import _schema, inflect
from rhev.error import Error
from rhev._schema import BaseResource, BaseResources

from pyxb.binding.basis import complexTypeDefinition as ComplexType
from pyxb.binding.basis import simpleTypeDefinition as SimpleType
from pyxb.exceptions_ import PyXBException

# Maintaining this table is a (small) price we pay for slight naming
# inconsistencies in the API

_mapping_data = \
[
    # (resource, collection, relationship)
    (_schema.API, None, '/'),
    (_schema.DataCenter, _schema.DataCenters, 'datacenters'),
    (_schema.Cluster, _schema.Clusters, 'clusters'),
    (_schema.StorageDomain, _schema.StorageDomains, 'storagedomains'),
    (_schema.Network, _schema.Networks, 'networks'),
    (_schema.Host, _schema.Hosts, 'hosts'),
    (_schema.HostNIC, _schema.HostNics, 'nics'),
    (_schema.HostStorage, _schema.Storage, 'storage'),
    (_schema.VM, _schema.VMs, 'vms'),
    (_schema.NIC, _schema.Nics, 'nics'),
    (_schema.Disk, _schema.Disks, 'disks'),
    (_schema.CdRom, _schema.CdRoms, 'cdroms'),
    (_schema.Template, _schema.Templates, 'templates'),
    (_schema.VmPool, _schema.VmPools, 'vmpools'),
    (_schema.User, _schema.Users, 'users'),
    (_schema.Event, _schema.Events, 'events'),
    (_schema.Tag, _schema.Tags, 'tags'),
    (_schema.Action, _schema.Actions, None),
    (_schema.Capabilities, None, 'capabilities')
]

# This is a mapping of binding class -> element constructor. Element
# constructors are generated for all top-level elements.

_element_constructors = {}

for key in _schema.Namespace.elementBindings():
    cls = _schema.Namespace.elementBindings()[key].typeDefinition()
    _element_constructors[cls] = getattr(_schema, key)


def _type_info(typ_or_rel):
    """Return information for a mapping type or relationship."""
    for info in _mapping_data:
        if isinstance(typ_or_rel, basestring):
            if info[2] == typ_or_rel:
                break
        elif isinstance(typ_or_rel, type):
            if issubclass(typ_or_rel, info[0]) or \
                    info[1] is not None and issubclass(typ_or_rel, info[1]):
                break
    else:
        return
    info += (_element_constructors[info[0]].name().localName(),)
    if info[1] and info[1] in _element_constructors:
        info += (_element_constructors[info[1]].name().localName(),)
    else:
        info += (None,)
    info += (_element_constructors[info[0]],)
    if info[1] and info[1] in _element_constructors:
        info += (_element_constructors[info[1]],)
    else:
        info += (None,)
    return info

def subtype(prop):
    """Return the binding type of a property."""
    return prop.fget.im_self.elementBinding().typeDefinition()

def new(cls, *args, **kwargs):
    """Create a new object."""
    info = _type_info(cls)
    if info is not None:
        if issubclass(cls, info[0]):
            factory = info[5]
        elif issubclass(cls, info[1]):
            factory = info[6]
    elif issubclass(cls, ComplexType):
        factory = cls
    else:
        raise TypeError, 'Do not know how to construct %s' % cls
    obj = factory(*args, **kwargs)
    return obj

def ref(obj):
    """Return a reference to an object. This is a copy of the object, with
    only the "id"  parameter set."""
    newobj = new(type(obj))
    newobj.id = obj.id
    return newobj

def href(obj):
    """Return a reference to an object. This is a copy of the object, with
    only the "id" and "href" parameters set."""
    newobj = new(type(obj))
{'fraction_non_alphanumeric': 0.12091503267973856, 'fraction_numerical': 0.00522875816993464, 'mean_word_length': 3.402186421173763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4521459', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2354, 'n_words': 715}