| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
from RBFoundation.XMLClassBuilder import XMLClassBuilder |
|---|
| 27 |
from RBFoundation.BindCallable import BindCallable |
|---|
| 28 |
import SkinContext |
|---|
| 29 |
import SkinObject |
|---|
| 30 |
import weakref |
|---|
| 31 |
import os |
|---|
| 32 |
|
|---|
| 33 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
#~ Classes |
|---|
| 35 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 36 |
|
|---|
| 37 |
class XMLSkinner(XMLClassBuilder): |
|---|
| 38 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 39 |
#~ Constants / Variables / Etc. |
|---|
| 40 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 41 |
|
|---|
| 42 |
DefaultContext = None |
|---|
| 43 |
rootPath = '' |
|---|
| 44 |
|
|---|
| 45 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 46 |
#~ Special |
|---|
| 47 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 48 |
|
|---|
| 49 |
def __init__(self, mapSupportedSkinNamespaces={}, rootPath=None): |
|---|
| 50 |
if self.rootPath is not None: |
|---|
| 51 |
self.rootPath = rootPath |
|---|
| 52 |
XMLClassBuilder.__init__(self) |
|---|
| 53 |
self.AddElementFactories(mapSupportedSkinNamespaces) |
|---|
| 54 |
|
|---|
| 55 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 56 |
#~ Public Methods |
|---|
| 57 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 58 |
|
|---|
| 59 |
def AddDefaultContext(self, contextDict={}, **kwAddedContext): |
|---|
| 60 |
if self.DefaultContext is None: |
|---|
| 61 |
self.DefaultContext = SkinContext.SkinContext(None) |
|---|
| 62 |
self.DefaultContext._update(contextDict) |
|---|
| 63 |
self.DefaultContext._update(kwAddedContext) |
|---|
| 64 |
|
|---|
| 65 |
def SkinXML(self, xml, contextIn=None, **kwAddedContext): |
|---|
| 66 |
rootPath = self._GetRootPath(kwAddedContext) |
|---|
| 67 |
self._elements, self._LastCompleteElement = [], None |
|---|
| 68 |
parser = self._CreateParser() |
|---|
| 69 |
contextIn = contextIn or self.DefaultContext |
|---|
| 70 |
self.context = SkinContext.SkinContext(contextIn, kwAddedContext) |
|---|
| 71 |
self.context.__skinner__ = weakref.ref(self) |
|---|
| 72 |
self.context.__root__ = rootPath |
|---|
| 73 |
|
|---|
| 74 |
self._do_parse(parser.Parse, xml) |
|---|
| 75 |
|
|---|
| 76 |
self.context = None |
|---|
| 77 |
result, self._LastCompleteElement = self._LastCompleteElement, None |
|---|
| 78 |
del self._elements |
|---|
| 79 |
del self._LastCompleteElement |
|---|
| 80 |
return result |
|---|
| 81 |
|
|---|
| 82 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 83 |
|
|---|
| 84 |
def SkinFile(self, file, contextIn=None, **kwAddedContext): |
|---|
| 85 |
if isinstance(file, str): |
|---|
| 86 |
rootPath = os.path.dirname(os.path.abspath(file)) |
|---|
| 87 |
file = os.path.abspath(file) |
|---|
| 88 |
file = open(file, 'r') |
|---|
| 89 |
else: |
|---|
| 90 |
rootPath = self._GetRootPath(kwAddedContext) |
|---|
| 91 |
|
|---|
| 92 |
self._elements, self._LastCompleteElement = [], None |
|---|
| 93 |
parser = self._CreateParser() |
|---|
| 94 |
contextIn = contextIn or self.DefaultContext |
|---|
| 95 |
self.context = SkinContext.SkinContext(contextIn, kwAddedContext) |
|---|
| 96 |
self.context.__skinner__ = weakref.ref(self) |
|---|
| 97 |
self.context.__root__ = rootPath |
|---|
| 98 |
|
|---|
| 99 |
self._do_parse(parser.ParseFile, file) |
|---|
| 100 |
|
|---|
| 101 |
self.context = None |
|---|
| 102 |
result, self._LastCompleteElement = self._LastCompleteElement, None |
|---|
| 103 |
del self._elements |
|---|
| 104 |
del self._LastCompleteElement |
|---|
| 105 |
return result |
|---|
| 106 |
|
|---|
| 107 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 108 |
|
|---|
| 109 |
def GraftXML(self, GraftElements, xml, **kwAddedContext): |
|---|
| 110 |
rootPath = self._GetRootPath(kwAddedContext) |
|---|
| 111 |
self._elements, self._LastCompleteElement = GraftElements, GraftElements[-1] |
|---|
| 112 |
|
|---|
| 113 |
parser = self._CreateParser() |
|---|
| 114 |
|
|---|
| 115 |
parentContext = GraftElements[-1].context |
|---|
| 116 |
self.context = SkinContext.SkinContext(parentContext, kwAddedContext) |
|---|
| 117 |
self.context.__skinner__ = weakref.ref(self) |
|---|
| 118 |
self.context.__root__ = rootPath |
|---|
| 119 |
self._LastCompleteElement.context = self.context |
|---|
| 120 |
|
|---|
| 121 |
self._do_parse(parser.Parse, xml) |
|---|
| 122 |
|
|---|
| 123 |
self.context = None |
|---|
| 124 |
GraftElements[-1].context = parentContext |
|---|
| 125 |
result, self._LastCompleteElement = self._LastCompleteElement, None |
|---|
| 126 |
del self._elements |
|---|
| 127 |
del self._LastCompleteElement |
|---|
| 128 |
return result |
|---|
| 129 |
|
|---|
| 130 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 131 |
|
|---|
| 132 |
def GraftFile(self, GraftElements, file, **kwAddedContext): |
|---|
| 133 |
if isinstance(file, str): |
|---|
| 134 |
rootPath = os.path.dirname(os.path.abspath(file)) |
|---|
| 135 |
file = os.path.abspath(file) |
|---|
| 136 |
file = open(file, 'r') |
|---|
| 137 |
else: |
|---|
| 138 |
rootPath = self._GetRootPath(kwAddedContext) |
|---|
| 139 |
|
|---|
| 140 |
self._elements, self._LastCompleteElement = GraftElements, GraftElements[-1] |
|---|
| 141 |
|
|---|
| 142 |
parser = self._CreateParser() |
|---|
| 143 |
|
|---|
| 144 |
parentContext = GraftElements[-1].context |
|---|
| 145 |
self.context = SkinContext.SkinContext(parentContext, kwAddedContext) |
|---|
| 146 |
self.context.__skinner__ = weakref.ref(self) |
|---|
| 147 |
self.context.__root__ = rootPath |
|---|
| 148 |
self._LastCompleteElement.context = self.context |
|---|
| 149 |
|
|---|
| 150 |
self._do_parse(parser.ParseFile, file) |
|---|
| 151 |
|
|---|
| 152 |
self.context = None |
|---|
| 153 |
GraftElements[-1].context = parentContext |
|---|
| 154 |
result, self._LastCompleteElement = self._LastCompleteElement, None |
|---|
| 155 |
del self._elements |
|---|
| 156 |
del self._LastCompleteElement |
|---|
| 157 |
return result |
|---|
| 158 |
|
|---|
| 159 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 160 |
#~ Protected Methods |
|---|
| 161 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 162 |
|
|---|
| 163 |
def _SkinXMLFromTopElement(self, xml, kwAddedContext): |
|---|
| 164 |
element = self._elements[-1] |
|---|
| 165 |
parser = self._CreateParser() |
|---|
| 166 |
element.context.__skinner__ = weakref.ref(self) |
|---|
| 167 |
element.context._update(kwAddedContext) |
|---|
| 168 |
rootPath = self._GetRootPath(kwAddedContext) |
|---|
| 169 |
if rootPath: element.context.__root__ = rootPath |
|---|
| 170 |
|
|---|
| 171 |
self._do_parse(parser.Parse, xml) |
|---|
| 172 |
|
|---|
| 173 |
return self._LastCompleteElement |
|---|
| 174 |
|
|---|
| 175 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 176 |
|
|---|
| 177 |
def _SkinFileFromTopElement(self, file, kwAddedContext): |
|---|
| 178 |
element = self._elements[-1] |
|---|
| 179 |
if isinstance(file, str): |
|---|
| 180 |
rootPath = os.path.dirname(os.path.abspath(file)) |
|---|
| 181 |
file = os.path.join(element.context.__root__, file) |
|---|
| 182 |
file = os.path.abspath(file) |
|---|
| 183 |
file = open(file, 'r') |
|---|
| 184 |
else: self._GetRootPath(kwAddedContext) |
|---|
| 185 |
|
|---|
| 186 |
parser = self._CreateParser() |
|---|
| 187 |
element.context.__skinner__ = weakref.ref(self) |
|---|
| 188 |
if rootPath: element.context.__root__ = rootPath |
|---|
| 189 |
element.context._update(kwAddedContext) |
|---|
| 190 |
|
|---|
| 191 |
self._do_parse(parser.ParseFile, file) |
|---|
| 192 |
|
|---|
| 193 |
return self._LastCompleteElement |
|---|
| 194 |
|
|---|
| 195 |
def _GetRootPath(self, context, default=None): |
|---|
| 196 |
if default is None: |
|---|
| 197 |
result = context.get('__root__', None) or self.rootPath or os.getcwd() |
|---|
| 198 |
else: |
|---|
| 199 |
result = context.get('__root__', default) |
|---|
| 200 |
return result |
|---|
| 201 |
|
|---|