techrec/server/techrec.py

145 lines
3.5 KiB
Python
Raw Normal View History

import logging
2013-08-28 01:09:53 +02:00
logging.basicConfig(level=logging.DEBUG)
import sys
2013-09-10 21:18:45 +02:00
import datetime
try:
2013-09-10 21:18:45 +02:00
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
except:
sys.exit("No SQLAlchemy.")
import json
import yaml
2013-08-28 01:09:53 +02:00
"""
This class describe a single Record (Rec() class) and the
records manager (RecDB() class)
"""
Base = declarative_base()
""" ************
RECORD ABSTRACTION
************ """
QUEUE = 0
RUN = 2
DONE = 4
2013-08-28 01:09:53 +02:00
class Rec(Base):
2013-09-10 21:18:45 +02:00
__tablename__ = 'rec'
id = Column(Integer, primary_key=True)
name = Column(String)
# TODO: timestamp
endtime = Column(DateTime)
starttime = Column(DateTime)
state = Column(Integer)
def __init__(self, name="", starttime=None, endtime=None, asjson=""):
self.error = 0
self.values = {"name":"","starttime":"", "endtime":""}
if len(asjson) == 0:
self.name = name
self.starttime = starttime
self.endtime = endtime
else:
#try:
# dec = json.loads( unicode(asjson) )
# dec = yaml.load( asjson )
dec = json.dumps( asjson )
# except:
# self.error = 0
print("dec %s %s" % (dec,type(dec)))
print("asjson %s %s" % (asjson,type(asjson)))
self.name = asjson[0]['name']
self.starttime = asjson[0]['starttime']
selfendtime = asjson[0]['endtime']
self.state = QUEUE
logging.info("New REC: ", self.values)
# launch the job for processing files
def start(self):
# calcola file da splittare
pass
def getvalue(self,val=None):
if val != None:
if self.values.has_key(val):
return self.values[val]
return self.values
2013-09-10 21:18:45 +02:00
def err(self):
return self.error
2013-09-10 21:18:45 +02:00
def set_run(self):
self.state = RUN
2013-09-10 21:18:45 +02:00
def set_done(self):
self.state = DONE
2013-08-28 01:09:53 +02:00
2013-09-10 21:18:45 +02:00
def __repr__(self):
return "<Rec('%s','%s','%s', '%s', '%s')>" % (self.id, self.name, self.starttime, self.endtime, self.state)
2013-08-28 01:09:53 +02:00
""" ************
RecDB
************ """
class RecDB:
def __init__(self):
self.engine = create_engine('sqlite:///techrec.db', echo=True)
self.conn = self.engine.connect()
Base.metadata.create_all(self.engine) # create Database
Session = sessionmaker(bind=self.engine)
self.session = Session()
def add(self, simplerecord):
logging.debug("New Record: %s" % simplerecord)
self.session.add( simplerecord )
self.session.commit()
# print all records
def printall(self):
2013-09-10 21:18:45 +02:00
print(" ---- DB ---- ")
2013-08-28 01:09:53 +02:00
allrecords = self.session.query(Rec).all()
2013-09-10 21:18:45 +02:00
print [ "R: %s" % record for record in allrecords ]
2013-08-28 01:09:53 +02:00
def execsql(self,sql):
records = self.conn.execute( sql )
def search(self, name="", starttime="", endtime=""):
2013-09-10 21:18:45 +02:00
logging.debug("Looking for %s - %s - %s" % (name, starttime, endtime) )
ret = self.session.query(Rec).filter_by(name='Mimmo1').all()
print "[SEARCH] RET (",type(ret),")" , ret
for row in ret:
print "[SEARCH] Found:", row
if __name__ == "__main__":
# text
db = RecDB()
_mytime = datetime.datetime(2014,05,24,15,16,17)
a = Rec(name="Mimmo1", starttime=_mytime, endtime=None)
2013-08-28 01:09:53 +02:00
2013-09-10 21:18:45 +02:00
db.add( a )
2013-08-28 01:09:53 +02:00
2013-09-10 21:18:45 +02:00
raw_input()
db.printall()
raw_input()
# db.search( Rec(name="Mimmo1") )
db.search( name="Mimmo1" )