blob: f0c24ce1446481c687dcee5368118177da3a49e0 [file] [log] [blame]
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# reclass — recursive external node classifier
#
# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
# Released under the terms of the Artistic Licence 2.0
#
__prog__ = 'reclass'
__description__ = 'classify nodes based on an external data source'
__version__ = '1.0'
__author__ = 'martin f. krafft <madduck@madduck.net>'
__copyright__ = 'Copyright © 2007–13 ' + __author__
__licence__ = 'Artistic Licence 2.0'
import sys, os, posix, time
import config
from output import OutputLoader
from storage import StorageBackendLoader
def get_options(config_file=None):
return config.get_options(__name__, __version__, __description__, config_file)
def get_data(storage_type, nodes_uri, classes_uri, node):
storage_class = StorageBackendLoader(storage_type).load()
storage = storage_class(os.path.abspath(os.path.expanduser(nodes_uri)),
os.path.abspath(os.path.expanduser(classes_uri)))
if node is False:
ret = storage.inventory()
else:
ret = storage.nodeinfo(node)
ret['RECLASS']['timestamp'] = time.strftime('%c')
return ret
def output(data, fmt, pretty_print=False):
output_class = OutputLoader(fmt).load()
outputter = output_class()
return outputter.dump(data, pretty_print=pretty_print)
if __name__ == '__main__':
__name__ = __prog__
config_file = None
for d in (os.getcwd(), os.path.dirname(sys.argv[0])):
f = os.path.join(d, __name__ + '-config.yml')
if os.access(f, os.R_OK):
config_file = f
break
options = get_options(config_file)
data = get_data(options.storage_type, options.nodes_uri,
options.classes_uri, options.node)
print output(data, options.output, options.pretty_print)
sys.exit(posix.EX_OK)