def Trash(filename,called): cfg = Config() logger = logging.getLogger('Media_Manager.builtin') tmp_path,tmp_filename = os.path.split(filename); watched_dirs = cfg.read_config('Watch_dirs'); try: trash_dir = cfg.get_config_value('Trash','trash_dir') except: logger.error("Trash Dir not set please check config"); return(); for all in watched_dirs: # Resolve Items Dir Structure if it has one if tmp_path[:len(watched_dirs[all])] == watched_dirs[all]: trash_dest = trash_dir + (tmp_path[len(watched_dirs[all]):]) + '/'; if os.path.exists(trash_dest): shutil.move(filename,trash_dest); logger.info("%s moved to Trash" %filename); else: os.makedirs(trash_dest); shutil.move(filename,trash_dest); logger.info("%s moved to Trash" %filename);
def Move(filename,called): logger = logging.getLogger('Media_Manager.builtin') cfg= Config(); if called == '': logger.warn("Builtin_move no destination specified"); return(); src_path,src_filename = os.path.split(filename); watched_dirs = cfg.read_config('Watch_dirs'); for all in watched_dirs: if src_path[:len(watched_dirs[all])] == watched_dirs[all]: dest_path = called +(src_path[len(watched_dirs[all]):]); if os.path.exists(dest_path): shutil.move(filename,dest_path) logger.info("%s moved to %s" %(filename,dest_path)) else: os.makedirs(dest_path); shutil.move(filename,dest_path); logger.info("%s moved to %s" %(filename,dest_path))
from modules.camera import Camera from modules.body import Body from modules.config import Config from modules.sensors import Sensors body = Body() camera = Camera() config = Config() sensors = Sensors() sensors.daemon = True sensors.start() try: while True: time.sleep(0.1) Config.read_config(config) # Keep reading config for changes Body.move(body, Config.retrieve(config, 'Body', 'direction'), Sensors.retrieve(sensors), config) Camera.move(camera, Config.retrieve(config, 'Head', 'x'), Config.retrieve(config, 'Head', 'y')) except KeyboardInterrupt: Sensors.__exit__(sensors) Body.__exit__(body) Camera.__exit__(camera) Config.__exit__(config) exit()
class MediaHandler(pyinotify.ProcessEvent): logger = logging.getLogger('Media_Manager.media') def process_IN_MOVED_TO(self,event): self.logger.warn("file was moved %s" %(os.path.join(event.path,event.name))) self.process_IN_CLOSE_WRITE(event); def process_IN_CREATE(self, event): if event.dir: self.logger.debug("Detected Dir creation %s" %event.name) return() def process_IN_CLOSE_WRITE(self,event): self.logger.debug("Detected File Creation: %s" % os.path.join(event.path, event.name)); self.process_newitem(os.path.join(event.path,event.name)) def process_IN_DELETE(self,event): if event.dir: self.logger.debug("Detected Dir Deleted %s" %event.name) else: self.logger.debug("File removed %s" % os.path.join(event.path, event.name)); def process_newitem(self,filename): #Check to see if the ext has been configured self.cfg = Config(); try: ext = Tools.get_ext(filename); self.logger.debug("Found %s" %ext) section = self.cfg.read_config(ext) except: self.logger.info ("No Config Found for %s" % ext); return (); for all in section: tmp = section[all] if tmp[:6] == 'plugin': self.launch_plugin(section[all],filename) elif tmp[:7] =='builtin': self.launch_builtin(section[all],filename) def launch_builtin(self,plugin,filename): if self.still_exists(filename): try: # Check to see if anything was attached to the plugin (e.g a destination); builtin, opts = plugin.split() except: opts = ''; builtin = plugin builtin = getattr (Builtin,builtin[8:]) method = builtin(filename,opts); def launch_plugin(self,pluginname,filename): if self.still_exists(filename): plug = Plugins() plug.load_plugin(pluginname,filename) def still_exists(self,filename): if os.path.isfile(filename): return (True) else: self.logger.debug("file no longer exists %s" % filename); return(False)
class Main(): def __init__(self): opts = self.cmdline(); # Parse command line options self.configure(opts); # parse config self.setup_logging(); self.watch_media(); def cmdline(self): parser = argparse.ArgumentParser(description='Automatily Manage Media items'); parser.add_argument('-c','--config',help="Specify a config file to use",required=False) parser.add_argument('-l','--log file',help='Specify the Log file to use if blank stdout is used',required=False) parser.add_argument('-p','--plugin_dir',help='Path to plugins',required=False); #TODO fix Debug parser.add_argument('-d','--debug',help='Debug Mode lots of junk in the logfile',required=False); args = vars(parser.parse_args()); return (args) def configure(self,args): # Load Config file if args['config'] == None: print ("Please point me at a config file") sys.exit(); else: Config.config_file = (args['config']) self.cfg = Config(); #Configure log file if args['log file'] == None: self.log_filename = self.cfg.get_config_value('logging','logfile'); # check for logfile in the config else: self.log_filename = args['log file']; def setup_logging(self): #Log Format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #Create Logger self.logger = logging.getLogger('Media_Manager'); self.logger.setLevel(logging.DEBUG); if self.log_filename == None: self.logger.info("No Logfile found"); else: #Create File handler self.logger.info("Logging to %s" % self.log_filename); fh = logging.FileHandler(self.log_filename) fh.setLevel(logging.DEBUG); fh.setFormatter(formatter) self.logger.addHandler(fh) #ch = logging.StreamHandler(); #ch.setLevel(logging.INFO); #ch.setFormatter(formatter) #self.logger.addHandler(ch) def watch_media(self): wd = self.cfg.read_config('Watch_dirs') if wd == None: self.logger.warn("No directorys to watch nothing for me todo"); sys.exit(); else: Mm = Media() for all in wd: Mm.add_dir(wd[all]); Mm.start_watching();
#!/bin/python # coding: utf-8 from flask import Flask, render_template from watchdog.observers import Observer from modules.watch import ChangeHandler from modules.config import Config app = Flask(__name__) image_path = "./image_test" config_path = "./config/config.ini" config = Config.read_config(config_path) @app.route("/") def top(): return render_template("index.html") @app.route("/config") def config(): return render_template("config.html") @app.route("/save_config", methods=["POST"]) def save_config(): return render_template("index.html") if __name__ == "__main__":