_yaml.pyx 63 KB

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