From 0b16f758259ac7ca448b9d97aba945150af7aeb2 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 17 Aug 2012 18:44:24 -0700 Subject: [PATCH] more updates, got save working better... was overwriting all rows. have working list_of_jobs() function, basically ready to be used now... --- indenture.py | 73 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/indenture.py b/indenture.py index efaca57..fd60e57 100644 --- a/indenture.py +++ b/indenture.py @@ -5,7 +5,7 @@ activate_this = "venv/bin/activate_this.py" execfile(activate_this, dict(__file__=activate_this)) ###### -### What are we going to be using? +### What are we going to be using today? ###### import os @@ -33,6 +33,9 @@ class _db: return False def commit(self): + """ commit() + commits any pending changes to the database file. + """ return self._conn.commit() def query(self,query,returns="list",timeout=10,db="jobs.db"): @@ -74,14 +77,12 @@ class Job: self.originator = originator self.completed = completed - def _full_exec(self): """ _full_exec() returns the full command to be executed with the arguments """ return self.command+' '+self.arguments - def _disconnect(self): """ _disconnect() flushes writes to sqlite and closes the connection @@ -89,37 +90,40 @@ class Job: self._conn.commit() self._c.close() - def describe(self): + def describe(self,prints=True,returns=False): """ describe(): a public function that describes the job to be completed. """ description = [] description.append('UUID: ' + self.uuid) - description.append('Commmand: ' + self._full_exec) + description.append('Commmand: ' + self._full_exec()) description.append('Due Date: ' + self.what_time()) if self.completed: description.append('Completion: Completed') else: description.append('Completion: Incomplete') - print '\n'.join(description) - return '\n'.join(description) - - + if prints == True: + print '\n'.join(description) + if returns == True: + return '\n'.join(description) def do_work(self): """ do_work() actually performs the work to be completed. """ - return call(self.full_exec().split()) - + print self._full_exec().split() + r = call(self._full_exec().split()) + if not r > 0: + return True + else: + return False def open(self, uuid): """ open() runs a query to populate with the information of an existing job in the database """ - jobsdb = _db() data = jobsdb.query('SELECT uuid,command,arguments,due,originator,completed FROM jobs WHERE uuid = "' + uuid + '" LIMIT 1;', returns="one") @@ -137,18 +141,20 @@ class Job: jobsdb = _db() test = 'SELECT uuid FROM jobs WHERE uuid = "' + str(self.uuid) + '";' - if jobsdb.query(test): + if len(jobsdb.query(test,returns="list")) > 0: query = 'UPDATE jobs SET ' - query += 'uuid = "' + str(self.uuid) + '", ' query += 'command = "' + self.command + '", ' query += 'arguments = "' + self.arguments + '", ' query += 'due = "' + str(self.due) + '", ' query += 'originator = "' + self.originator + '", ' if self.completed: - query += 'completed = 1' + query += 'completed = 1 ' else: - query += 'completed = 0' - query += ';' + query += 'completed = 0 ' + query += 'WHERE uuid = "'+str(self.uuid)+'" LIMIT 1;' + + print query + jobsdb.query(query) jobsdb.commit() else: @@ -173,7 +179,6 @@ class Job: """ return pretty_date(self.due) - def what_work(self): """ what_work() public function to see what work is going to be done. @@ -187,7 +192,7 @@ class Job: def pretty_date(time=False): - """ + """ pretty_date() Get a datetime object or a int() Epoch timestamp and return a pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc @@ -266,8 +271,34 @@ def pretty_date(time=False): ###### -def list_jobs(include_completed=False): - """ list_jobs(include_completed = False) +def list_of_jobs(include_completed=False,only_past_due=True): + """ list_of_jobs() + returns a python list of all the outstanding jobs. + """ + + query = "SELECT uuid FROM jobs" + if include_completed == False and only_past_due == True: + query += " WHERE completed = 0 and due <= " + str(time.time()) + ';' + elif include_completed == False and only_past_due == False: + query += " WHERE completed = 0;" + elif include_completed == True and only_past_due == True: + query += " WHERE due <= " + str(time.time()) + ';' + + jobsdb = _db() + + job_list = [] + + for row in jobsdb.query(query): + j = Job() + j.open(uuid=row[0]) + job_list.append(j) + + return job_list + + + +def print_jobs(include_completed=False): + """ print_jobs() list all the jobs that need to be done, does not include completed jobs by default. """