-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports.py
executable file
·2226 lines (2073 loc) · 110 KB
/
imports.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
# -*- coding: UTF-8 -*-
#
# imports.py
#
# Copyright 2009-2014 Giuseppe Penone <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import HTMLParser, htmlentitydefs
import gtk, gio, os, xml.dom.minidom, re, base64, urllib2
import cons, machines
def get_internal_link_from_http_url(link_url):
"""Get internal cherrytree link attribute from HTTP link url"""
if link_url[0:4] == "http": return "webs %s" % link_url
elif link_url[0:7] == "file://": return "file %s" % base64.b64encode(link_url[7:])
else: return "webs %s" % ("http://" + link_url)
def get_web_links_offsets_from_plain_text(plain_text):
"""Parse plain text for possible web links"""
web_links = []
max_end_offset = len(plain_text)
max_start_offset = max_end_offset - 7
start_offset = 0
while start_offset < max_start_offset:
is_link = False
if plain_text[start_offset] == "h":
if plain_text[start_offset:start_offset+4] == "http":
is_link = True
elif plain_text[start_offset] == "f":
if plain_text[start_offset:start_offset+3] == "ftp":
is_link = True
elif plain_text[start_offset] == "w":
if plain_text[start_offset:start_offset+4] == "www.":
is_link = True
if is_link:
end_offset = start_offset + 3
while (end_offset < max_end_offset)\
and (plain_text[end_offset] not in [cons.CHAR_SPACE, cons.CHAR_NEWLINE]):
end_offset += 1
web_links.append([start_offset, end_offset])
start_offset = end_offset + 1
else: start_offset += 1
return web_links
class LeoHandler:
"""The Handler of the Leo File Parsing"""
def __init__(self):
"""Machine boot"""
self.xml_handler = machines.XMLHandler(self)
def parse_leo_xml(self, leo_string):
"""Leo XML Parsing"""
self.tnodes_dict = {}
dom = xml.dom.minidom.parseString(leo_string)
dom_iter = dom.firstChild
while dom_iter:
if dom_iter.nodeName == "leo_file": break
dom_iter = dom_iter.nextSibling
child_dom_iter = dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "vnodes":
vnode_dom_iter = child_dom_iter.firstChild
elif child_dom_iter.nodeName == "tnodes":
tnode_dom_iter = child_dom_iter.firstChild
while tnode_dom_iter:
if tnode_dom_iter.nodeName == "t":
if tnode_dom_iter.firstChild: fill_text = tnode_dom_iter.firstChild.data
else: fill_text = ""
self.tnodes_dict[tnode_dom_iter.attributes['tx'].value] = fill_text
tnode_dom_iter = tnode_dom_iter.nextSibling
child_dom_iter = child_dom_iter.nextSibling
while vnode_dom_iter:
if vnode_dom_iter.nodeName == "v": self.append_leo_node(vnode_dom_iter)
vnode_dom_iter = vnode_dom_iter.nextSibling
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
#for tag_property in cons.TAG_PROPERTIES:
# if self.curr_attributes[tag_property] != "":
# dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.nodes_list[-1].appendChild(dom_iter)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def append_leo_node(self, vnode_dom_iter):
"""Append a Leo Node"""
self.nodes_list.append(self.dom.createElement("node"))
node_name = "?"
child_dom_iter = vnode_dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "vh":
if child_dom_iter.firstChild: node_name = child_dom_iter.firstChild.data
self.nodes_list[-1].setAttribute("name", node_name)
self.nodes_list[-1].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.nodes_list[-2].appendChild(self.nodes_list[-1])
self.rich_text_serialize(self.tnodes_dict[vnode_dom_iter.attributes['t'].value])
elif child_dom_iter.nodeName == "v": self.append_leo_node(child_dom_iter)
child_dom_iter = child_dom_iter.nextSibling
self.nodes_list.pop()
def get_cherrytree_xml(self, leo_string):
"""Returns a CherryTree string Containing the Leo Nodes"""
self.dom = xml.dom.minidom.Document()
self.nodes_list = [self.dom.createElement(cons.APP_NAME)]
self.dom.appendChild(self.nodes_list[0])
self.parse_leo_xml(leo_string)
return self.dom.toxml()
class TuxCardsHandler(HTMLParser.HTMLParser):
"""The Handler of the TuxCards File Parsing"""
def __init__(self):
"""Machine boot"""
HTMLParser.HTMLParser.__init__(self)
self.xml_handler = machines.XMLHandler(self)
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
for tag_property in cons.TAG_PROPERTIES:
if self.curr_attributes[tag_property] != "":
dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.nodes_list[-1].appendChild(dom_iter)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def start_parsing(self, tuxcards_string):
"""Start the Parsing"""
dom = xml.dom.minidom.parseString(tuxcards_string)
dom_iter = dom.firstChild
while dom_iter:
if dom_iter.nodeName == "InformationCollection": break
dom_iter = dom_iter.nextSibling
child_dom_iter = dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "InformationElement": break
child_dom_iter = child_dom_iter.nextSibling
self.node_add(child_dom_iter)
def node_add(self, dom_iter):
"""Add a Node"""
child_dom_iter = dom_iter.firstChild
self.nodes_list.append(self.dom.createElement("node"))
while child_dom_iter:
if child_dom_iter.nodeName == "Description":
if child_dom_iter.firstChild: node_name = child_dom_iter.firstChild.data
else: node_name = ""
self.nodes_list[-1].setAttribute("name", node_name)
self.nodes_list[-1].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.nodes_list[-2].appendChild(self.nodes_list[-1])
elif child_dom_iter.nodeName == "Information":
if child_dom_iter.firstChild: node_string = child_dom_iter.firstChild.data
else: node_string = ""
self.curr_state = 0
# curr_state 0: standby, taking no data
# curr_state 1: waiting for node content, take many data
self.pixbuf_vector = []
self.chars_counter = 0
self.feed(node_string.decode(cons.STR_UTF8, cons.STR_IGNORE))
for pixbuf_element in self.pixbuf_vector:
self.xml_handler.pixbuf_element_to_xml(pixbuf_element, self.nodes_list[-1], self.dom)
elif child_dom_iter.nodeName == "InformationElement":
self.node_add(child_dom_iter)
child_dom_iter = child_dom_iter.nextSibling
self.nodes_list.pop()
def handle_starttag(self, tag, attrs):
"""Encountered the beginning of a tag"""
if self.curr_state == 0:
if tag == "body": self.curr_state = 1
else: # self.curr_state == 1
if tag == "span" and attrs[0][0] == cons.TAG_STYLE:
if "font-weight" in attrs[0][1]: self.curr_attributes[cons.TAG_WEIGHT] = cons.TAG_PROP_HEAVY
elif "font-style" in attrs[0][1] and cons.TAG_PROP_ITALIC in attrs[0][1]: self.curr_attributes[cons.TAG_STYLE] = cons.TAG_PROP_ITALIC
elif "text-decoration" in attrs[0][1] and cons.TAG_UNDERLINE in attrs[0][1]: self.curr_attributes[cons.TAG_UNDERLINE] = cons.TAG_PROP_SINGLE
elif "color" in attrs[0][1]:
match = re.match("(?<=^).+:(.+);(?=$)", attrs[0][1])
if match != None: self.curr_attributes[cons.TAG_FOREGROUND] = match.group(1).strip()
elif tag == "a" and len(attrs) > 0:
link_url = attrs[0][1]
if len(link_url) > 7:
self.curr_attributes[cons.TAG_LINK] = get_internal_link_from_http_url(link_url)
elif tag == "img" and len(attrs) > 0:
img_path = attrs[0][1]
if os.path.isfile(img_path):
pixbuf = gtk.gdk.pixbuf_new_from_file(img_path)
self.pixbuf_vector.append([self.chars_counter, pixbuf, cons.TAG_PROP_LEFT])
self.chars_counter += 1
else: print "%s not found" % img_path
elif tag == "br":
# this is a data block composed only by an endline
self.rich_text_serialize(cons.CHAR_NEWLINE)
self.chars_counter += 1
elif tag == "hr":
# this is a data block composed only by an horizontal rule
self.rich_text_serialize(cons.CHAR_NEWLINE+self.h_rule+cons.CHAR_NEWLINE)
self.chars_counter += len(self.h_rule)+2
elif tag == "li":
self.rich_text_serialize(cons.CHAR_NEWLINE+cons.CHAR_LISTBUL+cons.CHAR_SPACE)
self.chars_counter += 3
def handle_endtag(self, tag):
"""Encountered the end of a tag"""
if self.curr_state == 0: return
if tag == "p":
# this is a data block composed only by an endline
self.rich_text_serialize(cons.CHAR_NEWLINE)
self.chars_counter += 1
elif tag == "span":
self.curr_attributes[cons.TAG_WEIGHT] = ""
self.curr_attributes[cons.TAG_STYLE] = ""
self.curr_attributes[cons.TAG_UNDERLINE] = ""
self.curr_attributes[cons.TAG_FOREGROUND] = ""
elif tag == "a": self.curr_attributes[cons.TAG_LINK] = ""
def handle_data(self, data):
"""Found Data"""
if self.curr_state == 0 or data in [cons.CHAR_NEWLINE, cons.CHAR_NEWLINE*2]: return
data = data.replace(cons.CHAR_NEWLINE, "")
self.rich_text_serialize(data)
self.chars_counter += len(data)
def handle_entityref(self, name):
"""Found Entity Reference like &name;"""
if self.curr_state == 0: return
if name in htmlentitydefs.name2codepoint:
unicode_char = unichr(htmlentitydefs.name2codepoint[name])
self.rich_text_serialize(unicode_char)
self.chars_counter += 1
def get_cherrytree_xml(self, tuxcards_string):
"""Returns a CherryTree string Containing the TuxCards Nodes"""
self.dom = xml.dom.minidom.Document()
self.nodes_list = [self.dom.createElement(cons.APP_NAME)]
self.dom.appendChild(self.nodes_list[0])
self.curr_attributes = {}
for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = ""
self.latest_span = ""
self.start_parsing(tuxcards_string)
return self.dom.toxml()
class KeepnoteHandler(HTMLParser.HTMLParser):
"""The Handler of the KeepNote Folder Parsing"""
def __init__(self, folderpath):
"""Machine boot"""
HTMLParser.HTMLParser.__init__(self)
self.folderpath = folderpath
self.xml_handler = machines.XMLHandler(self)
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
for tag_property in cons.TAG_PROPERTIES:
if self.curr_attributes[tag_property] != "":
dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.nodes_list[-1].appendChild(dom_iter)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def start_parsing(self):
"""Start the Parsing"""
for element in reversed(os.listdir(self.folderpath)):
if os.path.isdir(os.path.join(self.folderpath, element))\
and element not in ["__TRASH__", "__NOTEBOOK__"]:
self.node_add(os.path.join(self.folderpath, element))
def node_add(self, node_folder):
"""Add a Node"""
self.nodes_list.append(self.dom.createElement("node"))
self.nodes_list[-1].setAttribute("name", os.path.basename(node_folder))
self.nodes_list[-1].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.nodes_list[-2].appendChild(self.nodes_list[-1])
filepath = os.path.join(node_folder, "page.html")
if os.path.isfile(filepath):
try:
file_descriptor = open(filepath, 'r')
node_string = file_descriptor.read()
file_descriptor.close()
except:
print "Error opening the file %s" % filepath
return
else: node_string = "" # empty node
self.curr_state = 0
# curr_state 0: standby, taking no data
# curr_state 1: waiting for node content, take many data
self.pixbuf_vector = []
self.curr_folder = node_folder
self.chars_counter = 0
self.feed(node_string.decode(cons.STR_UTF8, cons.STR_IGNORE))
for pixbuf_element in self.pixbuf_vector:
self.xml_handler.pixbuf_element_to_xml(pixbuf_element, self.nodes_list[-1], self.dom)
# check if the node has children
for element in reversed(os.listdir(node_folder)):
if os.path.isdir(os.path.join(node_folder, element)):
self.node_add(os.path.join(node_folder, element))
self.nodes_list.pop()
def handle_starttag(self, tag, attrs):
"""Encountered the beginning of a tag"""
if self.curr_state == 0:
if tag == "body": self.curr_state = 1
else: # self.curr_state == 1
if tag == "b": self.curr_attributes[cons.TAG_WEIGHT] = cons.TAG_PROP_HEAVY
elif tag == "i": self.curr_attributes[cons.TAG_STYLE] = cons.TAG_PROP_ITALIC
elif tag == "u": self.curr_attributes[cons.TAG_UNDERLINE] = cons.TAG_PROP_SINGLE
elif tag == "strike": self.curr_attributes[cons.TAG_STRIKETHROUGH] = cons.TAG_PROP_TRUE
elif tag == "span" and attrs[0][0] == cons.TAG_STYLE:
match = re.match("(?<=^)(.+):(.+)(?=$)", attrs[0][1])
if match != None:
if match.group(1) == "color":
self.curr_attributes[cons.TAG_FOREGROUND] = match.group(2).strip()
self.latest_span = cons.TAG_FOREGROUND
elif match.group(1) == "background-color":
self.curr_attributes[cons.TAG_BACKGROUND] = match.group(2).strip()
self.latest_span = cons.TAG_BACKGROUND
elif tag == "a" and len(attrs) > 0:
link_url = attrs[0][1]
if len(link_url) > 7:
self.curr_attributes[cons.TAG_LINK] = get_internal_link_from_http_url(link_url)
elif tag == "img" and len(attrs) > 0:
img_name = attrs[0][1]
img_path = os.path.join(self.curr_folder, img_name)
if os.path.isfile(img_path):
pixbuf = gtk.gdk.pixbuf_new_from_file(img_path)
self.pixbuf_vector.append([self.chars_counter, pixbuf, cons.TAG_PROP_LEFT])
self.chars_counter += 1
else: print "%s not found" % img_path
elif tag == "br":
# this is a data block composed only by an endline
self.rich_text_serialize(cons.CHAR_NEWLINE)
self.chars_counter += 1
elif tag == "hr":
# this is a data block composed only by an horizontal rule
self.rich_text_serialize(cons.CHAR_NEWLINE+self.h_rule+cons.CHAR_NEWLINE)
self.chars_counter += len(self.h_rule)+2
elif tag == "li":
self.rich_text_serialize(cons.CHAR_NEWLINE+cons.CHAR_LISTBUL+cons.CHAR_SPACE)
self.chars_counter += 3
def handle_endtag(self, tag):
"""Encountered the end of a tag"""
if self.curr_state == 0: return
if tag == "b": self.curr_attributes[cons.TAG_WEIGHT] = ""
elif tag == "i": self.curr_attributes[cons.TAG_STYLE] = ""
elif tag == "u": self.curr_attributes[cons.TAG_UNDERLINE] = ""
elif tag == "strike": self.curr_attributes[cons.TAG_STRIKETHROUGH] = ""
elif tag == "span":
if self.latest_span == cons.TAG_FOREGROUND: self.curr_attributes[cons.TAG_FOREGROUND] = ""
elif self.latest_span == cons.TAG_BACKGROUND: self.curr_attributes[cons.TAG_BACKGROUND] = ""
elif tag == "a": self.curr_attributes[cons.TAG_LINK] = ""
def handle_data(self, data):
"""Found Data"""
if self.curr_state == 0 or data in [cons.CHAR_NEWLINE, cons.CHAR_NEWLINE*2]: return
data = data.replace(cons.CHAR_NEWLINE, "")
self.rich_text_serialize(data)
self.chars_counter += len(data)
def handle_entityref(self, name):
"""Found Entity Reference like &name;"""
if self.curr_state == 0: return
if name in htmlentitydefs.name2codepoint:
unicode_char = unichr(htmlentitydefs.name2codepoint[name])
self.rich_text_serialize(unicode_char)
self.chars_counter += 1
def get_cherrytree_xml(self):
"""Returns a CherryTree string Containing the KeepNote Nodes"""
self.dom = xml.dom.minidom.Document()
self.nodes_list = [self.dom.createElement(cons.APP_NAME)]
self.dom.appendChild(self.nodes_list[0])
self.curr_attributes = {}
for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = ""
self.latest_span = ""
self.start_parsing()
return self.dom.toxml()
class ZimHandler():
"""The Handler of the Zim Folder Parsing"""
def __init__(self, folderpath):
"""Machine boot"""
self.folderpath = folderpath
self.xml_handler = machines.XMLHandler(self)
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
for tag_property in cons.TAG_PROPERTIES:
if self.curr_attributes[tag_property] != "":
dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.nodes_list[-1].appendChild(dom_iter)
self.chars_counter += len(text_data)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def parse_folder(self, curr_folder):
"""Start the Parsing"""
for element in os.listdir(curr_folder):
if len(element) > 4 and element[-4:] == ".txt" \
and os.path.isfile(os.path.join(curr_folder, element)):
file_descriptor = open(os.path.join(curr_folder, element), 'r')
wiki_string = file_descriptor.read()
file_descriptor.close()
#
node_name = os.path.splitext(element)[0]
self.node_add(wiki_string.decode(cons.STR_UTF8), node_name, curr_folder)
# check if the node has children
children_folder = os.path.join(curr_folder, node_name)
if os.path.isdir(children_folder):
self.parse_folder(children_folder)
self.nodes_list.pop()
def node_add(self, wiki_string, node_name, curr_folder):
"""Add a node"""
self.nodes_list.append(self.dom.createElement("node"))
self.nodes_list[-1].setAttribute("name", node_name)
self.nodes_list[-1].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.nodes_list[-2].appendChild(self.nodes_list[-1])
#
self.pixbuf_vector = []
self.chars_counter = 0
self.node_wiki_parse(wiki_string, node_name, curr_folder)
for pixbuf_element in self.pixbuf_vector:
self.xml_handler.pixbuf_element_to_xml(pixbuf_element, self.nodes_list[-1], self.dom)
def node_wiki_parse(self, wiki_string, node_name, curr_folder):
"""Parse the node wiki content"""
for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = ""
self.in_block = False
self.in_link = False
self.in_plain_link = False
curr_pos = 0
wiki_string = wiki_string.replace(cons.CHAR_CR, "")
wiki_string = wiki_string.replace(cons.CHAR_NEWLINE+cons.CHAR_STAR+cons.CHAR_SPACE, cons.CHAR_NEWLINE+cons.CHAR_LISTBUL+cons.CHAR_SPACE)
wiki_string = wiki_string.replace(cons.CHAR_TAB+cons.CHAR_STAR+cons.CHAR_SPACE, cons.CHAR_TAB+cons.CHAR_LISTBUL+cons.CHAR_SPACE)
max_pos = len(wiki_string)
newline_count = 0
self.wiki_slot = ""
def wiki_slot_flush():
if self.wiki_slot:
#print self.wiki_slot
self.rich_text_serialize(self.wiki_slot)
self.wiki_slot = ""
probably_url = False
in_hN = [False, False, False, False, False]
while curr_pos < max_pos:
curr_char = wiki_string[curr_pos:curr_pos+1]
next_char = wiki_string[curr_pos+1:curr_pos+2] if curr_pos+1 < max_pos else cons.CHAR_SPACE
third_char = wiki_string[curr_pos+2:curr_pos+3] if curr_pos+2 < max_pos else cons.CHAR_SPACE
fourth_char = wiki_string[curr_pos+3:curr_pos+4] if curr_pos+3 < max_pos else cons.CHAR_SPACE
if newline_count < 4:
if curr_char == cons.CHAR_NEWLINE: newline_count += 1
else:
if self.in_block:
if curr_char == cons.CHAR_SQUOTE and next_char == cons.CHAR_SQUOTE and third_char == cons.CHAR_SQUOTE:
wiki_slot_flush()
self.curr_attributes[cons.TAG_FAMILY] = ""
curr_pos += 2
self.in_block = False
else: self.wiki_slot += curr_char
elif self.in_plain_link:
if curr_char in [cons.CHAR_SPACE, cons.CHAR_NEWLINE]:
self.curr_attributes[cons.TAG_LINK] = "webs %s" % self.wiki_slot
wiki_slot_flush()
self.curr_attributes[cons.TAG_LINK] = ""
self.in_plain_link = False
self.wiki_slot += curr_char
elif self.in_link:
if curr_char == cons.CHAR_BR_CLOSE and next_char == cons.CHAR_BR_CLOSE:
valid_image = False
if cons.CHAR_QUESTION in self.wiki_slot:
splitted_wiki_slot = self.wiki_slot.split(cons.CHAR_QUESTION)
self.wiki_slot = splitted_wiki_slot[0]
if self.wiki_slot.startswith("./"): self.wiki_slot = os.path.join(curr_folder, node_name, self.wiki_slot[2:])
if os.path.isfile(self.wiki_slot):
try:
pixbuf = gtk.gdk.pixbuf_new_from_file(self.wiki_slot)
self.pixbuf_vector.append([self.chars_counter, pixbuf, cons.TAG_PROP_LEFT])
self.chars_counter += 1
valid_image = True
except: pass
if not valid_image: print "! error: '%s' is not a valid image" % self.wiki_slot
self.wiki_slot = ""
curr_pos += 1
self.in_link = False
elif curr_char == cons.CHAR_SQ_BR_CLOSE and next_char == cons.CHAR_SQ_BR_CLOSE:
if cons.CHAR_PIPE in self.wiki_slot:
target_n_label = self.wiki_slot.split(cons.CHAR_PIPE)
else:
target_n_label = [self.wiki_slot, self.wiki_slot]
exp_filepath = target_n_label[0]
if exp_filepath.startswith("./"): exp_filepath = os.path.join(curr_folder, node_name, exp_filepath[2:])
if exp_filepath.startswith("http") or exp_filepath.startswith("ftp") or exp_filepath.startswith("www.")\
and not cons.CHAR_SPACE in exp_filepath:
self.curr_attributes[cons.TAG_LINK] = "webs %s" % exp_filepath
self.rich_text_serialize(target_n_label[1])
self.curr_attributes[cons.TAG_LINK] = ""
elif cons.CHAR_SLASH in exp_filepath:
self.curr_attributes[cons.TAG_LINK] = "file %s" % base64.b64encode(exp_filepath)
self.rich_text_serialize(target_n_label[1])
self.curr_attributes[cons.TAG_LINK] = ""
else:
self.links_to_node_list.append({'name_dest': target_n_label[0],
'node_source': node_name,
'char_start': self.chars_counter,
'char_end': self.chars_counter+len(target_n_label[1])})
self.rich_text_serialize(target_n_label[1])
self.wiki_slot = ""
curr_pos += 1
self.in_link = False
else: self.wiki_slot += curr_char
elif curr_char == cons.CHAR_STAR and next_char == cons.CHAR_STAR:
wiki_slot_flush()
if self.curr_attributes[cons.TAG_WEIGHT]: self.curr_attributes[cons.TAG_WEIGHT] = ""
else: self.curr_attributes[cons.TAG_WEIGHT] = cons.TAG_PROP_HEAVY
curr_pos += 1
elif curr_char == cons.CHAR_SLASH and next_char == cons.CHAR_SLASH:
if probably_url:
self.wiki_slot += curr_char
curr_pos += 1
continue
wiki_slot_flush()
if self.curr_attributes[cons.TAG_STYLE]: self.curr_attributes[cons.TAG_STYLE] = ""
else: self.curr_attributes[cons.TAG_STYLE] = cons.TAG_PROP_ITALIC
curr_pos += 1
elif curr_char == cons.CHAR_USCORE and next_char == cons.CHAR_USCORE:
wiki_slot_flush()
if self.curr_attributes[cons.TAG_BACKGROUND]: self.curr_attributes[cons.TAG_BACKGROUND] = ""
else: self.curr_attributes[cons.TAG_BACKGROUND] = cons.COLOR_48_YELLOW
curr_pos += 1
elif curr_char == cons.CHAR_TILDE and next_char == cons.CHAR_TILDE:
wiki_slot_flush()
if self.curr_attributes[cons.TAG_STRIKETHROUGH]: self.curr_attributes[cons.TAG_STRIKETHROUGH] = ""
else: self.curr_attributes[cons.TAG_STRIKETHROUGH] = cons.TAG_PROP_TRUE
curr_pos += 1
elif curr_char == cons.CHAR_SQUOTE and next_char == cons.CHAR_SQUOTE:
wiki_slot_flush()
if self.curr_attributes[cons.TAG_FAMILY]: self.curr_attributes[cons.TAG_FAMILY] = ""
else: self.curr_attributes[cons.TAG_FAMILY] = "monospace"
if third_char == cons.CHAR_SQUOTE:
curr_pos += 2
self.in_block = True if self.curr_attributes[cons.TAG_FAMILY] else False
else: curr_pos += 1
elif curr_char == 'h' and next_char == 't' and third_char == 't' and fourth_char == 'p':
wiki_slot_flush()
self.wiki_slot += curr_char
self.in_plain_link = True
# ==
elif not in_hN[4] and curr_char == cons.CHAR_EQUAL and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_SPACE:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H3
curr_pos += 2
in_hN[4] = True
#print "H5sta"
elif in_hN[4] and curr_char == cons.CHAR_SPACE and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
curr_pos += 2
in_hN[4] = False
#print "H5end"
## ===
elif not in_hN[3] and curr_char == cons.CHAR_EQUAL and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_SPACE:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H3
curr_pos += 3
in_hN[3] = True
#print "H4sta"
elif in_hN[3] and curr_char == cons.CHAR_SPACE and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
curr_pos += 3
in_hN[3] = False
#print "H4end"
## ====
elif not in_hN[2] and curr_pos+4 < max_pos and curr_char == cons.CHAR_EQUAL and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+5] == cons.CHAR_SPACE:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H3
curr_pos += 4
in_hN[2] = True
#print "H3sta"
elif curr_pos+4 < max_pos and in_hN[2] and curr_char == cons.CHAR_SPACE and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+5] == cons.CHAR_EQUAL:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
curr_pos += 4
in_hN[2] = False
#print "H3end"
## =====
elif not in_hN[1] and curr_pos+5 < max_pos and curr_char == cons.CHAR_EQUAL and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+6] == (cons.CHAR_EQUAL+cons.CHAR_SPACE):
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H2
curr_pos += 5
in_hN[1] = True
#print "H2sta"
elif curr_pos+5 < max_pos and in_hN[1] and curr_char == cons.CHAR_SPACE and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+6] == 2*cons.CHAR_EQUAL:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
curr_pos += 5
in_hN[1] = False
#print "H2end"
# ======
elif not in_hN[0] and curr_pos+6 < max_pos and curr_char == cons.CHAR_EQUAL and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+7] == (2*cons.CHAR_EQUAL+cons.CHAR_SPACE):
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H1
curr_pos += 6
in_hN[0] = True
#print "H1sta"
elif curr_pos+6 < max_pos and in_hN[0] and curr_char == cons.CHAR_SPACE and next_char == cons.CHAR_EQUAL and third_char == cons.CHAR_EQUAL and fourth_char == cons.CHAR_EQUAL and wiki_string[curr_pos+4:curr_pos+7] == 3*cons.CHAR_EQUAL:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
curr_pos += 6
in_hN[0] = False
#print "H1end"
#
elif curr_char == cons.CHAR_CARET and next_char == cons.CHAR_BR_OPEN:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = "sup"
curr_pos += 1
elif curr_char == cons.CHAR_USCORE and next_char == cons.CHAR_BR_OPEN:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = "sub"
curr_pos += 1
elif curr_char == cons.CHAR_BR_CLOSE and self.curr_attributes[cons.TAG_SCALE] in ["sup", "sub"]:
wiki_slot_flush()
self.curr_attributes[cons.TAG_SCALE] = ""
elif curr_char == cons.CHAR_BR_OPEN and next_char == cons.CHAR_BR_OPEN \
or curr_char == cons.CHAR_SQ_BR_OPEN and next_char == cons.CHAR_SQ_BR_OPEN:
wiki_slot_flush()
curr_pos += 1
self.in_link = True
elif curr_char == cons.CHAR_SQ_BR_OPEN\
and next_char in [cons.CHAR_SPACE, cons.CHAR_STAR, 'x']\
and third_char == cons.CHAR_SQ_BR_CLOSE:
if next_char == cons.CHAR_SPACE: self.wiki_slot += cons.CHAR_LISTTODO
elif next_char == cons.CHAR_STAR: self.wiki_slot += cons.CHAR_LISTDONEOK
else: self.wiki_slot += cons.CHAR_LISTDONEFAIL
self.wiki_slot += cons.CHAR_SPACE
curr_pos += 2
else:
self.wiki_slot += curr_char
#print self.wiki_slot
if curr_char == ":" and next_char == cons.CHAR_SLASH:
probably_url = True
elif curr_char in [cons.CHAR_SPACE, cons.CHAR_NEWLINE]:
probably_url = False
curr_pos += 1
wiki_slot_flush()
def get_cherrytree_xml(self):
"""Returns a CherryTree string Containing the Zim Nodes"""
self.dom = xml.dom.minidom.Document()
self.nodes_list = [self.dom.createElement(cons.APP_NAME)]
self.dom.appendChild(self.nodes_list[0])
self.curr_attributes = {}
for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = ""
self.links_to_node_list = []
self.parse_folder(self.folderpath)
return self.dom.toxml()
def set_links_to_nodes(self, dad):
"""After the node import, set the links to nodes on the new tree"""
for link_to_node in self.links_to_node_list:
node_dest = dad.get_tree_iter_from_node_name(link_to_node['name_dest'])
node_source = dad.get_tree_iter_from_node_name(link_to_node['node_source'])
if not node_dest:
#print "node_dest not found"
continue
if not node_source:
#print "node_source not found"
continue
source_buffer = dad.get_textbuffer_from_tree_iter(node_source)
if source_buffer.get_char_count() < link_to_node['char_end']:
continue
property_value = cons.LINK_TYPE_NODE + cons.CHAR_SPACE + str(dad.treestore[node_dest][3])
source_buffer.apply_tag_by_name(dad.apply_tag_exist_or_create(cons.TAG_LINK, property_value),
source_buffer.get_iter_at_offset(link_to_node['char_start']),
source_buffer.get_iter_at_offset(link_to_node['char_end']))
class TomboyHandler():
"""The Handler of the Tomboy Folder Parsing"""
def __init__(self, folderpath):
"""Machine boot"""
self.folderpath = folderpath
self.xml_handler = machines.XMLHandler(self)
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
for tag_property in cons.TAG_PROPERTIES:
if self.curr_attributes[tag_property] != "":
dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.dest_dom_new.appendChild(dom_iter)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def start_parsing(self):
"""Start the Parsing"""
for element in reversed(os.listdir(self.folderpath)):
if os.path.isfile(os.path.join(self.folderpath, element)):
file_descriptor = open(os.path.join(self.folderpath, element), 'r')
xml_string = file_descriptor.read()
file_descriptor.close()
self.doc_parse(xml_string, element)
def doc_parse(self, xml_string, file_name):
"""Parse an xml file"""
dest_dom_father = self.dest_orphans_dom_node
try: dom = xml.dom.minidom.parseString(xml_string)
except:
print "? non xml file:", file_name
return
dom_iter = dom.firstChild
while dom_iter:
if dom_iter.nodeName == "note": break
dom_iter = dom_iter.nextSibling
child_dom_iter = dom_iter.firstChild
self.node_title = "???"
while child_dom_iter:
if child_dom_iter.nodeName == "title":
self.node_title = child_dom_iter.firstChild.data if child_dom_iter.firstChild else "???"
if len(self.node_title) > 18 and self.node_title[-18:] == " Notebook Template":
return
elif child_dom_iter.nodeName == "text":
text_dom_iter = child_dom_iter
elif child_dom_iter.nodeName == "tags":
tag_dom_iter = child_dom_iter.firstChild
while tag_dom_iter:
if tag_dom_iter.nodeName == "tag":
if tag_dom_iter.firstChild:
tag_text = tag_dom_iter.firstChild.data
if len(tag_text) > 16 and tag_text[:16] == "system:notebook:":
dest_dom_father = self.notebook_exist_or_create(tag_text[16:])
tag_dom_iter = tag_dom_iter.nextSibling
child_dom_iter = child_dom_iter.nextSibling
nephew_dom_iter = text_dom_iter.firstChild
while nephew_dom_iter:
if nephew_dom_iter.nodeName == "note-content":
self.node_add(nephew_dom_iter, dest_dom_father)
break
nephew_dom_iter = nephew_dom_iter.nextSibling
def node_add(self, content_iter, dest_dom_father):
"""Add a Node"""
self.dest_dom_new = self.dom.createElement("node")
self.dest_dom_new.setAttribute("name", self.node_title)
self.dest_dom_new.setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
dest_dom_father.appendChild(self.dest_dom_new)
for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = ""
self.chars_counter = 0
self.node_add_iter(content_iter.firstChild)
def node_add_iter(self, dom_iter):
"""Recursively parse nodes"""
while dom_iter:
if dom_iter.nodeName == "#text":
text_data = dom_iter.data
if self.curr_attributes[cons.TAG_LINK] == "webs ":
self.curr_attributes[cons.TAG_LINK] += dom_iter.data
elif self.is_list_item:
text_data = cons.CHAR_LISTBUL+cons.CHAR_SPACE + text_data
elif self.is_link_to_node:
self.links_to_node_list.append({'name_dest':text_data,
'node_source':self.node_title,
'char_start':self.chars_counter,
'char_end':self.chars_counter+len(text_data)})
self.rich_text_serialize(text_data)
self.chars_counter += len(text_data)
elif dom_iter.nodeName == "bold":
self.curr_attributes[cons.TAG_WEIGHT] = cons.TAG_PROP_HEAVY
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_WEIGHT] = ""
elif dom_iter.nodeName == cons.TAG_PROP_ITALIC:
self.curr_attributes[cons.TAG_STYLE] = cons.TAG_PROP_ITALIC
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_STYLE] = ""
elif dom_iter.nodeName == cons.TAG_STRIKETHROUGH:
self.curr_attributes[cons.TAG_STRIKETHROUGH] = cons.TAG_PROP_TRUE
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_STRIKETHROUGH] = ""
elif dom_iter.nodeName == "highlight":
self.curr_attributes[cons.TAG_BACKGROUND] = cons.COLOR_48_YELLOW
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_BACKGROUND] = ""
elif dom_iter.nodeName == "monospace":
self.curr_attributes[cons.TAG_FAMILY] = dom_iter.nodeName
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_FAMILY] = ""
elif dom_iter.nodeName == "size:small":
self.curr_attributes[cons.TAG_SCALE] = "small"
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_SCALE] = ""
elif dom_iter.nodeName == "size:large":
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H2
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_SCALE] = ""
elif dom_iter.nodeName == "size:huge":
self.curr_attributes[cons.TAG_SCALE] = cons.TAG_PROP_H1
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_SCALE] = ""
elif dom_iter.nodeName == "link:url":
self.curr_attributes[cons.TAG_LINK] = "webs "
self.node_add_iter(dom_iter.firstChild)
self.curr_attributes[cons.TAG_LINK] = ""
elif dom_iter.nodeName == "list-item":
self.is_list_item = True
self.node_add_iter(dom_iter.firstChild)
self.is_list_item = False
elif dom_iter.nodeName == "link:internal":
self.is_link_to_node = True
self.node_add_iter(dom_iter.firstChild)
self.is_link_to_node = False
elif dom_iter.firstChild:
#print dom_iter.nodeName
self.node_add_iter(dom_iter.firstChild)
dom_iter = dom_iter.nextSibling
def notebook_exist_or_create(self, notebook_title):
"""Check if there's already a notebook with this title"""
if not notebook_title in self.dest_notebooks_dom_nodes:
self.dest_notebooks_dom_nodes[notebook_title] = self.dom.createElement("node")
self.dest_notebooks_dom_nodes[notebook_title].setAttribute("name", notebook_title)
self.dest_notebooks_dom_nodes[notebook_title].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.dest_top_dom.appendChild(self.dest_notebooks_dom_nodes[notebook_title])
return self.dest_notebooks_dom_nodes[notebook_title]
def get_cherrytree_xml(self):
"""Returns a CherryTree string Containing the Tomboy Nodes"""
self.dom = xml.dom.minidom.Document()
self.dest_top_dom = self.dom.createElement(cons.APP_NAME)
self.dom.appendChild(self.dest_top_dom)
self.curr_attributes = {}
self.is_list_item = False
self.is_link_to_node = False
self.links_to_node_list = []
# orphans node
self.dest_orphans_dom_node = self.dom.createElement("node")
self.dest_orphans_dom_node.setAttribute("name", "ORPHANS")
self.dest_orphans_dom_node.setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.dest_top_dom.appendChild(self.dest_orphans_dom_node)
# notebooks nodes
self.dest_notebooks_dom_nodes = {}
# start parsing
self.start_parsing()
return self.dom.toxml()
def set_links_to_nodes(self, dad):
"""After the node import, set the links to nodes on the new tree"""
for link_to_node in self.links_to_node_list:
node_dest = dad.get_tree_iter_from_node_name(link_to_node['name_dest'])
node_source = dad.get_tree_iter_from_node_name(link_to_node['node_source'])
if not node_dest:
#print "node_dest not found"
continue
if not node_source:
#print "node_source not found"
continue
source_buffer = dad.get_textbuffer_from_tree_iter(node_source)
if source_buffer.get_char_count() < link_to_node['char_end']:
continue
property_value = cons.LINK_TYPE_NODE + cons.CHAR_SPACE + str(dad.treestore[node_dest][3])
source_buffer.apply_tag_by_name(dad.apply_tag_exist_or_create(cons.TAG_LINK, property_value),
source_buffer.get_iter_at_offset(link_to_node['char_start']),
source_buffer.get_iter_at_offset(link_to_node['char_end']))
class BasketHandler(HTMLParser.HTMLParser):
"""The Handler of the Basket Folder Parsing"""
def __init__(self, dad, folderpath):
"""Machine boot"""
HTMLParser.HTMLParser.__init__(self)
self.folderpath = folderpath
self.dad = dad
self.xml_handler = machines.XMLHandler(dad)
def check_basket_structure(self):
"""Check the Selected Folder to be a Basket Folder"""
self.baskets_xml_filepath = os.path.join(self.folderpath, "baskets.xml")
if os.path.isfile(self.baskets_xml_filepath): return True
self.folderpath = os.path.join(self.folderpath, "baskets")
self.baskets_xml_filepath = os.path.join(self.folderpath, "baskets.xml")
return os.path.isfile(self.baskets_xml_filepath)
def rich_text_serialize(self, text_data):
"""Appends a new part to the XML rich text"""
dom_iter = self.dom.createElement("rich_text")
for tag_property in cons.TAG_PROPERTIES:
if self.curr_attributes[tag_property] != "":
dom_iter.setAttribute(tag_property, self.curr_attributes[tag_property])
self.nodes_list[-1].appendChild(dom_iter)
text_iter = self.dom.createTextNode(text_data)
dom_iter.appendChild(text_iter)
def start_parsing(self):
"""Start the Parsing"""
file_descriptor = open(self.baskets_xml_filepath, 'r')
baskets_xml_string = file_descriptor.read()
file_descriptor.close()
dom = xml.dom.minidom.parseString(baskets_xml_string)
dom_iter = dom.firstChild
while dom_iter.firstChild == None:
dom_iter = dom_iter.nextSibling
child_dom_iter = dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "basket": self.node_add(child_dom_iter)
child_dom_iter = child_dom_iter.nextSibling
def node_add(self, top_dom_iter):
"""Add a Node"""
self.pixbuf_vector = []
self.chars_counter = 0
folder_name = top_dom_iter.attributes["folderName"].value[0:-1]
node_name = "?"
child_dom_iter = top_dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "properties":
nephew_dom_iter = child_dom_iter.firstChild
while nephew_dom_iter:
if nephew_dom_iter.nodeName == "name":
if nephew_dom_iter.firstChild: node_name = nephew_dom_iter.firstChild.data
nephew_dom_iter = nephew_dom_iter.nextSibling
child_dom_iter = child_dom_iter.nextSibling
self.subfolder_path = os.path.join(self.folderpath, folder_name)
node_xml_filepath = os.path.join(self.subfolder_path, ".basket")
file_descriptor = open(node_xml_filepath, 'r')
node_xml_string = file_descriptor.read()
file_descriptor.close()
dom = xml.dom.minidom.parseString(node_xml_string)
dom_iter = dom.firstChild
child_dom_iter = dom_iter.firstChild
while not child_dom_iter:
dom_iter = dom_iter.nextSibling
child_dom_iter = dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "properties":
self.nodes_list.append(self.dom.createElement("node"))
self.nodes_list[-1].setAttribute("name", node_name)
self.nodes_list[-1].setAttribute("prog_lang", cons.CUSTOM_COLORS_ID)
self.nodes_list[-2].appendChild(self.nodes_list[-1])
elif child_dom_iter.nodeName == "notes":
self.notes_parse(child_dom_iter)
child_dom_iter = child_dom_iter.nextSibling
for pixbuf_element in self.pixbuf_vector:
self.xml_handler.pixbuf_element_to_xml(pixbuf_element, self.nodes_list[-1], self.dom)
# check if the node has children
child_dom_iter = top_dom_iter.firstChild
while child_dom_iter:
if child_dom_iter.nodeName == "basket": self.node_add(child_dom_iter)
child_dom_iter = child_dom_iter.nextSibling
self.nodes_list.pop()
def notes_parse(self, notes_dom_iter):
"""Parse a 'notes'"""
nephew_dom_iter = notes_dom_iter.firstChild
while nephew_dom_iter:
if nephew_dom_iter.nodeName == "group":
self.notes_parse(nephew_dom_iter)
elif nephew_dom_iter.nodeName == "note":
self.note_parse(nephew_dom_iter)
nephew_dom_iter = nephew_dom_iter.nextSibling
def note_parse(self, note_dom_iter):
"""Parse a 'note'"""
if note_dom_iter.attributes['type'].value == "html":
# curr_state 0: standby, taking no data
# curr_state 1: waiting for node content, take many data
self.curr_state = 0
content_dom_iter = note_dom_iter.firstChild
while content_dom_iter:
if content_dom_iter.nodeName == "content":
content_path = os.path.join(self.subfolder_path, content_dom_iter.firstChild.data)
if os.path.isfile(content_path):
file_descriptor = open(content_path, 'r')
node_string = file_descriptor.read()
file_descriptor.close()
else: node_string = "" # empty node