1. pkrone
  2. pyred

Commits

andrea  committed 40f6f67

more robust bus implementation.
enforced "plugin pattern" use
minimal definition of "const"

  • Participants
  • Parent commits 2a590f8
  • Branches default

Comments (0)

Files changed (11)

File pyred/__init__.py

View file
  • Ignore whitespace
+
+from const import *
+from errors import * 

File pyred/bus.py

View file
  • Ignore whitespace
 class AbstractBus(dobj):
     
     def __init__(self, address=None, port=None, name=None):
+        super(AbstractBus, self).__init__()
+        
+    def start(self):
         raise NotImplementedError
-        
-    def serve(self):
+    
+    def stop(self):
+        raise NotImplementedError
+    
+    def restart(self):
+        raise NotImplementedError
+    
+    def shutdown(self):
+        raise NotImplementedError
+    
+    def useChannel(self, channelId):
         raise NotImplementedError
         
     def sendMsg(self, message=None):

File pyred/const.py

View file
  • Ignore whitespace
+
+
+from utils import Struct
+
+DEFAULT=0
+ALL=0
+
+priority = Struct()
+priority.LEAST=0
+priority.LOW=200
+priority.NORMAL=400
+priority.MEDIUM=600
+priority.HIGH=800
+priority.HIGHEST=999
+
+type=Struct()
+type.INFO=0
+type.WARNING=1
+type.ERROR=2
+

File pyred/domain.py

View file
  • Ignore whitespace
 
 class domainObj(object):
     def __init__(self):
-        pass
+        super(domainObj, self).__init__()

File pyred/errors.py

View file
  • Ignore whitespace
+
+class pyRedException(Exception):
+    pass
+
+class ResourceNotAvaiable(pyRedException):
+    pass

File pyred/message.py

View file
  • Ignore whitespace
+
+import time
+from domain import domainObj as dobj
+
+def Filter(dobj):
+	def __init__(self):
+		super(Filter, self).__init__()
+		
+class TimeStruct(dobj):
+	def __init__(self):
+		super(TimeStruct, self).__init__()
+		self.timeDict={}
+	def setTime(self, timeName, timeValue):
+		self.timeDict[timeName]=timeValue
+	def getTime(self, timeName):
+		self.timeDict[timeName]
+	def mark(self, timeName):
+		self.timeDict[timeName]=time.time()
+
+class Message(dobj):
+	def __init__(self, dest='', msgClass='', msgType='', body=''):
+		super(Message, self).__init__()
+		self.toAddress=dest
+		self.msgClass=msgClass
+		self.msgType=msgType
+		self.body=body
+		self.timeStruct=TimeStruct()
+		self.timeStruct.mark('INIT_TIME')

File pyred/plugins/beansbus.py

View file
  • Ignore whitespace
+
+import subprocess
+import string
+import shlex
+
+from plugin import AbstractPlugin, PluginInfo
+from pyred.bus import AbstractBus
+
+from beanstalk import serverconn
+
+
+class BeanBus(AbstractPlugIn, AbstractBus):
+    srvProc=None
+    server=None
+    started=False
+    address=''
+    port=''
+    cmdString="beanstalkd -l $addr -p $prt"
+    
+    def __init__ (self,address=None, port=None, name=None):
+        self.address = address
+        self.port=port
+        super(BeanBus, self).__init__()
+        try:
+            self.start()
+        except ResourceNotAvaible:
+            raise ResourceNotAvaiable
+            
+    def start(self):
+        addr=self.address
+        prt=self.port
+        cmdTemplate= string.Template(self.cmdString)
+        if addr != None and prt != None:
+            cmdArgs = shlex.split(cmdTemplate.substitute(addr=addr, prt=prt))
+            try:
+                self.srvProc= subprocess.Popen(cmdArgs)
+                retCode=srvProc.poll()
+                if retCode == 0:
+                    self.server=serverconn.ServerConn(address, port)
+                    self.started=True
+                elif retCode == 111:
+                    raise ResourceNotAvaiable
+                
+            except ResourceNotAvaible:
+                raise ResourceNotAvaiable
+          
+    def stop(self):
+        if self.started == True and self.server != None:
+            self.started=False
+            
+    def restart(self):
+        self.stop()
+        self.start()
+            
+    def shutdown(self):
+        self.stop()
+        self.srvProc.terminate()
+
+    def useChannel(self, channelId):
+        if self.started:
+            self.server.use(channelId)
+
+    def sendMsg(self, msg):
+        if self.started:
+            returnDict = self.server.put(msg)
+            if returnDict[state] != 'ok':
+                return False
+    
+    def recvMsg(self):
+        if self.started:
+            m=self.server.reserve()
+            if m['state'] == 'ok':
+                return m['data']
+
+
+    def registerPlugIn(self):
+        info=PluginInfo()
+        info.name="beantalkd_bus"
+        info.ver='0.1'
+        info.type='bus'
+        info.desc="main bus implementation based on beanstalkd deamon"
+        return info

File pyred/plugins/plugin.py

View file
  • Ignore whitespace
 from domain import domainObj as dobj
 
 class AbstractPlugin(dobj):
-    def __init__(self, name=None):
-        pass
-    def setVersion(self, version=""):
-        pass
+    def __init__(self):
+        super(AbstractPlugin, self).__init__()
     
     @staticmethod
     def registerPlugin(self):
 
 class PluginManager(dobj):
     def __init__(self):
+        super(PluginManager, self).__init__()
         self.pluginReg={}
         
     def buildPluginList(self):
     
     def getPlugIn(self, name=None):
         pass
+
+
+class PluginInfo(dobj):
+    def __init__(self):
+        super(PluginInfo, self).__init__()
+        self.name=''
+        self.ver=''
+        self.type=''
+        self.dec=''

File pyred/settings.py

View file
  • Ignore whitespace
+
+
+class Settings(object):
+    pass

File pyred/test/dream.py

View file
  • Ignore whitespace
+
+
+import pyred
+
+settings=pyred.settings.Settings()
+
+settings['server_type']=pyred.DEFAULT
+settings['address']='127.0.0.1'
+settings['port']=9090
+
+
+server=pyred.getPluginByName(name='defaultServer', initData=settings)
+client1=pyred.getPluginByName(name='dummyclient', initData=settings)
+client2=pyred.getPluginByName(name='dummyclient', initData=settings)
+client3=pyred.getPluginByName(name='dummyclient', initData=settings)
+
+client1.setName("Client1")
+client2.setName("Client2")
+client3.setName("Client3")
+
+m1 = pyred.getMessage(msgType=pyred.INFO, body=1 )
+m2 = pyred.getMessage(msgType=pyred.WARNING, body=2 )
+m3 = pyred.getMessage(msgType=pyred.ERROR, body=3 )
+m4 = pyred.getMessage(msgType=pyred.INFO, body=4 )
+
+f1=pyred.message.Filter()
+f1.setOpt('priority', pyred.ALL)
+f1.setOpt('msg_type', pyred.ALL)
+
+f2=pyred.message.Filter()
+f2.setOpt('priority', pyred.priority.LEAST)
+f2.setOpt('msg_type', pyred.type.INFO)
+
+f3=pyred.message.Filter()
+f3.setOpt('priority', pyred.priority.LEAST)
+f3.setOpt('msg_type', pyred.type.WARNING)
+
+client1.setFilter(f1)
+client2.setFilter(f2)
+client3.setFilter(f3)
+

File pyred/utils.py

View file
  • Ignore whitespace
+
+class Struct(dict):
+    def __getattr__(self,name):
+        try:
+            val=self[name]
+        except KeyError:
+            val=super(Struct,self).getattr(self,name)
+        return val
+
+    def __setattr__(self,name,val):
+        l=dir(self)
+        if name in self:
+            super(Struct,self).setattr(self,name,val)
+        else:
+            self[name]=val