Example #1
0
    def testStrict(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)
Example #2
0
 def testIssue(self):
     elm = sample.sampleelementType()
     elm.ValueAmount = "100"
     elm.ValueAmount.currencyID = "abc"
     sam = sample.samplerootelement()
     sam.sampleelement.append(elm)
     bds = pyxb.utils.domutils.BindingDOMSupport()
     bds.setDefaultNamespace(sample.Namespace)
     bds.declareNamespace(xsi)
     samdom = sam.toDOM(bds)
     bds.addAttribute(samdom.documentElement, xsi.createExpandedName("schemaLocation"), "c:\sample.xsd")
     xmld = samdom.toprettyxml(encoding="utf-8")
     self.assertEqual(self.Expectedd, xmld)
Example #3
0
 def testIssue (self):
     elm = sample.sampleelementType()
     elm.ValueAmount = '100'
     elm.ValueAmount.currencyID = 'abc'
     sam = sample.samplerootelement()
     sam.sampleelement.append(elm)
     bds = pyxb.utils.domutils.BindingDOMSupport()
     bds.setDefaultNamespace(sample.Namespace)
     bds.declareNamespace(xsi)
     samdom = sam.toDOM(bds)
     bds.addAttribute(samdom.documentElement, xsi.createExpandedName('schemaLocation'), "c:\sample.xsd")
     xmld = samdom.toprettyxml(encoding = "utf-8")
     self.assertEqual(self.Expectedd, xmld)
Example #4
0
 def testIssue (self):
     elm = sample.sampleelementType()
     elm.ValueAmount = '100'
     elm.ValueAmount.currencyID = 'abc'
     sam = sample.samplerootelement()
     sam.sampleelement.append(elm)
     bds = pyxb.utils.domutils.BindingDOMSupport()
     bds.setDefaultNamespace(sample.Namespace)
     bds.declareNamespace(xsi)
     samdom = sam.toDOM(bds)
     bds.addAttribute(samdom.documentElement, xsi.createExpandedName('schemaLocation'), "c:\sample.xsd")
     # xsi is probably not referenced elsewhere, so add the XMLNS declaration too
     bds.addXMLNSDeclaration(samdom.documentElement, xsi)
     xmld = samdom.toprettyxml(encoding = "utf-8")
     self.assertEqual(self.Expectedd, xmld)
Example #5
0
    def to_xml(self, encoding='utf-8'):
        """
        Get an XML document from this instance and its values.

        :param encoding: Character encoding to declare the XML document with. Default: utf-8
        :type encoding: basestring
        :return: The complete XML document of brreg:Melding
        :rtype: unicode
        """
        bds = pyxb.utils.domutils.BindingDOMSupport()
        bds.setDefaultNamespace(melding.Namespace)
        bds.declareNamespace(xsi)
        elem_dom = self.element.toDOM(bds)
        bds.addAttribute(elem_dom.documentElement,
                         xsi.createExpandedName('schemaLocation'),
                         self.schemaLocation)
        bds.addXMLNSDeclaration(elem_dom.documentElement, xsi)
        return elem_dom.toxml(encoding=encoding)
Example #6
0
    def testStrict(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        # This only raises with saxer, which treats the bogus xsi:type as
        # a content failure.  With saxdom and minidom, that error gets
        # lost by the code that thinks it's ok to pass a DOM node where
        # no binding could be created.  This should be reconciled.
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)
Example #7
0
    def startElementNS(self, name, qname, attrs):
        (this_state, parent_state, ns_ctx,
         name_en) = super(PyXBSAXHandler,
                          self).startElementNS(name, qname, attrs)

        # Delegate processing if in DOM mode
        if this_state.inDOMMode():
            return this_state.startDOMElement(attrs)

        # Resolve the element within the appropriate context.  Note
        # that global elements have no use, only the binding.
        if parent_state.enclosingCTD() is not None:
            (element_binding, element_decl) = parent_state.enclosingCTD(
            )._ElementBindingDeclForName(name_en)
        else:
            element_decl = None
            element_binding = name_en.elementBinding()
        this_state.setElementBinding(element_binding)

        # Non-root elements should have an element use, from which we can
        # extract the binding if we couldn't find one elsewhere.  (Keep any
        # current binding, since it may be a member of a substitution group.)
        if (element_decl is not None) and (element_binding is None):
            assert self.__rootObject is not None
            element_binding = element_decl.elementBinding()
            assert element_binding is not None

        # Start knowing nothing
        type_class = None
        if element_binding is not None:
            element_binding = element_binding.elementForName(name)
            type_class = element_binding.typeDefinition()

        # Process an xsi:type attribute, if present
        if self.__XSITypeTuple in attrs:
            (did_replace, type_class) = XSI._InterpretTypeAttribute(
                attrs.getValue(self.__XSITypeTuple), ns_ctx,
                self.fallbackNamespace(), type_class)
            if did_replace:
                element_binding = None

        if type_class is None:
            # Bother.  We don't know what this thing is.  But that's not an
            # error, if the schema accepts wildcards.  For consistency with
            # the DOM-based interface, we need to build a DOM node.
            return this_state.enterDOMMode(attrs)

        if element_binding is not None:
            # Invoke binding __call__ method not Factory, so can check for
            # abstract elements.
            new_object_factory = element_binding
        else:
            new_object_factory = type_class.Factory

        # Update the enclosing complex type definition for this
        # element state.
        assert type_class is not None
        if issubclass(type_class, pyxb.binding.basis.complexTypeDefinition):
            this_state.setEnclosingCTD(type_class)
        else:
            this_state.setEnclosingCTD(parent_state.enclosingCTD())

        # Process the element start.  This may or may not return a
        # binding object.
        binding_object = this_state.startBindingElement(
            type_class, new_object_factory, element_decl, attrs)

        # If the top-level element has complex content, this sets the
        # root object.  If it has simple content, see endElementNS.
        if self.__rootObject is None:
            self.__rootObject = binding_object
Example #8
0
def Export_NeuroML (network_file, neuroML_file ):
    """Reads .net file and exports it to NeuroML (Level3) format
    @param *network_file (.net) network model file name
    @param *neuroML_file neuroML file name
    @return Finished reading file if the process goes well"""

    filename = network_file
    if os.path.isfile ( filename )==False:
        print('\nERROR: unable to read neurons _network_ data file')
        os._exit(0)
        return None

    bds = pyxb.utils.domutils.BindingDOMSupport()
    bds.declareNamespace(xsi)
    network = ml.neuroml()
    py_net.Read_Network(network_file)

    t = time.clock()

    network.properties  
    network.length_units='micrometer'
    network.populations = net.Populations()
    network.cells = ml.Level3Cells()
    xsi.createExpandedName('schemaLocation')
    network.channels = cml.ChannelML()
    network.channels.units = meta.Units( "Physiological Units")



    # Channels
    print('writing channels')
    for sy in range(1):
        network.channels.synapse_type.append(cml.SynapseType())

    sy = 0  # synapse_type Dual Exponential

    network.channels.synapse_type[sy].name = 'Exp2Syn'
    network.channels.synapse_type[sy].status= meta.Status()
    network.channels.synapse_type[sy].status.value_ = 'stable'
    network.channels.synapse_type[sy].doub_exp_syn = cml.DoubleExponentialSynapse()
    network.channels.synapse_type[sy].doub_exp_syn.max_conductance = bio.ConductanceValue()
    network.channels.synapse_type[sy].doub_exp_syn.reversal_potential = bio.VoltageValue()
    network.channels.synapse_type[sy].doub_exp_syn.decay_time = bio.TimeConstantValue(1.0)
    network.channels.synapse_type[sy].doub_exp_syn.rise_time = bio.TimeConstantValueIncZero()
    network.channels.synapse_type[sy].doub_exp_syn.max_conductance = 0.001
    network.channels.synapse_type[sy].doub_exp_syn.reversal_potential = 0
    network.channels.synapse_type[sy].doub_exp_syn.decay_time = 5.0
    network.channels.synapse_type[sy].doub_exp_syn.rise_time = 1.0

    for ch in range (2):
        network.channels.channel_type.append(cml.ChannelType())
        
    ch = 0 # channel_type Passive conductance

    network.channels.channel_type[ch].name = 'pas'
    network.channels.channel_type[ch].status= meta.Status()
    network.channels.channel_type[ch].status.value_ = 'stable'
    network.channels.channel_type[ch].current_voltage_relation = cml.CurrentVoltageRelation()
    network.channels.channel_type[ch].current_voltage_relation.cond_law = 'ohmic'
    network.channels.channel_type[ch].current_voltage_relation.ion = 'non_specific'
    network.channels.channel_type[ch].current_voltage_relation.default_erev = -70.0
    network.channels.channel_type[ch].current_voltage_relation.default_gmax = 0.001

    ch = 1 # channel-type Integrate and fire

    network.channels.channel_type[ch].name = 'IF'
    network.channels.channel_type[ch].status= meta.Status()
    network.channels.channel_type[ch].status.value_ = 'stable'
    network.channels.channel_type[ch].current_voltage_relation = cml.CurrentVoltageRelation()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire = cml.IntegrateAndFire()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.threshold = bio.VoltageValue()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.t_refrac = bio.TimeValue()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.v_reset = bio.VoltageValue()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.g_refrac = bio.ConductanceValue()
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.threshold = -50.0 
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.t_refrac = 5.0
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.v_reset = -70.0
    network.channels.channel_type[ch].current_voltage_relation.integrate_and_fire.g_refrac = 100.0

    for pop in range(py_net.total_populations):

        # Cells
        print('writing cells')
        network.cells.cell.append(ml.Level3Cell())
        network.cells.cell[pop].name = 'Cell_' + str(pop)
        network.cells.cell[pop].notes = meta.Notes()
        network.cells.cell[pop].notes = 'Cell type created in NeuralSyns simulation environment'
        network.cells.cell[pop].segments = mml.CTD_ANON_3()
        network.cells.cell[pop].cables = mml.CTD_ANON_4()
        network.cells.cell[pop].biophysics = ml.Level3Biophysics()
        network.cells.cell[pop].biophysics.units="Physiological Units"
        network.cells.cell[pop].connectivity = net.Level3Connectivity()
        
       
        for fdm in range (py_net.population[pop].n_fdomains):

            # Cables        
            
            network.cells.cell[pop].cables.cable.append(mml.Cable())
            network.cells.cell[pop].cables.cable[fdm].id = py_net.population[pop].fdomain[fdm].fdm_ID
            network.cells.cell[pop].cables.cable[fdm].name = str('group_' + py_net.population[pop].fdomain[fdm].label)
            network.cells.cell[pop].cables.cable[fdm].group = meta.Group()
            network.cells.cell[pop].cables.cable[fdm].group.append('group_' + py_net.population[pop].fdomain[fdm].label)
            
    for pop in range(py_net.total_populations):            
        for fdm in range (py_net.population[pop].n_fdomains):
            # Fdomains (Segments)

            network.cells.cell[pop].segments.segment.append(mml.Segment())
            network.cells.cell[pop].segments.segment[fdm].id = py_net.population[pop].fdomain[fdm].fdm_ID
            network.cells.cell[pop].segments.segment[fdm].name = py_net.population[pop].fdomain[fdm].label
            network.cells.cell[pop].segments.segment[fdm].proximal = meta.Point()
            network.cells.cell[pop].segments.segment[fdm].distal = meta.Point()
            if py_net.population[pop].fdomain[fdm].fdm_ID == 0:
                network.cells.cell[pop].segments.segment[fdm].proximal.x = py_net.population[pop].fdomain[fdm].position.x
                network.cells.cell[pop].segments.segment[fdm].proximal.y = py_net.population[pop].fdomain[fdm].position.y
                network.cells.cell[pop].segments.segment[fdm].proximal.z = py_net.population[pop].fdomain[fdm].position.z
                network.cells.cell[pop].segments.segment[fdm].proximal.diameter = float(py_net.population[pop].fdomain[fdm].position.r) * 2.0
                network.cells.cell[pop].segments.segment[fdm].distal.x = py_net.population[pop].fdomain[fdm].position.x
                network.cells.cell[pop].segments.segment[fdm].distal.y = py_net.population[pop].fdomain[fdm].position.y
                network.cells.cell[pop].segments.segment[fdm].distal.z = py_net.population[pop].fdomain[fdm].position.z
                network.cells.cell[pop].segments.segment[fdm].distal.diameter = float(py_net.population[pop].fdomain[fdm].position.r) * 2.0
            else :
                network.cells.cell[pop].segments.segment[fdm].parent = py_net.population[pop].fdomain[fdm].parent_fdomain
                network.cells.cell[pop].segments.segment[fdm].proximal.x = py_net.population[pop].fdomain[py_net.population[pop].fdomain[fdm].parent_fdomain].position.x
                network.cells.cell[pop].segments.segment[fdm].proximal.y = py_net.population[pop].fdomain[py_net.population[pop].fdomain[fdm].parent_fdomain].position.y
                network.cells.cell[pop].segments.segment[fdm].proximal.z = py_net.population[pop].fdomain[py_net.population[pop].fdomain[fdm].parent_fdomain].position.z
                network.cells.cell[pop].segments.segment[fdm].proximal.diameter = float(py_net.population[pop].fdomain[py_net.population[pop].fdomain[fdm].parent_fdomain].position.r) * 2    
                network.cells.cell[pop].segments.segment[fdm].distal.x = py_net.population[pop].fdomain[fdm].position.x
                network.cells.cell[pop].segments.segment[fdm].distal.y = py_net.population[pop].fdomain[fdm].position.y
                network.cells.cell[pop].segments.segment[fdm].distal.z = py_net.population[pop].fdomain[fdm].position.z
                network.cells.cell[pop].segments.segment[fdm].distal.diameter = float(py_net.population[pop].fdomain[fdm].position.r) * 2.0
            network.cells.cell[pop].segments.segment[fdm].cable = network.cells.cell[pop].cables.cable[fdm].id


        # Biophysics
        print('writing biophysics')
         

        for m in range(2):
            network.cells.cell[pop].biophysics.mechanism.append(bio.Mechanism())
            network.cells.cell[pop].biophysics.mechanism[m].type = bio.MechanismType('Channel Mechanism')

        m = 0 # IF model    
        network.cells.cell[pop].biophysics.mechanism[m].name = network.channels.channel_type[1].name
        network.cells.cell[pop].biophysics.mechanism[m].passive_conductance = 'true'
        network.cells.cell[pop].biophysics.mechanism[m].parameter.append(bio.NamedParameter())
        network.cells.cell[pop].biophysics.mechanism[m].parameter[0].group.append( network.cells.cell[pop].cables.cable[0].name )
        network.cells.cell[pop].biophysics.mechanism[m].parameter[0].name = 'V_rest'
        network.cells.cell[pop].biophysics.mechanism[m].parameter[0].value_ = -70.0
        network.cells.cell[pop].biophysics.mechanism[m].parameter.append(bio.NamedParameter())
        network.cells.cell[pop].biophysics.mechanism[m].parameter[1].group.append( network.cells.cell[pop].cables.cable[0].name )
        network.cells.cell[pop].biophysics.mechanism[m].parameter[1].name = 'gmax'
        network.cells.cell[pop].biophysics.mechanism[m].parameter[1].value_ = 0.3

        m = 1 # passive model
        network.cells.cell[pop].biophysics.mechanism[m].name = network.channels.channel_type[0].name
        network.cells.cell[pop].biophysics.mechanism[m].passive_conductance = 'true'
        network.cells.cell[pop].biophysics.mechanism[m].parameter.append(bio.NamedParameter())
        for fdm in range (0,py_net.population[pop].n_fdomains) :
            network.cells.cell[pop].biophysics.mechanism[m].parameter[0].group.append( network.cells.cell[pop].cables.cable[fdm].name )
        network.cells.cell[pop].biophysics.mechanism[m].parameter[0].name = 'e'
        network.cells.cell[pop].biophysics.mechanism[m].parameter[0].value_ = 0.001
        network.cells.cell[pop].biophysics.mechanism[m].parameter.append(bio.NamedParameter())
        for fdm in range (0,py_net.population[pop].n_fdomains) :
            network.cells.cell[pop].biophysics.mechanism[m].parameter[1].group.append( network.cells.cell[pop].cables.cable[fdm].name )
        network.cells.cell[pop].biophysics.mechanism[m].parameter[1].name = 'gmax'
        network.cells.cell[pop].biophysics.mechanism[m].parameter[1].value_ = 0.3

        # Capacitance and axial resistance        
        network.cells.cell[pop].biophysics.spec_capacitance = bio.SpecCapacitance()
        network.cells.cell[pop].biophysics.spec_capacitance.parameter.append(bio.UnnamedParameter())
        network.cells.cell[pop].biophysics.spec_capacitance.parameter[0].value_ = 1.0 # default_value
        network.cells.cell[pop].biophysics.spec_capacitance.parameter[0].group.append('all')
        network.cells.cell[pop].biophysics.spec_axial_resistance = bio.SpecAxialResistance()
        for fdm in range (py_net.population[pop].n_fdomains):
            network.cells.cell[pop].biophysics.spec_axial_resistance.parameter.append(bio.UnnamedParameter())
            network.cells.cell[pop].biophysics.spec_axial_resistance.parameter[fdm].value_ = 0.1 #default_value
            network.cells.cell[pop].biophysics.spec_axial_resistance.parameter[fdm].group.append( network.cells.cell[pop].cables.cable[fdm].name )
            
    #connections
    #default (connections between any segment will be available in neuroconstruct)
    for popi in range(py_net.total_populations):
        network.cells.cell[popi].connectivity.potential_syn_loc.append(net.PotentialSynLoc())
        network.cells.cell[popi].connectivity.potential_syn_loc[0].group_.append('all')
        network.cells.cell[popi].connectivity.potential_syn_loc[0].synapse_type = network.channels.synapse_type[0].name
                    
    for pop in range(py_net.total_populations): 
                
        # Populations    
        print('writing populations')
        network.populations.population.append(net.Population())
        network.populations.population[pop].name = py_net.population[pop].label
        network.populations.population[pop].cell_type_ = network.cells.cell[pop].name
        network.populations.population[pop].instances = net.Instances()
        network.populations.population[pop].instances.size = py_net.population[pop].n_neurons    
        for nrn in range (py_net.population[pop].n_neurons):
            network.populations.population[pop].instances.instance.append(net.CellInstance())
            network.populations.population[pop].instances.instance[nrn].id = nrn
            network.populations.population[pop].instances.instance[nrn].location =  meta.Point3D()
            network.populations.population[pop].instances.instance[nrn].location.x =  py_net.neuron[py_net.population[pop].first_neuron+nrn].position.x
            network.populations.population[pop].instances.instance[nrn].location.y =  py_net.neuron[py_net.population[pop].first_neuron+nrn].position.y
            network.populations.population[pop].instances.instance[nrn].location.z =  py_net.neuron[py_net.population[pop].first_neuron+nrn].position.z

              
    # Connectivity (projections and connections)
    print('writing synapses')
    if py_net.total_synapses != 0:

        network.projections = net.Projections()
        network.projections.units = meta.Units('Physiological Units')
        pairs = 0
        for popi in range (py_net.total_populations):
            #total_connections = 0
            #total_connections = py_net.population[popi].n_neurons * py_net.neuron[py_net.population[popi].first_neuron].n_synapses
            for popj in range (py_net.total_populations):
                if py_net.connection[popi][popj]!=0:
                    pop_source = popi
                    pop_target = popj
                    print(popi, popj)
                    network.projections.projection.append(net.Projection())
                    network.projections.projection[pairs].name = py_net.connection[pop_source][pop_target].label
                    network.projections.projection[pairs].source_ = py_net.population[pop_source].label
                    network.projections.projection[pairs].target_ = str(py_net.population[pop_target].label)
                    network.projections.projection[pairs].synapse_props.append( net.GlobalSynapticProperties())
                    network.projections.projection[pairs].synapse_props[0].synapse_type_ = network.channels.synapse_type[0].name
                    network.projections.projection[pairs].synapse_props[0].internal_delay = py_net.neuron[py_net.population[pop_source].first_neuron].synapse[0].delay
                    network.projections.projection[pairs].synapse_props[0].weight = py_net.neuron[py_net.population[pop_source].first_neuron].synapse[0].weight
                    network.projections.projection[pairs].connections = net.Connections()
                   
                        
                    
                    c = 0
                    post_cell = []
                    pre_cell = []
                    for nrn in range (py_net.population[pop_source].first_neuron,py_net.population[pop_source].first_neuron + py_net.population[pop_source].n_neurons):
                        for syn in range(py_net.neuron[nrn].n_synapses):
                            p_target = py_net.neuron[py_net.neuron[nrn].synapse[syn].target_neuron].population
                            post_cell_id = py_net.neuron[nrn].synapse[syn].target_neuron - py_net.population[p_target].first_neuron
                                                  
                            if (p_target == pop_target) :
                                post_cell.append(int(post_cell_id))
                                pre_cell.append(nrn)
                                c = c + 1
                   
                    network.projections.projection[pairs].connections.size = c
                    for conn in range (c):
                        network.projections.projection[pairs].connections.connection.append(net.Connection())
                        network.projections.projection[pairs].connections.connection[conn].properties_.append(net.LocalSynapticProperties())
                        network.projections.projection[pairs].connections.connection[conn].id = conn
                        network.projections.projection[pairs].connections.connection[conn].pre_cell_id = pre_cell[conn] - py_net.population[pop_source].first_neuron
                        network.projections.projection[pairs].connections.connection[conn].pre_segment_id = 0 # source segment is always soma
                        network.projections.projection[pairs].connections.connection[conn].post_cell_id = post_cell[conn]
                        network.projections.projection[pairs].connections.connection[conn].post_segment_id = py_net.connection[pop_source][pop_target].fdomain
                        network.projections.projection[pairs].connections.connection[conn].post_fraction_along = meta.ZeroToOne()
                        network.projections.projection[pairs].connections.connection[conn].post_fraction_along = 0.5
                        
                    pairs = pairs + 1

          
    #Inputs

    network.inputs = net.Inputs()
    network.inputs.units = meta.Units('SI Units')
    network.inputs.input.append(net.Input())
    network.inputs.input[0].name = 'RandomInput'
    network.inputs.input[0].target = net.InputTarget()
    network.inputs.input[0].target.population = network.populations.population[0].name
    network.inputs.input[0].target.sites = net.InputSites()
    network.inputs.input[0].target.sites.size = 1
    network.inputs.input[0].target.sites.site.append(net.InputSite())
    network.inputs.input[0].target.sites.site[0].cell_id = net.CellIdInNetwork()
    network.inputs.input[0].target.sites.site[0].cell_id = 0
    network.inputs.input[0].random_stim = net.RandomStim()
    network.inputs.input[0].random_stim.frequency = bio.FrequencyValue()
    network.inputs.input[0].random_stim.frequency = 10
    network.inputs.input[0].random_stim.synaptic_mechanism = network.channels.synapse_type[0].name

    print('Writing your xml network')

    file(neuroML_file, 'w').write(network.toxml("utf-8"))
    print "\n\nFinished writing file. Elapsed time = ", time.clock()-t, 'secs.'
Example #9
0
    def startElementNS (self, name, qname, attrs):
        (this_state, parent_state, ns_ctx, name_en) = super(PyXBSAXHandler, self).startElementNS(name, qname, attrs)

        # Delegate processing if in DOM mode
        if this_state.inDOMMode():
            return this_state.startDOMElement(attrs)

        # Resolve the element within the appropriate context.  Note
        # that global elements have no use, only the binding.
        if parent_state.enclosingCTD() is not None:
            (element_binding, element_use) = parent_state.enclosingCTD()._ElementBindingUseForName(name_en)
        else:
            element_use = None
            element_binding = name_en.elementBinding()
        this_state.setElementBinding(element_binding)

        # Non-root elements should have an element use, from which we can
        # extract the binding if we couldn't find one elsewhere.  (Keep any
        # current binding, since it may be a member of a substitution group.)
        if (element_use is not None) and (element_binding is None):
            assert self.__rootObject is not None
            element_binding = element_use.elementBinding()
            assert element_binding is not None

        # Start knowing nothing
        type_class = None
        if element_binding is not None:
            element_binding = element_binding.elementForName(name)
            type_class = element_binding.typeDefinition()

        # Process an xsi:type attribute, if present
        if attrs.has_key(self.__XSITypeTuple):
            (did_replace, type_class) = XSI._InterpretTypeAttribute(attrs.getValue(self.__XSITypeTuple), ns_ctx, self.fallbackNamespace(), type_class)
            if did_replace:
                element_binding = None

        if type_class is None:
            # Bother.  We don't know what this thing is.  But that's not an
            # error, if the schema accepts wildcards.  For consistency with
            # the DOM-based interface, we need to build a DOM node.
            return this_state.enterDOMMode(attrs)

        if element_binding is not None:
            # Invoke binding __call__ method not Factory, so can check for
            # abstract elements.
            new_object_factory = element_binding
        else:
            new_object_factory = type_class.Factory

        # Update the enclosing complex type definition for this
        # element state.
        assert type_class is not None
        if issubclass(type_class, pyxb.binding.basis.complexTypeDefinition):
            this_state.setEnclosingCTD(type_class)
        else:
            this_state.setEnclosingCTD(parent_state.enclosingCTD())

        # Process the element start.  This may or may not return a
        # binding object.
        binding_object = this_state.startBindingElement(type_class, new_object_factory, element_use, attrs)

        # If the top-level element has complex content, this sets the
        # root object.  If it has simple content, see endElementNS.
        if self.__rootObject is None:
            self.__rootObject = binding_object
Example #10
0
 def tearDown(self):
     self.__basis_log.level = self.__basis_loglevel
     XSI.ProcessTypeAttribute(XSI.PT_strict)
Example #11
0
class TestTrac_0060(unittest.TestCase):
    def setUp(self):
        self.__basis_log = logging.getLogger('pyxb.binding.basis')
        self.__basis_loglevel = self.__basis_log.level

    def tearDown(self):
        self.__basis_log.level = self.__basis_loglevel
        XSI.ProcessTypeAttribute(XSI.PT_strict)

    AString = 'hi'
    NotAnInteger = 'not an integer'
    AnInteger = 34
    BaseUntyped = '<base><child>%s</child></base>' % (NotAnInteger, )
    BaseString = '<base xsi:type="String" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    BaseInteger = '<base xsi:type="Integer" xmlns:xsi="%s"><child>%d</child></base>' % (
        XSI.uri(), AnInteger)
    BaseForeign = '<base xsi:type="Foreign" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    BaseUnknown = '<base xsi:type="Unknown" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    CorrectString = '<string><child>%s</child></string>' % (AString, )
    CorrectInteger = '<integer><child>%d</child></integer>' % (AnInteger, )
    BadInteger = '<integer><child>%s</child></integer>' % (NotAnInteger, )
    ConflictString = '<integer xsi:type="String" xmlns:xsi="%s"><child>%s</child></integer>' % (
        XSI.uri(), NotAnInteger)
    ConflictStringInteger = '<integer xsi:type="String" xmlns:xsi="%s"><child>%d</child></integer>' % (
        XSI.uri(), AnInteger)
    UntypedIntegerElement = '<element><child>%d</child></element>' % (
        AnInteger, )
    TypedElement = '<element xsi:type="Integer" xmlns:xsi="%s"><child>%d</child></element>' % (
        XSI.uri(), AnInteger)
    # There is no type Float in the schema
    BogusTypedElement = '<element xsi:type="Float" xmlns:xsi="%s"><child>%d</child></element>' % (
        XSI.uri(), AnInteger)
    BogusTypedInteger = '<integer xsi:type="Float" xmlns:xsi="%s"><child>%d</child></integer>' % (
        XSI.uri(), AnInteger)

    def makeWC(self, body):
        return '<wildcard>%s</wildcard>' % (body, )

    def testWildcardString(self):
        xmls = '<wildcard>%s</wildcard>' % (self.CorrectString, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.AString, instance.child)

    def testWildcardInteger(self):
        xmls = '<wildcard>%s</wildcard>' % (self.CorrectInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

    def testWildcardIntegerBad(self):
        xmls = '<wildcard>%s</wildcard>' % (self.BadInteger, )
        self.assertRaises(pyxb.SimpleTypeValueError, CreateFromDocument, xmls)

    def testWildcardUntyped(self):
        # Inhibit warning about conversion; we get a DOM node out of this.
        self.__basis_log.level = logging.ERROR
        xmls = '<wildcard>%s</wildcard>' % (self.UntypedIntegerElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

    def testWildcardTyped(self):
        xmls = '<wildcard>%s</wildcard>' % (self.TypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))

    def testBase(self):
        xmls = self.makeWC(self.BaseString)
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BaseInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

    def testStrict(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        # This only raises with saxer, which treats the bogus xsi:type as
        # a content failure.  With saxdom and minidom, that error gets
        # lost by the code that thinks it's ok to pass a DOM node where
        # no binding could be created.  This should be reconciled.
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

    def testLax(self):
        # Lax won't convert DOM nodes
        self.__basis_log.level = logging.ERROR
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_lax)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

    def testSkip(self):
        # Skip won't convert DOM nodes
        self.__basis_log.level = logging.ERROR
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_skip)

        # skip uses element name to force to integer, which content doesn't match
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.SimpleTypeValueError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.SimpleTypeValueError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseString)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseInteger)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)
Example #12
0
    def testSkip(self):
        # Skip won't convert DOM nodes
        self.__basis_log.level = logging.ERROR
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_skip)

        # skip uses element name to force to integer, which content doesn't match
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.SimpleTypeValueError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.SimpleTypeValueError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseString)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseInteger)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)
Example #13
0
    def testLax(self):
        # Lax won't convert DOM nodes
        self.__basis_log.level = logging.ERROR
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_lax)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)
Example #14
0
 def tearDown(self):
     XSI.ProcessTypeAttribute(XSI.PT_strict)
Example #15
0
class TestTrac_0060(unittest.TestCase):
    def tearDown(self):
        XSI.ProcessTypeAttribute(XSI.PT_strict)

    AString = 'hi'
    NotAnInteger = 'not an integer'
    AnInteger = 34
    BaseUntyped = '<base><child>%s</child></base>' % (NotAnInteger, )
    BaseString = '<base xsi:type="String" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    BaseInteger = '<base xsi:type="Integer" xmlns:xsi="%s"><child>%d</child></base>' % (
        XSI.uri(), AnInteger)
    BaseForeign = '<base xsi:type="Foreign" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    BaseUnknown = '<base xsi:type="Unknown" xmlns:xsi="%s"><child>%s</child></base>' % (
        XSI.uri(), NotAnInteger)
    CorrectString = '<string><child>%s</child></string>' % (AString, )
    CorrectInteger = '<integer><child>%d</child></integer>' % (AnInteger, )
    BadInteger = '<integer><child>%s</child></integer>' % (NotAnInteger, )
    ConflictString = '<integer xsi:type="String" xmlns:xsi="%s"><child>%s</child></integer>' % (
        XSI.uri(), NotAnInteger)
    ConflictStringInteger = '<integer xsi:type="String" xmlns:xsi="%s"><child>%d</child></integer>' % (
        XSI.uri(), AnInteger)
    UntypedIntegerElement = '<element><child>%d</child></element>' % (
        AnInteger, )
    TypedElement = '<element xsi:type="Integer" xmlns:xsi="%s"><child>%d</child></element>' % (
        XSI.uri(), AnInteger)
    BogusTypedElement = '<element xsi:type="Float" xmlns:xsi="%s"><child>%d</child></element>' % (
        XSI.uri(), AnInteger)
    BogusTypedInteger = '<integer xsi:type="Float" xmlns:xsi="%s"><child>%d</child></integer>' % (
        XSI.uri(), AnInteger)

    def makeWC(self, body):
        return '<wildcard>%s</wildcard>' % (body, )

    def testWildcardString(self):
        xmls = '<wildcard>%s</wildcard>' % (self.CorrectString, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.AString, instance.child)

    def testWildcardInteger(self):
        xmls = '<wildcard>%s</wildcard>' % (self.CorrectInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

    def testWildcardIntegerBad(self):
        xmls = '<wildcard>%s</wildcard>' % (self.BadInteger, )
        self.assertRaises(pyxb.BadTypeValueError, CreateFromDocument, xmls)

    def testWildcardUntyped(self):
        xmls = '<wildcard>%s</wildcard>' % (self.UntypedIntegerElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

    def testWildcardTyped(self):
        xmls = '<wildcard>%s</wildcard>' % (self.TypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))

    def testBase(self):
        xmls = self.makeWC(self.BaseString)
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BaseInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

    def testStrict(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadDocumentError, CreateFromDOM, dom)

    def testLax(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_lax)
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, String))
        self.assertEqual('%d' % (self.AnInteger, ), instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Foreign))
        self.assertEqual(self.NotAnInteger, instance.child)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

    def testSkip(self):
        self.assertEqual(XSI.ProcessTypeAttribute(), XSI.PT_strict)
        XSI.ProcessTypeAttribute(XSI.PT_skip)

        # skip uses element name to force to integer, which content doesn't match
        xmls = '<wildcard>%s</wildcard>' % (self.ConflictString, )
        self.assertRaises(pyxb.BadTypeValueError, CreateFromDocument, xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.BadTypeValueError, CreateFromDOM, dom)

        xmls = '<wildcard>%s</wildcard>' % (self.ConflictStringInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedElement, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertFalse(
            isinstance(instance, pyxb.binding.basis._TypeBinding_mixin))

        xmls = '<wildcard>%s</wildcard>' % (self.BogusTypedInteger, )
        wc = CreateFromDocument(xmls)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        wc = CreateFromDOM(dom)
        instance = wc.wildcardElements()[0]
        self.assertTrue(isinstance(instance, Integer))
        self.assertTrue(isinstance(instance.child, long))
        self.assertEqual(self.AnInteger, instance.child)

        xmls = self.makeWC(self.BaseUntyped)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseString)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseInteger)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseForeign)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)

        xmls = self.makeWC(self.BaseUnknown)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDocument,
                          xmls)
        dom = pyxb.utils.domutils.StringToDOM(xmls)
        self.assertRaises(pyxb.AbstractInstantiationError, CreateFromDOM, dom)