示例#1
0
 def load(self, filename):
     if filename == '':
         raise IOError('Empty filename. Specify a filename and try again!')
     if os.access(filename, os.F_OK):
         logging.getLogger().debug('attempting to restore state of ' +
                                   self.__str__() + ' from ' + filename +
                                   '...')
         try:
             logging.getLogger().debug('trying binary mode...')
             self.set_items(cPickle.load(open(filename, 'rb')))
         except:
             try:
                 logging.getLogger().debug('trying text mode...')
                 self.set_items(cPickle.load(open(filename, 'r')))
             except:
                 try:
                     logging.getLogger().debug(
                         'trying unicode text mode...')
                     self.set_items(cPickle.load(open(filename, 'rU')))
                 except:
                     logging.getLogger().debug(
                         'failed to restore state of ' + self.__str__() +
                         '.')
                     raise IOError('Load failed.')
         logging.getLogger().debug('state of ' + self.__str__() +
                                   ' restored.')
     else:
         raise IOError('File does not exist.')
示例#2
0
文件: test_exc.py 项目: Mu-L/pypy
    def test_exc(self):
        from exceptions import Exception, BaseException

        assert issubclass(Exception, BaseException)
        assert isinstance(Exception(), Exception)
        assert isinstance(Exception(), BaseException)
        assert repr(Exception(3, "x")) == "Exception(3, 'x')"
        assert str(IOError("foo", "bar")) == "[Errno foo] bar"
        assert isinstance(IOError("foo", "bar"), IOError)
        assert str(IOError(1, 2)) == "[Errno 1] 2"
示例#3
0
    def getColumn(self, name, context=None):

        # if val is not None and varBindHead[idx].isPrefixOf(name): in cmdgen
        # fails so I can't use the oneliner method
        oid = ''
        if self.isDotNotation(name):
            oid = name
        else:
            oid = self.mibs[name].get('oid')

        if context == None:
            context = self.context

        cmdGen = MyCmdGen()
        errorIndication, errorStatus, _, varBinds = cmdGen.getColumn(
            self.agent, context, self.host, self.port, rfc1902.ObjectName(oid))
        if errorIndication:
            raise IOError(errorIndication)
        if errorStatus:
            if len(varBinds) > 0 and errorStatus == 32:
                pass
            else:
                raise SNMPError('getcolumn on ' + oid + '(' + name + ')',
                                errorStatus)

        #if errorStatus:
        # Throw an error??

        # do we need to do error checking on this???
        # what format should this return in???
        formattedList = []
        for val in varBinds:
            formattedList.append(val[0][1].prettyPrint())

        return formattedList
示例#4
0
    def get(self, name, instance=0, context=None):
        oid = ''

        if self.isDotNotation(name):
            oid = name
        else:
            oid = self.mibs[name].get('oid')
        # 2012-04-04, pcankar: if instance is -1 leave oid unchanged
        if instance != -1:
            oid += '.' + str(instance)

        if context == None:
            context = self.context

        cmdGen = MyCmdGen()
        errorIndication, errorStatus, _, varBinds = cmdGen.getCmd(
            self.agent, context, self.host, self.port, rfc1902.ObjectName(oid))

        if errorIndication:
            raise IOError(errorIndication)
        if errorStatus:
            raise SNMPError('get on ' + oid + '(' + name + ')', errorStatus)
            # Throw an error??
        # do we need to do error checking on this???
        #print "Getting %s (%s) = %s" % (name, oid, str(varBinds[-1][1].prettyPrint()))

        return varBinds[-1][1].prettyPrint()
    def _build_attribute_table(self, path, nodata_value):
        if not os.path.exists(path):
            raise IOError("File not found: {}".format(path))

        shapefile = ogr.Open(path, 1)
        layer = shapefile.GetLayer(0)
        layer.CreateField(ogr.FieldDefn(self._id_attribute, ogr.OFTInteger))
        next_value_id = 1
        for feature in self._get_features(layer):
            attribute_values = tuple(
                attr.sub(feature[attr.name]) if attr.filter(feature[attr.name]
                                                            ) else None
                for attr in self._attributes)

            value_id = nodata_value
            if None not in attribute_values:
                existing_key = [
                    item[0] for item in self._attribute_table.items()
                    if item[1] == attribute_values
                ]
                if existing_key:
                    value_id = existing_key[0]
                else:
                    value_id = next_value_id
                    self._attribute_table[value_id] = attribute_values
                    next_value_id += 1

            feature[self._id_attribute] = value_id
            layer.SetFeature(feature)
示例#6
0
 def save(self, filename):
     """detects the format of the savefile and saves it according to the file-ending. .txt and .asc result in an ascii sav,
     .pyd in a pickled python save with mode='asc' and .pys in a pickled python file with mode='bin'"""
     if not filename:
         raise IOError('Empty filename. Specify a filename and try again!')
     with open(filename, 'wb') as fil:
         cPickle.dump(self.get_items(), fil, 1)
示例#7
0
    def getnext(self, name, instance=0, context=None):

        # if val is not None and varBindHead[idx].isPrefixOf(name): in cmdgen
        # fails so I can't use the oneliner method

        oid = ''
        if self.isDotNotation(name):
            oid = name
        else:
            oid = self.mibs[name].get('oid')
        # 2012-04-04, pcankar: if instance is -1 leave oid unchanged
        if instance != -1:
            oid += '.' + str(instance)

        if context == None:
            context = self.context

        cmdGen = MyCmdGen()
        errorIndication, errorStatus, _, varBinds = cmdGen.nextCmd(
            self.agent, context, self.host, self.port, rfc1902.ObjectName(oid))

        if errorIndication:
            raise IOError(errorIndication)
        if errorStatus:
            raise SNMPError('getnext on ' + oid + '(' + name + ')',
                            errorStatus)
        #if errorStatus:
        # Throw an error??

        # do we need to do error checking on this???
        # what format should this return in???
        return varBinds
示例#8
0
def reprojectShp(infile, reproj, fromProj="EPSG:4326", toProj="EPSG:26905"):
    """Reprojects a shapefile using ogr2ogr
    
    Args:
        | infile (str): File to reproject
        | reproj (str): Reprojected output file
    
    Kwargs:
        | fromProj (str): Original projection
        | toProj (str): New projection
    """

    cmd = "ogr2ogr -overwrite -s_srs %s -t_srs %s %s %s;" % (fromProj, toProj,
                                                             reproj, infile)

    process = subprocess.Popen(cmd,
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    dumpstr = process.stdout.readlines()
    errorstr = process.stderr.readlines()

    #print len(dumpstr)
    #print len(errorstr)
    #
    #print "".join(dumpstr)
    #print "".join(errorstr)

    if len(errorstr) > 0:
        raise IOError("".join(errorstr))
示例#9
0
    def checkFolders(self, src, dst):
        """
        Check if the source and dest folders are valid and if it is not the case,
        throws an exception
        @param srcr: the folder with java sources to check
        @param dst: destination folder to check
        """
        # Check if src folder exists
        if not os.path.exists(src):
            raise IOError("The source folder", src, "does not exist")
        elif not os.path.isdir(src):
            raise IOError("The source folder", src, "is not a directory")

        # Check if the destination folder exists
        if not os.path.exists(dst):
            raise IOError("The destination folder", dst, "does not exist")
        elif not os.path.isdir(dst):
            raise IOError("The destination folder", dst, "is not a directory")
示例#10
0
    def __init__(self, path, attributes=None, attribute_table=None, nodata_value=-1, data_type=None):
        self._name = os.path.basename(path)
        self._path = path
        self._attributes = attributes or []
        self._attribute_table = attribute_table or {}
        self._nodata_value = nodata_value
        self._data_type = data_type

        if not os.path.exists(path):
            raise IOError("File not found: {}".format(path))
示例#11
0
    def init_path(self, path):
        """
            Checks whether the given path exists and is a directory.
            Cleans the path and saves it to the instance.

                :param path: The path in which urltomd should search for
                files.
        """
        if not os.path.isdir(path):
            raise IOError('%s does not exist or is not a directory.' % path)
        if not path.endswith('/'):
            path += '/'
        self.path = path
示例#12
0
    def getBulk(self,
                name,
                nonRepeaters,
                maxRepititions,
                context=None,
                dict=False):

        # if val is not None and varBindHead[idx].isPrefixOf(name): in cmdgen
        # fails so I can't use the oneliner method
        oid = ''
        if self.isDotNotation(name):
            oid = name
        else:
            oid = self.mibs[name].get('oid')

        if context == None:
            context = self.context


#        cmdGen = MyCmdGen()
#        errorIndication, errorStatus, _, varBinds = cmdGen.getBulk(
#        self.agent, context,
#        self.host, self.port,
#        nonRepeaters,
#        maxRepititions,
#        rfc1902.ObjectName(oid))

        errorIndication, errorStatus, _, varBinds = cg.CommandGenerator(
        ).bulkCmd(cg.CommunityData(self.agent, context),
                  cg.UdpTransportTarget((self.host, self.port)), nonRepeaters,
                  maxRepititions, rfc1902.ObjectName(oid))

        if errorIndication:
            raise IOError(errorIndication)
        if errorStatus:
            if len(varBinds) > 0 and errorStatus == 32:
                pass
            else:
                raise SNMPError('getbulk on ' + oid + '(' + name + ')',
                                errorStatus)

        formattedList = []
        for val in varBinds:
            formattedList.append(val[0][1].prettyPrint())
        formattedDict = {}
        for val in varBinds:
            formattedDict[val[0][0].prettyPrint()] = val[0][1].prettyPrint()
        if dict:
            return formattedDict
        return formattedList
示例#13
0
 def __init__(self, env=None, file=None):
     """
     Instantiating a SolexaConfig object causes the config
     file to be read and parsed.
     """
     cfg = self._read_config(env, file)
     if not cfg:
         raise IOError()
     for section in cfg.sections():
         section_dict = {}
         for k, v in cfg.items(section):
             section_dict[k] = v
         setattr(self, section, section_dict)
     self._coerce_values()
示例#14
0
 def save(self, filename):
     """
     Saves the plot to a graphics file, e.g. .png or .pdf.
     
     Example of usage:
     
         plot = my_instance.line_plot
         filename = 'foo.png'
         save_figure(plot, filename)
     """
     if filename:
         if os.path.splitext(filename)[-1] == ".pdf":
             self.save_pdf(filename)
         else:
             self.save_raster(filename)
     else:
         raise IOError('Empty filename.')
示例#15
0
def makeTableFromFile(ffile,name=None,nameInFile='No',\
                      columnsNames=None,columnsNamesInFile='Yes',
                      columnsUnits=None,columnsUnitsInFile='No'):
    """Makes a table from the given file. Reads table's names (if they are present) from the file.
    Line of Name in file should begin with #TITLE:
    Line of ColumnsName in file should begin with #COLUMN_TITLES: and colum names should be separate by |"""
    from exceptions import IOError
    t = None
    try:
        file = open(ffile, 'r')
    except:
        msg = "can't open file <%s>...\n" % ffile
        raise IOError(msg)
    if name and nameInFile.lower() == 'yes':
        raise Warning("You give a table name and ask to get name from file")
    if columnsNames and columnsNameInFile.lower() == 'yes':
        raise Warning(
            "You ask for colum names in the file while already defining them")
    if columnsUnits and columnsUnitsInFile.lower() == 'yes':
        raise Warning(
            "You ask for units in the file while already defining them")
    # table creation
    if name:
        verifyType(name, StringType)
        t = DataTable(name)
        pass
    else:
        t = DataTable('table')
        pass
    # affect columns names if necessary
    if columnsNames:
        t.setColumnNames(columnsNames)
        pass
    # affect columns units if necessary
    if columnsUnits:
        t.setColumnUnits(columnsUnits)
        pass
    t.readFromFile(ffile,
                   columnsNames=columnsNamesInFile,
                   name=nameInFile,
                   colonnesnames=columnsNames,
                   columnsUnits=columnsUnitsInFile)

    return t
示例#16
0
    def __init__(self,
                 name,
                 path,
                 attributes,
                 raw=False,
                 nodata_value=-1,
                 data_type=None):
        self._data_type = data_type
        self._nodata_value = nodata_value
        self._name = name
        self._path = path
        self._raw = raw
        self._id_attribute = "value_id" if not raw else attributes.name
        self._attribute_table = {}
        self._attributes = [attributes] if isinstance(attributes, Attribute) \
                                        else attributes

        if not os.path.exists(path):
            raise IOError("File not found: {}".format(path))
示例#17
0
    def __init__(self,
                 name,
                 path,
                 layer,
                 attributes,
                 raw=False,
                 nodata_value=-1):
        self._nodata_value = nodata_value
        self._name = name
        self._path = path
        self._layer = layer
        self._raw = raw
        self._id_attribute = "value_id" if not raw else attributes
        self._attribute_table = {}
        self._attributes = [attributes] if isinstance(attributes, basestring) \
                                        else attributes

        if not os.path.exists(path):
            raise IOError("File not found: {}".format(path))
示例#18
0
  def __init__(self, fileName, symbolTable):
    # Check if the requested file to parse exists
    if not os.path.exists(fileName):
      raise IOError("The file %s could not be found" % fileName)

    # initialize the commands array
    self.text = []
    self.symbolTable = symbolTable
    # open the requested file for reading
    with open(fileName, 'r') as f:
      # read each line from the file
      for line in f.readlines():
        # check if the line is valid for parsing ( Notice that here
        # all the comments  and the white spaces are removed)
        if not self.whiteSpaceOrComment(line):
          cleanLine = self.cleanEmptyLinesAndComments(line)
          self.text.insert(0,cleanLine)

    # init the current command line
    self.currentCommand = ""
    self.parsedCommand = ""
示例#19
0
 def error(self, x, a):
     # Raise one of the exceptions from db_error
     raise IOError() if PY2 else OSError
示例#20
0
 def __init__(self,errStr = ''):
     IOError.__init__(self)
     self.errStr = errStr
示例#21
0
文件: vfs.py 项目: NeatNerdPrime/bup
 def __init__(self, errno, message, terminus=None):
     py_IOError.__init__(self, errno, message)
     self.terminus = terminus
示例#22
0
def open(file, band_file=None):
    '''
    Returns a SpyFile object for an AVIRIS image file.

    Arguments:

        `file` (str):

            Name of the AVIRIS data file.

        `band_file` (str):

            Optional name of the AVIRIS spectral calibration file.

    Returns:

        A SpyFile object for the image file.

    Raises:

        IOError
    '''

    import numpy as np
    from spectral.io.bipfile import BipFile
    import os
    import glob
    from exceptions import IOError
    from .spyfile import find_file_path
    import spectral

    class Params:
        pass

    p = Params()

    p.filename = find_file_path(file)
    p.nbands = 224
    p.ncols = 614
    fileSize = os.stat(p.filename)[6]
    if fileSize % 275072 != 0:
        raise IOError('File size not consistent with AVIRIS format.')
    p.nrows = int(fileSize / 275072)
    p.byte_order = 1
    p.dtype = np.dtype('i2').str
    if spectral.byte_order != 1:
        p.dtype = np.dtype(p.dtype).newbyteorder().str
    metadata = {'default bands': ['29', '18', '8']}
    p.offset = 0

    img = BipFile(p, metadata)
    img.scale_factor = 10000.0

    if band_file:
        img.bands = read_aviris_bands(find_file_path(band_file))
    else:
        # Let user know if band cal files are available
        fileDir = os.path.split(p.filename)[0]
        calFiles = glob.glob(fileDir + '/*.spc')
        if len(calFiles) > 0:
            print('\nThe following band calibration files are located in ' \
                'the same directory as the opened AVIRIS file:\n')
            for f in calFiles:
                print("    " + os.path.split(f)[1])
            print('\nTo associate a band calibration file with an AVIRIS ' \
                  'data file, re-open the AVIRIS file with the following ' \
                  'syntax:\n')
            print('    >>> img = aviris.open(fileName, calFileName)\n')
    return img
示例#23
0
文件: tables.py 项目: apdimier/Etumos
    def readFromFile(self,ffile,nbcolumns=None,columnsNames='yes',name='no',columnsUnits='no'):
        """Reads a table from the given file."""
        from exceptions import IOError
        try:
            if self.values.shape != (0,0):
                raise "The table already contains values"
            file = open(ffile, 'r')
        except :
            msg="can't open file <%s>...\n"%ffile
            raise IOError(msg)


        fileNameColumns=[]
        fileNameUnits=[]
        fileName=None
        
        filemaxnbcol=0
        fileminnbcol=100
        isonvalues=0
        allvaluesbycolonne=[]
        nbvalueline=0
        cpt=1
        for line in file.readlines():
            separe = line.split()
            if (len(separe) == 0 ):
                # blank line
                continue
                
            
            if ( separe[0] == '#' ):
                # comment line
                cpt=cpt+1
                continue
            elif ( separe[0] == '#TITLE:' ):
                # name line
                separe = separe[1:]
                s=''
                for isep in range(len(separe)):
                    s=s+separe[isep]+' '
                    fileName=s
                    pass
            elif ( separe[0] == '#COLUMN_TITLES:' ):
                # column name line
                separe = separe[1:]
                s=''
                for isep in range(len(separe)):
                    s=s+separe[isep]
                    s=string.strip(s)
                    if ( len(s) == 0 ):
                        fileNameColumns=[]
                        continue
                    fileNameColumns = s.split('|')
                    pass
            elif ( separe[0] == '#COLUMN_UNITS:' ):
                # unit name line
                fileNameUnits = separe[1:]
                pass
            elif ( cpt == 1 ):
                # column name line
                pass
            else:
                # values line
                nbvalueline=nbvalueline+1
                linenbcol=len(separe)
                filemaxnbcol=max(linenbcol,filemaxnbcol)
                fileminnbcol=min(linenbcol,fileminnbcol)
                linevalues=[]
        
                for isep in range(linenbcol):
                    linevalues.append(float(separe[isep]))
                    pass
                # adjust nb columns if not the same on each line
                # or if the first value's line
                if ( filemaxnbcol < len(allvaluesbycolonne) ):
                    for icol in range(filemaxnbcol,len(allvaluesbycolonne)):
                        allvaluesbycolonne.append([])
                        for il in range(nbvalueline-1):
                            allvaluesbycolonne[il].append(0)
                            pass
                        pass
                    pass
                elif ( filemaxnbcol > len(allvaluesbycolonne) ):
                    for icol in range(len(allvaluesbycolonne),filemaxnbcol):
                        allvaluesbycolonne.append([])
                        for il in range(nbvalueline-1):
                            allvaluesbycolonne[icol].append(0)
                            pass
                        pass
                    pass
                # add values
                for icol in range(linenbcol):
                    allvaluesbycolonne[icol].append(linevalues[icol])
                    pass
                for icol in range(linenbcol,filemaxnbcol):
                    allvaluesbycolonne[icol].append(0)
                    pass
                
        cpt=cpt+1
    
        file.close()
        
        # check consistency beetwen arguments and file contents
#
# controlling the table parameters
#        
        if ( fileminnbcol != filemaxnbcol ):
            raise IOError("colums must have the same number of rows")
    
        if nbcolumns:
            if ( filemaxnbcol != nbcolumns ):
                raise IOError(" problem with the number of columns")
            pass
        
        # Warnings
        if ( ( columnsNames.lower() == 'no' ) and ( len(fileNameColumns) > 0 ) ):
            raise Warning(" you should specify column names")
        
        if ( ( columnsNames.lower() == 'yes' ) and ( len(fileNameColumns) == 0 ) ):
            raise Warning("you specified columnName(s) but the file doesn\'t entail column names")
        
        if ( len(fileNameColumns) < filemaxnbcol ):
            nbcol=len(fileNameColumns)
            for icol in range (nbcol,filemaxnbcol):
                fileNameColumns.append('col'+str(icol+1))
                pass
            effectivecolumnNames=fileNameColumns
            pass
            
        
        if ( ( name.lower() == 'no' ) and fileName ):
            msg='WARNING: you specified no name but there is name in file'
            print(msg)
        
        if ( ( name.lower() == 'yes' ) and ( fileName == None ) ):
            msg='WARNING: you specified name but there is no name in file'
            print(msg)
        
        if ( ( columnsUnits.lower() == 'no' ) and ( len(fileNameUnits) > 0 ) ):
            msg='WARNING: you specified no units name but there are units name in file'
            print(msg)
    
        if ( ( columnsUnits.lower() == 'yes' ) and ( len(fileNameUnits) == 0 ) ):
            msg='WARNING: you specified units name but there are no units name in file'
            print(msg)
    
        if ( ( len(fileNameUnits) > 0 ) and ( len(fileNameUnits) < filemaxnbcol ) ):
            nbcol=len(fileNameUnits)
            for icol in range (nbcol,filemaxnbcol):
                fileNameUnits.append('col'+str(icol+1))
                pass
            pass
    


        if fileName:
            self.setName(fileName)
            pass
        if len(fileNameUnits):
            self.setColumnUnits(fileNameUnits)
            pass
        for i in range(filemaxnbcol):
            if columnsNames.lower()=='yes':
                self.addColumn(effectivecolumnNames[i],allvaluesbycolonne[i])
                pass
            else:
                self.addColumnValues(allvaluesbycolonne[i])
                pass
            pass
        return
示例#24
0
    def set(self, name, value, instance=0, context=None, type='i'):
        '''
        snmpojb.set('swDetailsVersion', 1, '05.00.96')
        '''
        oid = ''
        if self.isDotNotation(name):
            oid = name
            # TODO do we try and guess what our value is if we are given dot notation
            #rfcValue=rfc1902.OctetString(value)
            # type can be:  i INTEGER, u  UNSIGNED, s  STRING, x  HEX STRING, d  DECIMAL STRING
            # n  NULLOBJ, o  OBJID, t  TIMETICKS, a  IPADDRESS, b  BITS
            assert (type in 'iusxdnotab'
                    ), "ERROR: Value type {0} not supported".format(type)
            try:
                if type == 'i':
                    rfcValue = rfc1902.Integer32(value)
                elif type == 'u':
                    rfcValue = rfc1902.Unsigned32(value)
                elif type == 't':
                    rfcValue = rfc1902.TimeTicks(value)
                elif type == 'a':
                    rfcValue = rfc1902.IpAddress(value)
                elif type == 'b':
                    rfcValue = rfc1902.Bits(value)
                elif type == 's':
                    rfcValue = rfc1902.OctetString(value)
                elif type == 'o':
                    rfcValue = univ.ObjectIdentifier(value)
                elif type in 'xdn':
                    print 'WARNING: Type ' + type + ' not fully supported (hope default will do)'
                    rfcValue = rfc1902.OctetString(value)
                else:
                    print "unknown type"
            except:
                print("Error in SNMPClient, could not convert {} to type {}"
                      ).format(value, type)
        else:
            oid = self.mibs[name].get('oid')
            # TODO: incode the type
            type = self.mibs[name].get('type')
            # TODO: do something similar as if a couple of lines up
            rfcValue = self.mibs.encode(type, value)
        #if instance!=0:
        oid += '.' + str(instance)

        if context == None:
            context = self.context

        if SNMPClient.DEBUG:
            print "Setting %s (%s) = %s" % (name, oid, str(value))
        cmdGen = MyCmdGen()
        errorIndication, errorStatus, _, varBinds = cmdGen.setCmd(
            self.agent, context, self.host, self.port,
            (rfc1902.ObjectName(oid), rfcValue))

        if SNMPClient.DEBUG:
            print 'agent', self.agent
            print 'context', context
            print 'host', self.host
            print 'port', self.port
            print 'type', type

        if errorIndication:
            raise IOError(errorIndication)
        if errorStatus:
            raise SNMPError('set on ' + oid + '(' + name + ')', errorStatus)

        return varBinds[-1][1].prettyPrint()