Skip to content

Commit

Permalink
Refactored to match updated coding standard.
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryvp committed Jun 3, 2013
1 parent 66144f5 commit 84c4f90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
5 changes: 5 additions & 0 deletions parabridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import settings
import info


HELP_APP = """Paradox to SQLite bridge. This tool monitors specified
Paradox database and reflects all changes to specified SQLite database
that can be used by any application that has problems with Paradox."""
Expand Down Expand Up @@ -51,17 +52,20 @@ def status( _ ) :
except socket.error :
print( "Daemon is not running." )


def task_add( m_args ) :
sName = m_args[ 'task_name' ]
sSrc = m_args[ 'task_src' ]
sDst = m_args[ 'task_dst' ]
if not settings.instance.taskAdd( sName, sSrc, sDst ) :
logging.warning( "Already has '{0}' task".format( sName ) )


def task_del( m_args ) :
if not settings.instance.taskDelByName( m_args[ 'task_name' ] ) :
logging.warning( "No task named '{0}'".format( m_args[ 'task_name' ] ) )


def task_list( _ ) :
lTasks = settings.instance.taskList()
if 0 == len( lTasks ) :
Expand All @@ -73,6 +77,7 @@ def task_list( _ ) :
mTask[ 'src' ],
mTask[ 'dst' ] ) )


def main() :
settings.instance.init( f_notify = True )
oParser = argparse.ArgumentParser( description = HELP_APP )
Expand Down
50 changes: 25 additions & 25 deletions parabridge/parabridge_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@

class Worker( threading.Thread ) :

__oInstance = None
_instance_o = None


def __init__( self ) :
super( Worker, self ).__init__()
self.__fShutdown = False
self.__oShutdown = threading.Event()
self.__fCfgChanged = True
self.__mResults = {}
self.__oTimeReloadLast = None
self._shutdown_f = False
self._shutdown_o = threading.Event()
self._cfgCHanged_f = True
self._results_m = {}
self._timeReloadLast_o = None


def run( self ) :
while not self.__fShutdown :
while not self._shutdown_f :
lTasks = []
if self.__fCfgChanged :
if self._cfgCHanged_f :
lTasks = settings.instance.taskList()
self.__fCfgChanged = False
self.__oTimeReloadLast = time.localtime()
self._cfgCHanged_f = False
self._timeReloadLast_o = time.localtime()
for mTask in lTasks :
sSrc = os.path.expanduser( mTask[ 'src' ] )
sDst = os.path.expanduser( mTask[ 'dst' ] )
Expand All @@ -52,7 +52,7 @@ def run( self ) :
def processTask( self, s_guid, s_name, s_src, s_dst ) :

def setRes( i_sTxt ) :
self.__mResults[ s_name ] = i_sTxt
self._results_m[ s_name ] = i_sTxt
return False

if not os.path.exists( s_src ) :
Expand All @@ -76,7 +76,7 @@ def setRes( i_sTxt ) :
setRes( "Processing {0}/{1}".format( i + 1, nTotal ) )
if self.processParadoxFile( s_guid, sSrcFile, oConn ) :
lProcessed.append( True )
if self.__fShutdown :
if self._shutdown_f :
return
## Sleep some time so we don't overuse HDD and CPU.
time.sleep( 1 )
Expand All @@ -92,7 +92,7 @@ def processParadoxFile( self, s_guid, s_src, o_conn ) :
try :
sFile = os.path.basename( s_src )
nIndexLast = settings.instance.indexLastGet( s_guid, sFile )
mArgs = { 'shutdown' : self.__oShutdown }
mArgs = { 'shutdown' : self._shutdown_o }
## First time parse of this file?
if nIndexLast is None :
oDb = pyparadox.open( s_src, ** mArgs )
Expand Down Expand Up @@ -165,28 +165,28 @@ def FieldKey( i_sParadoxName ) :


def shutdown( self ) :
self.__fShutdown = True
##! After |__fShutdown| is set to prevent races.
self.__oShutdown.set()
self._shutdown_f = True
##! After |_shutdown_f| is set to prevent races.
self._shutdown_o.set()


@classmethod
def instance( cls ) :
if not cls.__oInstance :
cls.__oInstance = Worker()
return cls.__oInstance
if not cls._instance_o :
cls._instance_o = Worker()
return cls._instance_o


def cfgChanged( self ) :
self.__fCfgChanged = True
self._cfgCHanged_f = True


def results( self ) :
return self.__mResults
return self._results_m


def timeReloadLast( self ) :
return self.__oTimeReloadLast
return self._timeReloadLast_o


class Server( SimpleXMLRPCServer, object ) :
Expand All @@ -195,19 +195,19 @@ class Server( SimpleXMLRPCServer, object ) :
def __init__( self, n_port ) :
gAddr = ( 'localhost', n_port )
SimpleXMLRPCServer.__init__( self, gAddr, logRequests = False )
self.__fShutdown = False
self._shutdown_f = False
self.register_function( self.stop )
self.register_function( self.status )
self.register_function( self.cfgChanged )


def serve_forever( self, ** _ ) :
while not self.__fShutdown :
while not self._shutdown_f :
self.handle_request()


def stop( self ) :
self.__fShutdown = True
self._shutdown_f = True
return True


Expand Down
10 changes: 5 additions & 5 deletions parabridge/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ class Settings( object ) :


def __init__( self ) :
self.__fInit = False
self.__fNotify = False
self._init_f = False
self._notify_f = False


def init( self, f_notify = False ) :
self.__fNotify = f_notify
self.__fInit = True
self._notify_f = f_notify
self._init_f = True
with sqlite3.connect( info.FILE_CFG ) as oConn :
oConn.executescript( SQL_CREATE )


## Notify daemon process so it can read updated settings.
def notifyIfNeeded( self ) :
if not self.__fNotify :
if not self._notify_f :
return
try :
xmlrpclib.ServerProxy( info.COMM_ADDR ).cfg_changed()
Expand Down

0 comments on commit 84c4f90

Please sign in to comment.