coding utf-8 Name synxml py Purpose Syntax Mode Xml Interface Author C

  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
# -*- coding: utf-8 -*-
###############################################################################
# Name: synxml.py #
# Purpose: Syntax Mode Xml Interface #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
""" Interface for extending language support through the use of xml files
@summary: Editra Xml implementation
"""
xml_spec = """
<editra version="1">
<syntax language="Python" lexer="STC_LEX_PYTHON">
<keywordlist>
<keywords value="0">
if else elif for while in
</keywords>
<keywords value="1">
str len setattr getattr
</keywords>
</keywordlist>
<syntaxspeclist>
<syntaxspec value="STC_P_DEFAULT" tag="default_style"/>
<syntaxspec value="STC_P_WORD" tag="keyword_style"/>
</syntaxspeclist>
<propertylist>
<property value="fold" enable="1"/>
<property value="tab.timmy.whinge.level" enable="1"/>
</propertylist>
<commentpattern value="#"/>
<extensionlist>
<extension method="AutoIndenter" source="myextension.py"/>
<!-- <extension method="StyleText" source="myextension.py"/> -->
</extensionlist>
</syntax>
</editra>
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: synxml.py 62326 2009-10-08 04:05:06Z CJP $"
__revision__ = "$Revision: 62326 $"
#----------------------------------------------------------------------------#
# Imports
import os
from xml import sax
import wx.stc as stc
#----------------------------------------------------------------------------#
# Tag Definitions
EXML_START = u"editra"
EXML_SYNTAX = u"syntax"
EXML_KEYWORDLIST = u"keywordlist"
EXML_KEYWORDS = u"keywords"
EXML_SYNSPECLIST = u"syntaxspeclist"
EXML_SYNTAXSPEC = u"syntaxspec"
EXML_PROPERTYLIST = u"propertylist"
EXML_PROPERTY = u"property"
EXML_COMMENTPAT = u"commentpattern"
EXML_FEATURELIST = u"featurelist"
EXML_FEATURE = u"feature"
# Attributes
EXML_ENABLE = u"enable"
EXML_LANG = u"language"
EXML_LEXER = u"lexer"
EXML_METHOD = u"method"
EXML_SOURCE = u"source"
EXML_TAG = u"tag"
EXML_VALUE = u"value"
EXML_VERSION = u"version"
EXML_NAME = u"name"
#----------------------------------------------------------------------------#
class EditraXml(sax.ContentHandler):
"""Base class for all Editra syntax xml objects"""
def __init__(self, path=None):
sax.ContentHandler.__init__(self)
# Attributes
self.name = u'' # Tag name
self.level = 0 # Set the level of the element
self.indent = 3 # Indentation
self.path = path
self._context = None # Current parse context
self._reg_handler = dict() # Registered parse handlers
def __eq__(self, other):
return self.GetXml() == other.GetXml()
#---- Internal Parser Api ----#
def startElement(self, name, attrs):
if self._context is not None:
# Delegate to current context
self._context.startElement(name, attrs)
elif name in self._reg_handler:
self._context = self._reg_handler.get(name)
self._context.startElement(name, attrs)
else:
# Unknown tag
# raise?
pass
def endElement(self, name):
if self._context is not None:
if self._context.Name == name:
self._context = None
else:
# Delegate to registered handler
self._context.endElement(name)
else:
# Unknown tag ending
pass
def characters(self, chars):
if not chars.isspace():
if self._context is not None:
self._context.characters(chars)
else:
# No current context or context is self
pass
def GetXml(self):
"""Get the xml representation of this object
@return: string
"""
ident = self.GetIndentationStr()
xml = ident + self.GetStartTag()
xml += (self.GetSubElements() + os.linesep)
xml += (ident + self.GetEndTag())
return xml
def GetSubElements(self):
"""Get the sub elements
@return: string
"""
xml = u''
for handler in self.GetHandlers():
handler.SetLevel(self.Level + 1)
xml += (os.linesep + handler.GetXml())
return xml
def GetStartTag(self):
"""Get the opening tag
@return: string
"""
return u"<%s>" % self.name
def GetEndTag(self):
"""Get the closing tag
@return: string
"""
return u"</%s>" % self.name
def LoadFromDisk(self):
"""Load the object from from disk
@precondition: path has been set
@return: bool
"""
assert self.path is not None, "Must SetPath before calling Load"
try:
handle = open(self.path, "rb")
txt = handle.read()
handle.close()
txt = txt.decode('utf-8') # xml is utf-8 by specification
self.LoadFromString(txt)
except (OSError, UnicodeDecodeError):
return False
else:
return True
def LoadFromString(self, txt):
"""Load and intialize the object from an xml string
@param txt: string
"""
sax.parseString(txt, self)
#---- External Api ----#
@property
def Context(self):
return self._context
@property
def Indentation(self):
return self.indent
@property
def Level(self):
return self.level
@property
def Name(self):
return self.name
def GetHandler(self, tag):
"""Get the handler associated with the given tag
@param tag: string
@return: EditraXml object or None
"""
return self._reg_handler.get(tag, None)
def GetHandlers(self):
"""Get all the handlers registered with this element
@return: list of EditraXml objects
"""
return self._reg_handler.values()
def GetIndentation(self):
"""Get the indentation string
@return: int
"""
return self.indent
def GetIndentationStr(self):
"""Get the indentation string taking level into consideration
@return: string
"""
return (self.indent * u" ") * self.level
def GetLevel(self):
"""Get the level of this element
@return: int
"""
return self.level
def GetName(self):
"""Get the tag name for the handler
@return: string
"""
return self.name
def GetPath(self):
"""Get the xml files path
@return: string
"""
return self.path
def RegisterHandler(self, handler):
"""Register a handler for a tag. Parsing will be delegated to the
the registered handler untill its end tag is encountered.
@param handler: EditraXml instance
"""
tag = handler.GetName()
assert tag not in self._reg_handler, "%s already registered!" % tag
handler.SetLevel(self.Level + 1)
self._reg_handler[tag] = handler
def SetIndentation(self, indent):
"""Set the indentation level
@param indent: int
"""
self.indent = indent
def SetLevel(self, level):
"""Set the level of this element
@param level: int
"""
self.level = level
def SetName(self, tag):
"""Set this handlers tag name used for identifying the open and
end tags.
@param tag: string
"""
self.name = tag
def SetPath(self, path):
"""Set the path to load this element from
@param path: path
"""
self.path = path
#----------------------------------------------------------------------------#
class FileTypeHandler(EditraXml):
"""Main Xml interface to extending filetype handling support"""
def __init__(self, path=None):
EditraXml.__init__(self, path)
# Attributes
self._start = False
self._version = 0
self.syntax = Syntax()
# Setup
self.SetName(EXML_START)
self.SetIndentation(3)
self.RegisterHandler(self.syntax)
def startElement(self, name, attrs):
if self._start:
EditraXml.startElement(self, name, attrs)
elif name == EXML_START:
self._version = int(attrs.get(EXML_VERSION, 0))
self._start = True
def endElement(self, name):
if name == EXML_START:
self._start = False
else:
EditraXml.endElement(self, name)
def GetStartTag(self):
return u"<%s version=\"%s\">" % (self.GetName(), self.Version)
#---- Get External Api ----#
# Properties
@property
def CommentPattern(self):
return self.GetCommentPattern()
@property
def Keywords(self):
return self.GetKeywords()
@property
def Properties(self):
return self.GetProperties()
@property
def SyntaxSpec(self):
return self.GetSyntaxSpec()
@property
def Version(self):
return self._version
# Getters
def GetCommentPattern(self):
"""Get the comment pattern list
@return: list of strings
"""
return self.syntax.GetCommentPattern()
def GetKeywords(self):
"""Get the keyword list
@return: list of tuples [(idx, ['word', 'word2',]),]
"""
kwxml = self.syntax.GetKeywordXml()
return kwxml.GetKeywords()
def GetFeature(self, fet):
"""Get the callable associated with the given feature
@param fet: string
@return: string
"""
fetxml = self.syntax.GetFeatureXml()
return fetxml.GetFeature(fet)
def GetSyntaxSpec(self):
"""Get the syntax spec
@return: list of tuples [(style_id, "style_tag")]
"""
spxml = self.syntax.GetSyntaxSpecXml()
return spxml.GetStyleSpecs()
def GetProperties(self):
"""Get the property defs
@return: list of tuples [("fold", "1"),]
"""
propxml = self.syntax.GetPropertiesXml()
return propxml.GetProperties()
#----------------------------------------------------------------------------#
class Syntax(EditraXml):
"""Syntax definition for intializing a Scintilla Lexer"""
def __init__(self):
EditraXml.__init__(self)
# Attributes
self.language = u"Plain Text"
self.lexstr = u"STC_LEX_NULL"
self.lexer = stc.STC_LEX_NULL
# Sub Xml Objects
self.keywords = KeywordList()
self.synspec = SyntaxSpecList()
self.props = PropertyList()
self.features = FeatureList()
self.comment = list()
# Setup
self.SetName(EXML_SYNTAX)
self.RegisterHandler(self.keywords)
self.RegisterHandler(self.synspec)
self.RegisterHandler(self.props)
self.RegisterHandler(self.features)
def startElement(self, name, attrs):
"""Parse the Syntax Xml"""
if name == EXML_COMMENTPAT:
self.comment = attrs.get(EXML_VALUE, '').split()
elif name == self.Name:
lang = attrs.get(EXML_LANG, u"Plain Text")
lexer = attrs.get(EXML_LEXER, 'STC_LEX_NULL')
lexval = getattr(stc, lexer, None)
assert lexval is not None, "Invalid Lexer: %s" % lexer
self.language = lang
self.lexstr = lexer
self.lexer = lexval
else:
EditraXml.startElement(self, name, attrs)
def GetStartTag(self):
"""Get the syntax opening tag and attributes"""
return u"<%s %s=\"%s\" %s=\"%s\">" % (self.Name, EXML_LANG,
self.language, EXML_LEXER,
self.lexstr)
def GetSubElements(self):
"""Get the SubElements xml string
@return: string
"""
xml = EditraXml.GetSubElements(self)
ident = self.GetIndentationStr() + (self.Indentation * u" ")
xml += os.linesep
cpat = u" ".join(self.GetCommentPattern())
comment = u"<%s %s=\"%s\">" % (EXML_COMMENTPAT, EXML_VALUE, cpat.strip())
xml += os.linesep
xml += (ident + comment)
return xml
#---- External Api ----#
def GetCommentPattern(self):
"""Get the comment pattern
@return: list of strings ["/*", "*/"]
"""
return self.comment
def GetFeatureXml(self):
"""Get the FeatureList xml object"""
return self.features
def GetKeywordXml(self):
"""Get the Keyword Xml object"""
return self.keywords
def GetLanguage(self):
"""Get the language description/name
@return: string
"""
return self.language
def GetLexer(self):
"""Get the lexer to use for this language
@return: stc.STC_LEX_FOO
"""
return self.lexer
def GetSyntaxSpecXml(self):
"""Get the Syntax Spec Xml object"""
return self.synspec
def GetPropertiesXml(self):
"""Get the properties xml object"""
return self.props
#----------------------------------------------------------------------------#
class KeywordList(EditraXml):
"""Container object for all keyword sets"""
def __init__(self):
EditraXml.__init__(self)
# Attributes
self._current = None
self._keywords = dict() # { index : word_list }
# Setup
self.SetName(EXML_KEYWORDLIST)
def startElement(self, name, attrs):
if name == EXML_KEYWORDS:
idx = attrs.get(EXML_VALUE, None)
assert idx is not None, "value attribute not set"
idx = int(idx)
assert idx not in self._keywords, "Duplicate index set %d" % idx
self._keywords[idx] = list()
self._current = self._keywords[idx]
else:
pass
def endElement(self, name):
if name == EXML_KEYWORDS:
self._current = None
def characters(self, chars):
chars = chars.strip().split()
if self._current is not None:
if len(chars):
self._current.extend(chars)
def GetSubElements(self):
"""Get the keyword list elements"""
xml = u""
tag = u"<%s %s=" % (EXML_KEYWORDS, EXML_VALUE)
tag += "\"%s\">"
end = u"</%s>" % EXML_KEYWORDS
ident = self.GetIndentationStr() + (self.Indentation * u" ")
for key in sorted(self._keywords.keys()):
xml += os.linesep + ident
xml += (tag % key)
xml += os.linesep + ident
words = (self.Indentation * u" ") + u" ".join(self._keywords[key])
xml += words
xml += os.linesep + ident
xml += end
return xml
#---- External Api ----#
def GetKeywords(self):
"""Get the list of keyword strings
@return: sorted list of tuples [(kw_idx, [word1, word2,])]
"""
keys = sorted(self._keywords.keys())
keywords = [ (idx, self._keywords[idx]) for idx in keys ]
keywords.sort(key=lambda x: x[0])
return keywords
def GetKeywordList(self, idx):
"""Get the list of keywords associated with the given index
@return: list of strings
"""
return self._keywords.get(idx, None)
#----------------------------------------------------------------------------#
class SyntaxSpecList(EditraXml):
"""Container element for holding the syntax specification elements"""
def __init__(self):
EditraXml.__init__(self)
# Attributes
self._specs = list()
# Setup
self.SetName(EXML_SYNSPECLIST)
def startElement(self, name, attrs):
"""Parse all syntaxspec elements in the list"""
if name == EXML_SYNTAXSPEC:
lid = attrs.get(EXML_VALUE, '')
assert len(lid), "Style Id not specified"
if lid.isdigit():
style_id = int(lid)
else:
# Scintilla Value
style_id = getattr(stc, lid, None)
assert style_id is not None, "Invalid STC Value: %s" % lid
self._specs.append((style_id, attrs.get(EXML_TAG, 'default_style')))
else:
# Unknown Tag
# Raise?
pass
def GetSubElements(self):
"""Get the xml for all the syntax spec elements"""
xml = u""
tag = u"<%s %s=" % (EXML_SYNTAXSPEC, EXML_VALUE)
tag += ("\"%s\" " + EXML_TAG + "=\"%s\"/>")
ident = self.GetIndentationStr() + (self.Indentation * u" ")
for spec in self._specs:
xml += os.linesep + ident
xml += (tag % spec)
return xml
#---- External Api ----#
def GetStyleSpecs(self):
"""Get the list of keyword strings
@return: list of tuples [(style_id, "style_tag"),]
"""
return self._specs
#----------------------------------------------------------------------------#
class PropertyList(EditraXml):
"""Container class for the syntax properties"""
def __init__(self):
EditraXml.__init__(self)
# Attributes
self.properties = list()
# Setup
self.SetName(EXML_PROPERTYLIST)
def startElement(self, name, attrs):
if name == EXML_PROPERTY:
prop = attrs.get(EXML_VALUE, '')
if prop:
enable = attrs.get(EXML_ENABLE, '0')
self.properties.append((prop, enable))
else:
pass
def GetSubElements(self):
xml = u""
tag = u"<%s %s=" % (EXML_PROPERTY, EXML_VALUE)
tag += ("\"%s\" " + EXML_ENABLE + "=\"%s\"/>")
ident = self.GetIndentationStr() + (self.Indentation * u" ")
for prop in self.properties:
xml += os.linesep + ident
xml += (tag % prop)
return xml
#---- External Api ----#
def GetProperties(self):
"""Get the list of properties
@return: list of tuples [("property", "1")]
"""
return self.properties
#----------------------------------------------------------------------------#
class FeatureList(EditraXml):
"""Container for all other misc syntax features.
Currently Available Features:
- AutoIndent
- StyleText
"""
def __init__(self):
EditraXml.__init__(self)
# Attributes
self._features = dict()
# Setup
self.SetName(EXML_FEATURELIST)
def startElement(self, name, attrs):
if name == EXML_FEATURE:
meth = attrs.get(EXML_METHOD, None)
assert meth is not None, "method not defined"
mod = attrs.get(EXML_SOURCE, None)
assert mod is not None, "source not defined"
self._features[meth] = mod
else:
EditraXml.startElement(self, name, attrs)
def GetSubElements(self):
xml = u""
tag = u"<%s %s=" % (EXML_FEATURE, EXML_METHOD)
tag += ("\"%s\" " + EXML_SOURCE + "=\"%s\"/>")
for feature in self._features.iteritems():
xml += os.linesep
xml += (tag % feature)
return xml
#---- External Api ----#
def GetFeature(self, fet):
"""Get the callable feature by name
@param fet: string (module name)
"""
feature = None
src = self._features.get(fet, None)
if src is not None:
feature = src
return feature
#----------------------------------------------------------------------------#
def LoadHandler():
"""Load and intialize a FileTypeHandler from an on disk xml config file
@return: FileTypeHandler instance
"""
raise NotImplementedError