浏览代码

refactor...

steve 8 年之前
父节点
当前提交
1aad0c4acc
共有 2 个文件被更改,包括 60 次插入19 次删除
  1. 13 10
      config.json
  2. 47 9
      src/utils/agents/actor.py

+ 13 - 10
config.json

@@ -1,15 +1,18 @@
 {
 {
-	"virtual-env":{
-		"class":"Sandbox",
-		"config":{
-			"3-launchpad":{"requirements":"/Users/steve/Documents/git/repair-file/required.txt","sandbox":"/Users/steve/Documents/git/sandbox"}
-		}
+	"id":"zulu-hacker",
+	"key":"4q-h8r5-247&!570p=[0v8]x360",
+	"api":"localhost",
+	"delay":0.5,
+	"store":{
+		"class":{"read":"CouchdbReader","write":"CouchdbWriter"},
+		"args":{"uri":"http://localhost:5984","dbname":"monitor","uid":"logs"}
 	},
 	},
-	"processes":{
-		"class":"DetailProcess",
-		"config":{
-			"system":["postgresql","couchdb","httpd"]
-			}
+	"procs":["kate","firefox"],
+	"folders":["/home/steve/tmp","/home/steve/git","/home/steve/Downloads"],
+	"actions":{
+		"folders":{"threshold":"10mb","action":"archive"},
+		"apps":{"firefox":"","kate":""	}
 	}
 	}
 	
 	
+	
 }
 }

+ 47 - 9
src/utils/agents/actor.py

@@ -60,8 +60,11 @@ class Actor(Thread):
 class Folders(Actor):
 class Folders(Actor):
     def init(self,config,item):
     def init(self,config,item):
         Actor.init(self,config,item)
         Actor.init(self,config,item)
-        self.config = config['folders']
-        self.item   = item
+        self.lfolders   = config['folders']
+        self.config     = config['actions']['folders']
+        self.threshold  = self.get_size(self.config['threshold'])
+        self.item       = item
+    
     def archive(self,item):
     def archive(self,item):
     """
     """
         This function will archive all files in a given folder
         This function will archive all files in a given folder
@@ -82,13 +85,44 @@ class Folders(Actor):
     """
     """
         This function consists in deleting files from a given folder
         This function consists in deleting files from a given folder
     """
     """
-        shutil.rmtree(item['label'])
-        os.mkdir(item['label'])
-                      
+        rpath = item['label']
+        lists = os.listdir(item['label'])
+        for name in list() :
+            path = os.sep([item['label'],name])
+            if os.path.isdir(path) :
+                shutil.rmtree(path)
+            else:
+                os.remove(path)
+        #
+        # 
+
+    def get_size(self,value):
+        units = {'MB':1000,'GB':1000000,'TB':1000000000} # converting to kb
+        key = set(unites) & set(re.split('(\d+)',value.upper()))
+        if len(key) == 0:
+            return -1
+        key = key.pop()
+        return float(value.upper().replace('MB','').strip()) * units[key]
+    
     def isvalid(self,item):        
     def isvalid(self,item):        
-        return os.path.exists(item['label'])
+    """
+        This function returns whether the following :
+        p : folder exists
+        q : has_reached threashold
+    """
+        
+        
+        p = os.path.exists(item['label']) and item['label'] in self.lfolders
+    
+        q = self.get_size(item['size']) >= self.threshold
+        return p and q
+    
     def process(self,item):
     def process(self,item):
-        print item
+        if self.isValid(item) :
+            
+            name = self.config['action']
+            stream = "".join([name,'(',json.dumps(item),')'])
+            eval(stream)
         
         
         
         
 class Kill(Actor):
 class Kill(Actor):
@@ -109,7 +143,7 @@ class Start(Actor):
     
     
     def init(self,config,item):
     def init(self,config,item):
         Actor.init(self,config,item)
         Actor.init(self,config,item)
-        self.config = config['start']
+        self.config = config['apps']
         self.ng = ng(self.config.keys())
         self.ng = ng(self.config.keys())
 
 
     def isValid(self,name):
     def isValid(self,name):
@@ -169,7 +203,10 @@ class Apps(Actor):
             self.reboot()
             self.reboot()
         
         
         
         
-            
+class Event(Thread):    
+    def __init__(self,config):
+        pass
+    def run(self):
 """
 """
     The orchestrator class is designed to aggregate actions and communicate back to the caller
     The orchestrator class is designed to aggregate actions and communicate back to the caller
     Mesage passing is structured as follows {from,to,content} The content is designed to be understood by the actor
     Mesage passing is structured as follows {from,to,content} The content is designed to be understood by the actor
@@ -187,6 +224,7 @@ class Orchestrator(Actor):
             f.close()
             f.close()
         self.config = config
         self.config = config
         Actor.__init__(self)
         Actor.__init__(self)
+        
         self.actors = {"apps":Apps(),"folders":Folders()}
         self.actors = {"apps":Apps(),"folders":Folders()}
         self.is_master_node = False
         self.is_master_node = False
         self.items = []
         self.items = []