Skip to content

Commit

Permalink
Daemon now reads records from Paradox file.
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryvp committed Mar 2, 2012
1 parent a0ebbc2 commit 9c5aa03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
32 changes: 27 additions & 5 deletions parabridge_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def run( self ) :
for mTask in lTasks :
sSrc = os.path.expanduser( mTask[ 'src' ] )
sDst = os.path.expanduser( mTask[ 'dst' ] )
self.processTask( mTask[ 'name' ], sSrc, sDst )
self.processTask( mTask[ 'guid' ], mTask[ 'name' ], sSrc, sDst )
time.sleep( 1 )

def processTask( self, i_sName, i_sSrc, i_sDst ) :
def processTask( self, i_sGuid, i_sName, i_sSrc, i_sDst ) :
def setRes( i_sTxt ) :
self.m_mResults[ i_sName ] = i_sTxt
return False
Expand All @@ -63,7 +63,7 @@ def setRes( i_sTxt ) :
nTotal = len( lSrcFiles )
for i, sSrcFile in enumerate( lSrcFiles ) :
setRes( "Processing {0}/{1}".format( i + 1, nTotal ) )
if self.processParadoxFile( sSrcFile, i_sDst ) :
if self.processParadoxFile( i_sGuid, sSrcFile, i_sDst ) :
if self.m_fShutdown :
return
lProcessed.append( True )
Expand All @@ -73,9 +73,31 @@ def setRes( i_sTxt ) :

##x Process individual Paradox |.db| file and synchronize specified
## SQLite database file with it.
def processParadoxFile( self, i_sSrc, i_sDst ) :
def processParadoxFile( self, i_sGuid, i_sSrc, i_sDst ) :
try :
oDb = pyparadox.open( i_sSrc, shutdown = self.m_oShutdown )
sFile = os.path.basename( i_sSrc )
nIndexLast = Settings.indexLastGet( i_sGuid, sFile )
mArgs = { 'shutdown' : self.m_oShutdown }
## First time parse of this file?
if nIndexLast is None :
oDb = pyparadox.open( i_sSrc, ** mArgs )
else :
mArgs[ 'start' ] = nIndexLast + 1
oDb = pyparadox.open( i_sSrc, ** mArgs )
## We can handle only tables that has autoincrement field (if
## such field exists, it will be first for Paradox database. We
## need it to detect updates).
if len( oDb.fields ) < 1 or not oDb.fields[ 0 ].IsAutoincrement() :
return
## Table empty or not updated since saved last index.
if 0 == len( oDb.records ) :
return
for oRecord in oDb.records :
nIndex = oRecord.fields[ 0 ]
if nIndexLast is not None and nIndexLast >= nIndex :
raise Exception( "Consistency error." )
nIndexLast = nIndex
print( "{0}: {1}".format( sFile, nIndexLast ) )
except pyparadox.Shutdown :
return False
return True
Expand Down
14 changes: 14 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
WHERE guid = :guid AND file = :file"""
SQL_INDEX_LAST_ADD = """INSERT INTO index_last (guid, file, index_last)
VALUES (:guid, :file, :index_last)"""
SQL_INDEX_LAST_GET = """SELECT index_last FROM index_last WHERE
guid = :guid AND file = :file"""

class Settings( object ) :

Expand Down Expand Up @@ -83,6 +85,18 @@ def indexLastSet( i_sGuid, i_sFile, i_nIndex ) :
## No record for guid and name pair: add one.
oConn.execute( SQL_INDEX_LAST_ADD, mArgs )

@classmethod
def indexLastGet( self, i_sGuid, i_sFile ) :
with sqlite3.connect( FILE_CFG ) as oConn :
oConn.row_factory = sqlite3.Row
mArgs = { 'guid' : i_sGuid, 'file' : i_sFile }
lRet = oConn.execute( SQL_INDEX_LAST_GET, mArgs ).fetchall()
if 0 == len( lRet ) :
return None
if len( lRet ) > 1 :
raise Exception( "Consistency error." )
return lRet[ 0 ][ 'index_last' ]

@classmethod
def taskDelByName( self, i_sName ) :
with sqlite3.connect( FILE_CFG ) as oConn :
Expand Down

0 comments on commit 9c5aa03

Please sign in to comment.