<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" [ <NUM_LIT:11> : - <NUM_LIT:2> ] <EOL> __date__ = "<STR_LIT>" [ <NUM_LIT:7> : - <NUM_LIT:2> ] <EOL> __all__ = [ "<STR_LIT>" , ] <EOL> import datetime <EOL> import struct <EOL> import sys <EOL> from . import utils <EOL> class DbfFieldDef ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:start>" , "<STR_LIT:end>" , "<STR_LIT>" ) <EOL> length = None <EOL> typeCode = None <EOL> defaultValue = None <EOL> def __init__ ( self , name , length = None , decimalCount = None , <EOL> start = None , stop = None , ignoreErrors = False , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> assert self . typeCode is not None , "<STR_LIT>" <EOL> assert self . defaultValue is not None , "<STR_LIT>" <EOL> if len ( name ) > <NUM_LIT:10> : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> name = str ( name ) . upper ( ) <EOL> if self . __class__ . length is None : <EOL> if length is None : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> length = int ( length ) <EOL> if length <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % name ) <EOL> else : <EOL> length = self . length <EOL> if decimalCount is None : <EOL> decimalCount = <NUM_LIT:0> <EOL> self . name = name <EOL> self . length = length <EOL> self . decimalCount = decimalCount <EOL> self . ignoreErrors = ignoreErrors <EOL> self . start = start <EOL> self . end = stop <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( self . name , str ( other ) . upper ( ) ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . name ) <EOL> def fromString ( cls , string , start , ignoreErrors = False ) : <EOL> """<STR_LIT>""" <EOL> assert len ( string ) == <NUM_LIT:32> <EOL> _length = ord ( string [ <NUM_LIT:16> ] ) <EOL> return cls ( utils . unzfill ( string ) [ : <NUM_LIT:11> ] , _length , ord ( string [ <NUM_LIT> ] ) , <EOL> start , start + _length , ignoreErrors = ignoreErrors ) <EOL> fromString = classmethod ( fromString ) <EOL> def toString ( self ) : <EOL> """<STR_LIT>""" <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> _name = self . name [ : <NUM_LIT:11> ] + "<STR_LIT>" * ( <NUM_LIT:11> - len ( self . name ) ) <EOL> else : <EOL> _name = self . name . ljust ( <NUM_LIT:11> , '<STR_LIT>' ) <EOL> return ( <EOL> _name + <EOL> self . typeCode + <EOL> chr ( <NUM_LIT:0> ) * <NUM_LIT:4> + <EOL> chr ( self . length ) + <EOL> chr ( self . decimalCount ) + <EOL> chr ( <NUM_LIT:0> ) * <NUM_LIT> <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . fieldInfo ( ) <EOL> def fieldInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . name , self . typeCode , self . length , self . decimalCount ) <EOL> def rawFromRecord ( self , record ) : <EOL> """<STR_LIT>""" <EOL> return record [ self . start : self . end ] <EOL> def decodeFromRecord ( self , record ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . decodeValue ( self . rawFromRecord ( record ) ) <EOL> except : <EOL> if self . ignoreErrors : <EOL> return utils . INVALID_VALUE <EOL> else : <EOL> raise <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class DbfCharacterFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:C>" <EOL> defaultValue = "<STR_LIT>" <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value . rstrip ( "<STR_LIT:U+0020>" ) <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return str ( value ) [ : self . length ] . ljust ( self . length ) <EOL> class DbfNumericFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:N>" <EOL> defaultValue = <NUM_LIT:0> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = value . strip ( "<STR_LIT>" ) <EOL> if "<STR_LIT:.>" in value : <EOL> return float ( value ) <EOL> elif value : <EOL> return int ( value ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> _rv = ( "<STR_LIT>" % ( self . length , self . decimalCount , value ) ) <EOL> if len ( _rv ) > self . length : <EOL> _ppos = _rv . find ( "<STR_LIT:.>" ) <EOL> if <NUM_LIT:0> <= _ppos <= self . length : <EOL> _rv = _rv [ : self . length ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( self . name , _rv , self . length ) ) <EOL> return _rv <EOL> class DbfFloatFieldDef ( DbfNumericFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:F>" <EOL> class DbfIntegerFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:I>" <EOL> length = <NUM_LIT:4> <EOL> defaultValue = <NUM_LIT:0> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return struct . unpack ( "<STR_LIT>" , value ) [ <NUM_LIT:0> ] <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return struct . pack ( "<STR_LIT>" , int ( value ) ) <EOL> class DbfCurrencyFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:Y>" <EOL> length = <NUM_LIT:8> <EOL> defaultValue = <NUM_LIT:0.0> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return struct . unpack ( "<STR_LIT>" , value ) [ <NUM_LIT:0> ] / <NUM_LIT> <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return struct . pack ( "<STR_LIT>" , round ( value * <NUM_LIT> ) ) <EOL> class DbfLogicalFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:L>" <EOL> defaultValue = - <NUM_LIT:1> <EOL> length = <NUM_LIT:1> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value == "<STR_LIT:?>" : <EOL> return - <NUM_LIT:1> <EOL> if value in "<STR_LIT>" : <EOL> return False <EOL> if value in "<STR_LIT>" : <EOL> return True <EOL> raise ValueError ( "<STR_LIT>" % ( self . name , value ) ) <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is True : <EOL> return "<STR_LIT:T>" <EOL> if value == - <NUM_LIT:1> : <EOL> return "<STR_LIT:?>" <EOL> return "<STR_LIT:F>" <EOL> class DbfMemoFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:M>" <EOL> defaultValue = "<STR_LIT:U+0020>" * <NUM_LIT:10> <EOL> length = <NUM_LIT:10> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class DbfDateFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> typeCode = "<STR_LIT:D>" <EOL> defaultValue = utils . classproperty ( lambda cls : datetime . date . today ( ) ) <EOL> length = <NUM_LIT:8> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value . strip ( ) : <EOL> return utils . getDate ( value ) <EOL> else : <EOL> return None <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value : <EOL> return utils . getDate ( value ) . strftime ( "<STR_LIT>" ) <EOL> else : <EOL> return "<STR_LIT:U+0020>" * self . length <EOL> class DbfDateTimeFieldDef ( DbfFieldDef ) : <EOL> """<STR_LIT>""" <EOL> JDN_GDN_DIFF = <NUM_LIT> <EOL> typeCode = "<STR_LIT:T>" <EOL> defaultValue = utils . classproperty ( lambda cls : datetime . datetime . now ( ) ) <EOL> length = <NUM_LIT:8> <EOL> def decodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> assert len ( value ) == self . length <EOL> _jdn , _msecs = struct . unpack ( "<STR_LIT>" , value ) <EOL> if _jdn >= <NUM_LIT:1> : <EOL> _rv = datetime . datetime . fromordinal ( _jdn - self . JDN_GDN_DIFF ) <EOL> _rv += datetime . timedelta ( <NUM_LIT:0> , _msecs / <NUM_LIT> ) <EOL> else : <EOL> _rv = None <EOL> return _rv <EOL> def encodeValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value : <EOL> value = utils . getDateTime ( value ) <EOL> _rv = struct . pack ( "<STR_LIT>" , value . toordinal ( ) + self . JDN_GDN_DIFF , <EOL> ( value . hour * <NUM_LIT> + value . minute * <NUM_LIT> + value . second ) * <NUM_LIT:1000> ) <EOL> else : <EOL> _rv = "<STR_LIT>" * self . length <EOL> assert len ( _rv ) == self . length <EOL> return _rv <EOL> _fieldsRegistry = { } <EOL> def registerField ( fieldCls ) : <EOL> """<STR_LIT>""" <EOL> assert fieldCls . typeCode is not None , "<STR_LIT>" <EOL> _fieldsRegistry [ fieldCls . typeCode ] = fieldCls <EOL> def lookupFor ( typeCode ) : <EOL> """<STR_LIT>""" <EOL> return _fieldsRegistry [ typeCode ] <EOL> for ( _name , _val ) in globals ( ) . items ( ) : <EOL> if isinstance ( _val , type ) and issubclass ( _val , DbfFieldDef ) and ( _name != "<STR_LIT>" ) : <EOL> __all__ . append ( _name ) <EOL> registerField ( _val ) <EOL> del _name , _val </s>
