blob: f0c24ce1446481c687dcee5368118177da3a49e0 [file] [log] [blame]
martin f. krafftf37f0682013-06-14 16:36:20 +02001#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# reclass — recursive external node classifier
5#
6# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
7# Released under the terms of the Artistic Licence 2.0
8#
9__prog__ = 'reclass'
10__description__ = 'classify nodes based on an external data source'
11__version__ = '1.0'
12__author__ = 'martin f. krafft <madduck@madduck.net>'
13__copyright__ = 'Copyright © 2007–13 ' + __author__
14__licence__ = 'Artistic Licence 2.0'
15
16import sys, os, posix, time
17import config
18from output import OutputLoader
19from storage import StorageBackendLoader
20
21def get_options(config_file=None):
22 return config.get_options(__name__, __version__, __description__, config_file)
23
24def get_data(storage_type, nodes_uri, classes_uri, node):
25 storage_class = StorageBackendLoader(storage_type).load()
26 storage = storage_class(os.path.abspath(os.path.expanduser(nodes_uri)),
27 os.path.abspath(os.path.expanduser(classes_uri)))
28 if node is False:
29 ret = storage.inventory()
30 else:
31 ret = storage.nodeinfo(node)
32 ret['RECLASS']['timestamp'] = time.strftime('%c')
33
34 return ret
35
36def output(data, fmt, pretty_print=False):
37 output_class = OutputLoader(fmt).load()
38 outputter = output_class()
39 return outputter.dump(data, pretty_print=pretty_print)
40
41if __name__ == '__main__':
42 __name__ = __prog__
43 config_file = None
44 for d in (os.getcwd(), os.path.dirname(sys.argv[0])):
45 f = os.path.join(d, __name__ + '-config.yml')
46 if os.access(f, os.R_OK):
47 config_file = f
48 break
49 options = get_options(config_file)
50 data = get_data(options.storage_type, options.nodes_uri,
51 options.classes_uri, options.node)
52 print output(data, options.output, options.pretty_print)
53 sys.exit(posix.EX_OK)