refactor...

community
Steve L. Nyemba 8 years ago
parent f1b3e8abf2
commit 1aad0c4acc

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

@ -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.lfolders = config['folders']
self.item = item 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']) rpath = item['label']
os.mkdir(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 = []

Loading…
Cancel
Save