示例#1
0
    def fromString(self, data):
        Structure.fromString(self, data)
        # [MS-NLMP] page 27
        # Payload data can be present in any order within the Payload field,
        # with variable-length padding before or after the data

        domain_offset = self['domain_offset']
        domain_end = self['domain_len'] + domain_offset
        self['domain_name'] = data[domain_offset:domain_end]

        host_offset = self['host_offset']
        host_end = self['host_len'] + host_offset
        self['host_name'] = data[host_offset:host_end]

        user_offset = self['user_offset']
        user_end = self['user_len'] + user_offset
        self['user_name'] = data[user_offset:user_end]

        ntlm_offset = self['ntlm_offset']
        ntlm_end = self['ntlm_len'] + ntlm_offset
        self['ntlm'] = data[ntlm_offset:ntlm_end]

        lanman_offset = self['lanman_offset']
        lanman_end = self['lanman_len'] + lanman_offset
        self['lanman'] = data[lanman_offset:lanman_end]
    def conventional(self, symprec=1e-5):
        """
        conventional structure
        
        Arugments:
            symprec (symmetry tolerance): distance tolerance in Cartesian coordinates to find crystal symmetry 
        """
        cell = self.structure.formatting('cell')
        cell = (cell['lattice'], cell['positions'], cell['numbers'])

        # method 1
        lattice, scaled_positions, numbers = spglib.standardize_cell(
            cell, symprec=symprec)
        newcell = (lattice, scaled_positions, numbers)
        # method 2
        #newcell=spglib.standardize_cell(cell, symprec=symprec)

        if newcell == None:
            raise StructureFactoryError('The search is failed')
        else:
            poscar = self.__toPOSCAR(newcell)

            self.structure.pop()  # delete old object
            self.structure = Structure().append(poscar)
            return self.structure
示例#3
0
    def make_movie(self, filename, filepath=None):
        if 'structures' in self:
            from structure import Structure
            if filepath == None:
                filepath = os.path.join(self.abspath, filename)
            else:
                filepath = os.path.join(filepath, filename)
            #end if
            movie = ''
            structures = self.structures

            print structures

            aA = convert(self.input.system['celldm(1)'], 'B', 'A')
            cell = self.input.cell_parameters.vectors
            for i in range(len(structures)):
                s = structures[i]
                struct = Structure(elem=s.atoms,
                                   pos=s.positions,
                                   axes=cell,
                                   scale=aA,
                                   units='A')
                struct = struct.tile(2, 2, 2)
                ss = struct.write_xyz()
                movie += ss
                open(filepath, 'w').write(movie)
示例#4
0
def load_diffs():
    date_2011 = datetime.datetime(year=2011, month=1, day=1)
    date_2012 = datetime.datetime(year=2012, month=1, day=1)
    diff_2012 = [
        Diff(DiffType.ADD, position=4, add='u'),
        Diff(DiffType.REMOVE, position=4, remove=1)
    ]
    title_12_subsections = [
        Structure(1, Representation.ROMAN, 'Part'),
        Structure(3, Representation.ROMAN, 'Part')
    ]
    title_12_subsections[0].subsections = [
        Structure(1, Representation.UPPERCASE, 'Section'),
        Structure(2, Representation.UPPERCASE, 'Section')
    ]
    title_12 = Structure(12,
                         Representation.NUMERIC,
                         'Title',
                         dates=[date_2011, date_2012],
                         texts={
                             date_2011: 'Hello',
                             date_2012: 'Hellu'
                         },
                         diffs={
                             date_2011: [],
                             date_2012: diff_2012
                         },
                         subsections=title_12_subsections)
    return {12: title_12}
示例#5
0
    def read_xsf(self, filepath, component=None):
        component = self.process_component_name(component)

        vlog('Reading density data from XSF file for component "{}"'.format(
            component),
             time=True)

        if isinstance(filepath, XsfFile):
            vlog('XSF file already loaded, reusing data.')
            xsf = filepath
            copy_values = True
        else:
            vlog('Loading data from file', n=1, time=True)
            vlog('file location: {}'.format(filepath), n=2)
            vlog('memory before: ', n=2, mem=True)
            xsf = XsfFile(filepath)
            vlog('load complete', n=2, time=True)
            vlog('memory after: ', n=2, mem=True)
            copy_values = False
        #end if

        # read structure
        if not self.has_attribute('structure'):
            vlog('Reading structure from XSF data', n=1, time=True)
            s = Structure()
            s.read_xsf(xsf)
            self.set_attribute('structure', s)
        #end if

        # read grid
        if not self.has_attribute('grid'):
            vlog('Reading grid from XSF data', n=1, time=True)
            g = read_grid(xsf)
            self.set_attribute('grid', g)
            self.set_attribute('distance_units', 'B')
        #end if

        # read values
        xsf.remove_ghost()
        d = xsf.get_density()
        values = d.values_noghost.ravel()
        if copy_values:
            values = values.copy()
        #end if

        # create grid function for component
        vlog('Constructing grid function from XSF data', n=1, time=True)
        f = grid_function(
            type='parallelotope',
            grid=self.grid,
            values=values,
            copy=False,
        )

        self.set_attribute(component, f)
        self.set_attribute('distance_units', 'A')

        vlog('Read complete', n=1, time=True)
        vlog('Current memory:', n=1, mem=True)
示例#6
0
    def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency):
        self.pseudized = False

        if structure is None:
            self.structure = Structure()
        else:
            self.structure = structure
        #end if
        if particles is None:
            self.particles = Particles()
        else:
            self.particles = particles.copy()
        #end if

        self.folded_system = None
        if self.structure.has_folded():
            if self.structure.is_tiled():
                vratio = structure.volume()/structure.folded_structure.volume()
                ncells = int(round(vratio))
                if abs(vratio-ncells)>1e-4:
                    self.error('volume of system does not divide evenly into folded system')
                #end if
                if net_charge%ncells!=0:
                    self.error('net charge of system does not divide evenly into folded system')
                #end if
                if isinstance(net_spin,str):
                    net_spin_fold = net_spin
                elif net_spin%ncells!=0:
                    self.error('net_spin of system does not divide evenly into folded system')
                else:
                    net_spin_fold = net_spin/ncells 
                #end if
                net_charge_fold = net_charge/ncells
            elif not self.structure.has_axes(): # folded molecule
                # net charge/spin are not physically meaningful
                # for a point group folded molecule
                # set them to safe values; they will not be used later
                net_charge_fold = 0
                net_spin_fold   = 'low'
            else:
                self.error('folded structure is not correctly integrated with full structure\nfolded physical system cannot be constructed')
            #end if
                
            self.folded_system = PhysicalSystem(
                structure  = structure.folded_structure,
                net_charge = net_charge_fold,
                net_spin   = net_spin_fold,
                particles  = particles,
                **valency
                )
        #end if

        self.valency_in = obj(**valency)
        self.net_charge_in = net_charge
        self.net_spin_in   = net_spin

        self.update_particles(clear=False)

        self.check_folded_system()
示例#7
0
 def __init__(self, data=None, alignment=0):
     self.__ctx_items = []
     Structure.__init__(self, data, alignment)
     if data is None:
         self['Pad'] = ''
         self['ctx_items'] = ''
         self['sec_trailer'] = ''
         self['auth_data'] = ''
示例#8
0
 def __init__(self, data = None, alignment = 0):
     self.__ctx_items = []
     Structure.__init__(self,data,alignment)
     if data is None:
         self['Pad'] = ''
         self['ctx_items'] = ''
         self['sec_trailer'] = ''
         self['auth_data'] = ''
示例#9
0
文件: dcerpc.py 项目: zod331/py-kms-1
 def fromString(self, data):
     Structure.fromString(self,data)
     # Parse the ctx_items
     data = self['ctx_items']
     for i in range(self['ctx_num']):
         item = CtxItemResult(data)
         self.__ctx_items.append(item)
         data = data[len(item):]
示例#10
0
 def _get_fk_data(self, table_name, fk, fk_value):
     reltable = Structure.get_fk_referenced_table(table_name, fk)
     reltable_pk = Structure.get_primary_key(reltable)
     if not self._cache.relation_exists(table_name, reltable):
         sql = SQLBuilder(reltable)
         sql.add_where_literal(Column(reltable, reltable_pk).in_(self._cache.get_all_keys(table_name, fk)))
         data = Query().execute_and_fetch(**sql.build_select())
         self._cache.save_relation(table_name, reltable, data)
     return Row(self._cache.get_relation_row(reltable, reltable_pk, fk_value), reltable, self)
示例#11
0
文件: dcerpc.py 项目: zod331/py-kms-1
 def __init__(self, data = None, alignment = 0):
     Structure.__init__(self, data, alignment)
     if data is None:
         self['max_tfrag'] = 4280
         self['max_rfrag'] = 4280
         self['assoc_group'] = 0
         self['ctx_num'] = 1
         self['ctx_items'] = ''
     self.__ctx_items = []
示例#12
0
 def getBooStructure(self):
     natoms=sum(self.atoms)
     strcVecs=Structure(self.elements,self.atoms,self.positions,self.lattice).getStructure()
     keys=strcVecs.keys()
     booStructure={}
     for key in keys:
         vecs=strcVecs[key]
         boos=np.array([])
         for vec in vecs:
             boos=np.append(boos,Boo(vec).getBoo())
         booStructure[key]=boos
     return booStructure
示例#13
0
    def __init__(self,
                 structure=None,
                 net_charge=0,
                 net_spin=0,
                 particles=None,
                 **valency):
        self.pseudized = False

        if structure is None:
            self.structure = Structure()
        else:
            self.structure = structure
        #end if
        if particles is None:
            self.particles = Particles()
        else:
            self.particles = particles.copy()
        #end if

        self.folded_system = None
        if self.structure.folded_structure != None:
            vratio = structure.volume() / structure.folded_structure.volume()
            ncells = int(round(vratio))
            if abs(vratio - ncells) > 1e-4:
                self.error(
                    'volume of system does not divide evenly into folded system'
                )
            #end if
            if net_charge % ncells != 0:
                self.error(
                    'net charge of system does not divide evenly into folded system'
                )
            #end if
            if net_spin % ncells != 0:
                self.error(
                    'net_spin of system does not divide evenly into folded system'
                )
            #end if
            self.folded_system = PhysicalSystem(
                structure=structure.folded_structure,
                net_charge=net_charge / ncells,
                net_spin=net_spin / ncells,
                particles=particles,
                **valency)
        #end if

        self.valency_in = obj(**valency)
        self.net_charge_in = net_charge
        self.net_spin_in = net_spin

        self.update_particles(clear=False)

        self.check_folded_system()
示例#14
0
def load_us_code(src):
    ## Invariant: for the meta-structure, order = title #.
    file_ = open(src, 'r')
    file_.close()
    mar1 = datetime.date(2018, 3, 1)
    structure = Structure('Title', '18', 18,
                          subsections=[Structure('Section', '2', 2,
                                                 dates=[mar1],
                                                 texts={mar1:''},
                                                 diffs={mar1:[]})])
    structure = Structure('US Code', '0', 0, subsections=[structure])
    return structure
示例#15
0
 def __init__(self, url):
     '''Construct a new Firebase reference from a full Firebase URL.'''
     self.connection = Connection(url, self)
     self.base_url = url
     self.structure = Structure(self)
     self.subscriptions = {}
     self.history = []
     self.connection.daemon = True
     self.connection.start()
     self._keep_alive()
     atexit.register(self.close)
     DataRef.__init__(self, self, '')
示例#16
0
文件: dcerpc.py 项目: zod331/py-kms-1
 def __init__(self, data = None, alignment = 0):
     Structure.__init__(self,data, alignment)
     if data is None:
         self['ver_major'] = 5
         self['ver_minor'] = 0
         self['flags'] = MSRPC_FIRSTFRAG | MSRPC_LASTFRAG 
         self['type'] = MSRPC_REQUEST
         self.__frag_len_set = 0
         self['auth_len'] = 0
         self['pduData'] = ''
         self['auth_data'] = ''
         self['sec_trailer'] = ''
         self['pad'] = ''
示例#17
0
文件: vasp.py 项目: zenandrea/qmcpack
 def get_result(self, result_name, sim):
     result = obj()
     input = self.input
     if result_name == 'structure':
         # get structure from CONTCAR
         ccfile = os.path.join(self.locdir, self.identifier + '.CONTCAR')
         if not os.path.exists(ccfile):
             self.error(
                 'CONTCAR file does not exist for relax simulation at ' +
                 self.locdir)
         #end if
         contcar = Poscar(ccfile)
         structure = Structure()
         if contcar.elem is not None:
             structure.read_poscar(ccfile)
         else:
             elem, elem_count = self.system.structure.order_by_species()
             structure.read_poscar(ccfile, elem=elem)
         #end if
         if input.poscar.dynamic is not None:
             structure.freeze(input.poscar.dynamic, negate=True)
         #end if
         result.structure = structure
     else:
         self.error('ability to get result ' + result_name +
                    ' has not been implemented')
     #end if
     return result
示例#18
0
 def __init__(self):
     Structure.__init__(self)
     self['flags'] = (
         NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_KEY_EXCH |
         # NTLMSSP_LM_KEY      |
         NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_UNICODE |
         # NTLMSSP_ALWAYS_SIGN |
         NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_SEAL |
         # NTLMSSP_TARGET      |
         0)
     self['host_name'] = ''
     self['domain_name'] = ''
     self['os_version'] = ''
     self._workstation = ''
示例#19
0
    def __init__(self, version, revision, pageSize=8192, data=None):
        if (version < 0x620) or (version == 0x620 and revision < 0x0b):
            # For sure the old format
            self.structure = self.structure_2003_SP0 + self.common
        elif version == 0x620 and revision < 0x11:
            # Exchange 2003 SP1 and Windows Vista and later
            self.structure = self.structure_0x620_0x0b + self.common
        else:
            # Windows 7 and later
            self.structure = self.structure_win7 + self.common
            if pageSize > 8192:
                self.structure += self.extended_win7

        Structure.__init__(self, data)
    def supercell(self, dim):
        """
        create supercell
        
        Arguments:
            dim: size of supercell. i.e. [2,2,2]
            
        Return:
            object of supercell structure.
        """
        # step:
        # 1. move atom
        # 2. change the lattice vector
        # 3. call formatting
        # 4. pop old
        # 5. append new

        # check dim
        if (sum(1 for x in dim if x <= 0) >= 1) or (sum(
                1 for x in dim if not isinstance(x, int)) >= 1):
            raise StructureFactoryError('invalid dim')

        atom_set = self.structure.atoms.all()
        for i in xrange(0, dim[0]):  # x
            for j in xrange(0, dim[1]):  # y
                for k in xrange(0, dim[2]):  # z
                    for atom in atom_set:
                        Atom.objects.create(structure=self.structure,
                                            element=atom.element,
                                            x=(atom.x + i) / dim[0],
                                            y=(atom.y + j) / dim[1],
                                            z=(atom.z + k) / dim[2])
        # delete old atoms
        for atom in atom_set:
            atom.delete()

        lattice = self.structure.lattice
        for i in xrange(0, lattice.shape[0]):
            lattice[i] = lattice[i] * dim[i]

        self.structure.lattice = lattice
        self.structure.save()

        #poscar=self.structure.formatting(isConstraint=constrained)
        poscar = self.structure.formatting(isConstraint=False)
        self.structure.pop()  # delete old object
        self.structure = Structure().append(poscar)

        return self.structure
示例#21
0
文件: table.py 项目: davekr/pypg
 def select(self, *args):
     if args:
         map(lambda arg: self._check_is_instance(arg, 'Column'), args)
         args = list(args)
         args.append(Column(self._table_name, Structure.get_primary_key(self._table_name)))
         self._sql.add_select_args(args)
     return self._table_selected_instance()
示例#22
0
    def update(self, event):
        new = False
        if 'structures' in event:
            #  print('structure update')
            for id_, value in event['structures'].iteritems():
                #  print('%s -- %s' % (id_, value))
                if id_ in self._structures:
                    self._structures[id_].update(value)
                else:
                    new = True
                    self._structures.update(
                        {id_: Structure(id_, data=value, nestapi=self)})

        if 'devices' in event:
            #             print(event)
            for cat, device in event['devices'].iteritems():
                for k, v in device.iteritems():
                    k = bytes(k)
                    if k in self._devices:
                        self._devices[k].update(v)
                    else:
                        new = True
                        self._devices.update({k: get_device(cat)(
                            k, data=v, nestapi=self)})
        if new:
            self.upnp_devices = self._structures.values() +\
                self._devices.values()
            if self.parent:
                self.parent.update()
示例#23
0
def sizeof(form):
    try:
        from data import structures
    except:
        from .data import structures
    try:
        from structure import Structure
    except:
        from .structure import Structure

    if isinstance(form, str):
        form = getattr(structures, form)

    h = Structure(form)

    return h.sizeof()
示例#24
0
def read_atom_structure(path,
                        atoms_prim,
                        atom_basis,
                        defect_atom_index=None,
                        axis=None,
                        center=None):
    """ read atomic structure of super lattice
        Returns
        Args
    """
    if center is None:
        center = np.array([0, 0, 0])
    # axis = axis or 2
    # span = [1, 1, 0]
    # span[axis] = 0
    tol = 0.11
    struct = Structure.read_contcar(path, 'POSCAR')
    dist_mat_vec = struct.frac_dist_mat(span=[1, 1, 0], offset=center)

    if defect_atom_index is not None:
        # we deal with only one element!
        # First element! O.N.L.Y.!
        # TODO(Sunghyun Kim): treat n elements.
        indice = range(struct.get_n_elements()[0])
        for ind in defect_atom_index:
            indice.remove(ind)
        indice = np.array(indice)
        dist_mat_vec = dist_mat_vec[indice, :, :]
        dist_mat_vec = dist_mat_vec[:, indice, :]
        n_Si = len(indice)
    else:
        n_Si = struct.get_n_elements()[0]
        dist_mat_vec = dist_mat_vec[:n_Si, :n_Si]

    atoms_prim_new = []

    for i in atoms_prim:
        atoms_prim_new.append(i - np.sum(np.array(defect_atom_index) < i))

    atoms_prim = atoms_prim_new
    atom_basis_new = []
    for i in atom_basis:
        atom_basis_new.append(i - np.sum(np.array(defect_atom_index) < i))
    atom_basis = atom_basis_new

    lat_mat_sc = struct.lattice.get_matrix()
    lat_mat_prim = get_lat_mat_prim(lat_mat_sc, dist_mat_vec, atoms_prim)
    lat_mat_prim = np.dot(lat_mat_prim, lat_mat_sc)

    overlap = get_overlap(dist_mat_vec, atom_basis, lat_mat_sc, lat_mat_prim,
                          tol)

    proj_coord = get_proj_coord(dist_mat_vec, lat_mat_sc, lat_mat_prim)

    lat_mat_sc_refine = get_lat_mat_sc_refine(lat_mat_sc, lat_mat_prim)

    layer_ind = get_layer_index(struct, proj_coord, overlap, indice,
                                lat_mat_prim, axis, atom_basis)

    return lat_mat_sc, lat_mat_prim, lat_mat_sc_refine, proj_coord, dist_mat_vec, overlap, indice, layer_ind
示例#25
0
    def getBooStructure(self):

        bondsDics = Structure(self.lattice, self.positions, self.elements,
                              self.atoms).getStructure()

        keys = bondsDics.keys()
        #the keys are the atoms.
        booStructure = {}
        for key in keys:
            vecs = bondsDics[key]
            boos = []

            for vec in vecs:
                boos.append(Boo(vec).getBoo())
            booStructure[key] = np.array(boos)
        return booStructure
示例#26
0
def groFrameIterator(stream):
    """Read a GRO file stream frame by frame"""

    if type(stream) == str:
        if stream.endswith(".gz"):
            stream = gzip.open(stream)
        else:
            stream = open(stream)

    # For efficiency, we need to know the precision
    precision = None

    while True:
        try:
            title = stream.next()
        except StopIteration:
            break

        natoms = stream.next().strip()
        if not natoms:
            break

        natoms = int(natoms)

        if not precision:
            a = stream.next()
            prec = len(a.split(".")[-2]) + 1
            atoms = [groAtom(a, precision)]
            atoms.extend(
                [groAtom(stream.next(), precision) for i in range(natoms - 1)])
        else:
            atoms = [groAtom(stream.next(), precision) for i in range(natoms)]
        box = groBoxRead(stream.next())

        yield Structure(title=title, atoms=atoms, box=box)
示例#27
0
    def fromString(self, data):
        Structure.fromString(self, data)

        domain_offset = self['domain_offset']
        domain_end = self['domain_len'] + domain_offset
        self['domain_name'] = data[domain_offset:domain_end]

        host_offset = self['host_offset']
        host_end = self['host_len'] + host_offset
        self['host_name'] = data[host_offset:host_end]

        hasOsInfo = self['flags'] & NTLMSSP_NEGOTIATE_VERSION
        if len(data) >= 36 and hasOsInfo:
            self['os_version'] = data[32:40]
        else:
            self['os_version'] = ''
示例#28
0
文件: row.py 项目: davekr/pypg
 def _get_rel_data_restricted(self, sql):
     attr, pk = self._restricted_table_attr, self._get_pk()
     relation_fk = Structure.get_foreign_key_for_table(attr, self._table_name)
     sql.add_where_condition(relation_fk, self.data[pk])
     data = Query().execute_and_fetch(**sql.build_select())
     from resultset import ResultSet
     return ResultSet(data, attr)
示例#29
0
 def _get_rel_data_restricted(self, sql):
     saved = self._restricted_table_call
     table_name, relation, pk, pk_value = saved['table_name'], saved['relation'], saved['pk'], saved['pk_value']
     relation_fk = Structure.get_foreign_key_for_table(relation, table_name)
     if not self._cache.relation_exists(table_name, relation):
         if sql._select_args:
             sql.add_select_arg(Column(relation, relation_fk))
         if sql._limit:
             union_sql = []
             union_parameters = []
             union_dict = {}
             for id in self._cache.get_all_keys(table_name, pk):
                 limiting_sql = copy.deepcopy(sql)
                 limiting_sql.add_where_literal(Column(relation, relation_fk) == id)
                 union_dict = limiting_sql.build_select() 
                 union_sql.append('(%s)' % union_dict['sql'])
                 union_parameters.extend(union_dict['parameters'])
             union_dict['sql'], union_dict['parameters'] = ' UNION '.join(union_sql), union_parameters
             data = Query().execute_and_fetch(**union_dict)
             self._cache.save_relation(table_name, relation, data)
         else:
             sql.add_where_literal(Column(relation, relation_fk).in_(self._cache.get_all_keys(table_name, pk)))
             data = Query().execute_and_fetch(**sql.build_select())
             self._cache.save_relation(table_name, relation, data)
     return ResultSet(self._cache.get_relation_set(relation, relation_fk, pk_value), relation, self._cache)
示例#30
0
def domain_defect(complex, strand_num, domain_num, structure):
    """ Calculates the defect of the binding of the given domain in
  the complex against the domain's expected binding given in the
  specified target structure. If the domain is not completely bound
  or unbound, the results are not very meaningful.
  The defect is calculated as a fraction of the domain's length."""
    from structure import Structure

    strandlist = [lst[:] for lst in structure.to_strandlist()]
    strands = structure.strands

    domain_start = sum(
        [d.length for d in strands[strand_num].domains[:domain_num]])
    domain_end = domain_start + strands[strand_num].domains[domain_num].length
    n = 0
    for strand_n, strand_struct in enumerate(strandlist):
        for i, bound in enumerate(strand_struct):
            is_domain = (strand_n == strand_num
                         and domain_start <= i < domain_end)
            is_bound_to_domain = (bound != None and bound != '?'
                                  and bound[0] == strand_num
                                  and domain_start <= bound[1] < domain_end)
            if not is_domain and not is_bound_to_domain:
                strandlist[strand_n][i] = '?'
            else:
                n += 1

    new_struct = Structure(structure=strandlist, strands=structure.strands)
    return defect(complex, new_struct) / float(n)
示例#31
0
文件: row.py 项目: davekr/pypg
 def _get_pk(self):
     pk = Structure.get_primary_key(self._table_name)
     if self.data.get(pk) is not None:
         return pk
     else:
         raise PyPgException(
             'Incorectly formated naming for primary key. Please provide manager.Naming instance or set strict mode on.'
         )
示例#32
0
文件: row.py 项目: davekr/pypg
 def _get_rel_data_restricted(self, sql):
     attr, pk = self._restricted_table_attr, self._get_pk()
     relation_fk = Structure.get_foreign_key_for_table(
         attr, self._table_name)
     sql.add_where_condition(relation_fk, self.data[pk])
     data = Query().execute_and_fetch(**sql.build_select())
     from resultset import ResultSet
     return ResultSet(data, attr)
示例#33
0
文件: table.py 项目: davekr/pypg
 def select(self, *args):
     if args:
         map(lambda arg: self._check_is_instance(arg, 'Column'), args)
         args = list(args)
         args.append(
             Column(self._table_name,
                    Structure.get_primary_key(self._table_name)))
         self._sql.add_select_args(args)
     return self._table_selected_instance()
示例#34
0
    def make_movie(self,filename,filepath=None):
        if 'structures' in self:
            from structure import Structure
            if filepath==None:
                filepath = os.path.join(self.abspath,filename)
            else:
                filepath = os.path.join(filepath,filename)
            #end if
            movie = ''
            structures = self.structures

            aA = convert(self.input.system['celldm(1)'],'B','A')
            cell = self.input.cell_parameters.vectors
            for i in range(len(structures)):
                s = structures[i]
                struct = Structure(elem=s.atoms,pos=s.positions,axes=cell,scale=aA,units='A')
                struct=struct.tile(2,2,2)
                ss=struct.write_xyz()
                movie += ss
                open(filepath,'w').write(movie)
示例#35
0
 def __init__(self, url):
     """Construct a new Firebase reference from a full Firebase URL."""
     self.connection = Connection(url, self)
     self.base_url = url
     self.structure = Structure(self)
     self.subscriptions = {}
     self.history = []
     self.connection.daemon = True
     self.connection.start()
     atexit.register(self.close)
     DataRef.__init__(self, self, "")
示例#36
0
 def __init__(self, url, ssl_options=None):
     '''Construct a new Firebase reference from a full Firebase URL.'''
     self.connection = Connection(url, self, ssl_options=ssl_options)
     self.base_url = url
     self.structure = Structure(self)
     self.subscriptions = {}
     self.history = []
     self.connection.daemon = True
     self.connection.start()
     self._keep_alive()
     atexit.register(self.close)
     DataRef.__init__(self, self, '')
示例#37
0
文件: row.py 项目: davekr/pypg
 def __getattr__(self, attr):
     self._check_deleted()
     if Structure.is_foreign_key(self._table_name, attr):
         if self._result_set:
             return self._result_set._get_fk_data(self._table_name, attr, self.data[attr])
         else:
             reltable = Structure.get_fk_referenced_table(self._table_name, attr)
             reltable_pk = Structure.get_primary_key(reltable)
             sql = SQLBuilder(reltable)
             sql.add_where_condition(reltable_pk, self.data[attr])
             data = Query().execute_and_fetch(**sql.build_select())
             return Row(data[0], reltable)
     else:
         self._check_relation_exists(attr)
         pk = self._get_pk()
         if self._result_set:
             return self._result_set._get_rel_data(self._table_name, attr, pk, self.data[pk])
         else:
             self._restricted_table_attr = attr
             from restricted import RestrictedTableSelect
             return RestrictedTableSelect(attr, SQLBuilder(attr), self)
示例#38
0
    def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency):

        self.pseudized = False

        if structure is None:
            self.structure = Structure()
        else:
            self.structure = structure
        #end if
        if particles is None:
            self.particles = Particles()
        else:
            self.particles = particles.copy()
        #end if

        self.folded_system = None
        if self.structure.folded_structure!=None:
            vratio = structure.volume()/structure.folded_structure.volume()
            ncells = int(round(vratio))
            if abs(vratio-ncells)>1e-4:
                self.error('volume of system does not divide evenly into folded system')
            #end if
            if net_charge%ncells!=0:
                self.error('net charge of system does not divide evenly into folded system')
            #end if
            if net_spin%ncells!=0:
                self.error('net_spin of system does not divide evenly into folded system')
            #end if
            self.folded_system = PhysicalSystem(
                structure  = structure.folded_structure,
                net_charge = net_charge/ncells,
                net_spin   = net_spin/ncells,
                particles  = particles,
                **valency
                )
        #end if

        self.valency_in = obj(**valency)
        self.net_charge_in = net_charge
        self.net_spin_in   = net_spin

        self.update_particles(clear=False)

        self.check_folded_system()
示例#39
0
    def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency):

        self.structure = structure
        self.particles = particles
        if structure==None:
            self.structure = Structure()
        #end if
        if particles==None:
            self.particles = Particles()
        #end if

        self.folded_system = None
        if self.structure.folded_structure!=None:
            self.folded_system = PhysicalSystem(
                structure  = structure.folded_structure,
                net_charge = net_charge,
                net_spin   = net_spin,
                particles  = particles,
                **valency
                )
        #end if

        #add ions
        pc = dict()
        elem = list(self.structure.elem)
        for ion in set(elem):
            pc[ion] = elem.count(ion)
        #end for
        self.add_particles(**pc)

        #pseudize
        if len(valency)>0:
            self.pseudize(**valency)
        #end if

        #add electrons
        self.generate_electrons(net_charge,net_spin)

        self.check_folded_system()
示例#40
0
    def get_result(self, result_name, sim):
        result = obj()
        input = self.input
        if result_name == "structure":
            # OUTCAR structure is not as precise as CONTCAR structure
            # pa = self.load_analyzer_image()
            # elem       = input.poscar.elem
            # elem_count = input.poscar.elem_count
            # atoms = []
            # for i in range(len(elem)):
            #    atoms += elem_count[i]*[elem[i]]
            ##end for
            # structure = Structure(
            #    units = 'A',
            #    axes  = pa.lattice_vectors.copy(),
            #    elem  = atoms,
            #    pos   = pa.position.copy()
            #    )

            # get structure from CONTCAR
            ccfile = os.path.join(self.locdir, self.identifier + ".CONTCAR")
            if not os.path.exists(ccfile):
                self.error("CONTCAR file does not exist for relax simulation at " + self.locdir)
            # end if
            contcar = Poscar(ccfile)
            structure = Structure()
            if contcar.elem != None:
                structure.read_poscar(ccfile)
            else:
                elem, elem_count = self.system.structure.order_by_species()
                structure.read_poscar(ccfile, elem=elem)
            # end if
            if input.poscar.dynamic != None:
                structure.freeze(input.poscar.dynamic, negate=True)
            # end if
            result.structure = structure
        else:
            self.error("ability to get result " + result_name + " has not been implemented")
        # end if
        return result
示例#41
0
文件: table.py 项目: davekr/pypg
 def join(self, table, on=None):
     self._check_is_instance(table, 'Table')
     if on:
         self._check_is_instance(on, 'Literal')
         self._validate_on(table, on)
     else:
         Structure.tables_related(self._table_name, table._table_name)
         try:
             fk = Structure.get_foreign_key_for_table(self._table_name, table._table_name)
         except PyPgException:
             fk = Structure.get_foreign_key_for_table(table._table_name, self._table_name)
             on = Column(table._table_name, fk) == \
                     Column(self._table_name, Structure.get_primary_key(self._table_name))
         else:
             on = Column(self._table_name, fk) == \
                     Column(table._table_name, Structure.get_primary_key(table._table_name))
     self._sql.add_join(table._table_name, on)
     return self._table_select_instance()
示例#42
0
class RootDataRef(DataRef):
    '''A reference to a root of a Firbase.'''

    def __init__(self, url, ssl_options=None):
        '''Construct a new Firebase reference from a full Firebase URL.'''
        self.connection = Connection(url, self, ssl_options=ssl_options)
        self.base_url = url
        self.structure = Structure(self)
        self.subscriptions = {}
        self.history = []
        self.connection.daemon = True
        self.connection.start()
        self._keep_alive()
        atexit.register(self.close)
        DataRef.__init__(self, self, '')

    def _process(self, message):
        '''Process a single incoming message'''

        # This whole thing needs to be rewritten to use all the new information about the protocol
        debug(message)

        # If message type is data
        if message['t'] == 'd':
            data = message['d']
            # If r is in data and set to 1 then it's probably a response 
            # to something we sent like a sub request or an auth token
            if 'r' in data:
                historical_entry = self.history[data['r']-1]
                request = historical_entry['message']
                callbacks = historical_entry['callbacks']
                error = data['b']['s']
    
                if error != 'ok':
                    if error == 'permission_denied':
                        path = request['d']['b']['p']
                        print 'FIREBASE WARNING: on() or once() for %s failed: %s' % (path, error)

                    elif error == 'expired_token' or error == 'invalid_token':
                        print 'FIREBASE WARNING: auth() failed: %s' % (error)

                    else:
                        path = request['d']['b']['p']
                        print 'FIREBASE WARNING: unknown for %s failed: %s' % (path, error)

                    onCancel = callbacks.get('onCancel', None)
                    if not onCancel is None:
                        onCancel(error)

                onComplete = callbacks.get('onComplete', None)
                if not onComplete is None:
                    onComplete(error if error != 'ok' else None)

            # If t is in data then it's the response to the initial connection, maybe
            elif 't' in data:
                pass

            # If a is in data, it's a new data blob for a node, maybe
            elif 'a' in data:
                if data['a'] == 'c':
                    # This type of message is created when we lose permission to read
                    # and it requires some extra effort to implement as we call onCancel
                    # for all effected callbacks, which are any anscestors of the path
                    pass
                else:
                    b_data = data['b']
                    path = b_data['p']
                    path_data = b_data['d']
                    self._store(path, path_data)

        # If message type is... control?
        if message['t'] == 'c':
            pass

    def _store(self, path, path_data):
        '''Store a single path worth of data into the strucutre.'''
        if not path or path[0] != "/":
            path = "/%s" % path
        self.structure.store(path, path_data)

    def _send(self, message, callbacks=None):
        '''Send a single message to Firebase.'''

        historical_entry = {
            'message': message,
            'callbacks': {} if callbacks is None else callbacks
        }

        self.history.append(historical_entry)
        message['d']['r'] = len(self.history)
        self.connection.send(message)

    def _subscribe(self, path, query, callbacks=None):
        '''Subscribe to updates regarding a path'''

        isSubscribed = self._is_subscribed(path, query)

        # A subscription (listen) request takes two main arguments, p (path) and q (query). 
        if not isSubscribed:

            message = {"t":"d", "d":{"r":0, "a":"l", "b":{"p":path}}}

            if not query is None:
                message['d']['b']['q'] = query

            self._send(message, callbacks)
            self.subscriptions[path] = query
            return True
        else:
            # Should probably trigger callbacks['onComplete'] here
            return False

    def _keep_alive(self):
        '''Send a keep-alive packet to Firebase'''

        def send():
            self._send({"t":"d", "d":{"r":0}})
            Timer(60.0, send).start()

        Timer(60.0, send).start() 

    def _bind(self, path, event, callback):
        '''Bind a single callback to an event on a path'''

        event_key = '.event-'+event
        structure_path = self.structure.get(path, {})
        self.structure[path] = structure_path
        events = structure_path.get(event_key, [])
        events.append(callback)
        self.structure[path][event_key] = events

    def _is_subscribed(self, path, query):
        '''Return True if already subscribed to this path or an ancestor.'''

        return any([s.split('/')==path.split('/')[:len(s.split('/'))] for s in self.subscriptions.keys()])
示例#43
0
 def __init__(self, basename):
     Structure.__init__(self, basename)
     self.flaps = []
示例#44
0
文件: pwscf_input.py 项目: jyamu/qmc
    def return_system(self):
        ibrav = self.system.ibrav
        if ibrav!=0:
            self.error('ability to handle non-zero ibrav not yet implemented')
        #end if

        scale,axes,kaxes = self.get_common_vars('scale','axes','kaxes')

        elem = list(self.atomic_positions.atoms)
        ap = self.atomic_positions.copy()
        ap.change_specifier('bohr',self)
        pos = ap.positions

        kp = self.k_points.copy()
        kp.change_specifier('tpiba',self)
        kpoints = kp.kpoints*(2*pi)/scale

        center = axes.sum(0)/2
        structure = Structure(
            axes    = axes,
            elem    = elem,
            scale   = scale,
            pos     = pos,
            center  = center,
            kpoints = kpoints,
            units   = 'B',
            rescale = False
            )
        structure.zero_corner()
        structure.recenter()
  
        ion_charge = 0
        valency = dict()
        atoms   = list(self.atomic_positions.atoms)
        for atom in self.atomic_species.atoms:
            pseudo_file = self.atomic_species.pseudopotentials[atom]
            if self.pseudopotentials!=None and pseudo_file in self.pseudopotentials:                
                pseudopot = self.pseudopotentials[pseudo_file]
                element = pseudopot.element
                valence = int(pseudopot.Z)
                ion_charge += atoms.count(atom)*valence
                valency[atom] = valence
            else:
                self.error('file '+pseudo_file+' was not listed in Pseudopotentials object\n  please specify pseudopotentials with the settings function',trace=False)
            #end if
        #end for

        if 'nelup' in self.system:
            nup = self.system.nelup
            ndn = self.system.neldw
            net_charge = ion_charge - nup - ndn
            net_spin   = nup - ndn
        elif 'tot_magnetization' in self.system:
            net_spin = self.system.tot_magnetization
            if 'nelec' in self.system:
                net_charge = ion_charge - self.system.nelec
            else:
                net_charge = 0
            #end if
        else:
            net_spin = 0
            if 'nelec' in self.system:
                net_charge = ion_charge - self.system.nelec
            else:
                net_charge = 0
            #end if
        #end if

        system = PhysicalSystem(structure,net_charge,net_spin,**valency)

        return system
示例#45
0
文件: nmb.py 项目: 0xc0da/impacket
 def getData(self):
     addr = self['SourceIP'].split('.')
     addr = [int(x) for x in addr]
     addr = (((addr[0] << 8) + addr[1] << 8) + addr[2] << 8) + addr[3]
     self['_SourceIP'] = addr
     return Structure.getData(self)
示例#46
0
 def __init__(self, result, reason, tsUUID, tsVer):
         Structure.__init__(self)
         self['Result'] = result
         self['Reason'] = reason
         self['TransferSyntaxUUID'] = tsUUID.bytes_le
         self['TransferSyntaxVer'] = tsVer
示例#47
0
文件: row.py 项目: davekr/pypg
 def _check_relation_exists(self, relation):
     Structure.table_exists(relation) 
     Structure.tables_related(relation, self._table_name)
示例#48
0
class PhysicalSystem(Matter):

    def __init__(self,structure=None,net_charge=0,net_spin=0,particles=None,**valency):

        self.pseudized = False

        if structure is None:
            self.structure = Structure()
        else:
            self.structure = structure
        #end if
        if particles is None:
            self.particles = Particles()
        else:
            self.particles = particles.copy()
        #end if

        self.folded_system = None
        if self.structure.folded_structure!=None:
            vratio = structure.volume()/structure.folded_structure.volume()
            ncells = int(round(vratio))
            if abs(vratio-ncells)>1e-4:
                self.error('volume of system does not divide evenly into folded system')
            #end if
            if net_charge%ncells!=0:
                self.error('net charge of system does not divide evenly into folded system')
            #end if
            if net_spin%ncells!=0:
                self.error('net_spin of system does not divide evenly into folded system')
            #end if
            self.folded_system = PhysicalSystem(
                structure  = structure.folded_structure,
                net_charge = net_charge/ncells,
                net_spin   = net_spin/ncells,
                particles  = particles,
                **valency
                )
        #end if

        self.valency_in = obj(**valency)
        self.net_charge_in = net_charge
        self.net_spin_in   = net_spin

        self.update_particles(clear=False)

        self.check_folded_system()
    #end def __init__


    def update_particles(self,clear=True):
        #add ions
        pc = dict()
        elem = list(self.structure.elem)
        for ion in set(elem):
            pc[ion] = elem.count(ion)
        #end for
        missing = set(pc.keys())-set(self.particles.keys())
        if len(missing)>0 or len(elem)==0:
            if clear:
                self.particles.clear()
            #end if
            self.add_particles(**pc)

            #pseudize
            if len(self.valency_in)>0:
                self.pseudize(**self.valency_in)
            #end if

            #add electrons
            self.generate_electrons(self.net_charge_in,self.net_spin_in)
        #end if
    #end def update_particles


    def update(self):
        self.net_charge = self.structure.background_charge
        self.net_spin   = 0
        for p in self.particles:
            self.net_charge += p.count*p.charge
            self.net_spin   += p.count*p.spin
        #end for
        self.net_charge = int(round(float(self.net_charge)))
        self.net_spin   = int(round(float(self.net_spin)))
    #end def update


    def add_particles(self,**particle_counts):
        pc = self.particle_collection # all known particles
        plist = []
        for name,count in particle_counts.iteritems():
            particle = pc.get_particle(name)
            if particle is None:
                self.error('particle {0} is unknown'.format(name))
            else:
                particle = particle.copy()
            #end if
            particle.set_count(count)
            plist.append(particle)
        #end for
        self.particles.add_particles(plist)
        self.update()
    #end def add_particles


    def generate_electrons(self,net_charge=0,net_spin=0):
        nelectrons = -net_charge + self.net_charge
        if net_spin=='low':
            net_spin = nelectrons%2
        #end if
        nup   = float(nelectrons + net_spin - self.net_spin)/2
        ndown = float(nelectrons - net_spin + self.net_spin)/2        
        if abs(nup-int(nup))>1e-3:
            self.error('requested spin state {0} incompatible with {1} electrons'.format(net_spin,nelectrons))
        #end if
        nup   = int(nup)
        ndown = int(ndown)
        self.add_particles(up_electron=nup,down_electron=ndown)
    #end def generate_electrons


    def pseudize(self,**valency):
        errors = False
        for ion,valence_charge in valency.iteritems():
            if ion in self.particles:
                ionp = self.particles[ion]
                if isinstance(ionp,Ion):
                    self.particles[ion] = ionp.pseudize(valence_charge)
                    self.pseudized = True
                else:
                    self.error(ion+' cannot be pseudized',exit=False)
                #end if
            else:
                self.error(ion+' is not in the physical system',exit=False)
                errors = True
            #end if
        #end for
        if errors:
            self.error('system cannot be generated')
        #end if
        self.valency = obj(**valency)
        self.update()
    #end def pseudize

        
    def check_folded_system(self):
        sys_folded    = self.folded_system!=None
        struct_folded = self.structure.folded_structure!=None
        if sys_folded!=struct_folded:
            self.error('folding of physical system and structure is not consistent\n  system folded: {0}\n  structure folded: {1}'.format(sys_folded,struct_folded))
        #end if
        if sys_folded and id(self.structure.folded_structure)!=id(self.folded_system.structure):
            self.error('structure of folded system and folded structure are distinct\n  this is not allowed and may be a developer error')
        #end if
    #end def check_folded_system


    def change_units(self,units):
        self.structure.change_units(units,folded=False)
        if self.folded_system!=None:
            self.folded_system.change_units(units)
        #end if
    #end def change_units


    def group_atoms(self):
        self.structure.group_atoms(folded=False)
        if self.folded_system!=None:
            self.folded_system.group_atoms()
        #end if
    #end def group_atoms


    def rename(self,folded=True,**name_pairs):
        self.particles.rename(**name_pairs)
        self.structure.rename(folded=False,**name_pairs)
        if self.pseudized:
            for old,new in name_pairs.iteritems():
                if old in self.valency:
                    if new not in self.valency:
                        self.valency[new] = self.valency[old]
                    #end if
                    del self.valency[old]
                #end if
            #end for
            self.valency_in = self.valency
        #end if
        if self.folded_system!=None and folded:
            self.folded_system.rename(folded=folded,**name_pairs)
        #end if
    #end def rename


    def copy(self):
        cp = DevBase.copy(self)
        if self.folded_system!=None and self.structure.folded_structure!=None:
            del cp.folded_system.structure
            cp.folded_system.structure = cp.structure.folded_structure
        #end if
        return cp
    #end def copy


    def load(self,filepath):
        DevBase.load(self,filepath)
        if self.folded_system!=None and self.structure.folded_structure!=None:
            del self.folded_system.structure
            self.folded_system.structure = self.structure.folded_structure
        #end if
    #end def load


    def tile(self,*td,**kwargs):
        extensive = True
        net_spin  = None
        if 'extensive' in kwargs:
            extensive = kwargs['extensive']
        #end if
        if 'net_spin' in kwargs:
            net_spin = kwargs['net_spin']
        #end if
        supercell = self.structure.tile(*td)
        supercell.remove_folded()
        if extensive:
            ncells = int(round(supercell.volume()/self.structure.volume()))
            net_charge = ncells*self.net_charge
            if net_spin is None:
                net_spin   = ncells*self.net_spin
            #end if
        else:
            net_charge = self.net_charge
            if net_spin is None:
                net_spin   = self.net_spin
            #end if
        #end if
        system = self.copy()
        supersystem = PhysicalSystem(
            structure  = supercell,
            net_charge = net_charge,
            net_spin   = net_spin,
            **self.valency
            )
        supersystem.folded_system = system
        supersystem.structure.set_folded(system.structure)
        return supersystem
    #end def tile


    def remove_folded_system(self):
        self.folded_system = None
        self.structure.remove_folded_structure()
    #end def remove_folded_system


    def remove_folded(self):
        self.remove_folded_system()
    #end def remove_folded


    def get_primitive(self):
        if self.folded_system is None:
            fs = self
        else:
            fs = self.folded_system
            while fs.folded_system!=None:
                fs = fs.folded_system
            #end while
        #end if
        return fs
    #end def get_primitive


    def folded_representation(self,arg0,arg1=None):
        self.error('folded_representation needs a developers attention to make it equivalent with tile')
        if isinstance(arg0,PhysicalSystem):
            folded_system = arg0
        elif isinstance(arg0,str):
            shape = arg0
            tiling    = arg1
            if tiling is None:
                tiling = (1,1,1)
            #end if
            if not 'generation_info' in self:
                self.error('system was not formed with generate_physical_system, cannot form folded representation')
            #end if
            structure,element,scale,units,net_charge,net_spin,particles,valency = \
                self.generation_info.tuple('structure','element','scale','units', \
                                               'net_charge','net_spin','particles','valency')
            folded_system = generate_physical_system(
                structure  = structure,
                shape      = shape,
                element    = element,
                tiling     = tiling,
                scale      = scale,
                units      = units,
                net_charge = net_charge,
                net_spin   = net_spin,
                particles  = particles,
                **valency
                )
        else:
            self.error('unrecognized inputs in folded_representation')
        #end if
        tilematrix,kmap = self.structure.fold(folded_system.structure,'tilematrix','kmap')
        self.set(
            folded_system = folded_system,
            tilematrix    = tilematrix,
            kmap          = kmap
            )
        return folded_system
    #end def folded_representation


    def large_Zeff_elem(self,Zmin):
        elem = []
        for atom,Zeff in self.valency.iteritems():
            if Zeff>Zmin:
                elem.append(atom)
            #end if
        #end for
        return elem
    #end def large_Zeff_elem


    def ae_pp_species(self):
        species = set(self.structure.elem)
        if self.pseudized:
            pp_species = set(self.valency.keys())
            ae_species = species-pp_species
        else:
            pp_species = set()
            ae_species = species
        #end if
        return ae_species,pp_species
示例#49
0
 def __init__(self, data = None, alignment = 0):
     Structure.__init__(self,data,alignment)
     if data is None:
         self['SupportedVersions'] = ''
示例#50
0
def test_structure(N,lt):
    '''
    N:
        The size of the lattice.
    lt:
        The lattice type.
    '''
    lts=construct_lattice(N=N,lattice_shape=lt)
    tgroup=TranslationGroup(Rs=lts.a*lts.N[:,newaxis],per=(True,False))
    lt=Structure(lts.sites)
    #test for bonds
    lt.usegroup(tgroup)
    lt.initbonds(K=17)
    #finding neighbors
    print 'find nearest neighbor %s'%lt.b1s.N
    print 'find second nearest neighbor %s'%lt.b2s.N
    print 'find third nearest neighbor %s'%lt.b3s.N
    isite=array([1]*lt.vdim)
    #find a site at specific position.
    print 'finding - %s'%isite,lt.findsite(isite)   
    i=0
    if lts.dimension==2:
        j=lts.l2index((0,N[1]-1,0))
        print 'measureing distance between site %s and %s -> %s'%(i,j,lt.measure(i,j))
        j=lts.l2index((N[0]-1,0,0))
        print 'measureing distance between site %s and %s -> %s'%(i,j,lt.measure(i,j))
    else:
        j=lt.nsite-1
        print 'measureing distance between site %s and %s -> %s'%(i,j,lt.measure(i,))
    ion()
    lt.show_bonds()
    lt.show_sites()
    pdb.set_trace()
    cla()
    #test for save and load functionality.
    lt.save_bonds()
    lt2=Structure(lts.sites)
    lt2.load_bonds()
    lt2.show_bonds()
    lt2.show_sites()
    axis('equal')
    pdb.set_trace()
示例#51
0
文件: row.py 项目: davekr/pypg
 def _get_pk(self):
     pk= Structure.get_primary_key(self._table_name)
     if self.data.get(pk) is not None:
         return pk
     else:
         raise PyPgException('Incorectly formated naming for primary key. Please provide manager.Naming instance or set strict mode on.')
示例#52
0
 def getData(self):
     self['ctx_num'] = len(self.__ctx_items)
     for i in self.__ctx_items:
         self['ctx_items'] += i.getData()
     return Structure.getData(self)
示例#53
0
文件: pypg.py 项目: davekr/pypg
 def __getattr__(self, name):
     Structure.table_exists(name)
     return Table(name)
示例#54
0
class RootDataRef(DataRef):
    """A reference to a root of a Firbase."""

    def __init__(self, url):
        """Construct a new Firebase reference from a full Firebase URL."""
        self.connection = Connection(url, self)
        self.base_url = url
        self.structure = Structure(self)
        self.subscriptions = {}
        self.history = []
        self.connection.daemon = True
        self.connection.start()
        atexit.register(self.close)
        DataRef.__init__(self, self, "")

    def _process(self, message):
        """Process a single incoming message"""

        # This whole thing needs to be rewritten to use all the new information about the protocol
        debug(message)

        # If message type is data
        if message["t"] == "d":
            data = message["d"]
            # If r is in data and set to 1 then it's probably a response
            # to something we sent like a sub request or an auth token
            if "r" in data:
                historical_entry = self.history[data["r"] - 1]
                request = historical_entry["message"]
                callbacks = historical_entry["callbacks"]
                error = data["b"]["s"]

                if error != "ok":
                    if error == "permission_denied":
                        path = request["d"]["b"]["p"]
                        print "FIREBASE WARNING: on() or once() for %s failed: %s" % (path, error)

                    elif error == "expired_token" or error == "invalid_token":
                        print "FIREBASE WARNING: auth() failed: %s" % (error)

                    else:
                        path = request["d"]["b"]["p"]
                        print "FIREBASE WARNING: unknown for %s failed: %s" % (path, error)

                    onCancel = callbacks.get("onCancel", None)
                    if not onCancel is None:
                        onCancel(error)

                onComplete = callbacks.get("onComplete", None)
                if not onComplete is None:
                    onComplete(error if error != "ok" else None)

            # If t is in data then it's the response to the initial connection, maybe
            elif "t" in data:
                pass

            # If a is in data, it's a new data blob for a node, maybe
            elif "a" in data:
                if data["a"] == "c":
                    # This type of message is created when we lose permission to read
                    # and it requires some extra effort to implement as we call onCancel
                    # for all effected callbacks, which are any anscestors of the path
                    pass
                else:
                    b_data = data["b"]
                    path = b_data["p"]
                    path_data = b_data["d"]
                    self._store(path, path_data)

        # If message type is... control?
        if message["t"] == "c":
            pass

    def _store(self, path, path_data):
        """Store a single path worth of data into the strucutre."""
        if not path or path[0] != "/":
            path = "/%s" % path
        self.structure.store(path, path_data)

    def _send(self, message, callbacks=None):
        """Send a single message to Firebase."""

        historical_entry = {"message": message, "callbacks": {} if callbacks is None else callbacks}

        self.history.append(historical_entry)
        message["d"]["r"] = len(self.history)
        self.connection.send(message)

    def _subscribe(self, path, query, callbacks=None):
        """Subscribe to updates regarding a path"""

        isSubscribed = self._is_subscribed(path, query)

        # A subscription (listen) request takes two main arguments, p (path) and q (query).
        if not isSubscribed:

            message = {"t": "d", "d": {"r": 0, "a": "l", "b": {"p": path}}}

            if not query is None:
                message["d"]["b"]["q"] = query

            self._send(message, callbacks)
            self.subscriptions[path] = query
            return True
        else:
            # Should probably trigger callbacks['onComplete'] here
            return False

    def _bind(self, path, event, callback):
        """Bind a single callback to an event on a path"""

        event_key = ".event-" + event
        structure_path = self.structure.get(path, {})
        self.structure[path] = structure_path
        events = structure_path.get(event_key, [])
        events.append(callback)
        self.structure[path][event_key] = events

    def _is_subscribed(self, path, query):
        """Return True if already subscribed to this path or an ancestor."""

        return any([s.split("/") == path.split("/")[: len(s.split("/"))] for s in self.subscriptions.keys()])
示例#55
0
文件: pypg.py 项目: davekr/pypg
 def __dir__(self):
     attrs = ['create_mview', 'drop_view', 'set_debug', 'set_log', 'set_strict', 'set_naming', 'set_logger']
     return Structure.get_all_tables() + attrs