source: cows_wps/trunk/render_policy_xml_file.py @ 7157

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/render_policy_xml_file.py@7157
Revision 7157, 2.1 KB checked in by astephen, 9 years ago (diff)

Added script and templates to create policy.xml file.

Line 
1#!/usr/bin/env python
2
3"""
4render_policy_xml_file.py
5=========================
6
7Uses the following configuration files to render the POLICY_FILE for
8NDG security.
9"""
10
11# Standard library imports
12import ConfigParser
13import os
14
15# Local imports
16#import cows_wps.renderer.policy_file_renderer as pfr
17
18roles_config = "roles.ini"
19policy_rules_config = "policy_rules.ini"
20POLICY_XML = "policy-test.xml"
21policy_template = "templates/policy_template.xml"
22
23
24def readConfig(cfile, as_ints = False):
25    """
26    Reads a config file and returns a dictionary of {key: value}
27    """
28    c = ConfigParser.ConfigParser()
29    c.read(cfile)
30
31    cd = {}
32
33    for (key, value) in c.defaults().items():
34
35        if as_ints == True:
36            value = int(value)
37 
38        cd[key] = value
39
40    return cd
41
42def readRolesConfig(cfile = "roles.ini"):
43    return readConfig(cfile, as_ints = True)
44
45def readPolicyRulesConfig(cfile = "policy_rules.ini"):
46    return readConfig(cfile)
47
48def renderPolicyFile(roles_conf, policy_conf, policy_file):
49    policy_template = "cows_wps/templates/policy_template.xml"
50#    tmpl = genshi.load_template(policy_template)
51#    genshi.render(tmpl, roles_conf, policy_conf)
52
53    pt = open(policy_template).read()
54
55    pol_tmpl = """      <role_mapper>
56                <procID>%s</procID>
57                <roleID>%s</roleID>
58                <roleDataset>%s</roleDataset>
59        </role_mapper>
60"""
61
62    pf = open(policy_file, "w")
63
64    ordered_proc_ids = policy_conf.keys()
65    ordered_proc_ids.sort()
66
67#    role_keys = [i[1] for i in ordered_roles]
68    xml = ""
69
70    for proc_id in ordered_proc_ids:
71
72        roles = policy_conf[proc_id].split()
73       
74        for role in roles:
75            role_map = roles_conf[role]
76
77            xml += pol_tmpl % (proc_id, role_map, role)
78
79    policy = pt.replace("INSERT", xml)
80    pf.write(policy)
81    pf.close()
82
83    print "Wrote: %s" % policy_file
84       
85       
86def main():
87    roles_conf = readRolesConfig()
88    policy_conf = readPolicyRulesConfig()
89    renderPolicyFile(roles_conf, policy_conf, policy_file = POLICY_XML)
90
91
92if __name__ == "__main__":
93
94    main()
Note: See TracBrowser for help on using the repository browser.