aboutsummaryrefslogtreecommitdiff
blob: 76ecf82484be68f79a3d2740f96e35f86466f341 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    g_sorcery.py
    ~~~~~~~~~~~~
    
    the main module
    
    :copyright: (c) 2013 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""


import importlib
import os
import sys

from .compatibility import configparser
from .fileutils import FileJSON
from .exceptions import FileJSONError
from .logger import Logger

def main():
    logger = Logger()
    if len(sys.argv) < 2:
        logger.error("no backend specified")
        return -1
    name = sys.argv[1]
    cfg = name + '.json'
    cfg_path = None
    for path in '.', '~', '/etc/g-sorcery':
        current = os.path.join(path, cfg)
        if (os.path.isfile(current)):
            cfg_path = path
            break
    if not cfg_path:
        logger.error('no config file for ' + name + ' backend\n')
        return -1
    cfg_f = FileJSON(cfg_path, cfg, ['package'])
    try:
        config = cfg_f.read()
    except FileJSONError as e:
        logger.error('error loading config file for ' \
                     + name + ': ' + str(e) + '\n')
        return -1
    backend = get_backend(config['package'])

    config_file = None
    for path in '.', '~', '/etc/g-sorcery':
        config_file = os.path.join(path, "g-sorcery.cfg")
        if (os.path.isfile(config_file)):
            break
        else:
            config_file = None

    if not config_file:
        logger.error('no global config file\n')
        return -1
    
    global_config = configparser.ConfigParser()
    global_config.read(config_file)

    return backend.instance(sys.argv[2:], config, global_config)


def get_backend(package):
    """
    Load backend by package name.
    """
    logger = Logger()
    try:
        module = importlib.import_module(package + '.backend')
    except ImportError as error:
        logger.error("error importing backend: " + str(error))
        return None
    
    return module


if __name__ == "__main__":
    sys.exit(main())