-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathddrsplit.py
executable file
·1362 lines (1103 loc) · 45.8 KB
/
ddrsplit.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/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
import io
import unicodedata
import time
import binascii
import base64
import hashlib
import pprint
pp = pprint.pprint
import pdb
import xml.etree.ElementTree
ElementTree = xml.etree.ElementTree
import xml.dom
import xml.dom.minidom
minidom = xml.dom.minidom
import xml.parsers.expat
import Config
import ReferenceCollector
# py3 stuff
py3 = False
try:
unicode('')
punicode = unicode
pstr = str
punichr = unichr
except NameError:
punicode = str
pstr = bytes
py3 = True
punichr = chr
#
# globals
#
gCancel = False
gREF = ReferenceCollector.ReferenceCollector()
#
# tools
#
def makeunicode(s, srcencoding="utf-8", normalizer="NFC"):
if type(s) not in (punicode, pstr):
s = str( s )
if type(s) != punicode:
s = punicode(s, srcencoding)
s = unicodedata.normalize(normalizer, s)
return s
def stringhash( s ):
m = hashlib.sha1()
m.update(s)
return m.hexdigest().upper()
def logfunction(s):
s = s + u"\n"
sys.stdout.write( s )
#
# parsers
#
def fullexportfilepath(basefolder, dbname, category, obname, obid="", ext=".xml"):
# create or get folder where to put layout, script or basetable xml
path = os.path.abspath(basefolder)
catfolder = os.path.join( path, dbname, category)
if not os.path.exists(catfolder):
os.makedirs( catfolder )
if obid:
obid = str(obid).rjust(7,"0") + " "
filename = obid + obname + ext
filename = filename.replace('/', '_')
filename = filename.replace(':', '_')
filename = filename.replace('\\', '_')
fullpath = os.path.join( catfolder, filename)
fullpath = makeunicode( fullpath, normalizer="NFD" )
return fullpath
def get_text_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename, cur_node, cur_object):
# check for global variables and merge fields
pass
def get_script_step(cfg, cur_fmpxml, cur_db, cur_fmpbasename, cur_node, cur_object):
# BAUSTELLE
# check for scripstep and field parameters
# catch
# perform scrip
# exit script (parameter)
# set variable
# install on timer script
# go to layout
# go to related record
# go to object
# go to field
# enter find mode
step_id = cur_node.attrib.get("id", -1)
step_name = cur_node.attrib.get("name", "NO SCRIPTSTEP NAME")
fref_id = fref_name = step_calc_text = None
for subnode in cur_node.iter():
if subnode.tag == "FileReference":
fref_id = subnode.attrib.get("id", -1)
fref_name = subnode.attrib.get("name", "NO FILEREFERENCE NAME")
elif subnode.tag == "DisplayCalculation":
get_displaycalculation(cfg, cur_fmpxml, cur_db, cur_fmpbasename, subnode)
elif subnode.tag == "Calculation":
step_calc_text = subnode.text
external = fref_id and fref_name
def get_displaycalculation(cfg, cur_fmpxml, cur_db, cur_fmpbasename, cur_node):
clc_text = clc_noref = clc_fnctref = clc_fieldref = clc_cf = ""
for node in cur_node.iter():
dpc_tag = node.tag
dpc_typ = node.attrib.get( "type", "")
if dpc_typ == "NoRef":
clc_noref = node.text
elif dpc_typ == "FunctionRef":
pass
elif dpc_typ == "FieldRef":
# <Field id="1" name="F1" table="to_Test1" />
pass
elif dpc_typ == "CustomFunctionRef":
pass
def get_authfilecatalog(cfg, cur_fmpxml, cur_db, cur_fmpbasename, authfiles,
groups, exportfolder, idx):
for authfile in authfiles:
authfile_attr = authfile.attrib
authfile_tag = authfile.tag
authfile_name = authfile_attr.get("name", "NONAME")
cur_object = (cur_fmpxml, 'AuthFile', authfile_name)
path = "AuthFiles"
sortid = authfile_attr.get("id", "0").rjust(7,"0")
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
path,
authfile_name,
objectID)
if 1:
xml2file( path, authfile, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(authfile, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
return idx
def get_externaldatasources(cfg, cur_fmpxml, cur_db, cur_fmpbasename, externaldatasources,
groups, exportfolder, idx):
for externaldatasource in externaldatasources:
externaldatasource_attr = externaldatasource.attrib
externaldatasource_tag = externaldatasource.tag
externaldatasource_name = externaldatasource_attr.get("name", "NONAME")
cur_object = (cur_fmpxml, 'ExternalDataSource', externaldatasource_name)
path = "ExternalDataSources"
sortid = externaldatasource_attr.get("id", "0").rjust(7,"0")
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
path,
externaldatasource_name,
objectID)
if 1:
xml2file( path, externaldatasource, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(externaldatasource, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
return idx
def get_themecatalog(cfg, cur_fmpxml, cur_db, cur_fmpbasename, themes,
groups, exportfolder, idx):
for theme in themes:
theme_attr = theme.attrib
theme_tag = theme.tag
theme_name = theme_attr.get("name", "NONAME")
cur_object = (cur_fmpxml, 'ThemeCatalog', theme_name)
path = "Themes"
sortid = theme_attr.get("id", "0").rjust(7,"0")
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
path,
theme_name,
objectID)
if 1:
xml2file( path, theme, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(theme, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
return idx
def get_basedirectories(cfg, cur_fmpxml, cur_db, cur_fmpbasename, basedirectories,
groups, exportfolder, idx):
for basedirectory in basedirectories:
basedirectory_attr = basedirectory.attrib
basedirectory_tag = basedirectory.tag
basedirectory_name = basedirectory_attr.get("name", "NONAME")
cur_object = (cur_fmpxml, 'BaseDirectoryCatalog', basedirectory_name)
path = "BaseDirectoryCatalog"
sortid = basedirectory_attr.get("id", "0").rjust(7,"0")
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
path,
basedirectory_name,
objectID)
if 1:
xml2file( path, basedirectory, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(basedirectory, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
return idx
def get_layouts_and_groups(cfg, cur_fmpxml, cur_db, cur_fmpbasename, laynode,
groups, exportfolder, idx):
for layout in laynode:
layout_attr = layout.attrib
layout_tag = layout.tag
layout_name = layout_attr.get("name", "NONAME")
cur_object = (cur_fmpxml, 'Layout', layout_name)
if layout_tag == "Group":
grp_attrib = layout_attr
groupid = layout_attr.get("id", "0")
# get layout folder name
groupname = ( groupid.rjust(7,"0")
+ ' '
+ layout_name )
if cfg.layoutOrder:
groupname = ( str(idx).rjust(5,"0")
+ ' '
+ groupid.rjust(7,"0")
+ ' '
+ layout_name )
if cfg.ignoreFilenameIDs:
groupname = layout_name
groups.append( groupname )
idx += 1
idx = get_layouts_and_groups(cfg, cur_fmpxml, cur_db, cur_fmpbasename, layout,
groups, exportfolder, idx)
groups.pop()
else:
path = "Layouts"
if groups and cfg.layoutGroups:
path = os.path.join("Layouts", *groups)
sortid = layout_attr.get("id", "0").rjust(7,"0")
if cfg.layoutOrder:
sortid = (str(idx).rjust(5,"0")
+ ' '
+ layout_attr.get("id", "0").rjust(7,"0") )
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
path,
layout_name,
objectID)
if 1:
xml2file( path, layout, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(layout, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
if not cfg.assets:
continue
for l in list(layout):
t = l.tag
if t == u'Object':
get_layout_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
l, cur_object, exportfolder)
return idx
def get_layout_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename, laynode,
cur_object, exportfolder):
nodes = list(laynode)
extensions = dict(zip( ("JPEG","PDF ", "PNGf", "PICT",
"GIFf", "8BPS", "BMPf"),
(".jpg",".pdf", ".png", ".pict",
".gif", ".psd", ".bmp")))
exttypelist = extensions.keys()
cur_tableOccurrenceName = cur_tableOccurrenceID = ""
for node in nodes:
cur_tag = node.tag
if cur_tag == u'Object':
# get layout object
get_layout_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename, node,
cur_object, exportfolder)
elif cur_tag == u'ObjectStyle':
continue
elif cur_tag == u'Table':
# <Table id="13631489" name="to_Bildarchiv" />
cur_tableOccurrenceID = node.get("id", -1)
cur_tableOccurrenceName = node.get("name",
"NO TABLE OCCURRENCE NAME FOR LAYOUT")
cur_objectID = gREF.addObject( cur_object )
gREF.addFilemakerAttribute( cur_objectID, "tableOccurrenceID",
cur_tableOccurrenceID)
gREF.addFilemakerAttribute( cur_objectID, "tableOccurrenceName",
cur_tableOccurrenceName)
elif cur_tag == u'GraphicObj':
for grobnode in node:
if grobnode.tag == "Stream":
stype = []
sdata = ""
for streamnode in grobnode:
streamtag = streamnode.tag
streamtext = streamnode.text
if streamtag == "Type":
if streamtext not in exttypelist:
stype.append( '.' + streamtext )
else:
stype.append( streamtext )
elif streamtag in ("Data", "HexData"):
if not stype:
continue
curtype = stype[-1]
ext = extensions.get( curtype, False )
if not ext:
ext = curtype
data = None
if streamtag == "HexData":
try:
data = binascii.unhexlify ( streamtext )
except TypeError as err:
pass
elif streamtag == "Data":
try:
data = base64.b64decode( streamtext )
except TypeError as err:
pass
if not data:
continue
fn = stringhash( data )
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
"Assets",
fn,
"",
ext)
# write Asset file
if not os.path.exists( path ):
with io.open(path, 'wb') as f:
f.write( data )
# the following tags are for reference collection only
# laynode is current node
# cur_object is ref1
elif cur_tag == u'GroupButtonObj':
# recurse
get_layout_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
node, cur_object, exportfolder)
elif cur_tag == u'FieldObj':
# check for scripstep and field parameters
for subnode in node.iter():
if subnode.tag == "Field":
# <Field id="2" maxRepetition="1" name="F2"
# repetition="1" table="Test1" />
fld_id = int(subnode.attrib.get("id", -1))
fld_name = subnode.attrib.get("name",
"NO FIELD NAME")
fld_to = subnode.attrib.get("table",
"NO TABLE OCCURRENCE")
fld_obj = (cur_object[0], "Field", fld_name, fld_to)
fld_obj_id = gREF.addObject( fld_obj )
gREF.addFilemakerAttribute(fld_obj_id, "id", fld_id)
gREF.addReference(cur_object, fld_obj)
elif cur_tag == u'Step':
get_script_step(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
node, cur_object)
elif cur_tag == u'TextObj':
get_text_object(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
node, cur_object)
def get_scripts_and_groups(cfg, cur_fmpxml, cur_db, cur_fmpbasename, scriptnode,
exportfolder, groups, namecache, idx):
for scpt in scriptnode:
if scpt.tag == "Script":
path = "Scripts"
if groups and cfg.scriptGroups:
path = os.path.join("Scripts", *groups)
sortid = scpt.get("id", "0").rjust(7,"0")
if cfg.scriptOrder:
sortid = (str(idx).rjust(5,"0")
+ ' '
+ scpt.get("id", "0").rjust(7,"0") )
objectID = sortid
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder, cur_fmpbasename, path,
scpt.get("name", "NONAME"),
objectID)
if 1:
xml2file( path, scpt, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(scpt, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
idx += 1
elif scpt.tag == "Group":
grp_attrib = scpt.attrib
groupid = grp_attrib.get("id", "0")
# script folder name (if any)
groupname = (groupid.rjust(7,"0")
+ ' '
+ grp_attrib.get("name", "No folder name") )
if cfg.scriptOrder:
groupname = (str(idx).rjust(5,"0")
+ ' ' + groupid.rjust(7,"0")
+ ' ' + grp_attrib.get("name", "No folder name"))
if cfg.ignoreFilenameIDs:
groupname = grp_attrib.get("name", "No folder name")
groups.append( groupname )
idx += 1
idx = get_scripts_and_groups(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
scpt, exportfolder, groups, namecache, idx)
groups.pop()
return idx
def get_relationshipgraph_catalog(cfg, cur_fmpxml, cur_db, cur_fmpbasename,
rg_cat, exportfolder):
for tablst in rg_cat:
if tablst.tag == u'TableList':
for tab in tablst:
if tab.tag == u'Table':
to_attr = tab.attrib
to_name = tab.attrib.get("name", "NO TABLE OCCURRENCE NAME")
to_id = tab.attrib.get("id", -1)
to_btid = tab.attrib.get("baseTableId", -1)
to_bt = tab.attrib.get("baseTable", "NO BASETABLE FOR TABLE OCCURRENCE")
objectID = to_id
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
"Relationships/TableList",
to_name,
objectID)
s = ElementTree.tostring(tab, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
external = eto_id = eto_name = False
for node in tab.iter():
if node.tag == "FileReference":
external = True
eto_id = node.get("id", -1)
eto_name = node.get("name",
"NO EXTERNAL FILEREF NAME FOR TABLE OCCURRENCE")
toObject = (cur_fmpxml, "TableOccurrence", to_name)
if external:
toObject = (cur_fmpxml, "ExternalTableOccurrence", to_name)
toObjectId = gREF.addObject( toObject )
gREF.addFilemakerAttribute( toObjectId, 'baseTableId', to_btid)
gREF.addFilemakerAttribute( toObjectId, 'baseTable', to_bt)
if external:
gREF.addFilemakerAttribute( toObjectId, 'fileReferenceID', eto_id)
gREF.addFilemakerAttribute( toObjectId, 'fileReferenceName', eto_name)
# <Table baseTable="bt_Bildarchiv" baseTableId="32769"
# color="#777777" id="13631489" name="to_Bildarchiv" />
# <Table baseTable="bt_Text" baseTableId="32769" color="#777777"
# id="13631498" name="eto_TEX_artnum">
# <FileReference id="1" name="Text" />
# </Table>
elif tablst.tag == u'RelationshipList':
for rel in tablst:
if rel.tag == u'Relationship':
rel_cat = {}
re_attr = rel.attrib
relid = re_attr.get("id", "0")
rel_cat['id'] = re_attr.get("id", "0")
for rel_component in list(rel):
if rel_component.tag == "LeftTable":
rel_cat['lefttable'] = rel_component.attrib.get("name",
"NO-LEFTTABLENAME")
elif rel_component.tag == "RightTable":
rel_cat['righttable'] = rel_component.attrib.get("name",
"NO-LEFTTABLENAME")
filename = (rel_cat['lefttable']
+ "---"
+ rel_cat['righttable'])
objectID = rel_cat['id']
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_fmpbasename,
"Relationships/Relationship",
filename,
objectID)
if 1:
xml2file( path, rel, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(rel, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
class XMLDDRFile:
def __init__( self, xmlfilename, fmpfilename, fmppathOrHost, xmlbasename ):
self.xmlfilename = xmlfilename
self.fmpfilename = fmpfilename
self.fmppathOrHost = fmppathOrHost
self.xmlbasename = xmlbasename
def handleSummaryFile( xmlfile, log ):
ddr = ElementTree.parse( xmlfile )
summary = ddr.getroot()
files = summary.findall( "File" )
nooffiles = len( files )
filelist = {}
isSummary = isXMLExport = 0
result = []
for fmpreport in summary.iter("FMPReport"):
for xmlfile in fmpreport.iter("File"):
# print( xmlfile )
isSummary += 1
xml_fmpfilename = xmlfile.get("name", "NO FILE NAME")
xml_xmllink = xmlfile.get("link", "")
xml_fmppath = xmlfile.get("path", "")
if not xml_xmllink:
s = u"\nERROR: Could not find XML file '%s'\nContinue.\n"
log( s % xml_xmllink)
continue
# xml_xmllink
# cleanup filename
while xml_xmllink.startswith( './/' ): xml_xmllink = xml_xmllink[ 3: ]
while xml_xmllink.startswith( './' ): xml_xmllink = xml_xmllink[ 2: ]
xmlbasename, ext = os.path.splitext( xml_xmllink )
# old
filelist[ xml_xmllink ] = (xml_fmpfilename, xml_fmppath, xmlbasename)
# new
xml = XMLDDRFile( xml_xmllink, xml_fmpfilename, xml_fmppath, xmlbasename )
result.append( xml )
#pp(result)
#pp( filelist )
return result
def xml2file( path, node, indent=False ):
"""Write xmlfile to path. Indent or not.
Currently indentation is too expensive ( time * 5 ) and too ugly. -> OFF
"""
# s = ElementTree.tostring(node, method="xml")
if 0: #indent:
dom = minidom.parseString(s)
pretty_xml_as_string = dom.toprettyxml(newl='').replace("\n\n", "\n")
pretty_xml_as_string = pretty_xml_as_string .replace( "\t\t", "\t" )
s = pretty_xml_as_string.encode("utf-8")
else:
s = ElementTree.tostring(node, encoding="utf-8", method="xml")
with io.open( path, 'wb' ) as f:
f.write( s )
def handleXMLFile( cfg, xmlfolder, xmlfile, log, filetype="ddr" ):
"""Parse and filet the XML file.
cfg - Config struct
xmlffolder - folder of xmlfilepath
xmlfile - XMLDDR instance
log - logging function
filetype - "ddr", "saveas" or "???"
"""
cur_xmlfilename = xmlfile.xmlfilename
xmlfilepath = os.path.join( xmlfolder, cur_xmlfilename )
try:
basenode = ElementTree.parse( xmlfilepath )
except (xml.parsers.expat.ExpatError, SyntaxError) as v:
xml.parsers.expat.error()
log( u"EXCEPTION: '%s'" % v )
log( u"Failed parsing '%s'\n" % cur_xmlfilename )
return
# more often the xml filename is required for identification
# cur_fmpfilename = allxmlfiles[ cur_xml_file_name ][0]
cur_fmpfilename = xmlfile.fmpfilename
# cur_xmlbasename = allxmlfiles[ cur_xml_file_name ][2]
cur_xmlbasename = xmlfile.xmlbasename
cur_fileRef = (cur_xmlfilename, "DatabaseFile", cur_fmpfilename)
exportfolder = cfg.exportfolder
# relationships need to be analyzed first for the baseTable -> TO graph
# for that to happen, filereferences must go before that
print()
print()
#
# FileReferenceCatalog
#
# todo check if refs && cfg.filereferences
log( u'File References "%s"' % cur_xmlfilename )
for fr_cat in basenode.iter ( "FileReferenceCatalog" ):
for fileref in list(fr_cat):
fileref_attrib = fileref.attrib
prefix = ""
if fileref.tag == "OdbcDataSource":
prefix = "ODBC-"
elif fileref.tag == "FileReference":
prefix = "FREF-"
#
# <FileReference id="2" link="Menu_fp7.xml" name="Menu"
# pathList="file:Menu.fp7" />
#
frf_id = fileref.attrib.get("id", -1)
frf_link = fileref.attrib.get("link", "NO DDR.XML FILE")
frf_name = fileref.attrib.get("name", "NO FILEREF NAME")
frf_pathList = fileref.attrib.get("pathList",
"NO FILEREF PATHLIST")
gREF.addFileReference(cur_xmlfilename, frf_link, frf_name,
frf_id, frf_pathList)
frf_object = (cur_xmlfilename, 'FileReference', frf_name)
gREF.addObject(frf_object)
gREF.addReference(cur_fileRef, frf_object)
else:
prefix = "UNKN-"
name = prefix + fileref_attrib.get("name", "NONAME")
objectID = fileref_attrib.get("id", "0")
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_xmlbasename,
"Filereferences",
name,
objectID)
if 1:
xml2file( path, fileref, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(fileref, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
# collect references to fields, CFs, value lists,
# merge fields, scripts, TOs, FileReferences
#
# relationship graph
#
if cfg.relationships:
log( u'Relationship Graph "%s"' % cur_xmlfilename )
for rg_cat in basenode.iter ( "RelationshipGraph" ):
get_relationshipgraph_catalog(cfg, cur_xmlfilename, cur_fmpfilename,
cur_xmlbasename, rg_cat, exportfolder)
# collect references from FRF to FRF
#
# base table catalog
#
if cfg.basetables:
log( u'Base Tables "%s"' % cur_xmlfilename )
for base_table_catalog in basenode.iter( u'BaseTableCatalog' ):
for base_table in base_table_catalog.iter( u'BaseTable' ):
bt_name = base_table.get("name", "NONAME")
bt_id = base_table.get("id", "0")
objectID = bt_id
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_xmlbasename,
"Basetables",
bt_name,
objectID)
if 1:
xml2file( path, base_table, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(base_table,
encoding="utf-8",
method="xml")
f = open(path, "wb")
f.write( s )
f.close()
cur_btRef = (cur_xmlfilename, "BaseTable", bt_name)
bt_objID = gREF.addObject( cur_btRef )
# make the basetable id known without using it for references
gREF.addFilemakerAttribute(bt_objID, "id", bt_id)
gREF.addReference( cur_fileRef, cur_btRef)
# TODO
#
# FIELDS
#
# cur_fmpfilename, cur_btRef, bt_name, bt_id, bt_objID
for field_catalog in base_table.iter( u'FieldCatalog' ):
for field in field_catalog.iter( u'Field' ):
# dataType="Date"
# fieldType="Normal"
# id="9"
# name="dat_BAR_created"
fld_name = field.get("name", "NONAME")
fld_id = field.get("id", "0")
fld_type = field.get("fieldType", "NO FIELD TYPE")
fld_dataType = field.get("dataType", "NO DATA TYPE")
cur_fldRef = (cur_xmlfilename, "Field", fld_name, bt_name)
fld_objID = gREF.addObject( cur_fldRef )
gREF.addFilemakerAttribute(fld_objID, "id", fld_id)
gREF.addFilemakerAttribute(fld_objID, "dataType",
fld_dataType)
gREF.addFilemakerAttribute(fld_objID, "fieldType",
fld_id)
gREF.addReference( cur_btRef, cur_fldRef)
# TODO
#
# add ref to TO (needs Calculations)
# collect references to fields, CFs, value lists, TOs, FileReferences
#
# LayoutCatalog
#
if cfg.layouts:
log( u'Layout Catalog "%s"' % cur_xmlfilename )
for layout_catalog in basenode.iter ( "LayoutCatalog" ):
groups = []
get_layouts_and_groups(cfg,
cur_xmlfilename,
cur_fmpfilename,
cur_xmlbasename,
layout_catalog,
groups,
exportfolder,
1)
# collect references to fields, CFs, value lists, merge fields,
# scripts, TOs, FileReferences
#
# account catalog
#
if cfg.accounts:
log( u'Accounts for "%s"' % cur_xmlfilename )
for acc_cat in basenode.iter ( "AccountCatalog" ):
for acc in list(acc_cat):
acc_attrib = acc.attrib
objectID = acc_attrib.get("id", "0")
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_xmlbasename,
"Accounts",
acc_attrib.get("name", "NONAME"),
objectID)
if 1:
xml2file( path, acc, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(acc, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
# collect references to fields, CFs, value lists, TOs, FileReferences
#
# script catalog
#
if cfg.scripts:
log( u'Scripts for "%s"' % cur_xmlfilename )
for scpt_cat in basenode.iter ( "ScriptCatalog" ):
groups = []
namecache = [{},{}]
get_scripts_and_groups(cfg,
cur_xmlfilename,
cur_fmpfilename,
cur_xmlbasename,
scpt_cat,
exportfolder,
groups,
namecache,
1)
# collect references to fields, CFs, value lists, scripts,
# TOs, FileReferences
#
# custom function catalog
#
#
if cfg.customfunctions:
log( u'Custom Functions for "%s"' % cur_xmlfilename )
for cf_cat in basenode.iter ( "CustomFunctionCatalog" ):
groups = []
for cf in list(cf_cat):
cf_attrib = cf.attrib
objectID = cf_attrib.get("id", "0")
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_xmlbasename,
"CustomFunctions",
cf_attrib.get("name", "NONAME"),
objectID)
if 1:
xml2file( path, cf, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(cf, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
# collect references to fields, CFs, value lists,TOs, FileReferences
#
# PrivilegesCatalog
#
if cfg.privileges:
log( u'Privileges for "%s"' % cur_xmlfilename )
for pv_cat in basenode.iter( "PrivilegesCatalog" ):
for pv in list(pv_cat):
pv_attrib = pv.attrib
objectID = pv_attrib.get("id", "0")
if cfg.ignoreFilenameIDs:
objectID = ""
path = fullexportfilepath(exportfolder,
cur_xmlbasename,
"Privileges",
pv_attrib.get("name", "NONAME"),
objectID)
if 1:
xml2file( path, pv, indent=cfg.xmlindent )
else:
s = ElementTree.tostring(pv, encoding="utf-8", method="xml")
f = open(path, "wb")
f.write( s )
f.close()
# collect references to fields, CFs, value lists, TOs, FileReferences