martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame^] | 1 | #!/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 | |
| 16 | import sys, os, posix, time |
| 17 | import config |
| 18 | from output import OutputLoader |
| 19 | from storage import StorageBackendLoader |
| 20 | |
| 21 | def get_options(config_file=None): |
| 22 | return config.get_options(__name__, __version__, __description__, config_file) |
| 23 | |
| 24 | def 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 | |
| 36 | def 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 | |
| 41 | if __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) |