_ruamel_yaml.pyx 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. def get_version_string():
  2. cdef char *value
  3. value = yaml_get_version_string()
  4. if PY_MAJOR_VERSION < 3:
  5. return value
  6. else:
  7. return PyUnicode_FromString(value)
  8. def get_version():
  9. cdef int major, minor, patch
  10. yaml_get_version(&major, &minor, &patch)
  11. return (major, minor, patch)
  12. #Mark = yaml.error.Mark
  13. from ruamel.yaml.error import YAMLError
  14. from ruamel.yaml.reader import ReaderError
  15. from ruamel.yaml.scanner import ScannerError
  16. from ruamel.yaml.parser import ParserError
  17. from ruamel.yaml.composer import ComposerError
  18. from ruamel.yaml.constructor import ConstructorError
  19. from ruamel.yaml.emitter import EmitterError
  20. from ruamel.yaml.serializer import SerializerError
  21. from ruamel.yaml.representer import RepresenterError
  22. from ruamel.yaml.tokens import StreamStartToken
  23. from ruamel.yaml.tokens import StreamEndToken
  24. from ruamel.yaml.tokens import DirectiveToken
  25. from ruamel.yaml.tokens import DocumentStartToken
  26. from ruamel.yaml.tokens import DocumentEndToken
  27. from ruamel.yaml.tokens import BlockSequenceStartToken
  28. from ruamel.yaml.tokens import BlockMappingStartToken
  29. from ruamel.yaml.tokens import BlockEndToken
  30. from ruamel.yaml.tokens import FlowSequenceStartToken
  31. from ruamel.yaml.tokens import FlowMappingStartToken
  32. from ruamel.yaml.tokens import FlowSequenceEndToken
  33. from ruamel.yaml.tokens import FlowMappingEndToken
  34. from ruamel.yaml.tokens import KeyToken
  35. from ruamel.yaml.tokens import ValueToken
  36. from ruamel.yaml.tokens import BlockEntryToken
  37. from ruamel.yaml.tokens import FlowEntryToken
  38. from ruamel.yaml.tokens import AliasToken
  39. from ruamel.yaml.tokens import AnchorToken
  40. from ruamel.yaml.tokens import TagToken
  41. from ruamel.yaml.tokens import ScalarToken
  42. from ruamel.yaml.events import StreamStartEvent
  43. from ruamel.yaml.events import StreamEndEvent
  44. from ruamel.yaml.events import DocumentStartEvent
  45. from ruamel.yaml.events import DocumentEndEvent
  46. from ruamel.yaml.events import AliasEvent
  47. from ruamel.yaml.events import ScalarEvent
  48. from ruamel.yaml.events import SequenceStartEvent
  49. from ruamel.yaml.events import SequenceEndEvent
  50. from ruamel.yaml.events import MappingStartEvent
  51. from ruamel.yaml.events import MappingEndEvent
  52. from ruamel.yaml.nodes import ScalarNode
  53. from ruamel.yaml.nodes import SequenceNode
  54. from ruamel.yaml.nodes import MappingNode
  55. cdef class Mark:
  56. cdef readonly object name
  57. cdef readonly size_t index
  58. cdef readonly size_t line
  59. cdef readonly size_t column
  60. cdef readonly buffer
  61. cdef readonly pointer
  62. def __init__(self, object name, size_t index, size_t line, size_t column,
  63. object buffer, object pointer):
  64. self.name = name
  65. self.index = index
  66. self.line = line
  67. self.column = column
  68. self.buffer = buffer
  69. self.pointer = pointer
  70. def get_snippet(self):
  71. return None
  72. def __str__(self):
  73. where = " in \"%s\", line %d, column %d" \
  74. % (self.name, self.line+1, self.column+1)
  75. return where
  76. #class YAMLError(Exception):
  77. # pass
  78. #
  79. #class MarkedYAMLError(YAMLError):
  80. #
  81. # def __init__(self, context=None, context_mark=None,
  82. # problem=None, problem_mark=None, note=None):
  83. # self.context = context
  84. # self.context_mark = context_mark
  85. # self.problem = problem
  86. # self.problem_mark = problem_mark
  87. # self.note = note
  88. #
  89. # def __str__(self):
  90. # lines = []
  91. # if self.context is not None:
  92. # lines.append(self.context)
  93. # if self.context_mark is not None \
  94. # and (self.problem is None or self.problem_mark is None
  95. # or self.context_mark.name != self.problem_mark.name
  96. # or self.context_mark.line != self.problem_mark.line
  97. # or self.context_mark.column != self.problem_mark.column):
  98. # lines.append(str(self.context_mark))
  99. # if self.problem is not None:
  100. # lines.append(self.problem)
  101. # if self.problem_mark is not None:
  102. # lines.append(str(self.problem_mark))
  103. # if self.note is not None:
  104. # lines.append(self.note)
  105. # return '\n'.join(lines)
  106. #
  107. #class ReaderError(YAMLError):
  108. #
  109. # def __init__(self, name, position, character, encoding, reason):
  110. # self.name = name
  111. # self.character = character
  112. # self.position = position
  113. # self.encoding = encoding
  114. # self.reason = reason
  115. #
  116. # def __str__(self):
  117. # if isinstance(self.character, str):
  118. # return "'%s' codec can't decode byte #x%02x: %s\n" \
  119. # " in \"%s\", position %d" \
  120. # % (self.encoding, ord(self.character), self.reason,
  121. # self.name, self.position)
  122. # else:
  123. # return "unacceptable character #x%04x: %s\n" \
  124. # " in \"%s\", position %d" \
  125. # % (ord(self.character), self.reason,
  126. # self.name, self.position)
  127. #
  128. #class ScannerError(MarkedYAMLError):
  129. # pass
  130. #
  131. #class ParserError(MarkedYAMLError):
  132. # pass
  133. #
  134. #class EmitterError(YAMLError):
  135. # pass
  136. #
  137. #cdef class Token:
  138. # cdef readonly Mark start_mark
  139. # cdef readonly Mark end_mark
  140. # def __init__(self, Mark start_mark, Mark end_mark):
  141. # self.start_mark = start_mark
  142. # self.end_mark = end_mark
  143. #
  144. #cdef class StreamStartToken(Token):
  145. # cdef readonly object encoding
  146. # def __init__(self, Mark start_mark, Mark end_mark, encoding):
  147. # self.start_mark = start_mark
  148. # self.end_mark = end_mark
  149. # self.encoding = encoding
  150. #
  151. #cdef class StreamEndToken(Token):
  152. # pass
  153. #
  154. #cdef class DirectiveToken(Token):
  155. # cdef readonly object name
  156. # cdef readonly object value
  157. # def __init__(self, name, value, Mark start_mark, Mark end_mark):
  158. # self.name = name
  159. # self.value = value
  160. # self.start_mark = start_mark
  161. # self.end_mark = end_mark
  162. #
  163. #cdef class DocumentStartToken(Token):
  164. # pass
  165. #
  166. #cdef class DocumentEndToken(Token):
  167. # pass
  168. #
  169. #cdef class BlockSequenceStartToken(Token):
  170. # pass
  171. #
  172. #cdef class BlockMappingStartToken(Token):
  173. # pass
  174. #
  175. #cdef class BlockEndToken(Token):
  176. # pass
  177. #
  178. #cdef class FlowSequenceStartToken(Token):
  179. # pass
  180. #
  181. #cdef class FlowMappingStartToken(Token):
  182. # pass
  183. #
  184. #cdef class FlowSequenceEndToken(Token):
  185. # pass
  186. #
  187. #cdef class FlowMappingEndToken(Token):
  188. # pass
  189. #
  190. #cdef class KeyToken(Token):
  191. # pass
  192. #
  193. #cdef class ValueToken(Token):
  194. # pass
  195. #
  196. #cdef class BlockEntryToken(Token):
  197. # pass
  198. #
  199. #cdef class FlowEntryToken(Token):
  200. # pass
  201. #
  202. #cdef class AliasToken(Token):
  203. # cdef readonly object value
  204. # def __init__(self, value, Mark start_mark, Mark end_mark):
  205. # self.value = value
  206. # self.start_mark = start_mark
  207. # self.end_mark = end_mark
  208. #
  209. #cdef class AnchorToken(Token):
  210. # cdef readonly object value
  211. # def __init__(self, value, Mark start_mark, Mark end_mark):
  212. # self.value = value
  213. # self.start_mark = start_mark
  214. # self.end_mark = end_mark
  215. #
  216. #cdef class TagToken(Token):
  217. # cdef readonly object value
  218. # def __init__(self, value, Mark start_mark, Mark end_mark):
  219. # self.value = value
  220. # self.start_mark = start_mark
  221. # self.end_mark = end_mark
  222. #
  223. #cdef class ScalarToken(Token):
  224. # cdef readonly object value
  225. # cdef readonly object plain
  226. # cdef readonly object style
  227. # def __init__(self, value, plain, Mark start_mark, Mark end_mark, style=None):
  228. # self.value = value
  229. # self.plain = plain
  230. # self.start_mark = start_mark
  231. # self.end_mark = end_mark
  232. # self.style = style
  233. cdef class CParser:
  234. cdef yaml_parser_t parser
  235. cdef yaml_event_t parsed_event
  236. cdef object stream
  237. cdef object stream_name
  238. cdef object current_token
  239. cdef object current_event
  240. cdef object anchors
  241. cdef object stream_cache
  242. cdef int stream_cache_len
  243. cdef int stream_cache_pos
  244. cdef int unicode_source
  245. def __init__(self, stream):
  246. cdef is_readable
  247. if yaml_parser_initialize(&self.parser) == 0:
  248. raise MemoryError
  249. self.parsed_event.type = YAML_NO_EVENT
  250. is_readable = 1
  251. try:
  252. stream.read
  253. except AttributeError:
  254. is_readable = 0
  255. self.unicode_source = 0
  256. if is_readable:
  257. self.stream = stream
  258. try:
  259. self.stream_name = stream.name
  260. except AttributeError:
  261. if PY_MAJOR_VERSION < 3:
  262. self.stream_name = '<file>'
  263. else:
  264. self.stream_name = u'<file>'
  265. self.stream_cache = None
  266. self.stream_cache_len = 0
  267. self.stream_cache_pos = 0
  268. yaml_parser_set_input(&self.parser, input_handler, <void *>self)
  269. else:
  270. if PyUnicode_CheckExact(stream) != 0:
  271. stream = PyUnicode_AsUTF8String(stream)
  272. if PY_MAJOR_VERSION < 3:
  273. self.stream_name = '<unicode string>'
  274. else:
  275. self.stream_name = u'<unicode string>'
  276. self.unicode_source = 1
  277. else:
  278. if PY_MAJOR_VERSION < 3:
  279. self.stream_name = '<byte string>'
  280. else:
  281. self.stream_name = u'<byte string>'
  282. if PyString_CheckExact(stream) == 0:
  283. if PY_MAJOR_VERSION < 3:
  284. raise TypeError("a string or stream input is required")
  285. else:
  286. raise TypeError(u"a string or stream input is required")
  287. self.stream = stream
  288. yaml_parser_set_input_string(&self.parser, PyString_AS_STRING(stream), PyString_GET_SIZE(stream))
  289. self.current_token = None
  290. self.current_event = None
  291. self.anchors = {}
  292. def __dealloc__(self):
  293. yaml_parser_delete(&self.parser)
  294. yaml_event_delete(&self.parsed_event)
  295. def dispose(self):
  296. pass
  297. cdef object _parser_error(self):
  298. if self.parser.error == YAML_MEMORY_ERROR:
  299. return MemoryError
  300. elif self.parser.error == YAML_READER_ERROR:
  301. if PY_MAJOR_VERSION < 3:
  302. return ReaderError(self.stream_name, self.parser.problem_offset,
  303. self.parser.problem_value, '?', self.parser.problem)
  304. else:
  305. return ReaderError(self.stream_name, self.parser.problem_offset,
  306. self.parser.problem_value, u'?', PyUnicode_FromString(self.parser.problem))
  307. elif self.parser.error == YAML_SCANNER_ERROR \
  308. or self.parser.error == YAML_PARSER_ERROR:
  309. context_mark = None
  310. problem_mark = None
  311. if self.parser.context != NULL:
  312. context_mark = Mark(self.stream_name,
  313. self.parser.context_mark.index,
  314. self.parser.context_mark.line,
  315. self.parser.context_mark.column, None, None)
  316. if self.parser.problem != NULL:
  317. problem_mark = Mark(self.stream_name,
  318. self.parser.problem_mark.index,
  319. self.parser.problem_mark.line,
  320. self.parser.problem_mark.column, None, None)
  321. context = None
  322. if self.parser.context != NULL:
  323. if PY_MAJOR_VERSION < 3:
  324. context = self.parser.context
  325. else:
  326. context = PyUnicode_FromString(self.parser.context)
  327. if PY_MAJOR_VERSION < 3:
  328. problem = self.parser.problem
  329. else:
  330. problem = PyUnicode_FromString(self.parser.problem)
  331. if self.parser.error == YAML_SCANNER_ERROR:
  332. return ScannerError(context, context_mark, problem, problem_mark)
  333. else:
  334. return ParserError(context, context_mark, problem, problem_mark)
  335. if PY_MAJOR_VERSION < 3:
  336. raise ValueError("no parser error")
  337. else:
  338. raise ValueError(u"no parser error")
  339. def raw_scan(self):
  340. cdef yaml_token_t token
  341. cdef int done
  342. cdef int count
  343. count = 0
  344. done = 0
  345. while done == 0:
  346. if yaml_parser_scan(&self.parser, &token) == 0:
  347. error = self._parser_error()
  348. raise error
  349. if token.type == YAML_NO_TOKEN:
  350. done = 1
  351. else:
  352. count = count+1
  353. yaml_token_delete(&token)
  354. return count
  355. cdef object _scan(self):
  356. cdef yaml_token_t token
  357. if yaml_parser_scan(&self.parser, &token) == 0:
  358. error = self._parser_error()
  359. raise error
  360. token_object = self._token_to_object(&token)
  361. yaml_token_delete(&token)
  362. return token_object
  363. cdef object _token_to_object(self, yaml_token_t *token):
  364. start_mark = Mark(self.stream_name,
  365. token.start_mark.index,
  366. token.start_mark.line,
  367. token.start_mark.column,
  368. None, None)
  369. end_mark = Mark(self.stream_name,
  370. token.end_mark.index,
  371. token.end_mark.line,
  372. token.end_mark.column,
  373. None, None)
  374. if token.type == YAML_NO_TOKEN:
  375. return None
  376. elif token.type == YAML_STREAM_START_TOKEN:
  377. encoding = None
  378. if token.data.stream_start.encoding == YAML_UTF8_ENCODING:
  379. if self.unicode_source == 0:
  380. encoding = u"utf-8"
  381. elif token.data.stream_start.encoding == YAML_UTF16LE_ENCODING:
  382. encoding = u"utf-16-le"
  383. elif token.data.stream_start.encoding == YAML_UTF16BE_ENCODING:
  384. encoding = u"utf-16-be"
  385. return StreamStartToken(start_mark, end_mark, encoding)
  386. elif token.type == YAML_STREAM_END_TOKEN:
  387. return StreamEndToken(start_mark, end_mark)
  388. elif token.type == YAML_VERSION_DIRECTIVE_TOKEN:
  389. return DirectiveToken(u"YAML",
  390. (token.data.version_directive.major,
  391. token.data.version_directive.minor),
  392. start_mark, end_mark)
  393. elif token.type == YAML_TAG_DIRECTIVE_TOKEN:
  394. handle = PyUnicode_FromString(token.data.tag_directive.handle)
  395. prefix = PyUnicode_FromString(token.data.tag_directive.prefix)
  396. return DirectiveToken(u"TAG", (handle, prefix),
  397. start_mark, end_mark)
  398. elif token.type == YAML_DOCUMENT_START_TOKEN:
  399. return DocumentStartToken(start_mark, end_mark)
  400. elif token.type == YAML_DOCUMENT_END_TOKEN:
  401. return DocumentEndToken(start_mark, end_mark)
  402. elif token.type == YAML_BLOCK_SEQUENCE_START_TOKEN:
  403. return BlockSequenceStartToken(start_mark, end_mark)
  404. elif token.type == YAML_BLOCK_MAPPING_START_TOKEN:
  405. return BlockMappingStartToken(start_mark, end_mark)
  406. elif token.type == YAML_BLOCK_END_TOKEN:
  407. return BlockEndToken(start_mark, end_mark)
  408. elif token.type == YAML_FLOW_SEQUENCE_START_TOKEN:
  409. return FlowSequenceStartToken(start_mark, end_mark)
  410. elif token.type == YAML_FLOW_SEQUENCE_END_TOKEN:
  411. return FlowSequenceEndToken(start_mark, end_mark)
  412. elif token.type == YAML_FLOW_MAPPING_START_TOKEN:
  413. return FlowMappingStartToken(start_mark, end_mark)
  414. elif token.type == YAML_FLOW_MAPPING_END_TOKEN:
  415. return FlowMappingEndToken(start_mark, end_mark)
  416. elif token.type == YAML_BLOCK_ENTRY_TOKEN:
  417. return BlockEntryToken(start_mark, end_mark)
  418. elif token.type == YAML_FLOW_ENTRY_TOKEN:
  419. return FlowEntryToken(start_mark, end_mark)
  420. elif token.type == YAML_KEY_TOKEN:
  421. return KeyToken(start_mark, end_mark)
  422. elif token.type == YAML_VALUE_TOKEN:
  423. return ValueToken(start_mark, end_mark)
  424. elif token.type == YAML_ALIAS_TOKEN:
  425. value = PyUnicode_FromString(token.data.alias.value)
  426. return AliasToken(value, start_mark, end_mark)
  427. elif token.type == YAML_ANCHOR_TOKEN:
  428. value = PyUnicode_FromString(token.data.anchor.value)
  429. return AnchorToken(value, start_mark, end_mark)
  430. elif token.type == YAML_TAG_TOKEN:
  431. handle = PyUnicode_FromString(token.data.tag.handle)
  432. suffix = PyUnicode_FromString(token.data.tag.suffix)
  433. if not handle:
  434. handle = None
  435. return TagToken((handle, suffix), start_mark, end_mark)
  436. elif token.type == YAML_SCALAR_TOKEN:
  437. value = PyUnicode_DecodeUTF8(token.data.scalar.value,
  438. token.data.scalar.length, 'strict')
  439. plain = False
  440. style = None
  441. if token.data.scalar.style == YAML_PLAIN_SCALAR_STYLE:
  442. plain = True
  443. style = u''
  444. elif token.data.scalar.style == YAML_SINGLE_QUOTED_SCALAR_STYLE:
  445. style = u'\''
  446. elif token.data.scalar.style == YAML_DOUBLE_QUOTED_SCALAR_STYLE:
  447. style = u'"'
  448. elif token.data.scalar.style == YAML_LITERAL_SCALAR_STYLE:
  449. style = u'|'
  450. elif token.data.scalar.style == YAML_FOLDED_SCALAR_STYLE:
  451. style = u'>'
  452. return ScalarToken(value, plain,
  453. start_mark, end_mark, style)
  454. else:
  455. if PY_MAJOR_VERSION < 3:
  456. raise ValueError("unknown token type")
  457. else:
  458. raise ValueError(u"unknown token type")
  459. def get_token(self):
  460. if self.current_token is not None:
  461. value = self.current_token
  462. self.current_token = None
  463. else:
  464. value = self._scan()
  465. return value
  466. def peek_token(self):
  467. if self.current_token is None:
  468. self.current_token = self._scan()
  469. return self.current_token
  470. def check_token(self, *choices):
  471. if self.current_token is None:
  472. self.current_token = self._scan()
  473. if self.current_token is None:
  474. return False
  475. if not choices:
  476. return True
  477. token_class = self.current_token.__class__
  478. for choice in choices:
  479. if token_class is choice:
  480. return True
  481. return False
  482. def raw_parse(self):
  483. cdef yaml_event_t event
  484. cdef int done
  485. cdef int count
  486. count = 0
  487. done = 0
  488. while done == 0:
  489. if yaml_parser_parse(&self.parser, &event) == 0:
  490. error = self._parser_error()
  491. raise error
  492. if event.type == YAML_NO_EVENT:
  493. done = 1
  494. else:
  495. count = count+1
  496. yaml_event_delete(&event)
  497. return count
  498. cdef object _parse(self):
  499. cdef yaml_event_t event
  500. if yaml_parser_parse(&self.parser, &event) == 0:
  501. error = self._parser_error()
  502. raise error
  503. event_object = self._event_to_object(&event)
  504. yaml_event_delete(&event)
  505. return event_object
  506. cdef object _event_to_object(self, yaml_event_t *event):
  507. cdef yaml_tag_directive_t *tag_directive
  508. start_mark = Mark(self.stream_name,
  509. event.start_mark.index,
  510. event.start_mark.line,
  511. event.start_mark.column,
  512. None, None)
  513. end_mark = Mark(self.stream_name,
  514. event.end_mark.index,
  515. event.end_mark.line,
  516. event.end_mark.column,
  517. None, None)
  518. if event.type == YAML_NO_EVENT:
  519. return None
  520. elif event.type == YAML_STREAM_START_EVENT:
  521. encoding = None
  522. if event.data.stream_start.encoding == YAML_UTF8_ENCODING:
  523. if self.unicode_source == 0:
  524. encoding = u"utf-8"
  525. elif event.data.stream_start.encoding == YAML_UTF16LE_ENCODING:
  526. encoding = u"utf-16-le"
  527. elif event.data.stream_start.encoding == YAML_UTF16BE_ENCODING:
  528. encoding = u"utf-16-be"
  529. return StreamStartEvent(start_mark, end_mark, encoding)
  530. elif event.type == YAML_STREAM_END_EVENT:
  531. return StreamEndEvent(start_mark, end_mark)
  532. elif event.type == YAML_DOCUMENT_START_EVENT:
  533. explicit = False
  534. if event.data.document_start.implicit == 0:
  535. explicit = True
  536. version = None
  537. if event.data.document_start.version_directive != NULL:
  538. version = (event.data.document_start.version_directive.major,
  539. event.data.document_start.version_directive.minor)
  540. tags = None
  541. if event.data.document_start.tag_directives.start != NULL:
  542. tags = {}
  543. tag_directive = event.data.document_start.tag_directives.start
  544. while tag_directive != event.data.document_start.tag_directives.end:
  545. handle = PyUnicode_FromString(tag_directive.handle)
  546. prefix = PyUnicode_FromString(tag_directive.prefix)
  547. tags[handle] = prefix
  548. tag_directive = tag_directive+1
  549. return DocumentStartEvent(start_mark, end_mark,
  550. explicit, version, tags)
  551. elif event.type == YAML_DOCUMENT_END_EVENT:
  552. explicit = False
  553. if event.data.document_end.implicit == 0:
  554. explicit = True
  555. return DocumentEndEvent(start_mark, end_mark, explicit)
  556. elif event.type == YAML_ALIAS_EVENT:
  557. anchor = PyUnicode_FromString(event.data.alias.anchor)
  558. return AliasEvent(anchor, start_mark, end_mark)
  559. elif event.type == YAML_SCALAR_EVENT:
  560. anchor = None
  561. if event.data.scalar.anchor != NULL:
  562. anchor = PyUnicode_FromString(event.data.scalar.anchor)
  563. tag = None
  564. if event.data.scalar.tag != NULL:
  565. tag = PyUnicode_FromString(event.data.scalar.tag)
  566. value = PyUnicode_DecodeUTF8(event.data.scalar.value,
  567. event.data.scalar.length, 'strict')
  568. plain_implicit = False
  569. if event.data.scalar.plain_implicit == 1:
  570. plain_implicit = True
  571. quoted_implicit = False
  572. if event.data.scalar.quoted_implicit == 1:
  573. quoted_implicit = True
  574. style = None
  575. if event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE:
  576. style = u''
  577. elif event.data.scalar.style == YAML_SINGLE_QUOTED_SCALAR_STYLE:
  578. style = u'\''
  579. elif event.data.scalar.style == YAML_DOUBLE_QUOTED_SCALAR_STYLE:
  580. style = u'"'
  581. elif event.data.scalar.style == YAML_LITERAL_SCALAR_STYLE:
  582. style = u'|'
  583. elif event.data.scalar.style == YAML_FOLDED_SCALAR_STYLE:
  584. style = u'>'
  585. return ScalarEvent(anchor, tag,
  586. (plain_implicit, quoted_implicit),
  587. value, start_mark, end_mark, style)
  588. elif event.type == YAML_SEQUENCE_START_EVENT:
  589. anchor = None
  590. if event.data.sequence_start.anchor != NULL:
  591. anchor = PyUnicode_FromString(event.data.sequence_start.anchor)
  592. tag = None
  593. if event.data.sequence_start.tag != NULL:
  594. tag = PyUnicode_FromString(event.data.sequence_start.tag)
  595. implicit = False
  596. if event.data.sequence_start.implicit == 1:
  597. implicit = True
  598. flow_style = None
  599. if event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE:
  600. flow_style = True
  601. elif event.data.sequence_start.style == YAML_BLOCK_SEQUENCE_STYLE:
  602. flow_style = False
  603. return SequenceStartEvent(anchor, tag, implicit,
  604. start_mark, end_mark, flow_style)
  605. elif event.type == YAML_MAPPING_START_EVENT:
  606. anchor = None
  607. if event.data.mapping_start.anchor != NULL:
  608. anchor = PyUnicode_FromString(event.data.mapping_start.anchor)
  609. tag = None
  610. if event.data.mapping_start.tag != NULL:
  611. tag = PyUnicode_FromString(event.data.mapping_start.tag)
  612. implicit = False
  613. if event.data.mapping_start.implicit == 1:
  614. implicit = True
  615. flow_style = None
  616. if event.data.mapping_start.style == YAML_FLOW_MAPPING_STYLE:
  617. flow_style = True
  618. elif event.data.mapping_start.style == YAML_BLOCK_MAPPING_STYLE:
  619. flow_style = False
  620. return MappingStartEvent(anchor, tag, implicit,
  621. start_mark, end_mark, flow_style)
  622. elif event.type == YAML_SEQUENCE_END_EVENT:
  623. return SequenceEndEvent(start_mark, end_mark)
  624. elif event.type == YAML_MAPPING_END_EVENT:
  625. return MappingEndEvent(start_mark, end_mark)
  626. else:
  627. if PY_MAJOR_VERSION < 3:
  628. raise ValueError("unknown event type")
  629. else:
  630. raise ValueError(u"unknown event type")
  631. def get_event(self):
  632. if self.current_event is not None:
  633. value = self.current_event
  634. self.current_event = None
  635. else:
  636. value = self._parse()
  637. return value
  638. def peek_event(self):
  639. if self.current_event is None:
  640. self.current_event = self._parse()
  641. return self.current_event
  642. def check_event(self, *choices):
  643. if self.current_event is None:
  644. self.current_event = self._parse()
  645. if self.current_event is None:
  646. return False
  647. if not choices:
  648. return True
  649. event_class = self.current_event.__class__
  650. for choice in choices:
  651. if event_class is choice:
  652. return True
  653. return False
  654. def check_node(self):
  655. self._parse_next_event()
  656. if self.parsed_event.type == YAML_STREAM_START_EVENT:
  657. yaml_event_delete(&self.parsed_event)
  658. self._parse_next_event()
  659. if self.parsed_event.type != YAML_STREAM_END_EVENT:
  660. return True
  661. return False
  662. def get_node(self):
  663. self._parse_next_event()
  664. if self.parsed_event.type != YAML_STREAM_END_EVENT:
  665. return self._compose_document()
  666. def get_single_node(self):
  667. self._parse_next_event()
  668. yaml_event_delete(&self.parsed_event)
  669. self._parse_next_event()
  670. document = None
  671. if self.parsed_event.type != YAML_STREAM_END_EVENT:
  672. document = self._compose_document()
  673. self._parse_next_event()
  674. if self.parsed_event.type != YAML_STREAM_END_EVENT:
  675. mark = Mark(self.stream_name,
  676. self.parsed_event.start_mark.index,
  677. self.parsed_event.start_mark.line,
  678. self.parsed_event.start_mark.column,
  679. None, None)
  680. if PY_MAJOR_VERSION < 3:
  681. raise ComposerError("expected a single document in the stream",
  682. document.start_mark, "but found another document", mark)
  683. else:
  684. raise ComposerError(u"expected a single document in the stream",
  685. document.start_mark, u"but found another document", mark)
  686. return document
  687. cdef object _compose_document(self):
  688. yaml_event_delete(&self.parsed_event)
  689. node = self._compose_node(None, None)
  690. self._parse_next_event()
  691. yaml_event_delete(&self.parsed_event)
  692. self.anchors = {}
  693. return node
  694. cdef object _compose_node(self, object parent, object index):
  695. self._parse_next_event()
  696. if self.parsed_event.type == YAML_ALIAS_EVENT:
  697. anchor = PyUnicode_FromString(self.parsed_event.data.alias.anchor)
  698. if anchor not in self.anchors:
  699. mark = Mark(self.stream_name,
  700. self.parsed_event.start_mark.index,
  701. self.parsed_event.start_mark.line,
  702. self.parsed_event.start_mark.column,
  703. None, None)
  704. if PY_MAJOR_VERSION < 3:
  705. raise ComposerError(None, None, "found undefined alias", mark)
  706. else:
  707. raise ComposerError(None, None, u"found undefined alias", mark)
  708. yaml_event_delete(&self.parsed_event)
  709. return self.anchors[anchor]
  710. anchor = None
  711. if self.parsed_event.type == YAML_SCALAR_EVENT \
  712. and self.parsed_event.data.scalar.anchor != NULL:
  713. anchor = PyUnicode_FromString(self.parsed_event.data.scalar.anchor)
  714. elif self.parsed_event.type == YAML_SEQUENCE_START_EVENT \
  715. and self.parsed_event.data.sequence_start.anchor != NULL:
  716. anchor = PyUnicode_FromString(self.parsed_event.data.sequence_start.anchor)
  717. elif self.parsed_event.type == YAML_MAPPING_START_EVENT \
  718. and self.parsed_event.data.mapping_start.anchor != NULL:
  719. anchor = PyUnicode_FromString(self.parsed_event.data.mapping_start.anchor)
  720. if anchor is not None:
  721. if anchor in self.anchors:
  722. mark = Mark(self.stream_name,
  723. self.parsed_event.start_mark.index,
  724. self.parsed_event.start_mark.line,
  725. self.parsed_event.start_mark.column,
  726. None, None)
  727. if PY_MAJOR_VERSION < 3:
  728. raise ComposerError("found duplicate anchor; first occurrence",
  729. self.anchors[anchor].start_mark, "second occurrence", mark)
  730. else:
  731. raise ComposerError(u"found duplicate anchor; first occurrence",
  732. self.anchors[anchor].start_mark, u"second occurrence", mark)
  733. self.descend_resolver(parent, index)
  734. if self.parsed_event.type == YAML_SCALAR_EVENT:
  735. node = self._compose_scalar_node(anchor)
  736. elif self.parsed_event.type == YAML_SEQUENCE_START_EVENT:
  737. node = self._compose_sequence_node(anchor)
  738. elif self.parsed_event.type == YAML_MAPPING_START_EVENT:
  739. node = self._compose_mapping_node(anchor)
  740. self.ascend_resolver()
  741. return node
  742. cdef _compose_scalar_node(self, object anchor):
  743. start_mark = Mark(self.stream_name,
  744. self.parsed_event.start_mark.index,
  745. self.parsed_event.start_mark.line,
  746. self.parsed_event.start_mark.column,
  747. None, None)
  748. end_mark = Mark(self.stream_name,
  749. self.parsed_event.end_mark.index,
  750. self.parsed_event.end_mark.line,
  751. self.parsed_event.end_mark.column,
  752. None, None)
  753. value = PyUnicode_DecodeUTF8(self.parsed_event.data.scalar.value,
  754. self.parsed_event.data.scalar.length, 'strict')
  755. plain_implicit = False
  756. if self.parsed_event.data.scalar.plain_implicit == 1:
  757. plain_implicit = True
  758. quoted_implicit = False
  759. if self.parsed_event.data.scalar.quoted_implicit == 1:
  760. quoted_implicit = True
  761. if self.parsed_event.data.scalar.tag == NULL \
  762. or (self.parsed_event.data.scalar.tag[0] == c'!'
  763. and self.parsed_event.data.scalar.tag[1] == c'\0'):
  764. tag = self.resolve(ScalarNode, value, (plain_implicit, quoted_implicit))
  765. else:
  766. tag = PyUnicode_FromString(self.parsed_event.data.scalar.tag)
  767. style = None
  768. if self.parsed_event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE:
  769. style = u''
  770. elif self.parsed_event.data.scalar.style == YAML_SINGLE_QUOTED_SCALAR_STYLE:
  771. style = u'\''
  772. elif self.parsed_event.data.scalar.style == YAML_DOUBLE_QUOTED_SCALAR_STYLE:
  773. style = u'"'
  774. elif self.parsed_event.data.scalar.style == YAML_LITERAL_SCALAR_STYLE:
  775. style = u'|'
  776. elif self.parsed_event.data.scalar.style == YAML_FOLDED_SCALAR_STYLE:
  777. style = u'>'
  778. node = ScalarNode(tag, value, start_mark, end_mark, style)
  779. if anchor is not None:
  780. self.anchors[anchor] = node
  781. yaml_event_delete(&self.parsed_event)
  782. return node
  783. cdef _compose_sequence_node(self, object anchor):
  784. cdef int index
  785. start_mark = Mark(self.stream_name,
  786. self.parsed_event.start_mark.index,
  787. self.parsed_event.start_mark.line,
  788. self.parsed_event.start_mark.column,
  789. None, None)
  790. implicit = False
  791. if self.parsed_event.data.sequence_start.implicit == 1:
  792. implicit = True
  793. if self.parsed_event.data.sequence_start.tag == NULL \
  794. or (self.parsed_event.data.sequence_start.tag[0] == c'!'
  795. and self.parsed_event.data.sequence_start.tag[1] == c'\0'):
  796. tag = self.resolve(SequenceNode, None, implicit)
  797. else:
  798. tag = PyUnicode_FromString(self.parsed_event.data.sequence_start.tag)
  799. flow_style = None
  800. if self.parsed_event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE:
  801. flow_style = True
  802. elif self.parsed_event.data.sequence_start.style == YAML_BLOCK_SEQUENCE_STYLE:
  803. flow_style = False
  804. value = []
  805. node = SequenceNode(tag, value, start_mark, None, flow_style)
  806. if anchor is not None:
  807. self.anchors[anchor] = node
  808. yaml_event_delete(&self.parsed_event)
  809. index = 0
  810. self._parse_next_event()
  811. while self.parsed_event.type != YAML_SEQUENCE_END_EVENT:
  812. value.append(self._compose_node(node, index))
  813. index = index+1
  814. self._parse_next_event()
  815. node.end_mark = Mark(self.stream_name,
  816. self.parsed_event.end_mark.index,
  817. self.parsed_event.end_mark.line,
  818. self.parsed_event.end_mark.column,
  819. None, None)
  820. yaml_event_delete(&self.parsed_event)
  821. return node
  822. cdef _compose_mapping_node(self, object anchor):
  823. start_mark = Mark(self.stream_name,
  824. self.parsed_event.start_mark.index,
  825. self.parsed_event.start_mark.line,
  826. self.parsed_event.start_mark.column,
  827. None, None)
  828. implicit = False
  829. if self.parsed_event.data.mapping_start.implicit == 1:
  830. implicit = True
  831. if self.parsed_event.data.mapping_start.tag == NULL \
  832. or (self.parsed_event.data.mapping_start.tag[0] == c'!'
  833. and self.parsed_event.data.mapping_start.tag[1] == c'\0'):
  834. tag = self.resolve(MappingNode, None, implicit)
  835. else:
  836. tag = PyUnicode_FromString(self.parsed_event.data.mapping_start.tag)
  837. flow_style = None
  838. if self.parsed_event.data.mapping_start.style == YAML_FLOW_MAPPING_STYLE:
  839. flow_style = True
  840. elif self.parsed_event.data.mapping_start.style == YAML_BLOCK_MAPPING_STYLE:
  841. flow_style = False
  842. value = []
  843. node = MappingNode(tag, value, start_mark, None, flow_style)
  844. if anchor is not None:
  845. self.anchors[anchor] = node
  846. yaml_event_delete(&self.parsed_event)
  847. self._parse_next_event()
  848. while self.parsed_event.type != YAML_MAPPING_END_EVENT:
  849. item_key = self._compose_node(node, None)
  850. item_value = self._compose_node(node, item_key)
  851. value.append((item_key, item_value))
  852. self._parse_next_event()
  853. node.end_mark = Mark(self.stream_name,
  854. self.parsed_event.end_mark.index,
  855. self.parsed_event.end_mark.line,
  856. self.parsed_event.end_mark.column,
  857. None, None)
  858. yaml_event_delete(&self.parsed_event)
  859. return node
  860. cdef int _parse_next_event(self) except 0:
  861. if self.parsed_event.type == YAML_NO_EVENT:
  862. if yaml_parser_parse(&self.parser, &self.parsed_event) == 0:
  863. error = self._parser_error()
  864. raise error
  865. return 1
  866. cdef int input_handler(void *data, char *buffer, int size, int *read) except 0:
  867. cdef CParser parser
  868. parser = <CParser>data
  869. if parser.stream_cache is None:
  870. value = parser.stream.read(size)
  871. if PyUnicode_CheckExact(value) != 0:
  872. value = PyUnicode_AsUTF8String(value)
  873. parser.unicode_source = 1
  874. if PyString_CheckExact(value) == 0:
  875. if PY_MAJOR_VERSION < 3:
  876. raise TypeError("a string value is expected")
  877. else:
  878. raise TypeError(u"a string value is expected")
  879. parser.stream_cache = value
  880. parser.stream_cache_pos = 0
  881. parser.stream_cache_len = PyString_GET_SIZE(value)
  882. if (parser.stream_cache_len - parser.stream_cache_pos) < size:
  883. size = parser.stream_cache_len - parser.stream_cache_pos
  884. if size > 0:
  885. memcpy(buffer, PyString_AS_STRING(parser.stream_cache)
  886. + parser.stream_cache_pos, size)
  887. read[0] = size
  888. parser.stream_cache_pos += size
  889. if parser.stream_cache_pos == parser.stream_cache_len:
  890. parser.stream_cache = None
  891. return 1
  892. cdef class CEmitter:
  893. cdef yaml_emitter_t emitter
  894. cdef object stream
  895. cdef int document_start_implicit
  896. cdef int document_end_implicit
  897. cdef object use_version
  898. cdef object use_tags
  899. cdef object serialized_nodes
  900. cdef object anchors
  901. cdef int last_alias_id
  902. cdef int closed
  903. cdef int dump_unicode
  904. cdef object use_encoding
  905. def __init__(self, stream, canonical=None, indent=None, width=None,
  906. allow_unicode=None, line_break=None, encoding=None,
  907. explicit_start=None, explicit_end=None, version=None, tags=None):
  908. if yaml_emitter_initialize(&self.emitter) == 0:
  909. raise MemoryError
  910. self.stream = stream
  911. self.dump_unicode = 0
  912. if PY_MAJOR_VERSION < 3:
  913. if getattr3(stream, 'encoding', None):
  914. self.dump_unicode = 1
  915. else:
  916. if hasattr(stream, u'encoding'):
  917. self.dump_unicode = 1
  918. self.use_encoding = encoding
  919. yaml_emitter_set_output(&self.emitter, output_handler, <void *>self)
  920. if canonical:
  921. yaml_emitter_set_canonical(&self.emitter, 1)
  922. if indent is not None:
  923. yaml_emitter_set_indent(&self.emitter, indent)
  924. if width is not None:
  925. yaml_emitter_set_width(&self.emitter, width)
  926. if allow_unicode:
  927. yaml_emitter_set_unicode(&self.emitter, 1)
  928. if line_break is not None:
  929. if line_break == '\r':
  930. yaml_emitter_set_break(&self.emitter, YAML_CR_BREAK)
  931. elif line_break == '\n':
  932. yaml_emitter_set_break(&self.emitter, YAML_LN_BREAK)
  933. elif line_break == '\r\n':
  934. yaml_emitter_set_break(&self.emitter, YAML_CRLN_BREAK)
  935. self.document_start_implicit = 1
  936. if explicit_start:
  937. self.document_start_implicit = 0
  938. self.document_end_implicit = 1
  939. if explicit_end:
  940. self.document_end_implicit = 0
  941. self.use_version = version
  942. self.use_tags = tags
  943. self.serialized_nodes = {}
  944. self.anchors = {}
  945. self.last_alias_id = 0
  946. self.closed = -1
  947. def __dealloc__(self):
  948. yaml_emitter_delete(&self.emitter)
  949. def dispose(self):
  950. pass
  951. cdef object _emitter_error(self):
  952. if self.emitter.error == YAML_MEMORY_ERROR:
  953. return MemoryError
  954. elif self.emitter.error == YAML_EMITTER_ERROR:
  955. if PY_MAJOR_VERSION < 3:
  956. problem = self.emitter.problem
  957. else:
  958. problem = PyUnicode_FromString(self.emitter.problem)
  959. return EmitterError(problem)
  960. if PY_MAJOR_VERSION < 3:
  961. raise ValueError("no emitter error")
  962. else:
  963. raise ValueError(u"no emitter error")
  964. cdef int _object_to_event(self, object event_object, yaml_event_t *event) except 0:
  965. cdef yaml_encoding_t encoding
  966. cdef yaml_version_directive_t version_directive_value
  967. cdef yaml_version_directive_t *version_directive
  968. cdef yaml_tag_directive_t tag_directives_value[128]
  969. cdef yaml_tag_directive_t *tag_directives_start
  970. cdef yaml_tag_directive_t *tag_directives_end
  971. cdef int implicit
  972. cdef int plain_implicit
  973. cdef int quoted_implicit
  974. cdef char *anchor
  975. cdef char *tag
  976. cdef char *value
  977. cdef int length
  978. cdef yaml_scalar_style_t scalar_style
  979. cdef yaml_sequence_style_t sequence_style
  980. cdef yaml_mapping_style_t mapping_style
  981. event_class = event_object.__class__
  982. if event_class is StreamStartEvent:
  983. encoding = YAML_UTF8_ENCODING
  984. if event_object.encoding == u'utf-16-le' or event_object.encoding == 'utf-16-le':
  985. encoding = YAML_UTF16LE_ENCODING
  986. elif event_object.encoding == u'utf-16-be' or event_object.encoding == 'utf-16-be':
  987. encoding = YAML_UTF16BE_ENCODING
  988. if event_object.encoding is None:
  989. self.dump_unicode = 1
  990. if self.dump_unicode == 1:
  991. encoding = YAML_UTF8_ENCODING
  992. yaml_stream_start_event_initialize(event, encoding)
  993. elif event_class is StreamEndEvent:
  994. yaml_stream_end_event_initialize(event)
  995. elif event_class is DocumentStartEvent:
  996. version_directive = NULL
  997. if event_object.version:
  998. version_directive_value.major = event_object.version[0]
  999. version_directive_value.minor = event_object.version[1]
  1000. version_directive = &version_directive_value
  1001. tag_directives_start = NULL
  1002. tag_directives_end = NULL
  1003. if event_object.tags:
  1004. if len(event_object.tags) > 128:
  1005. if PY_MAJOR_VERSION < 3:
  1006. raise ValueError("too many tags")
  1007. else:
  1008. raise ValueError(u"too many tags")
  1009. tag_directives_start = tag_directives_value
  1010. tag_directives_end = tag_directives_value
  1011. cache = []
  1012. for handle in event_object.tags:
  1013. prefix = event_object.tags[handle]
  1014. if PyUnicode_CheckExact(handle):
  1015. handle = PyUnicode_AsUTF8String(handle)
  1016. cache.append(handle)
  1017. if not PyString_CheckExact(handle):
  1018. if PY_MAJOR_VERSION < 3:
  1019. raise TypeError("tag handle must be a string")
  1020. else:
  1021. raise TypeError(u"tag handle must be a string")
  1022. tag_directives_end.handle = PyString_AS_STRING(handle)
  1023. if PyUnicode_CheckExact(prefix):
  1024. prefix = PyUnicode_AsUTF8String(prefix)
  1025. cache.append(prefix)
  1026. if not PyString_CheckExact(prefix):
  1027. if PY_MAJOR_VERSION < 3:
  1028. raise TypeError("tag prefix must be a string")
  1029. else:
  1030. raise TypeError(u"tag prefix must be a string")
  1031. tag_directives_end.prefix = PyString_AS_STRING(prefix)
  1032. tag_directives_end = tag_directives_end+1
  1033. implicit = 1
  1034. if event_object.explicit:
  1035. implicit = 0
  1036. if yaml_document_start_event_initialize(event, version_directive,
  1037. tag_directives_start, tag_directives_end, implicit) == 0:
  1038. raise MemoryError
  1039. elif event_class is DocumentEndEvent:
  1040. implicit = 1
  1041. if event_object.explicit:
  1042. implicit = 0
  1043. yaml_document_end_event_initialize(event, implicit)
  1044. elif event_class is AliasEvent:
  1045. anchor = NULL
  1046. anchor_object = event_object.anchor
  1047. if PyUnicode_CheckExact(anchor_object):
  1048. anchor_object = PyUnicode_AsUTF8String(anchor_object)
  1049. if not PyString_CheckExact(anchor_object):
  1050. if PY_MAJOR_VERSION < 3:
  1051. raise TypeError("anchor must be a string")
  1052. else:
  1053. raise TypeError(u"anchor must be a string")
  1054. anchor = PyString_AS_STRING(anchor_object)
  1055. if yaml_alias_event_initialize(event, anchor) == 0:
  1056. raise MemoryError
  1057. elif event_class is ScalarEvent:
  1058. anchor = NULL
  1059. anchor_object = event_object.anchor
  1060. if anchor_object is not None:
  1061. if PyUnicode_CheckExact(anchor_object):
  1062. anchor_object = PyUnicode_AsUTF8String(anchor_object)
  1063. if not PyString_CheckExact(anchor_object):
  1064. if PY_MAJOR_VERSION < 3:
  1065. raise TypeError("anchor must be a string")
  1066. else:
  1067. raise TypeError(u"anchor must be a string")
  1068. anchor = PyString_AS_STRING(anchor_object)
  1069. tag = NULL
  1070. tag_object = event_object.tag
  1071. if tag_object is not None:
  1072. if PyUnicode_CheckExact(tag_object):
  1073. tag_object = PyUnicode_AsUTF8String(tag_object)
  1074. if not PyString_CheckExact(tag_object):
  1075. if PY_MAJOR_VERSION < 3:
  1076. raise TypeError("tag must be a string")
  1077. else:
  1078. raise TypeError(u"tag must be a string")
  1079. tag = PyString_AS_STRING(tag_object)
  1080. value_object = event_object.value
  1081. if PyUnicode_CheckExact(value_object):
  1082. value_object = PyUnicode_AsUTF8String(value_object)
  1083. if not PyString_CheckExact(value_object):
  1084. if PY_MAJOR_VERSION < 3:
  1085. raise TypeError("value must be a string")
  1086. else:
  1087. raise TypeError(u"value must be a string")
  1088. value = PyString_AS_STRING(value_object)
  1089. length = PyString_GET_SIZE(value_object)
  1090. plain_implicit = 0
  1091. quoted_implicit = 0
  1092. if event_object.implicit is not None:
  1093. plain_implicit = event_object.implicit[0]
  1094. quoted_implicit = event_object.implicit[1]
  1095. style_object = event_object.style
  1096. scalar_style = YAML_PLAIN_SCALAR_STYLE
  1097. if style_object == "'" or style_object == u"'":
  1098. scalar_style = YAML_SINGLE_QUOTED_SCALAR_STYLE
  1099. elif style_object == "\"" or style_object == u"\"":
  1100. scalar_style = YAML_DOUBLE_QUOTED_SCALAR_STYLE
  1101. elif style_object == "|" or style_object == u"|":
  1102. scalar_style = YAML_LITERAL_SCALAR_STYLE
  1103. elif style_object == ">" or style_object == u">":
  1104. scalar_style = YAML_FOLDED_SCALAR_STYLE
  1105. if yaml_scalar_event_initialize(event, anchor, tag, value, length,
  1106. plain_implicit, quoted_implicit, scalar_style) == 0:
  1107. raise MemoryError
  1108. elif event_class is SequenceStartEvent:
  1109. anchor = NULL
  1110. anchor_object = event_object.anchor
  1111. if anchor_object is not None:
  1112. if PyUnicode_CheckExact(anchor_object):
  1113. anchor_object = PyUnicode_AsUTF8String(anchor_object)
  1114. if not PyString_CheckExact(anchor_object):
  1115. if PY_MAJOR_VERSION < 3:
  1116. raise TypeError("anchor must be a string")
  1117. else:
  1118. raise TypeError(u"anchor must be a string")
  1119. anchor = PyString_AS_STRING(anchor_object)
  1120. tag = NULL
  1121. tag_object = event_object.tag
  1122. if tag_object is not None:
  1123. if PyUnicode_CheckExact(tag_object):
  1124. tag_object = PyUnicode_AsUTF8String(tag_object)
  1125. if not PyString_CheckExact(tag_object):
  1126. if PY_MAJOR_VERSION < 3:
  1127. raise TypeError("tag must be a string")
  1128. else:
  1129. raise TypeError(u"tag must be a string")
  1130. tag = PyString_AS_STRING(tag_object)
  1131. implicit = 0
  1132. if event_object.implicit:
  1133. implicit = 1
  1134. sequence_style = YAML_BLOCK_SEQUENCE_STYLE
  1135. if event_object.flow_style:
  1136. sequence_style = YAML_FLOW_SEQUENCE_STYLE
  1137. if yaml_sequence_start_event_initialize(event, anchor, tag,
  1138. implicit, sequence_style) == 0:
  1139. raise MemoryError
  1140. elif event_class is MappingStartEvent:
  1141. anchor = NULL
  1142. anchor_object = event_object.anchor
  1143. if anchor_object is not None:
  1144. if PyUnicode_CheckExact(anchor_object):
  1145. anchor_object = PyUnicode_AsUTF8String(anchor_object)
  1146. if not PyString_CheckExact(anchor_object):
  1147. if PY_MAJOR_VERSION < 3:
  1148. raise TypeError("anchor must be a string")
  1149. else:
  1150. raise TypeError(u"anchor must be a string")
  1151. anchor = PyString_AS_STRING(anchor_object)
  1152. tag = NULL
  1153. tag_object = event_object.tag
  1154. if tag_object is not None:
  1155. if PyUnicode_CheckExact(tag_object):
  1156. tag_object = PyUnicode_AsUTF8String(tag_object)
  1157. if not PyString_CheckExact(tag_object):
  1158. if PY_MAJOR_VERSION < 3:
  1159. raise TypeError("tag must be a string")
  1160. else:
  1161. raise TypeError(u"tag must be a string")
  1162. tag = PyString_AS_STRING(tag_object)
  1163. implicit = 0
  1164. if event_object.implicit:
  1165. implicit = 1
  1166. mapping_style = YAML_BLOCK_MAPPING_STYLE
  1167. if event_object.flow_style:
  1168. mapping_style = YAML_FLOW_MAPPING_STYLE
  1169. if yaml_mapping_start_event_initialize(event, anchor, tag,
  1170. implicit, mapping_style) == 0:
  1171. raise MemoryError
  1172. elif event_class is SequenceEndEvent:
  1173. yaml_sequence_end_event_initialize(event)
  1174. elif event_class is MappingEndEvent:
  1175. yaml_mapping_end_event_initialize(event)
  1176. else:
  1177. if PY_MAJOR_VERSION < 3:
  1178. raise TypeError("invalid event %s" % event_object)
  1179. else:
  1180. raise TypeError(u"invalid event %s" % event_object)
  1181. return 1
  1182. def emit(self, event_object):
  1183. cdef yaml_event_t event
  1184. self._object_to_event(event_object, &event)
  1185. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1186. error = self._emitter_error()
  1187. raise error
  1188. def open(self):
  1189. cdef yaml_event_t event
  1190. cdef yaml_encoding_t encoding
  1191. if self.closed == -1:
  1192. if self.use_encoding == u'utf-16-le' or self.use_encoding == 'utf-16-le':
  1193. encoding = YAML_UTF16LE_ENCODING
  1194. elif self.use_encoding == u'utf-16-be' or self.use_encoding == 'utf-16-be':
  1195. encoding = YAML_UTF16BE_ENCODING
  1196. else:
  1197. encoding = YAML_UTF8_ENCODING
  1198. if self.use_encoding is None:
  1199. self.dump_unicode = 1
  1200. if self.dump_unicode == 1:
  1201. encoding = YAML_UTF8_ENCODING
  1202. yaml_stream_start_event_initialize(&event, encoding)
  1203. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1204. error = self._emitter_error()
  1205. raise error
  1206. self.closed = 0
  1207. elif self.closed == 1:
  1208. if PY_MAJOR_VERSION < 3:
  1209. raise SerializerError("serializer is closed")
  1210. else:
  1211. raise SerializerError(u"serializer is closed")
  1212. else:
  1213. if PY_MAJOR_VERSION < 3:
  1214. raise SerializerError("serializer is already opened")
  1215. else:
  1216. raise SerializerError(u"serializer is already opened")
  1217. def close(self):
  1218. cdef yaml_event_t event
  1219. if self.closed == -1:
  1220. if PY_MAJOR_VERSION < 3:
  1221. raise SerializerError("serializer is not opened")
  1222. else:
  1223. raise SerializerError(u"serializer is not opened")
  1224. elif self.closed == 0:
  1225. yaml_stream_end_event_initialize(&event)
  1226. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1227. error = self._emitter_error()
  1228. raise error
  1229. self.closed = 1
  1230. def serialize(self, node):
  1231. cdef yaml_event_t event
  1232. cdef yaml_version_directive_t version_directive_value
  1233. cdef yaml_version_directive_t *version_directive
  1234. cdef yaml_tag_directive_t tag_directives_value[128]
  1235. cdef yaml_tag_directive_t *tag_directives_start
  1236. cdef yaml_tag_directive_t *tag_directives_end
  1237. if self.closed == -1:
  1238. if PY_MAJOR_VERSION < 3:
  1239. raise SerializerError("serializer is not opened")
  1240. else:
  1241. raise SerializerError(u"serializer is not opened")
  1242. elif self.closed == 1:
  1243. if PY_MAJOR_VERSION < 3:
  1244. raise SerializerError("serializer is closed")
  1245. else:
  1246. raise SerializerError(u"serializer is closed")
  1247. cache = []
  1248. version_directive = NULL
  1249. if self.use_version:
  1250. version_directive_value.major = self.use_version[0]
  1251. version_directive_value.minor = self.use_version[1]
  1252. version_directive = &version_directive_value
  1253. tag_directives_start = NULL
  1254. tag_directives_end = NULL
  1255. if self.use_tags:
  1256. if len(self.use_tags) > 128:
  1257. if PY_MAJOR_VERSION < 3:
  1258. raise ValueError("too many tags")
  1259. else:
  1260. raise ValueError(u"too many tags")
  1261. tag_directives_start = tag_directives_value
  1262. tag_directives_end = tag_directives_value
  1263. for handle in self.use_tags:
  1264. prefix = self.use_tags[handle]
  1265. if PyUnicode_CheckExact(handle):
  1266. handle = PyUnicode_AsUTF8String(handle)
  1267. cache.append(handle)
  1268. if not PyString_CheckExact(handle):
  1269. if PY_MAJOR_VERSION < 3:
  1270. raise TypeError("tag handle must be a string")
  1271. else:
  1272. raise TypeError(u"tag handle must be a string")
  1273. tag_directives_end.handle = PyString_AS_STRING(handle)
  1274. if PyUnicode_CheckExact(prefix):
  1275. prefix = PyUnicode_AsUTF8String(prefix)
  1276. cache.append(prefix)
  1277. if not PyString_CheckExact(prefix):
  1278. if PY_MAJOR_VERSION < 3:
  1279. raise TypeError("tag prefix must be a string")
  1280. else:
  1281. raise TypeError(u"tag prefix must be a string")
  1282. tag_directives_end.prefix = PyString_AS_STRING(prefix)
  1283. tag_directives_end = tag_directives_end+1
  1284. if yaml_document_start_event_initialize(&event, version_directive,
  1285. tag_directives_start, tag_directives_end,
  1286. self.document_start_implicit) == 0:
  1287. raise MemoryError
  1288. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1289. error = self._emitter_error()
  1290. raise error
  1291. self._anchor_node(node)
  1292. self._serialize_node(node, None, None)
  1293. yaml_document_end_event_initialize(&event, self.document_end_implicit)
  1294. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1295. error = self._emitter_error()
  1296. raise error
  1297. self.serialized_nodes = {}
  1298. self.anchors = {}
  1299. self.last_alias_id = 0
  1300. cdef int _anchor_node(self, object node) except 0:
  1301. if node in self.anchors:
  1302. if self.anchors[node] is None:
  1303. self.last_alias_id = self.last_alias_id+1
  1304. self.anchors[node] = u"id%03d" % self.last_alias_id
  1305. else:
  1306. self.anchors[node] = None
  1307. node_class = node.__class__
  1308. if node_class is SequenceNode:
  1309. for item in node.value:
  1310. self._anchor_node(item)
  1311. elif node_class is MappingNode:
  1312. for key, value in node.value:
  1313. self._anchor_node(key)
  1314. self._anchor_node(value)
  1315. return 1
  1316. cdef int _serialize_node(self, object node, object parent, object index) except 0:
  1317. cdef yaml_event_t event
  1318. cdef int implicit
  1319. cdef int plain_implicit
  1320. cdef int quoted_implicit
  1321. cdef char *anchor
  1322. cdef char *tag
  1323. cdef char *value
  1324. cdef int length
  1325. cdef int item_index
  1326. cdef yaml_scalar_style_t scalar_style
  1327. cdef yaml_sequence_style_t sequence_style
  1328. cdef yaml_mapping_style_t mapping_style
  1329. anchor_object = self.anchors[node]
  1330. anchor = NULL
  1331. if anchor_object is not None:
  1332. if PyUnicode_CheckExact(anchor_object):
  1333. anchor_object = PyUnicode_AsUTF8String(anchor_object)
  1334. if not PyString_CheckExact(anchor_object):
  1335. if PY_MAJOR_VERSION < 3:
  1336. raise TypeError("anchor must be a string")
  1337. else:
  1338. raise TypeError(u"anchor must be a string")
  1339. anchor = PyString_AS_STRING(anchor_object)
  1340. if node in self.serialized_nodes:
  1341. if yaml_alias_event_initialize(&event, anchor) == 0:
  1342. raise MemoryError
  1343. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1344. error = self._emitter_error()
  1345. raise error
  1346. else:
  1347. node_class = node.__class__
  1348. self.serialized_nodes[node] = True
  1349. self.descend_resolver(parent, index)
  1350. if node_class is ScalarNode:
  1351. plain_implicit = 0
  1352. quoted_implicit = 0
  1353. tag_object = node.tag
  1354. if self.resolve(ScalarNode, node.value, (True, False)) == tag_object:
  1355. plain_implicit = 1
  1356. if self.resolve(ScalarNode, node.value, (False, True)) == tag_object:
  1357. quoted_implicit = 1
  1358. tag = NULL
  1359. if tag_object is not None:
  1360. if PyUnicode_CheckExact(tag_object):
  1361. tag_object = PyUnicode_AsUTF8String(tag_object)
  1362. if not PyString_CheckExact(tag_object):
  1363. if PY_MAJOR_VERSION < 3:
  1364. raise TypeError("tag must be a string")
  1365. else:
  1366. raise TypeError(u"tag must be a string")
  1367. tag = PyString_AS_STRING(tag_object)
  1368. value_object = node.value
  1369. if PyUnicode_CheckExact(value_object):
  1370. value_object = PyUnicode_AsUTF8String(value_object)
  1371. if not PyString_CheckExact(value_object):
  1372. if PY_MAJOR_VERSION < 3:
  1373. raise TypeError("value must be a string")
  1374. else:
  1375. raise TypeError(u"value must be a string")
  1376. value = PyString_AS_STRING(value_object)
  1377. length = PyString_GET_SIZE(value_object)
  1378. style_object = node.style
  1379. scalar_style = YAML_PLAIN_SCALAR_STYLE
  1380. if style_object == "'" or style_object == u"'":
  1381. scalar_style = YAML_SINGLE_QUOTED_SCALAR_STYLE
  1382. elif style_object == "\"" or style_object == u"\"":
  1383. scalar_style = YAML_DOUBLE_QUOTED_SCALAR_STYLE
  1384. elif style_object == "|" or style_object == u"|":
  1385. scalar_style = YAML_LITERAL_SCALAR_STYLE
  1386. elif style_object == ">" or style_object == u">":
  1387. scalar_style = YAML_FOLDED_SCALAR_STYLE
  1388. if yaml_scalar_event_initialize(&event, anchor, tag, value, length,
  1389. plain_implicit, quoted_implicit, scalar_style) == 0:
  1390. raise MemoryError
  1391. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1392. error = self._emitter_error()
  1393. raise error
  1394. elif node_class is SequenceNode:
  1395. implicit = 0
  1396. tag_object = node.tag
  1397. if self.resolve(SequenceNode, node.value, True) == tag_object:
  1398. implicit = 1
  1399. tag = NULL
  1400. if tag_object is not None:
  1401. if PyUnicode_CheckExact(tag_object):
  1402. tag_object = PyUnicode_AsUTF8String(tag_object)
  1403. if not PyString_CheckExact(tag_object):
  1404. if PY_MAJOR_VERSION < 3:
  1405. raise TypeError("tag must be a string")
  1406. else:
  1407. raise TypeError(u"tag must be a string")
  1408. tag = PyString_AS_STRING(tag_object)
  1409. sequence_style = YAML_BLOCK_SEQUENCE_STYLE
  1410. if node.flow_style:
  1411. sequence_style = YAML_FLOW_SEQUENCE_STYLE
  1412. if yaml_sequence_start_event_initialize(&event, anchor, tag,
  1413. implicit, sequence_style) == 0:
  1414. raise MemoryError
  1415. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1416. error = self._emitter_error()
  1417. raise error
  1418. item_index = 0
  1419. for item in node.value:
  1420. self._serialize_node(item, node, item_index)
  1421. item_index = item_index+1
  1422. yaml_sequence_end_event_initialize(&event)
  1423. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1424. error = self._emitter_error()
  1425. raise error
  1426. elif node_class is MappingNode:
  1427. implicit = 0
  1428. tag_object = node.tag
  1429. if self.resolve(MappingNode, node.value, True) == tag_object:
  1430. implicit = 1
  1431. tag = NULL
  1432. if tag_object is not None:
  1433. if PyUnicode_CheckExact(tag_object):
  1434. tag_object = PyUnicode_AsUTF8String(tag_object)
  1435. if not PyString_CheckExact(tag_object):
  1436. if PY_MAJOR_VERSION < 3:
  1437. raise TypeError("tag must be a string")
  1438. else:
  1439. raise TypeError(u"tag must be a string")
  1440. tag = PyString_AS_STRING(tag_object)
  1441. mapping_style = YAML_BLOCK_MAPPING_STYLE
  1442. if node.flow_style:
  1443. mapping_style = YAML_FLOW_MAPPING_STYLE
  1444. if yaml_mapping_start_event_initialize(&event, anchor, tag,
  1445. implicit, mapping_style) == 0:
  1446. raise MemoryError
  1447. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1448. error = self._emitter_error()
  1449. raise error
  1450. for item_key, item_value in node.value:
  1451. self._serialize_node(item_key, node, None)
  1452. self._serialize_node(item_value, node, item_key)
  1453. yaml_mapping_end_event_initialize(&event)
  1454. if yaml_emitter_emit(&self.emitter, &event) == 0:
  1455. error = self._emitter_error()
  1456. raise error
  1457. self.ascend_resolver()
  1458. return 1
  1459. cdef int output_handler(void *data, char *buffer, int size) except 0:
  1460. cdef CEmitter emitter
  1461. emitter = <CEmitter>data
  1462. if emitter.dump_unicode == 0:
  1463. value = PyString_FromStringAndSize(buffer, size)
  1464. else:
  1465. value = PyUnicode_DecodeUTF8(buffer, size, 'strict')
  1466. emitter.stream.write(value)
  1467. return 1