forked from SaltaIT/python-piman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
siteppgen.py
111 lines (84 loc) · 3.51 KB
/
siteppgen.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
from __future__ import print_function
"""
site.pp generator
"""
import os
import sys
import json
import inflect
import argparse
from configparser import SafeConfigParser
debug = False
write_to = sys.stdout
def eprint(*args, **kwargs):
global debug
if debug:
print(*args, file=sys.stderr, **kwargs)
def print_resource(resource_name, resource_alias, strategy='deep'):
global debug, write_to
# lookup( <NAME>, [<VALUE TYPE>], [<MERGE BEHAVIOR>], [<DEFAULT VALUE>] )
print("", file=write_to)
# print("$resource_alias = lookup('"+resource_alias+"', undef, '"+strategy+"', {})", file=write_to)
print("$"+resource_alias+" = lookup("+resource_alias+", Hash, "+strategy+", {})", file=write_to)
# create_resources(postgresql::schema, $postgresschemas)
print("create_resources("+resource_name+", $"+resource_alias+")", file=write_to)
def generatesitepp(config_file, write_sitepp_to=sys.stdout):
global debug, write_to
write_to=write_sitepp_to
config = SafeConfigParser()
config.read(config_file)
try:
debug = config.getboolean('sitegen', 'debug')
except:
debug = False
try:
resource_file = config.get('sitegen', 'resource-file').strip('"').strip("'").strip()
except:
resource_file = "./siteppgen/resource.list"
try:
resource_hash = json.loads(config.get('sitegen','resource-hash'))
except:
resource_hash = {}
try:
deep_include_classes = json.loads(config.get('sitegen','deep-include-classes'))
except:
deep_include_classes = []
# resource_hash
for resource_alias in resource_hash:
# print resource hash
print_resource(resource_hash[resource_alias], resource_alias)
# resource_file
if not os.path.isfile(resource_file):
eprint("WARNING: resource-file ("+resource_file+") not found, ignoring resources")
with open(resource_file) as resource_file_handler:
resource_name = resource_file_handler.readline().rstrip(os.linesep).strip('"').strip("'").strip()
while resource_name:
resource_alias = resource_name.replace(':','').strip()+"s"
print_resource(resource_name, resource_alias)
p = inflect.engine()
plural_resource_alias = p.plural(resource_name.replace(':','').strip())
if plural_resource_alias != resource_alias:
print_resource(resource_name, plural_resource_alias)
resource_name = resource_file_handler.readline().rstrip(os.linesep).strip('"').strip("'").strip()
for resource_alias in config.sections():
if resource_alias!="sitegen":
try:
merge_strategy = config.get(resource_alias, 'merge-strategy').strip('"').strip("'").strip()
except:
merge_strategy = 'deep'
try:
resource_name = config.get(resource_alias, 'resource-name').strip('"').strip("'").strip()
print_resource(resource_name, resource_alias, merge_strategy)
except:
eprint("WARNING: skipping "+resource_alias+": resource-name not found")
# lookup('classes', Array[String], 'deep').include
for deep_include_class in deep_include_classes:
print("", file=write_to)
print("lookup('"+deep_include_class+"', Array[String], 'deep').include", file=write_to)
if __name__ == '__main__':
try:
config_file = sys.argv[1]
except IndexError:
config_file = './siteppgen.config'
generatesitepp(config_file=config_file)