| 1 |
import syck |
|---|
| 2 |
from pyramid import utils, core |
|---|
| 3 |
from pyramid.path import path |
|---|
| 4 |
from pprint import pprint |
|---|
| 5 |
from cStringIO import StringIO |
|---|
| 6 |
import re |
|---|
| 7 |
from urlparse import urlsplit |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
class fragment: |
|---|
| 11 |
''' an inline fragment which contains data and a template |
|---|
| 12 |
''' |
|---|
| 13 |
def __init__(self, node): |
|---|
| 14 |
self.template = node.get('template',None) |
|---|
| 15 |
self.locals = node.get('local',{}) |
|---|
| 16 |
self.globals = node.get('global',{}) |
|---|
| 17 |
self.file = node.get('file',None) |
|---|
| 18 |
|
|---|
| 19 |
def __repr__(self): |
|---|
| 20 |
s = StringIO() |
|---|
| 21 |
try: |
|---|
| 22 |
pprint({'template':self.template,'globals':self.globals,'locals':self.locals},s,4,10) |
|---|
| 23 |
except TypeError: |
|---|
| 24 |
pprint({'template':self.template,'globals':self.globals,'locals':self.locals},s) |
|---|
| 25 |
return s.getvalue() |
|---|
| 26 |
|
|---|
| 27 |
def __eq__(self,other): |
|---|
| 28 |
if self.template == other.template and \ |
|---|
| 29 |
self.locals == other.locals and \ |
|---|
| 30 |
self.globals == other.globals and \ |
|---|
| 31 |
self.file == other.file: |
|---|
| 32 |
return True |
|---|
| 33 |
else: |
|---|
| 34 |
return False |
|---|
| 35 |
|
|---|
| 36 |
def __ne__(self,other): |
|---|
| 37 |
if self.template == other.template and \ |
|---|
| 38 |
self.locals == other.locals and \ |
|---|
| 39 |
self.globals == other.globals and \ |
|---|
| 40 |
self.file == other.file: |
|---|
| 41 |
return False |
|---|
| 42 |
else: |
|---|
| 43 |
return True |
|---|
| 44 |
|
|---|
| 45 |
class fragmentFile: |
|---|
| 46 |
''' A file fragment which points at another yaml file |
|---|
| 47 |
''' |
|---|
| 48 |
def __init__(self, node): |
|---|
| 49 |
self.file = node |
|---|
| 50 |
|
|---|
| 51 |
def __repr__(self): |
|---|
| 52 |
s = StringIO() |
|---|
| 53 |
try: |
|---|
| 54 |
pprint({'file':self.file},s,4,10) |
|---|
| 55 |
except TypeError: |
|---|
| 56 |
pprint({'file':self.file},s) |
|---|
| 57 |
return s.getvalue() |
|---|
| 58 |
|
|---|
| 59 |
def __eq__(self,other): |
|---|
| 60 |
return self.file == other.file |
|---|
| 61 |
|
|---|
| 62 |
def __ne__(self,other): |
|---|
| 63 |
return self.file != other.file |
|---|
| 64 |
|
|---|
| 65 |
def fragmentConstructor(node): |
|---|
| 66 |
try: |
|---|
| 67 |
if node.keys(): |
|---|
| 68 |
return fragment(node) |
|---|
| 69 |
except AttributeError: |
|---|
| 70 |
return fragmentFile(node) |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
class rest(str): |
|---|
| 74 |
''' a rest string for converting to html |
|---|
| 75 |
''' |
|---|
| 76 |
pass |
|---|
| 77 |
|
|---|
| 78 |
class restfile(str): |
|---|
| 79 |
''' a rest file for converting to html |
|---|
| 80 |
''' |
|---|
| 81 |
pass |
|---|
| 82 |
|
|---|
| 83 |
class html(str): |
|---|
| 84 |
''' a html block for converting to html |
|---|
| 85 |
''' |
|---|
| 86 |
pass |
|---|
| 87 |
|
|---|
| 88 |
class htmlfile(str): |
|---|
| 89 |
''' a html file for converting to html |
|---|
| 90 |
''' |
|---|
| 91 |
pass |
|---|
| 92 |
|
|---|
| 93 |
class htfile(str): |
|---|
| 94 |
''' a ht file for converting to html |
|---|
| 95 |
''' |
|---|
| 96 |
pass |
|---|
| 97 |
|
|---|
| 98 |
class wikiurl(str): |
|---|
| 99 |
''' a ht file for converting to html |
|---|
| 100 |
''' |
|---|
| 101 |
pass |
|---|
| 102 |
|
|---|
| 103 |
class htfiledata: |
|---|
| 104 |
''' a ht file for converting to ht file datafile and key |
|---|
| 105 |
''' |
|---|
| 106 |
def __init__(self,node): |
|---|
| 107 |
self.file = node['file'] |
|---|
| 108 |
self.key = node.get('key',None) |
|---|
| 109 |
|
|---|
| 110 |
def __repr__(self): |
|---|
| 111 |
return '<htfiledata: file=%s, key=%s>'%(self.file,self.key) |
|---|
| 112 |
|
|---|
| 113 |
def __eq__(self,other): |
|---|
| 114 |
return self.file == other.file and self.key == other.key |
|---|
| 115 |
|
|---|
| 116 |
def __ne__(self,other): |
|---|
| 117 |
return self.file != other.file or self.key != other.key |
|---|
| 118 |
|
|---|
| 119 |
class restfiledata: |
|---|
| 120 |
''' a ht file for converting to ht file datafile and key |
|---|
| 121 |
''' |
|---|
| 122 |
def __init__(self,node): |
|---|
| 123 |
self.file = node['file'] |
|---|
| 124 |
self.key = node.get('key',None) |
|---|
| 125 |
|
|---|
| 126 |
def __repr__(self): |
|---|
| 127 |
return '<htfiledata: file=%s, key=%s>'%(self.file,self.key) |
|---|
| 128 |
|
|---|
| 129 |
def __eq__(self,other): |
|---|
| 130 |
return self.file == other.file and self.key == other.key |
|---|
| 131 |
|
|---|
| 132 |
def __ne__(self,other): |
|---|
| 133 |
return self.file != other.file or self.key != other.key |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
class rhref(str): |
|---|
| 137 |
''' relative href |
|---|
| 138 |
''' |
|---|
| 139 |
def __repr__(self): |
|---|
| 140 |
return '<rhref %s>' % str.__repr__(self) |
|---|
| 141 |
|
|---|
| 142 |
class ahref(str): |
|---|
| 143 |
''' absolute href |
|---|
| 144 |
''' |
|---|
| 145 |
def __repr__(self): |
|---|
| 146 |
return '<ahref %s>' % str.__repr__(self) |
|---|
| 147 |
|
|---|
| 148 |
def url(text): |
|---|
| 149 |
''' Factory to convert a string into a label and href (href is last space delimited item in string) |
|---|
| 150 |
''' |
|---|
| 151 |
|
|---|
| 152 |
t = text.split(' ') |
|---|
| 153 |
label = ' '.join(t[:-1]) |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
getlabelparts = re.compile('([^{}]*)(?: {(.*)})?$') |
|---|
| 157 |
m = getlabelparts.match(label) |
|---|
| 158 |
if m: |
|---|
| 159 |
label,title = m.groups() |
|---|
| 160 |
else: |
|---|
| 161 |
title = None |
|---|
| 162 |
if title is None: |
|---|
| 163 |
title = '' |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
url = t[-1] |
|---|
| 167 |
urlparts = urlsplit(url) |
|---|
| 168 |
if urlparts[0] != '' or urlparts[2].startswith('/'): |
|---|
| 169 |
url = ahref(url) |
|---|
| 170 |
else: |
|---|
| 171 |
url = rhref(url) |
|---|
| 172 |
|
|---|
| 173 |
return {'href': url, 'label': label, 'title': title} |
|---|
| 174 |
|
|---|
| 175 |
def linktree(node): |
|---|
| 176 |
''' nav directive creates a tree of data/children nodes from an indented list of strings |
|---|
| 177 |
''' |
|---|
| 178 |
return utils.parseIndentedList(node,url) |
|---|
| 179 |
|
|---|
| 180 |
class sectionnav(list): |
|---|
| 181 |
''' section navigation (inherited) |
|---|
| 182 |
''' |
|---|
| 183 |
def __init__(self, node): |
|---|
| 184 |
list.__init__(self) |
|---|
| 185 |
self.extend(utils.parseIndentedList(node,url)) |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
class breadcrumb(str): |
|---|
| 189 |
''' a marker for the breadcrumb renderer |
|---|
| 190 |
''' |
|---|
| 191 |
def __init__(self,node): |
|---|
| 192 |
fields = node.split() |
|---|
| 193 |
self.file = fields[0] |
|---|
| 194 |
self.name = fields[1] |
|---|
| 195 |
|
|---|
| 196 |
class acquire(str): |
|---|
| 197 |
''' A marker for for extracting data from another fragment (This may |
|---|
| 198 |
cause problems as the target data may not be available at the point of flattening) |
|---|
| 199 |
''' |
|---|
| 200 |
def __init__(self,node): |
|---|
| 201 |
fields = node.split() |
|---|
| 202 |
self.file = fields[0] |
|---|
| 203 |
self.name = fields[1] |
|---|
| 204 |
self.template = '' |
|---|
| 205 |
def __repr__(self): |
|---|
| 206 |
s = StringIO() |
|---|
| 207 |
try: |
|---|
| 208 |
pprint({'template':self.template,'file':self.file,'name':self.name},s,4,10) |
|---|
| 209 |
except TypeError: |
|---|
| 210 |
pprint({'template':self.template,'file':self.file,'name':self.name},s) |
|---|
| 211 |
return s.getvalue() |
|---|
| 212 |
|
|---|
| 213 |
def pathconstruct(pathstring): |
|---|
| 214 |
return path(pathstring) |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
def context(node): |
|---|
| 218 |
return core.Context(node) |
|---|
| 219 |
|
|---|
| 220 |
class Loader(syck.Loader): |
|---|
| 221 |
|
|---|
| 222 |
__constructs = {} |
|---|
| 223 |
|
|---|
| 224 |
def register_construct(cls, func, name, tld=None, year=None): |
|---|
| 225 |
if tld is not None: |
|---|
| 226 |
tld = str(tld) |
|---|
| 227 |
if year is not None: |
|---|
| 228 |
year = int(year) |
|---|
| 229 |
cls.__constructs[(tld,year,name)] = func |
|---|
| 230 |
|
|---|
| 231 |
register_construct = classmethod(register_construct) |
|---|
| 232 |
|
|---|
| 233 |
def construct(self, node): |
|---|
| 234 |
if node.tag is not None: |
|---|
| 235 |
parts = node.tag.split(':',2) |
|---|
| 236 |
if parts[0] == 'tag': |
|---|
| 237 |
tld, year = parts[1].split(',') |
|---|
| 238 |
year = int(year) |
|---|
| 239 |
name = parts[2] |
|---|
| 240 |
if parts[0] == 'x-private': |
|---|
| 241 |
tld = None |
|---|
| 242 |
year = None |
|---|
| 243 |
name = parts[1] |
|---|
| 244 |
|
|---|
| 245 |
if tld or year or name: |
|---|
| 246 |
construct = self.__constructs.get((tld,year,name)) |
|---|
| 247 |
if construct is not None: |
|---|
| 248 |
return construct(node.value) |
|---|
| 249 |
return syck.Loader.construct(self, node) |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
_typeRegistry = { |
|---|
| 253 |
('yaml.org','2002','fragment'): fragmentConstructor, |
|---|
| 254 |
('yaml.org','2002','rest'): rest, |
|---|
| 255 |
('yaml.org','2002','restfile'): restfile, |
|---|
| 256 |
('yaml.org','2002','url'): url, |
|---|
| 257 |
('yaml.org','2002','linktree'): linktree, |
|---|
| 258 |
('yaml.org','2002','sectionnav'): sectionnav, |
|---|
| 259 |
('yaml.org','2002','breadcrumb'): breadcrumb, |
|---|
| 260 |
('yaml.org','2002','acquire'): acquire, |
|---|
| 261 |
('yaml.org','2002','html'): html, |
|---|
| 262 |
('yaml.org','2002','htmlfile'): htmlfile, |
|---|
| 263 |
('yaml.org','2002','htfile'): htfile, |
|---|
| 264 |
('yaml.org','2002','htfiledata'): htfiledata, |
|---|
| 265 |
('yaml.org','2002','restfiledata'): restfiledata, |
|---|
| 266 |
('yaml.org','2002','wikiurl'): wikiurl, |
|---|
| 267 |
('python.yaml.org','2002','object:pyramid.path.path'): pathconstruct, |
|---|
| 268 |
('python.yaml.org','2002','object:pyramid.core.Context'): context, |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
for vartype, factory in _typeRegistry.items(): |
|---|
| 272 |
Loader.register_construct(factory, vartype[2],tld=vartype[0],year=vartype[1]) |
|---|