-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplates.py
More file actions
70 lines (61 loc) · 2.58 KB
/
templates.py
File metadata and controls
70 lines (61 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import objects
class Type:
SEARCH='search'
MODIFY='modify'
CREATE='create'
class Template:
def __init__(self, template_type, template_dict=None):
base = os.path.dirname(os.path.abspath(__file__))
if template_type == Type.SEARCH:
with open(base+'/templates/search_template.xml', 'r') as template_file:
self.template_content = template_file.read()
elif template_type == Type.MODIFY:
with open(base+'/templates/modify_template.xml', 'r') as template_file:
self.template_content = template_file.read()
elif template_type == Type.CREATE+objects.Type.USER:
template_type = Type.CREATE
with open(base+'/templates/create_user_template.xml', 'r') as template_file:
self.template_content = template_file.read()
elif template_type == Type.CREATE+objects.Type.ROLE:
template_type = Type.CREATE
with open(base+'/templates/create_role_template.xml', 'r') as template_file:
self.template_content = template_file.read()
getattr(self, '_Template__'+template_type)(template_dict)
def __craft_template(self, payload):
self.payload = self.template_content.replace('%payload%', payload)
def __search(self, template_dict):
payload = ''
# Empty query, search all
if template_dict is None:
return self.__craft_template(payload)
else:
payload = '<filter>'
payload = payload + '<q:' + template_dict['search_operator'] +'>'
for (path, value) in template_dict['search_filter'].iteritems():
payload = payload + '<q:equal><q:path>{0}</q:path><q:value>{1}</q:value></q:equal>'.format(path,value)
payload = payload + '</q:' + template_dict['search_operator'] +'>'
payload = payload + '</filter>'
return self.__craft_template(payload)
#TODO add suport for multiple mods at once
def __modify(self, template_dict):
for (path, value) in template_dict['modification'].iteritems():
payload = '<apit:itemDelta><t:modificationType>{0}</t:modificationType><t:path>{1}</t:path><t:value>{2}</t:value></apit:itemDelta>'.format(template_dict['modification_type'], path, value)
return self.__craft_template(payload)
def __create(self, template_dict):
extension = {}
payload = ''
for (attr, value) in template_dict.iteritems():
if 'extension' in attr:
extension[attr.split('/')[1]] = value
else:
payload += '<' + attr + '>' + value + '</' + attr + '>'
if extension != {}:
payload += '<extension>'
for (attr, value) in extension.iteritems():
payload += '<' + attr + '>' + value + '</' + attr + '>'
if extension != {}:
payload += '</extension>'
return self.__craft_template(payload)
def get_payload(self):
return self.payload