/
backxml.py
1139 lines (1104 loc) · 41.7 KB
/
backxml.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import codecs
from linecache import getline
import os
import re
import xml.etree.ElementTree as etree
import common
from debug import (printPretty,lineno,showConfig)
from globdata import (config,worldList,cities,people,places,states,placeList)
from status import status
import xmlout
locs = {}
lockeys = {}
statekeys = [{},{}]
def getCitiesIn(state):
if state.find(".xml") > -1: state = state.split('.')[0]
if state:
data = placeList.get(state)
if data is not None:
keys = data.keys()
if keys is not None:
return data
else:
common.bsay("?","State %s is empty in placeList" % state)
else:
common.bsay("?","State %s not found in placeList" % state)
return {}
def getCityList(order):
global locs
global lockeys
if not len(worldList): populateWorld()
# else:
# print len(worldList['c'])
if order == 0:
if not len(locs):
for city in worldList['c']:
cityloc = [None,None,None]
if len(city) > 0: cityloc = getCityLoc(city)
if cityloc:
locs[city] = cityloc
if cityloc[0] is not None and cityloc[1] is not None and cityloc[2] is not None:
(cityname,state,statename) = cityloc
if state is not None and city is not None: pushLoc(state,statename,city,cityname)
# printPretty([lineno(),locs],length=3,quiet=False)
return locs
elif order == 1:
if not len(locs): getCityList(0)
if not len(lockeys):
for loc in locs:
cityloc = [None,None,None]
if len(loc) > 0: cityloc = getCityLoc(loc)
if cityloc[0] is not None and cityloc[2] is not None:
lockeys["%s, %s" % (cityloc[0],cityloc[2])] = loc
# printPretty([lineno(),locs],length=3,quiet=False)
return lockeys
def getCityLoc(fileid):
root = etree.Element("place")
fn = os.path.join(config['realmdir'],"%s.xml" % fileid)
status.push(0,"reading city location from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print " Could not open configuration file: %s" % e
cityname = None
statename = None
statefile = None
if root.find("name") is not None: cityname = root.find("name").text.strip()
if root.find("state") is not None: statename = root.find("state").text.strip()
if root.find("statefile") is not None: statefile = root.find("statefile").text.strip()
return [cityname,statefile,statename]
def getPlaceList(pretty):
if pretty:
printPretty(placeList)
else:
print placeList
return True
def getPlacesIn(city):
state = common.getInf(cities.get(city),["info","statefile"],None)
if state.find(".xml") > -1: state = state.split('.')[0]
if city.find(".xml") > -1: city = city.split('.')[0]
if state:
data = placeList.get(state)
if data is not None:
data = placeList[state].get(city)
if data is not None:
return data
else:
common.say("City %s not found in placeList" %city)
else:
common.say("State %s not found in placeList" % state)
return {}
def getStateList(order):
global states
global statekeys
if not len(worldList): populateWorld()
# else:
# print len(worldList['c'])
if order == 0:
if not len(statekeys[0]):
if config['debug'] > 3: printPretty([__name__,lineno(),worldList])
for state in worldList['s']:
statename = None
if len(state) > 0: statename = getStateName(state)
if statename:
if config['debug'] > 3: print "%s %s" % (lineno(),statename)
statekeys[0][state] = statename
pushLoc(state,statename)
return statekeys[0]
elif order == 1:
if not len(statekeys[0]): getStateList(0)
if not len(statekeys[1]):
for state in statekeys[0]:
statename = None
if len(state) > 0: statename = getStateName(state)
if statename:
statekeys[1]["%s" % statename] = state
return statekeys[1]
def getStateName(fileid):
root = etree.Element("state")
fn = os.path.join(config['realmdir'],fileid + ".xml")
status.push(0,"reading city location from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print " Could not open configuration file: %s" % e
if root.find("name") is None: return ""
statename = root.find("name").text.strip()
return statename
def idExists(fileid):
global config
if config['debug'] > 3: print "seeking %s..." % os.path.join(os.path.abspath(config['realmdir']),"%s.xml" % fileid),
return os.path.exists(os.path.join(os.path.abspath(config['realmdir']),fileid + ".xml"))
def listThings(pretty):
if pretty:
printPretty(people)
printPretty(places)
printPretty(cities)
printPretty(states)
showConfig()
else:
print people
print places
print cities
print states
print config
getPlaceList(pretty)
return True
def loadCity(fileid):
"""Given an id (filename) matching an XML file in the appropriate
directory, loads the tree from the file and pushes its data into
two dictionaries, which it returns as a tuple.
"""
dinf = {}
root = etree.Element("city")
text = None
statename = ""
statefile = ""
cityname = ""
dinf['m'] = {}
dinf['m']['events'] = {}
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["name","state","statefile","start","scue","end","ecue","place","aspects"]
for tag in tags:
dinf[tag] = ["",False]
dinf['aspects'] = {}
if not dinf.get("places"): dinf['places'] = {}
if not idExists(fileid):
status.push(0,"new city created... '%s'" % fileid)
return dinf
fn = os.path.join(config['realmdir'],"%s.xml" % fileid)
status.push(0,"loading city from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print "c: Could not open configuration file: %s" % e
ir = 0
for i in range(len(root)):
if root[i].tag is not None:
if root[i].tag == "place":
if len(root[i]) > 0:
node = ""
node = root[i].find("file")
if node.text:
node = node.text.strip()
node = common.validateFileid(node)
dinf['places'][node] = {}
for j in root[i]:
if j.tag and j.text and j.tag != "file":
dinf['places'][node][j.tag] = [j.text.strip(),False]
if config['debug'] > 3: print dinf['places'][node]
else:
if config['debug'] > 0:
print "Invalid place tag:"
for c in root[i]:
print c.tag + ': ' + c.text,
else: # no relat length
if config['debug'] > 0: print "Empty place tag."
elif root[i].tag == "events":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['m']['events']))
dinf['m']['events'][k] = {}
for j in node:
if j.tag and j.text:
dinf['m']['events'][k][j.tag] = [j.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid milestone tag:"
for c in node:
print c.tag + ': ' + c.text,
if config['debug'] > 3: printPretty(dinf['m']['events'])
else: # no relat length
if config['debug'] > 0: print "Empty milestone tag."
elif root[i].tag == "aspects":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['aspects']))
dinf['aspects'][k] = {}
if node.tag and node.text:
dinf['aspects'][k] = [node.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid aspects tag:"
print node.tag + ': ' + node.text,
else: # no aspects length
if config['debug'] > 0: print "Empty aspects tag."
elif root[i].text is not None:
if root[i].tag == "statefile":
statefile = root[i].text.strip()
statefile = common.validateFileid(statefile)
if statefile is None: statefile = ""
elif root[i].tag == "state":
statename = root[i].text.strip()
elif root[i].tag == "name":
cityname = root[i].text.strip()
dinf[root[i].tag] = [root[i].text.strip(), False]
if config['debug'] > 2: print str(i) + " ",
if len(statefile) > 0: pushLoc(statefile,statename,fileid,cityname)
return dinf
def loadPerson(fileid):
"""Given an id (filename) matching an XML file in the appropriate
directory, loads the tree from the file and pushes its data into
two dictionaries, which it returns as a tuple.
"""
dinf = {}
drel = {}
root = etree.Element("person")
text = None
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["commonname", "ctitle", "gname", "mname", "fname", "nname", "nameorder", "gender", "bday", "dday", "stories", "mention", "appear1ch", "appear1wr", "conflict", "leadrel", "bodytyp", "age", "skin", "eyes", "hair", "dmarks", "dress", "attposs", "asmell", "personality", "speech", "formocc", "currocc", "strength", "weak", "mole", "hobby", "misc", "ethnic", "origin", "backstory", "residence", "minchar", "talent", "abil", "sgoal", "other", "relat", "aspects", "update"]
tags.remove("currocc")
tags.remove("formocc")
tags.remove("relat")
tags.append("file")
for tag in tags:
dinf[tag] = ["",False]
dinf['currocc'] = {}
dinf['currocc']['pos'] = ["",False]
dinf['formocc'] = {}
dinf['formocc']['pos'] = ["",False]
dinf['aspects'] = {}
events = {}
events['0'] = {}
events['0']['date'] = ["",False]
events['0']['event'] = ["",False]
dinf['currocc']['events'] = events
dinf['formocc']['events'] = events
if not idExists(fileid):
status.push(0,"new person created... '%s'" % fileid)
return (dinf,drel)
fn = os.path.join(config['realmdir'],fileid + ".xml")
status.push(0,"loading person from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print " Could not open configuration file: %s" % e
ir = 0
for i in range(len(root)):
if root[i].tag is not None:
if root[i].tag == "relat":
if len(root[i]) > 0:
node = ""
node = root[i].find("file")
if node is not None and node.text:
node = node.text.strip()
node = common.validateFileid(node)
drel[node] = {}
for j in root[i]:
if j.tag == "events":
if not drel[node].get('events'): drel[node]['events'] = {}
for k in j:
stone = str(len(drel[node]['events']))
drel[node]['events'][stone] = {}
for m in k:
if m.tag and m.text:
drel[node]['events'][stone][m.tag] = [m.text.strip(),False]
else: # elif j.tag != "file":
if j.tag and j.text:
drel[node][j.tag] = [j.text.strip(),False]
if config['debug'] > 3: print drel[node]
else:
if config['debug'] > 0:
print "Invalid relat tag:"
for c in root[i]:
print c.tag
else: # no relat length
if config['debug'] > 0: print "Empty relat tag."
elif root[i].tag == "currocc":
# print ",",
dinf['currocc'] = {}
try:
dinf['currocc']['pos'] = [root[i].find("pos").text.strip(),False]
except AttributeError:
del dinf['currocc']
if dinf.get('currocc'):
events = {}
if len(root[i]) > 1:
for j in root[i]:
if j.tag is not None:
if j.tag == "events":
for k in j:
if k.tag == "mstone":
le = str(len(events))
events[le] = {}
events[le]['date'] = ["",False]
events[le]['event'] = ["",False]
for m in k:
if m.tag and m.text:
events[le][m.tag] = [m.text.strip(),False]
else:
events['0'] = {}
events['0']['date'] = ["",False]
events['0']['event'] = ["",False]
dinf['currocc']['events'] = events
else:
dinf['currocc'] = {}
dinf['currocc']['pos'] = ["",False]
events = {}
events['0'] = {}
events['0']['date'] = ["",False]
events['0']['event'] = ["",False]
dinf['currocc']['events'] = events
elif root[i].tag == "formocc":
# print ",",
dinf['formocc'] = {}
try:
dinf['formocc']['pos'] = [root[i].find("pos").text.strip(),False]
except AttributeError:
del dinf['formocc']
if dinf.get('formocc'):
events = {}
if len(root[i]) > 1:
for j in root[i]:
if j.tag is not None:
if j.tag == "events":
for k in j:
if k.tag == "mstone":
le = str(len(events))
events[le] = {}
events[le]['date'] = ["",False]
events[le]['event'] = ["",False]
for m in k:
if m.tag and m.text:
events[le][m.tag] = [m.text.strip(),False]
else:
events['0'] = {}
events['0']['date'] = ["",False]
events['0']['event'] = ["",False]
dinf['formocc']['events'] = events
else:
dinf['formocc'] = {}
dinf['formocc']['pos'] = ["",False]
events = {}
events['0'] = {}
events['0']['date'] = ["",False]
events['0']['event'] = ["",False]
dinf['formocc']['events'] = events
elif root[i].tag == "aspects":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['aspects']))
dinf['aspects'][k] = {}
if node.tag and node.text:
dinf['aspects'][k] = [node.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid aspects tag:"
print node.tag + ': ' + node.text,
else: # no aspects length
if config['debug'] > 0: print "Empty aspects tag."
elif root[i].text is not None:
# print ".",
dinf[root[i].tag] = [root[i].text.strip(), False]
if config['debug'] > 2: print str(i) + " ",
# print str(dinf)
return (dinf,drel)
def loadPlace(fileid):
"""Given an id (filename) matching an XML file in the appropriate
directory, loads the tree from the file and pushes its data into
two dictionaries, which it returns as a tuple.
"""
dinf = {}
drel = {}
root = etree.Element("place")
text = None
city = ""
cityf = ""
state = ""
statef = ""
placename = ""
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["commonname","name","start","scue","end","ecue","stories","mention","desc","address","loc","locfile","state","statefile","note", "relat","update"]
tags.remove("note")
tags.remove("relat")
tags.append("file")
for tag in tags:
dinf[tag] = ["",False]
# if no relations or notes, leave blank
dinf['aspects'] = {}
if not idExists(fileid):
status.push(0,"new place created... '%s'" % fileid)
return (dinf,drel)
fn = os.path.join(config['realmdir'],"%s.xml" % fileid)
status.push(0,"loading place from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print " Could not open place file: %s" % e
ir = 0
for i in range(len(root)):
if root[i].tag is not None:
if root[i].tag == "relat":
node = ""
try:
node = root[i].find("file").text.strip()
except AttributeError:
common.bsay("?","XML formatting error in %s! Probably an empty relat tag." % fileid)
node = common.validateFileid(node)
drel[node] = {}
for j in root[i]:
if j.tag == "events":
if not drel[node].get('events'): drel[node]['events'] = {}
for k in j:
stone = str(len(drel[node]['events']))
drel[node]['events'][stone] = {}
for m in k:
if m.tag and m.text:
drel[node]['events'][stone][m.tag] = [m.text.strip(),False]
else: # elif j.tag != "file":
if j.tag and j.text:
drel[node][j.tag] = [j.text.strip(),False]
if config['debug'] > 3: print drel[node]
elif root[i].tag == "note":
# print ",",
if not dinf.get("notes"):
dinf['notes'] = {}
x = str(len(dinf['notes']))
dinf['notes'][x] = {}
try:
dinf['notes'][x]['content'] = [root[i].find("content").text.strip(),False]
except AttributeError:
del dinf['notes'][x]
if dinf['notes'].get(x):
dinf['notes'][x]['date'] = [root[i].find("date").text.strip(),False]
# elif root[i].tag == "formocc":
# print ",",
elif root[i].tag == "aspects":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['aspects']))
dinf['aspects'][k] = {}
if node.tag and node.text:
dinf['aspects'][k] = [node.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid aspects tag:"
print node.tag + ': ' + node.text,
else: # no aspects length
if config['debug'] > 0: print "Empty aspects tag."
elif root[i].text is not None:
if root[i].tag == "statefile":
statef = root[i].text.strip()
statef = common.validateFileid(statef)
if statef is None: statef = ""
elif root[i].tag == "state":
state = root[i].text.strip()
elif root[i].tag == "locfile":
cityf = root[i].text.strip()
cityf = common.validateFileid(cityf)
if cityf is None: cityf = ""
elif root[i].tag == "loc":
city = root[i].text.strip()
elif root[i].tag == "name":
placename = root[i].text.strip()
dinf[root[i].tag] = [root[i].text.strip(), False]
if config['debug'] > 2: print str(i) + " ",
if len(statef) > 0 and len(cityf) > 0: pushLoc(statef,state,cityf,city,fileid,placename)
return (dinf,drel)
def loadState(fileid):
"""Given an id (filename) matching an XML file in the appropriate
directory, loads the tree from the file and pushes its data into
a dictionary.
"""
dinf = {}
root = etree.Element("state")
text = None
statename = ""
statefile = ""
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["name","start","scue","end","ecue","aspects","update"]
for tag in tags:
dinf[tag] = ["",False]
dinf['cities'] = {}
dinf['m'] = {}
dinf['m']['events'] = {}
dinf['aspects'] = {}
if not idExists(fileid):
status.push(0,"new state created... '%s'" % fileid)
return dinf
statefile = fileid
fn = os.path.join(config['realmdir'],"%s.xml" % fileid)
status.push(0,"loading state from XML... '%s'" % fn)
try:
with codecs.open(fn,'rU','utf-8') as f:
tree = etree.parse(f)
f.close()
root = tree.getroot()
except IOError as e:
print "c: Could not open configuration file: %s" % e
ir = 0
for i in range(len(root)):
if root[i].tag is not None:
if root[i].tag == "city":
if len(root[i]) > 0:
node = ""
node = root[i].find("file")
if node.text:
node = node.text.strip()
node = common.validateFileid(node)
dinf['cities'][node] = {}
for j in root[i]:
if j.tag and j.text and j.tag != "file":
dinf['cities'][node][j.tag] = [j.text.strip(),False]
if config['debug'] > 3: printPretty(dinf['cities'][node])
else:
if config['debug'] > 0:
print "Invalid city tag:"
for c in root[i]:
print c.tag + ': ' + c.text,
else: # no relat length
if config['debug'] > 0: print "Empty city tag."
elif root[i].tag == "events":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['m']['events']))
dinf['m']['events'][k] = {}
for j in node:
if j.tag and j.text:
dinf['m']['events'][k][j.tag] = [j.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid milestone tag:"
for c in node:
print c.tag + ': ' + c.text,
if config['debug'] > 3: printPretty(dinf['m']['events'])
else: # no relat length
if config['debug'] > 0: print "Empty milestone tag."
elif root[i].tag == "aspects":
if len(root[i]) > 0:
nodes = root[i]
for node in nodes:
k = str(len(dinf['aspects']))
dinf['aspects'][k] = {}
if node.tag and node.text:
dinf['aspects'][k] = [node.text.strip(),False]
else:
if config['debug'] > 0:
print "Invalid aspects tag:"
print node.tag + ': ' + node.text,
else: # no aspects length
if config['debug'] > 0: print "Empty aspects tag."
elif root[i].text is not None:
dinf[root[i].tag] = [root[i].text.strip(), False]
if config['debug'] > 2: print str(i) + " ",
statename = dinf.get("name","")
if len(statename) > 1: pushLoc(statefile,statename)
return dinf
def populateWorld():
"""Looks in the realmdir and makes a list of fileids the program can
load. Makes worldList a tuple of lists.
"""
global config
global worldList
fn = os.path.join(config['realmdir'],"myrealm.cfg")
persons = []
places = []
cities = []
states = []
orgs = []
# other data types.
(found,fn) = common.findFile(lineno(),fn)
if config['uselistfile'] and found:
print " Loading worldList from file..."
lines = []
try:
with codecs.open(fn,'rU','utf-8') as conf:
lines = conf.readlines()
conf.close()
except IOError as e:
print " Could not open worldlist file: %s" % e
exit(1)
for line in lines:
try:
line = line.strip()
if line:
values = [x.strip() for x in line.split('=')]
if values[0] == "persons":
y = values[1]
persons.extend(y.split(','))
elif values[0] == "places":
y = values[1]
places.extend(y.split(','))
elif values[0] == "cities":
y = values[1]
cities.extend(y.split(','))
elif values[0] == "states":
y = values[1]
states.extend(y.split(','))
elif values[0] == "orgs":
y = values[1]
orgs.extend(y.split(','))
elif values[0] == "person":
persons.append(values[1])
elif values[0] == "place":
places.append(values[1])
elif values[0] == "city":
cities.append(values[1])
elif values[0] == "state":
states.append(values[1])
elif values[0] == "org":
orgs.append(values[1])
else:
print "Unknown type %s found" % values[0]
except Exception as e:
print " There was an error in the configuration file: %s" % e
# elif not found:
# print " Fatal error. Realm directory %s does not exist! Exiting." % config['realmdir']
# exit(-1)
else:
print " Generating worldList from directory..."
olist = os.listdir(config['realmdir'])
nlist = []
ilist = []
for i in range(len(olist)):
if re.search(r'.[Xx][Mm][Ll]$',olist[i]):
ilist.append(os.path.splitext(olist[i])[0])
nlist.append(olist[i])
for i in range(len(nlist)):
fn = os.path.join(config['realmdir'],nlist[i])
line = ""; match = -1; lno = 2
while match == -1 and lno < 6:
line = getline(fn,lno)
match = line.find("SYSTEM")
lno += 1
match2 = line.find(".dtd")
if match != -1 and match2 > match:
match += 8 # trims 'SYSTEM "'
line = line[match:match2] # trims '.dtd">\n'
if "person" in line:
persons.append(ilist[i])
elif "place" in line:
places.append(ilist[i])
elif "city" in line:
cities.append(ilist[i])
elif "state" in line:
states.append(ilist[i])
elif "org" in line:
orgs.append(ilist[i])
else:
print "Unknown type %s found" % line
if config['debug'] > 2: print "\nPersons: %s\nPlaces: %s\nCities: %s\nStates: %s\nOrgs: %s\n" % (persons,places,cities,states,orgs)
worldList['p'] = persons
worldList['l'] = places
worldList['c'] = cities
worldList['s'] = states
worldList['o'] = orgs
for key in worldList.keys():
if len(worldList[key]):
if not len(worldList[key][0]):
worldList[key] = []
for s in worldList['s']:
pushLoc(s)
getStateList(0)
getCityList(0)
if config['debug'] > 3:
printPretty(worldList)
printPretty(placeList)
def pushLoc(statefile,statename = "",cityfile = "",cityname = "",placefile = "",placename = ""):
for f in [statefile,cityfile,placefile]:
if f is None:
f = ""
if config['debug'] > 3:
printPretty([statefile,cityfile,placefile])
if len(statefile) > 0:
if statefile.find(".xml") > -1: statefile = statefile.split('.')[0]
if not placeList.get(statefile): placeList[statefile] = {}
if statename is not None and len(statename) > 0:
placeList[statefile]['_name'] = statename
if len(cityfile) > 0:
if cityfile.find(".xml") > -1: cityfile = cityfile.split('.')[0]
if not placeList[statefile].get(cityfile): placeList[statefile][cityfile] = {}
if cityname is not None and len(cityname) > 0:
placeList[statefile][cityfile]['_name'] = cityname
if len(placefile) > 0:
if placefile.find(".xml") > -1: placefile = placefile.split('.')[0]
if len(placename) > 0 and len(placefile) > 0: placeList[statefile][cityfile][placefile] = placename
elif len(placefile) > 0:
print "Could not place ",
printPretty([statefile,cityfile,placefile])
elif len(placefile) > 0:
print "Could not place ",
printPretty([statefile,cityfile,placefile])
def saveCity(fileid,data):
"""Given a filename, saves a city's values to an "id" XML file.
"""
global cities
info = data.get('info')
fn = fileid + ".xml"
city = etree.Element("city")
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["name","state","statefile","start","scue","end","ecue","events","places","aspects","update"]
for tag in tags:
if tag == "places":
nodes = info.get("places")
if nodes is not None:
for node in nodes.keys():
if nodes[node].get("name"):
connected = etree.Element("place")
value = info['places'][node].get("name")
if value is None: value = ['',False]
etree.SubElement(connected,"name").text = value[0]
value = node
if value is None: value = ''
etree.SubElement(connected,"file").text = value
value = info['places'][node].get("note")
if value is not None and len(value[0]) > 0: etree.SubElement(connected,"note").text = value[0]
city.append(connected)
# for t in range(len(connected)):
# print "%s: %s" % (connected[t].tag,connected[t].text)
else:
print "A required tag is missing from place %s." % node
else:
print "no places found"
elif tag == "events":
nodes = info.get("m")
nodes = nodes.get("events")
if nodes is not None:
events = etree.Element("events")
for node in nodes.keys():
if nodes[node].get("event"):
connected = etree.Element("mstone")
value = info['m']['events'][node].get("event")
if value is None: value = ['',False]
etree.SubElement(connected,"event").text = value[0]
value = info['m']['events'][node].get("date")
if value is None: value = ['',False]
etree.SubElement(connected,"date").text = value[0]
events.append(connected)
else:
print "A required tag is missing from event %s." % node
city.append(events)
else:
print "no events found"
# 820 #
elif tag == "aspects":
nodes = info.get("aspects")
if nodes is not None:
aspects = etree.Element("aspects")
for node in sorted(nodes.keys()):
value = nodes[node]
if value is None: value = ['',False]
etree.SubElement(aspects,"text").text = value[0]
city.append( aspects )
else:
print "no aspects found"
elif tag == "update":
etree.SubElement(city,tag).text = common.skrTimeStamp(config['datestyle'])
else:
value = info.get(tag)
if value is None: value = ['',False]
etree.SubElement(city,tag).text = value[0]
saveXMLtree(city,"city",fileid)
cities[fileid]['changed'] = False
return True
def savePerson(fileid,data):
"""Given a filename, saves a person's values to an "id" XML file.
"""
global people
info = data.get('info')
rels = data.get('relat')
fn = fileid + ".xml"
person = etree.Element("person")
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["commonname", "ctitle", "gname", "mname", "fname", "nname", "nameorder", "gender", "bday", "dday", "stories", "mention", "appear1ch", "appear1wr", "conflict", "leadrel", "bodytyp", "age", "skin", "eyes", "hair", "dmarks", "dress", "attposs", "asmell", "personality", "speech", "formocc", "currocc", "strength", "weak", "mole", "hobby", "misc", "ethnic", "origin", "backstory", "residence", "minchar", "talent", "abil", "sgoal", "other", "relat", "aspects", "update"]
reltags = ["related", "relation", "file", "rtype", "events", "cat", "realm"]
for tag in tags:
if tag == "relat":
if len(rels):
for r in rels:
if rels[r].get("cat") is None:
rels[r]['cat'] = common.getCat(r) # Try to figure it out
if rels[r].get("related") is not None and rels[r].get("relation") is not None and rels[r].get("rtype") is not None and rels[r].get("cat") is not None:
connected = etree.Element("relat")
for t in reltags:
if rels[r].get(t):
if t == "events":
if len(rels[r]['events']):
events = etree.Element("events")
elist = rels[r]['events'].keys()
chron = sorted(elist, key = lambda x: rels[r]['events'][x].get("date"))
for e in chron:
mstone = etree.Element("mstone")
etree.SubElement(mstone,"date").text = rels[r]['events'][e].get("date",("",False))[0]
etree.SubElement(mstone,"event").text = rels[r]['events'][e].get("event",("",False))[0]
if rels[r]['events'][e].get("Type"):
etree.SubElement(mstone,"type").text = rels[r]['events'][e].get("type")[0]
events.append(mstone)
connected.append(events)
else:
value = rels[r].get(t)
if value is None: value = ['',False]
etree.SubElement(connected,t).text = value[0]
elif t == "file":
value = [r,False]
etree.SubElement(connected,t).text = value[0]
person.append(connected)
else:
print "A required tag is missing from relation %s." % r,
printPretty(rels[r])
else:
print "no relations found"
elif tag == "currocc" or tag == "formocc":
if info[tag].get("pos"):
occ = etree.Element(tag)
value = info[tag].get("pos")
if value is None: value = ['',False]
etree.SubElement(occ,"pos").text = value[0]
if info[tag].get("events"):
if len(info[tag]['events']):
events = etree.Element("events")
elist = info[tag]['events'].keys()
chron = sorted(elist, key = lambda x: info[tag]['events'][x].get("date"))
for e in chron:
mstone = etree.Element("mstone")
etree.SubElement(mstone,"date").text = info[tag]['events'][e].get("date",("",False))[0]
etree.SubElement(mstone,"event").text = info[tag]['events'][e].get("event",("",False))[0]
if info[tag]['events'][e].get("Type"):
etree.SubElement(mstone,"type").text = info[tag]['events'][e].get("type")[0]
events.append(mstone)
occ.append(events)
person.append( occ )
# 820 #
elif tag == "aspects":
nodes = info.get("aspects")
if nodes is not None:
aspects = etree.Element("aspects")
for node in sorted(nodes.keys()):
value = nodes[node]
if value is None: value = ['',False]
etree.SubElement(aspects,"text").text = value[0]
person.append( aspects )
else:
print "no aspects found"
elif tag == "update":
etree.SubElement(person,tag).text = common.skrTimeStamp(config['datestyle'])
else:
value = info.get(tag)
if value is None: value = ['',False]
etree.SubElement(person,tag).text = value[0]
saveXMLtree(person,"person",fileid)
people[fileid]['changed'] = False
return True
def savePlace(fileid,data):
"""Given a filename, saves a place's values to an "id" XML file.
"""
global places
info = data.get('info')
rels = data.get('relat')
fn = fileid + ".xml"
place = etree.Element("place")
# TODO: put this in a global variable, and make a function to populate it from the DTD.
tags = ["commonname","name","start","scue","end","ecue","stories","mention","desc","address","loc","locfile","state",\
"statefile","note", "relat", "aspects", "update"]
reltags = ["related", "relation", "file", "rtype", "events", "cat", "realm"]
for tag in tags:
if tag == "relat":
if len(rels):
for r in rels:
if rels[r].get("related") and rels[r].get("relation") and rels[r].get("rtype") and rels[r].get("cat"):
connected = etree.Element("relat")
for t in reltags:
if rels[r].get(t):
if t == "events":
if len(rels[r]['events']):
events = etree.Element("events")
elist = rels[r]['events'].keys()
chron = sorted(elist, key = lambda x: rels[r]['events'][x].get("date"))
for e in chron:
mstone = etree.Element("mstone")
etree.SubElement(mstone,"date").text = rels[r]['events'][e].get("date",("",False))[0]
etree.SubElement(mstone,"event").text = rels[r]['events'][e].get("event",("",False))[0]
if rels[r]['events'][e].get("Type"):
etree.SubElement(mstone,"type").text = rels[r]['events'][e].get("type")[0]
events.append(mstone)
connected.append(events)
else:
value = rels[r].get(t)
if value is None: value = ['',False]
etree.SubElement(connected,t).text = value[0]
place.append(connected)
else:
print "A required tag is missing from relation %s." % r
else:
print "no relations found"
elif tag == "note":
if info.get(tag):
for i in range(len(info[tag])):
note = etree.Element(tag)
di = info[tag].get(str(i))
if di:
value = di.get("content")
if value is not None:
etree.SubElement(note,"content").text = value[0]
value = di.get("date")
if value is not None:
etree.SubElement(note,"date").text = value[0]
place.append(note)
else:
print "no notes"
# 820 #
elif tag == "aspects":
nodes = info.get("aspects")
if nodes is not None:
aspects = etree.Element("aspects")
for node in sorted(nodes.keys()):
value = nodes[node]
if value is None: value = ['',False]
etree.SubElement(aspects,"text").text = value[0]
place.append( aspects )
else: