summaryrefslogtreecommitdiffstats
path: root/combox/cbox.py
blob: f49ca3f51d119062019bab44788b4a52ad3cab24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
#
#    Copyright (C) 2015 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
#   <http://www.gnu.org/licenses/>.

import os
import time
import yaml

from argparse import ArgumentParser
from os import path
from os.path import expanduser
from sys import exit
from threading import Lock
from watchdog.observers import Observer

from combox.config import config_cb
from combox.events import ComboxDirMonitor


## 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'])
    db_lock = Lock()
    event_handler = ComboxDirMonitor(config, db_lock)

    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()


def main():
    """
    Main functions, parses args and calls run_cb()
    """
    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(expanduser("~"),'.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)


if __name__ == "__main__":
    main()