techrec/server/techrec.py

114 lines
2.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-08-28 01:09:53 +02:00
try:
from sqlalchemy import create_engine, Column, Integer, String
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):
__tablename__ = 'rec'
id = Column(Integer, primary_key=True)
name = Column(String)
endtime = Column(String)
starttime = Column(String)
state = Column(Integer)
def __init__(self, name="", starttime="", endtime="",asjson=""):
self.error = 0
if len(asjson) == 0:
self.name = name
self.starttime = starttime
self.endtime = endtime
else:
try:
# dec = json.loads( unicode(asjson) )
dec = yaml.load( asjson )
except:
self.error = 0
self.name = dec[0]['name']
self.starttime = dec[0]['starttime']
self.endtime = dec[0]['endtime']
# self.id = None
self.state = QUEUE
print "DECC" , dec
print "NAME", self.name
print "Sstart", self.starttime
print "END", self.endtime
def err(self): return self.error
def setrun(self):
self.state = RUN
def setdone(self):
self.state = DONE
2013-08-28 01:09:53 +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):
logging.info("DB")
allrecords = self.session.query(Rec).all()
for record in allrecords:
logging.info("R: %s " % record)
def execsql(self,sql):
records = self.conn.execute( sql )
def search(self, name="", starttime="", endtime=""):
logging.info("Looking for %s" % recfilter)
# self.session.query( Rec ).filter( Rec.name=name ).first()
2013-08-28 01:09:53 +02:00
for row in records:
print "Found:", row