#! /usr/bin/env python # # Copyright(C) 2006 INL # Written by Jean Gillaux <jean@inl.fr> # # This program 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, version 3 of the License. # # This program 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 this program; if not, see <http://www.gnu.org/licenses/>. # # """Generates nuauth periods xml file from nuface acls file Nuperiod takes an nufw acls files as inputs and generates a periods xml file in nuauth format. """ __version__ = '0.2' __revision__ = '' __author__ = 'Jean Gillaux' __copyright__ = 'Copyright 2006, INL' from nupyf.nuxml import parse_periodicities, parse_durations from optparse import OptionParser from sys import argv, stdin, stderr, exit, stdout from xml.dom import minidom DAYS_T = 'days' HOURS_T = 'hours' DATES_T = 'dates' def period_elt_to_dom(dom, elt, type, doc_dom): """ transform an elt from acls xml file to dom objects """ if type == "duration": node = doc_dom.createElement("duration") node.setAttribute('length', elt.get("duration")) dom.appendChild(node) else: node = doc_dom.createElement("days") node.setAttribute('start', elt.get('startday')) node.setAttribute('end', elt.get('stopday')) dom.appendChild(node) node = doc_dom.createElement("hours") node.setAttribute('start', elt.get('starthour')) node.setAttribute('end', elt.get('stophour')) dom.appendChild(node) def period_group_to_dom(periodicities, doc_dom): """ transform a period from acls xml file to dom objects """ period_n = doc_dom.createElement('period') period_n.setAttribute('name', periodicities.name) for eltset in periodicities.iter_eltsets(): perioditem_n = doc_dom.createElement('perioditem') for elt in eltset: period_elt_to_dom(perioditem_n, elt, "periodicities", doc_dom) period_n.appendChild(perioditem_n) return period_n def duration_group_to_dom(durations, doc_dom): """ transform a period from acls xml file to dom objects """ period_n = doc_dom.createElement('period') period_n.setAttribute('name', durations.name) for eltset in durations.iter_eltsets(): perioditem_n = doc_dom.createElement('perioditem') for elt in eltset: period_elt_to_dom(perioditem_n, elt, "duration", doc_dom) period_n.appendChild(perioditem_n) return period_n def period_group_list_to_dom(periodicities, durations, version='1.0'): """ transform a period list from acls xml file to dom objects """ impl = minidom.getDOMImplementation() doc_dom = impl.createDocument(None, "periods", None) root = doc_dom.documentElement root.setAttribute('version', version) for eltgroup in periodicities.get_eltgrps(): period_n = period_group_to_dom(eltgroup, doc_dom) root.appendChild(period_n) for eltgroup in durations.get_eltgrps(): period_n = duration_group_to_dom(eltgroup, doc_dom) root.appendChild(period_n) return doc_dom if __name__ == '__main__': usage = """ usage: %prog [options] for options that specify a file, - means stdin or stdout """ # parse arguments from command line parser = OptionParser(usage, version='%prog ' + __version__) parser.add_option('-f', '--file', dest='acls_file', help='path to acls file, - means stdin') parser.add_option('-o', '--output', dest='out_file', help='output file to write nuauth periods in, - means stdout') parser.add_option('--xmlver', dest='periods_version', help='version number of xml file schema') parser.set_defaults(acls_file='-', out_file='-', periods_version='1.0') (options, args)=parser.parse_args(argv) # set input/output files file_h = None if options.acls_file == '-': file_h = stdin else: try: file_h = file(options.acls_file) except IOError,e : stderr.write("Unable to open %s for reading"%options.acls_file) exit(1); file_out = None if options.out_file == '-': file_out = stdout else: try: file_out = file(options.out_file, 'w') except IOError, e: stderr.write("Unable to open %s for writing"%options.out_file) exit(2); #parse input file try: doc = minidom.parse(file_h) periodicities = parse_periodicities(doc) durations = parse_durations(doc) except Exception, err: print "error loading acls file: %s" % err exit(3) # Resolv operators periodicities.normall() durations.normall() # Create XML tree xml = period_group_list_to_dom(periodicities, durations, options.periods_version) # Write nuauth xml file periods file_out.write(xml.toprettyxml())