-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentities_to_java_breaked.py
179 lines (117 loc) · 4.51 KB
/
entities_to_java_breaked.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu
import os
os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get('DJANGO_SETTINGS_MODULE',
'settings')
# from bolibana.models import EntityType
from unfpa_core.models import UEntity
header = """
package unfpa;
import java.util.Hashtable;
"""
header2 = """
/**
* List of static codes and names for Entities/Locations
* Automatically generated.
* @author reg
*/
public class StaticCodes {
public Hashtable cercles = new Hashtable();
public Hashtable names = new Hashtable();
public StaticCodes() {"""
header_cercle = """
package unfpa;
import java.util.Hashtable;
/**
* List of static codes and names for Entities/Locations
* Automatically generated.
* @author reg
*/
"""
function_intro = """
public static Hashtable ht() {"""
function_outro = """
return ht;"""
commune_intro = """
// COMMUNES HASH TABLES
// contains a list of vil_code/vil_name for each village in commune."""
cercle_intro = """
// CERCLES HASH TABLES
// contains a list of com_code/com_ht for each commune in cercle."""
main_intro = """
// MAIN HASH TABLE
// contains a list of cercle_code/cercle_ht for each cercle."""
names_intro = """
// NAMES HASH TABLE
// contains names of code/name for all communes and cercles"""
footer = """
}
}"""
FILE_HANDLERS = {}
def println(text, file=None):
text = u"%s\n" % text
FILE_HANDLERS[file].write(text.encode('utf-8'))
# print(text.encode('utf-8'))
def open_fh():
# create file handlers
FILE_HANDLERS[None] = open('StaticCodes.java', 'w+')
for cercle in UEntity.objects.filter(type__slug='cercle'):
FILE_HANDLERS[cercle.slug] = open('StaticCodes%(slug)s.java'
% {'slug': cercle.slug}, 'w+')
def close_fh():
for f in FILE_HANDLERS.values():
f.close()
# create files
open_fh()
println(header)
for cercle in UEntity.objects.filter(type__slug='cercle'):
println(u"import unfpa.StaticCodes%(slug)s.*;" % {'slug': cercle.slug})
println(header2)
println(main_intro)
for cercle in UEntity.objects.filter(type__slug='cercle'):
# println(u"\t\tcercles.put(\"%(slug)s\", %(slug)s_ht); // %(name)s"
# % {'slug': cercle.slug, 'name': cercle.name.title()})
println(u"\t\tcercles.put(\"%(slug)s\", StaticCodes%(slug)s.ht()); // %(name)s"
% {'slug': cercle.slug, 'name': cercle.name.title()})
println(names_intro)
for entity in UEntity.objects.filter(type__slug__in=('cercle', 'commune')):
println(u"\t\tnames.put(\"%(slug)s\", \"%(name)s\");"
% {'slug': entity.slug, 'name': entity.name.title()})
println(footer)
######################## cercle files
for cercle in UEntity.objects.filter(type__slug='cercle'):
println(header_cercle, cercle.slug)
println(u"public class StaticCodes%(slug)s {" % {'slug': cercle.slug}, cercle.slug)
println(function_intro, cercle.slug)
println(u"\t\tHashtable ht = new Hashtable();", cercle.slug)
println(commune_intro, cercle.slug)
# loop on communes to add villages
for commune in cercle.get_children():
println(u"", cercle.slug)
println(u"\t\t// %s" % commune.name.title(), cercle.slug)
println(u"\t\tHashtable %s_ht = new Hashtable();" % commune.slug, cercle.slug)
for village in commune.get_children():
println(u"\t\t%(com_slug)s_ht.put(\"%(vil_slug)s\", \"%(vil_name)s\");"
% {'com_slug': commune.slug,
'vil_name': village.name.title(),
'vil_slug': village.slug}, cercle.slug)
println(u"\t\tht.put(\"%(com_slug)s\", %(com_slug)s_ht);"
u" // %(com_name)s"
% {'c_slug': cercle.slug,
'com_slug': commune.slug,
'com_name': commune.name.title()}, cercle.slug)
println(function_outro, cercle.slug)
# println(u"")
# println(u"\t\t// %s" % cercle.name.title())
# println(u"\t\tHashtable %s_ht = new Hashtable();" % cercle.slug)
# for commune in cercle.get_children():
# println(u"\t\t%(c_slug)s_ht.put(\"%(com_slug)s\", %(com_slug)s_ht);"
# u" // %(com_name)s"
# % {'c_slug': cercle.slug,
# 'com_slug': commune.slug,
# 'com_name': commune.name.title()})
println(footer, cercle.slug)
###################################
# close files
close_fh()