Commit ab45bb55 authored by Tomas Hlavacek's avatar Tomas Hlavacek

Rework session handling to support old Python

Remove static methods from Session object in order to make it possible
to dump/pickle the object on older Python versions.
parent 71bfba5e
...@@ -98,7 +98,7 @@ def testULGSessions(sessionid=None): ...@@ -98,7 +98,7 @@ def testULGSessions(sessionid=None):
try: try:
s1 = ulg.Session(sessionid,routerid=1,commandid=2,parameters={'baba':'bubu'},result='reeesult') s1 = ulg.Session(sessionid,routerid=1,commandid=2,parameters={'baba':'bubu'},result='reeesult')
sid = s1.getSessionId() sid = s1.getSessionId()
s2 = ulg.Session.load(sid) s2 = ulg.loadSession(sid)
if(s1.getSessionId() == s2.getSessionId() and s1.getRouterId() == s2.getRouterId()): if(s1.getSessionId() == s2.getSessionId() and s1.getRouterId() == s2.getRouterId()):
print "OK: Test sessions." print "OK: Test sessions."
return True return True
......
...@@ -46,6 +46,18 @@ ipv6_annotate_regexp = re.compile(IPV6_ANNOTATE_REGEXP) ...@@ -46,6 +46,18 @@ ipv6_annotate_regexp = re.compile(IPV6_ANNOTATE_REGEXP)
### CGI output handler ### CGI output handler
def getSessionFileName(sessionid):
if(re.compile('^[a-zA-Z0-9]{10,128}$').match(sessionid)):
return defaults.session_dir+'/'+'ulg-'+sessionid+'.session'
else:
raise Exception('Invalid session id passed. Value was: '+sessionid)
def getSessionOutputFileName(sessionid):
if(re.compile('^[a-zA-Z0-9]{10,128}$').match(sessionid)):
return defaults.session_dir+'/'+'ulg-'+sessionid+'.out.session'
else:
raise Exception('Invalid session id passed. Value was: '+sessionid)
class Session(object): class Session(object):
def __init__(self,sessionid=None,routerid=None,commandid=None,parameters=[],result=None,finished=False,error=None,resrange=None,copy=None): def __init__(self,sessionid=None,routerid=None,commandid=None,parameters=[],result=None,finished=False,error=None,resrange=None,copy=None):
if(copy): if(copy):
...@@ -79,42 +91,9 @@ class Session(object): ...@@ -79,42 +91,9 @@ class Session(object):
def __genSessionId__(self): def __genSessionId__(self):
return md5.new(str(time.time())+str(random.randint(1,1000000))).hexdigest() return md5.new(str(time.time())+str(random.randint(1,1000000))).hexdigest()
@staticmethod
def getSessionFileName(sessionid):
if(re.compile('^[a-zA-Z0-9]{10,128}$').match(sessionid)):
return defaults.session_dir+'/'+'ulg-'+sessionid+'.session'
else:
raise Exception('Invalid session id passed. Value was: '+sessionid)
@staticmethod
def getSessionOutputFileName(sessionid):
if(re.compile('^[a-zA-Z0-9]{10,128}$').match(sessionid)):
return defaults.session_dir+'/'+'ulg-'+sessionid+'.out.session'
else:
raise Exception('Invalid session id passed. Value was: '+sessionid)
@staticmethod
def load(sessionid):
if(sessionid == None):
return None
try:
fn = Session.getSessionFileName(sessionid)
if(os.path.isfile(fn)):
f = open(fn, 'rb')
s = pickle.load(f)
f.close()
return s
else:
return None
except Exception:
return None
def save(self): def save(self):
try: try:
fn = Session.getSessionFileName(self.getSessionId()) fn = getSessionFileName(self.getSessionId())
f = open(fn,'wb') f = open(fn,'wb')
pickle.dump(self, f) pickle.dump(self, f)
f.close() f.close()
...@@ -159,7 +138,7 @@ class Session(object): ...@@ -159,7 +138,7 @@ class Session(object):
return self.parameters return self.parameters
def setResult(self,result): def setResult(self,result):
fn = Session.getSessionOutputFileName(self.sessionid) fn = getSessionOutputFileName(self.sessionid)
f = open(fn, 'w') f = open(fn, 'w')
f.write(result) f.write(result)
...@@ -167,7 +146,7 @@ class Session(object): ...@@ -167,7 +146,7 @@ class Session(object):
def getResult(self): def getResult(self):
try: try:
fn = Session.getSessionOutputFileName(self.sessionid) fn = getSessionOutputFileName(self.sessionid)
if(os.path.isfile(fn)): if(os.path.isfile(fn)):
f = open(fn, 'r') f = open(fn, 'r')
...@@ -194,7 +173,7 @@ class Session(object): ...@@ -194,7 +173,7 @@ class Session(object):
return '' return ''
def appendResult(self,result_fragment): def appendResult(self,result_fragment):
fn = Session.getSessionOutputFileName(self.sessionid) fn = getSessionOutputFileName(self.sessionid)
f = open(fn, 'a') f = open(fn, 'a')
f.write(result_fragment) f.write(result_fragment)
...@@ -202,7 +181,7 @@ class Session(object): ...@@ -202,7 +181,7 @@ class Session(object):
def clearResult(self): def clearResult(self):
try: try:
fn = Session.getSessionOutputFileName(self.sessionid) fn = getSessionOutputFileName(self.sessionid)
if(os.path.isfile(fn)): if(os.path.isfile(fn)):
f=open(fn,'w') f=open(fn,'w')
...@@ -251,6 +230,24 @@ class Session(object): ...@@ -251,6 +230,24 @@ class Session(object):
self.data = data self.data = data
self.save() self.save()
def loadSession(sessionid):
if(sessionid == None):
return None
try:
fn = getSessionFileName(sessionid)
if(os.path.isfile(fn)):
f = open(fn, 'rb')
s = pickle.load(f)
f.close()
return s
else:
return None
except Exception:
return None
class DecoratorHelper: class DecoratorHelper:
def __init__(self): def __init__(self):
...@@ -600,7 +597,7 @@ class ULGCgi: ...@@ -600,7 +597,7 @@ class ULGCgi:
if(sessionid==None): if(sessionid==None):
return self.HTTPRedirect(self.decorator_helper.getErrorURL()) return self.HTTPRedirect(self.decorator_helper.getErrorURL())
session = Session.load(sessionid) session = loadSession(sessionid)
if(session == None): if(session == None):
return self.HTTPRedirect(self.decorator_helper.getErrorURL()) return self.HTTPRedirect(self.decorator_helper.getErrorURL())
...@@ -635,7 +632,7 @@ class ULGCgi: ...@@ -635,7 +632,7 @@ class ULGCgi:
result_text = defaults.STRING_ARBITRARY_ERROR result_text = defaults.STRING_ARBITRARY_ERROR
session = Session.load(sessionid) session = loadSession(sessionid)
if(session!=None): if(session!=None):
result_text=self.decorator_helper.pre(self.sessions[sessionid].getError()) result_text=self.decorator_helper.pre(self.sessions[sessionid].getError())
...@@ -672,7 +669,7 @@ class ULGCgi: ...@@ -672,7 +669,7 @@ class ULGCgi:
if(sessionid==None): if(sessionid==None):
return self.HTTPRedirect(self.decorator_helper.getErrorURL()) return self.HTTPRedirect(self.decorator_helper.getErrorURL())
session = Session.load(sessionid) session = loadSession(sessionid)
if(session == None): if(session == None):
return self.HTTPRedirect(self.decorator_helper.getErrorURL()) return self.HTTPRedirect(self.decorator_helper.getErrorURL())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment