From eb94c8cd206c0c053f809be0217db1deea6c4bbf Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Fri, 16 Jan 2015 09:28:25 -0500 Subject: added combox/cbox.py: pretty rough. contains function that runs combox. --- combox/cbox.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 combox/cbox.py diff --git a/combox/cbox.py b/combox/cbox.py new file mode 100644 index 0000000..2dffeab --- /dev/null +++ b/combox/cbox.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# +# Copyright (C) 2014 Combox author(s). See AUTHORS. +# +# This file is part of Combox. +# +# Combox is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Combox is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Combox (see COPYING). If not, see +# . + +import os +import time +import yaml + +from argparse import ArgumentParser +from os import path +from sys import exit +from watchdog.observers import Observer + +from combox.config import config_cb +from combox.events import ComboxEventHandler + + +## Function adapted from Watchdog's docs: +## http://pythonhosted.org/watchdog/quickstart.html#quickstart + +def run_cb(config): + """ + Runs combox. + """ + c_path = path.abspath(config['combox_dir']) + event_handler = ComboxEventHandler(config) + + observer = Observer() + observer.schedule(event_handler, c_path, recursive=True) + observer.start() + print "Hit Ctrl-C to quit." + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join() + + +if __name__ == "__main__": + + parser = ArgumentParser() + parser.add_argument("-t", "--test", + help="Use the combox config file in testing area.", + action="store_true") + args = parser.parse_args() + if args.test: + CONFIG_DIR = path.join('tests', 'test-config') + else: + CONFIG_DIR = path.join(os.getenv('HOME'),'.combox/') + + config_file = path.join(CONFIG_DIR, 'config.yaml') + + print CONFIG_DIR + + if not path.exists(CONFIG_DIR): + # combox not configured. + config_cb(CONFIG_DIR) + try: + config = yaml.load(file(config_file, 'r')) + except yaml.YAMLError, exc: + print "Error opening configuration file:", exc + exit(1) + + # run combox. + run_cb(config) -- cgit v1.2.3