emitter.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. # coding: utf-8
  2. from __future__ import absolute_import
  3. from __future__ import print_function
  4. # Emitter expects events obeying the following grammar:
  5. # stream ::= STREAM-START document* STREAM-END
  6. # document ::= DOCUMENT-START node DOCUMENT-END
  7. # node ::= SCALAR | sequence | mapping
  8. # sequence ::= SEQUENCE-START node* SEQUENCE-END
  9. # mapping ::= MAPPING-START (node node)* MAPPING-END
  10. import sys
  11. from ruamel.yaml.error import YAMLError, YAMLStreamError
  12. from ruamel.yaml.events import * # NOQA
  13. # fmt: off
  14. from ruamel.yaml.compat import utf8, text_type, PY2, nprint, dbg, DBG_EVENT, \
  15. check_anchorname_char
  16. # fmt: on
  17. if False: # MYPY
  18. from typing import Any, Dict, List, Union, Text, Tuple, Optional # NOQA
  19. from ruamel.yaml.compat import StreamType # NOQA
  20. __all__ = ['Emitter', 'EmitterError']
  21. class EmitterError(YAMLError):
  22. pass
  23. class ScalarAnalysis(object):
  24. def __init__(
  25. self,
  26. scalar,
  27. empty,
  28. multiline,
  29. allow_flow_plain,
  30. allow_block_plain,
  31. allow_single_quoted,
  32. allow_double_quoted,
  33. allow_block,
  34. ):
  35. # type: (Any, Any, Any, bool, bool, bool, bool, bool) -> None
  36. self.scalar = scalar
  37. self.empty = empty
  38. self.multiline = multiline
  39. self.allow_flow_plain = allow_flow_plain
  40. self.allow_block_plain = allow_block_plain
  41. self.allow_single_quoted = allow_single_quoted
  42. self.allow_double_quoted = allow_double_quoted
  43. self.allow_block = allow_block
  44. class Indents(object):
  45. # replacement for the list based stack of None/int
  46. def __init__(self):
  47. # type: () -> None
  48. self.values = [] # type: List[Tuple[int, bool]]
  49. def append(self, val, seq):
  50. # type: (Any, Any) -> None
  51. self.values.append((val, seq))
  52. def pop(self):
  53. # type: () -> Any
  54. return self.values.pop()[0]
  55. def last_seq(self):
  56. # type: () -> bool
  57. # return the seq(uence) value for the element added before the last one
  58. # in increase_indent()
  59. try:
  60. return self.values[-2][1]
  61. except IndexError:
  62. return False
  63. def seq_flow_align(self, seq_indent, column):
  64. # type: (int, int) -> int
  65. # extra spaces because of dash
  66. if len(self.values) < 2 or not self.values[-1][1]:
  67. return 0
  68. # -1 for the dash
  69. base = self.values[-1][0] if self.values[-1][0] is not None else 0
  70. return base + seq_indent - column - 1
  71. def __len__(self):
  72. # type: () -> int
  73. return len(self.values)
  74. class Emitter(object):
  75. # fmt: off
  76. DEFAULT_TAG_PREFIXES = {
  77. u'!': u'!',
  78. u'tag:yaml.org,2002:': u'!!',
  79. }
  80. # fmt: on
  81. MAX_SIMPLE_KEY_LENGTH = 128
  82. def __init__(
  83. self,
  84. stream,
  85. canonical=None,
  86. indent=None,
  87. width=None,
  88. allow_unicode=None,
  89. line_break=None,
  90. block_seq_indent=None,
  91. top_level_colon_align=None,
  92. prefix_colon=None,
  93. brace_single_entry_mapping_in_flow_sequence=None,
  94. dumper=None,
  95. ):
  96. # type: (StreamType, Any, Optional[int], Optional[int], Optional[bool], Any, Optional[int], Optional[bool], Any, Optional[bool], Any) -> None # NOQA
  97. self.dumper = dumper
  98. if self.dumper is not None and getattr(self.dumper, '_emitter', None) is None:
  99. self.dumper._emitter = self
  100. self.stream = stream
  101. # Encoding can be overriden by STREAM-START.
  102. self.encoding = None # type: Optional[Text]
  103. self.allow_space_break = None
  104. # Emitter is a state machine with a stack of states to handle nested
  105. # structures.
  106. self.states = [] # type: List[Any]
  107. self.state = self.expect_stream_start # type: Any
  108. # Current event and the event queue.
  109. self.events = [] # type: List[Any]
  110. self.event = None # type: Any
  111. # The current indentation level and the stack of previous indents.
  112. self.indents = Indents()
  113. self.indent = None # type: Optional[int]
  114. # flow_context is an expanding/shrinking list consisting of '{' and '['
  115. # for each unclosed flow context. If empty list that means block context
  116. self.flow_context = [] # type: List[Text]
  117. # Contexts.
  118. self.root_context = False
  119. self.sequence_context = False
  120. self.mapping_context = False
  121. self.simple_key_context = False
  122. # Characteristics of the last emitted character:
  123. # - current position.
  124. # - is it a whitespace?
  125. # - is it an indention character
  126. # (indentation space, '-', '?', or ':')?
  127. self.line = 0
  128. self.column = 0
  129. self.whitespace = True
  130. self.indention = True
  131. self.compact_seq_seq = True # dash after dash
  132. self.compact_seq_map = True # key after dash
  133. # self.compact_ms = False # dash after key, only when excplicit key with ?
  134. self.no_newline = None # type: Optional[bool] # set if directly after `- `
  135. # Whether the document requires an explicit document end indicator
  136. self.open_ended = False
  137. # colon handling
  138. self.colon = u':'
  139. self.prefixed_colon = self.colon if prefix_colon is None else prefix_colon + self.colon
  140. # single entry mappings in flow sequence
  141. self.brace_single_entry_mapping_in_flow_sequence = (
  142. brace_single_entry_mapping_in_flow_sequence # NOQA
  143. )
  144. # Formatting details.
  145. self.canonical = canonical
  146. self.allow_unicode = allow_unicode
  147. # set to False to get "\Uxxxxxxxx" for non-basic unicode like emojis
  148. self.unicode_supplementary = sys.maxunicode > 0xFFFF
  149. self.sequence_dash_offset = block_seq_indent if block_seq_indent else 0
  150. self.top_level_colon_align = top_level_colon_align
  151. self.best_sequence_indent = 2
  152. self.requested_indent = indent # specific for literal zero indent
  153. if indent and 1 < indent < 10:
  154. self.best_sequence_indent = indent
  155. self.best_map_indent = self.best_sequence_indent
  156. # if self.best_sequence_indent < self.sequence_dash_offset + 1:
  157. # self.best_sequence_indent = self.sequence_dash_offset + 1
  158. self.best_width = 80
  159. if width and width > self.best_sequence_indent * 2:
  160. self.best_width = width
  161. self.best_line_break = u'\n' # type: Any
  162. if line_break in [u'\r', u'\n', u'\r\n']:
  163. self.best_line_break = line_break
  164. # Tag prefixes.
  165. self.tag_prefixes = None # type: Any
  166. # Prepared anchor and tag.
  167. self.prepared_anchor = None # type: Any
  168. self.prepared_tag = None # type: Any
  169. # Scalar analysis and style.
  170. self.analysis = None # type: Any
  171. self.style = None # type: Any
  172. self.scalar_after_indicator = True # write a scalar on the same line as `---`
  173. self.alt_null = 'null'
  174. @property
  175. def stream(self):
  176. # type: () -> Any
  177. try:
  178. return self._stream
  179. except AttributeError:
  180. raise YAMLStreamError('output stream needs to specified')
  181. @stream.setter
  182. def stream(self, val):
  183. # type: (Any) -> None
  184. if val is None:
  185. return
  186. if not hasattr(val, 'write'):
  187. raise YAMLStreamError('stream argument needs to have a write() method')
  188. self._stream = val
  189. @property
  190. def serializer(self):
  191. # type: () -> Any
  192. try:
  193. if hasattr(self.dumper, 'typ'):
  194. return self.dumper.serializer
  195. return self.dumper._serializer
  196. except AttributeError:
  197. return self # cyaml
  198. @property
  199. def flow_level(self):
  200. # type: () -> int
  201. return len(self.flow_context)
  202. def dispose(self):
  203. # type: () -> None
  204. # Reset the state attributes (to clear self-references)
  205. self.states = []
  206. self.state = None
  207. def emit(self, event):
  208. # type: (Any) -> None
  209. if dbg(DBG_EVENT):
  210. nprint(event)
  211. self.events.append(event)
  212. while not self.need_more_events():
  213. self.event = self.events.pop(0)
  214. self.state()
  215. self.event = None
  216. # In some cases, we wait for a few next events before emitting.
  217. def need_more_events(self):
  218. # type: () -> bool
  219. if not self.events:
  220. return True
  221. event = self.events[0]
  222. if isinstance(event, DocumentStartEvent):
  223. return self.need_events(1)
  224. elif isinstance(event, SequenceStartEvent):
  225. return self.need_events(2)
  226. elif isinstance(event, MappingStartEvent):
  227. return self.need_events(3)
  228. else:
  229. return False
  230. def need_events(self, count):
  231. # type: (int) -> bool
  232. level = 0
  233. for event in self.events[1:]:
  234. if isinstance(event, (DocumentStartEvent, CollectionStartEvent)):
  235. level += 1
  236. elif isinstance(event, (DocumentEndEvent, CollectionEndEvent)):
  237. level -= 1
  238. elif isinstance(event, StreamEndEvent):
  239. level = -1
  240. if level < 0:
  241. return False
  242. return len(self.events) < count + 1
  243. def increase_indent(self, flow=False, sequence=None, indentless=False):
  244. # type: (bool, Optional[bool], bool) -> None
  245. self.indents.append(self.indent, sequence)
  246. if self.indent is None: # top level
  247. if flow:
  248. # self.indent = self.best_sequence_indent if self.indents.last_seq() else \
  249. # self.best_map_indent
  250. # self.indent = self.best_sequence_indent
  251. self.indent = self.requested_indent
  252. else:
  253. self.indent = 0
  254. elif not indentless:
  255. self.indent += (
  256. self.best_sequence_indent if self.indents.last_seq() else self.best_map_indent
  257. )
  258. # if self.indents.last_seq():
  259. # if self.indent == 0: # top level block sequence
  260. # self.indent = self.best_sequence_indent - self.sequence_dash_offset
  261. # else:
  262. # self.indent += self.best_sequence_indent
  263. # else:
  264. # self.indent += self.best_map_indent
  265. # States.
  266. # Stream handlers.
  267. def expect_stream_start(self):
  268. # type: () -> None
  269. if isinstance(self.event, StreamStartEvent):
  270. if PY2:
  271. if self.event.encoding and not getattr(self.stream, 'encoding', None):
  272. self.encoding = self.event.encoding
  273. else:
  274. if self.event.encoding and not hasattr(self.stream, 'encoding'):
  275. self.encoding = self.event.encoding
  276. self.write_stream_start()
  277. self.state = self.expect_first_document_start
  278. else:
  279. raise EmitterError('expected StreamStartEvent, but got %s' % (self.event,))
  280. def expect_nothing(self):
  281. # type: () -> None
  282. raise EmitterError('expected nothing, but got %s' % (self.event,))
  283. # Document handlers.
  284. def expect_first_document_start(self):
  285. # type: () -> Any
  286. return self.expect_document_start(first=True)
  287. def expect_document_start(self, first=False):
  288. # type: (bool) -> None
  289. if isinstance(self.event, DocumentStartEvent):
  290. if (self.event.version or self.event.tags) and self.open_ended:
  291. self.write_indicator(u'...', True)
  292. self.write_indent()
  293. if self.event.version:
  294. version_text = self.prepare_version(self.event.version)
  295. self.write_version_directive(version_text)
  296. self.tag_prefixes = self.DEFAULT_TAG_PREFIXES.copy()
  297. if self.event.tags:
  298. handles = sorted(self.event.tags.keys())
  299. for handle in handles:
  300. prefix = self.event.tags[handle]
  301. self.tag_prefixes[prefix] = handle
  302. handle_text = self.prepare_tag_handle(handle)
  303. prefix_text = self.prepare_tag_prefix(prefix)
  304. self.write_tag_directive(handle_text, prefix_text)
  305. implicit = (
  306. first
  307. and not self.event.explicit
  308. and not self.canonical
  309. and not self.event.version
  310. and not self.event.tags
  311. and not self.check_empty_document()
  312. )
  313. if not implicit:
  314. self.write_indent()
  315. self.write_indicator(u'---', True)
  316. if self.canonical:
  317. self.write_indent()
  318. self.state = self.expect_document_root
  319. elif isinstance(self.event, StreamEndEvent):
  320. if self.open_ended:
  321. self.write_indicator(u'...', True)
  322. self.write_indent()
  323. self.write_stream_end()
  324. self.state = self.expect_nothing
  325. else:
  326. raise EmitterError('expected DocumentStartEvent, but got %s' % (self.event,))
  327. def expect_document_end(self):
  328. # type: () -> None
  329. if isinstance(self.event, DocumentEndEvent):
  330. self.write_indent()
  331. if self.event.explicit:
  332. self.write_indicator(u'...', True)
  333. self.write_indent()
  334. self.flush_stream()
  335. self.state = self.expect_document_start
  336. else:
  337. raise EmitterError('expected DocumentEndEvent, but got %s' % (self.event,))
  338. def expect_document_root(self):
  339. # type: () -> None
  340. self.states.append(self.expect_document_end)
  341. self.expect_node(root=True)
  342. # Node handlers.
  343. def expect_node(self, root=False, sequence=False, mapping=False, simple_key=False):
  344. # type: (bool, bool, bool, bool) -> None
  345. self.root_context = root
  346. self.sequence_context = sequence # not used in PyYAML
  347. self.mapping_context = mapping
  348. self.simple_key_context = simple_key
  349. if isinstance(self.event, AliasEvent):
  350. self.expect_alias()
  351. elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)):
  352. if (
  353. self.process_anchor(u'&')
  354. and isinstance(self.event, ScalarEvent)
  355. and self.sequence_context
  356. ):
  357. self.sequence_context = False
  358. if (
  359. root
  360. and isinstance(self.event, ScalarEvent)
  361. and not self.scalar_after_indicator
  362. ):
  363. self.write_indent()
  364. self.process_tag()
  365. if isinstance(self.event, ScalarEvent):
  366. # nprint('@', self.indention, self.no_newline, self.column)
  367. self.expect_scalar()
  368. elif isinstance(self.event, SequenceStartEvent):
  369. # nprint('@', self.indention, self.no_newline, self.column)
  370. i2, n2 = self.indention, self.no_newline # NOQA
  371. if self.event.comment:
  372. if self.event.flow_style is False and self.event.comment:
  373. if self.write_post_comment(self.event):
  374. self.indention = False
  375. self.no_newline = True
  376. if self.write_pre_comment(self.event):
  377. self.indention = i2
  378. self.no_newline = not self.indention
  379. if (
  380. self.flow_level
  381. or self.canonical
  382. or self.event.flow_style
  383. or self.check_empty_sequence()
  384. ):
  385. self.expect_flow_sequence()
  386. else:
  387. self.expect_block_sequence()
  388. elif isinstance(self.event, MappingStartEvent):
  389. if self.event.flow_style is False and self.event.comment:
  390. self.write_post_comment(self.event)
  391. if self.event.comment and self.event.comment[1]:
  392. self.write_pre_comment(self.event)
  393. if (
  394. self.flow_level
  395. or self.canonical
  396. or self.event.flow_style
  397. or self.check_empty_mapping()
  398. ):
  399. self.expect_flow_mapping(single=self.event.nr_items == 1)
  400. else:
  401. self.expect_block_mapping()
  402. else:
  403. raise EmitterError('expected NodeEvent, but got %s' % (self.event,))
  404. def expect_alias(self):
  405. # type: () -> None
  406. if self.event.anchor is None:
  407. raise EmitterError('anchor is not specified for alias')
  408. self.process_anchor(u'*')
  409. self.state = self.states.pop()
  410. def expect_scalar(self):
  411. # type: () -> None
  412. self.increase_indent(flow=True)
  413. self.process_scalar()
  414. self.indent = self.indents.pop()
  415. self.state = self.states.pop()
  416. # Flow sequence handlers.
  417. def expect_flow_sequence(self):
  418. # type: () -> None
  419. ind = self.indents.seq_flow_align(self.best_sequence_indent, self.column)
  420. self.write_indicator(u' ' * ind + u'[', True, whitespace=True)
  421. self.increase_indent(flow=True, sequence=True)
  422. self.flow_context.append('[')
  423. self.state = self.expect_first_flow_sequence_item
  424. def expect_first_flow_sequence_item(self):
  425. # type: () -> None
  426. if isinstance(self.event, SequenceEndEvent):
  427. self.indent = self.indents.pop()
  428. popped = self.flow_context.pop()
  429. assert popped == '['
  430. self.write_indicator(u']', False)
  431. if self.event.comment and self.event.comment[0]:
  432. # eol comment on empty flow sequence
  433. self.write_post_comment(self.event)
  434. elif self.flow_level == 0:
  435. self.write_line_break()
  436. self.state = self.states.pop()
  437. else:
  438. if self.canonical or self.column > self.best_width:
  439. self.write_indent()
  440. self.states.append(self.expect_flow_sequence_item)
  441. self.expect_node(sequence=True)
  442. def expect_flow_sequence_item(self):
  443. # type: () -> None
  444. if isinstance(self.event, SequenceEndEvent):
  445. self.indent = self.indents.pop()
  446. popped = self.flow_context.pop()
  447. assert popped == '['
  448. if self.canonical:
  449. self.write_indicator(u',', False)
  450. self.write_indent()
  451. self.write_indicator(u']', False)
  452. if self.event.comment and self.event.comment[0]:
  453. # eol comment on flow sequence
  454. self.write_post_comment(self.event)
  455. else:
  456. self.no_newline = False
  457. self.state = self.states.pop()
  458. else:
  459. self.write_indicator(u',', False)
  460. if self.canonical or self.column > self.best_width:
  461. self.write_indent()
  462. self.states.append(self.expect_flow_sequence_item)
  463. self.expect_node(sequence=True)
  464. # Flow mapping handlers.
  465. def expect_flow_mapping(self, single=False):
  466. # type: (Optional[bool]) -> None
  467. ind = self.indents.seq_flow_align(self.best_sequence_indent, self.column)
  468. map_init = u'{'
  469. if (
  470. single
  471. and self.flow_level
  472. and self.flow_context[-1] == '['
  473. and not self.canonical
  474. and not self.brace_single_entry_mapping_in_flow_sequence
  475. ):
  476. # single map item with flow context, no curly braces necessary
  477. map_init = u''
  478. self.write_indicator(u' ' * ind + map_init, True, whitespace=True)
  479. self.flow_context.append(map_init)
  480. self.increase_indent(flow=True, sequence=False)
  481. self.state = self.expect_first_flow_mapping_key
  482. def expect_first_flow_mapping_key(self):
  483. # type: () -> None
  484. if isinstance(self.event, MappingEndEvent):
  485. self.indent = self.indents.pop()
  486. popped = self.flow_context.pop()
  487. assert popped == '{' # empty flow mapping
  488. self.write_indicator(u'}', False)
  489. if self.event.comment and self.event.comment[0]:
  490. # eol comment on empty mapping
  491. self.write_post_comment(self.event)
  492. elif self.flow_level == 0:
  493. self.write_line_break()
  494. self.state = self.states.pop()
  495. else:
  496. if self.canonical or self.column > self.best_width:
  497. self.write_indent()
  498. if not self.canonical and self.check_simple_key():
  499. self.states.append(self.expect_flow_mapping_simple_value)
  500. self.expect_node(mapping=True, simple_key=True)
  501. else:
  502. self.write_indicator(u'?', True)
  503. self.states.append(self.expect_flow_mapping_value)
  504. self.expect_node(mapping=True)
  505. def expect_flow_mapping_key(self):
  506. # type: () -> None
  507. if isinstance(self.event, MappingEndEvent):
  508. # if self.event.comment and self.event.comment[1]:
  509. # self.write_pre_comment(self.event)
  510. self.indent = self.indents.pop()
  511. popped = self.flow_context.pop()
  512. assert popped in [u'{', u'']
  513. if self.canonical:
  514. self.write_indicator(u',', False)
  515. self.write_indent()
  516. if popped != u'':
  517. self.write_indicator(u'}', False)
  518. if self.event.comment and self.event.comment[0]:
  519. # eol comment on flow mapping, never reached on empty mappings
  520. self.write_post_comment(self.event)
  521. else:
  522. self.no_newline = False
  523. self.state = self.states.pop()
  524. else:
  525. self.write_indicator(u',', False)
  526. if self.canonical or self.column > self.best_width:
  527. self.write_indent()
  528. if not self.canonical and self.check_simple_key():
  529. self.states.append(self.expect_flow_mapping_simple_value)
  530. self.expect_node(mapping=True, simple_key=True)
  531. else:
  532. self.write_indicator(u'?', True)
  533. self.states.append(self.expect_flow_mapping_value)
  534. self.expect_node(mapping=True)
  535. def expect_flow_mapping_simple_value(self):
  536. # type: () -> None
  537. self.write_indicator(self.prefixed_colon, False)
  538. self.states.append(self.expect_flow_mapping_key)
  539. self.expect_node(mapping=True)
  540. def expect_flow_mapping_value(self):
  541. # type: () -> None
  542. if self.canonical or self.column > self.best_width:
  543. self.write_indent()
  544. self.write_indicator(self.prefixed_colon, True)
  545. self.states.append(self.expect_flow_mapping_key)
  546. self.expect_node(mapping=True)
  547. # Block sequence handlers.
  548. def expect_block_sequence(self):
  549. # type: () -> None
  550. if self.mapping_context:
  551. indentless = not self.indention
  552. else:
  553. indentless = False
  554. if not self.compact_seq_seq and self.column != 0:
  555. self.write_line_break()
  556. self.increase_indent(flow=False, sequence=True, indentless=indentless)
  557. self.state = self.expect_first_block_sequence_item
  558. def expect_first_block_sequence_item(self):
  559. # type: () -> Any
  560. return self.expect_block_sequence_item(first=True)
  561. def expect_block_sequence_item(self, first=False):
  562. # type: (bool) -> None
  563. if not first and isinstance(self.event, SequenceEndEvent):
  564. if self.event.comment and self.event.comment[1]:
  565. # final comments on a block list e.g. empty line
  566. self.write_pre_comment(self.event)
  567. self.indent = self.indents.pop()
  568. self.state = self.states.pop()
  569. self.no_newline = False
  570. else:
  571. if self.event.comment and self.event.comment[1]:
  572. self.write_pre_comment(self.event)
  573. nonl = self.no_newline if self.column == 0 else False
  574. self.write_indent()
  575. ind = self.sequence_dash_offset # if len(self.indents) > 1 else 0
  576. self.write_indicator(u' ' * ind + u'-', True, indention=True)
  577. if nonl or self.sequence_dash_offset + 2 > self.best_sequence_indent:
  578. self.no_newline = True
  579. self.states.append(self.expect_block_sequence_item)
  580. self.expect_node(sequence=True)
  581. # Block mapping handlers.
  582. def expect_block_mapping(self):
  583. # type: () -> None
  584. if not self.mapping_context and not (self.compact_seq_map or self.column == 0):
  585. self.write_line_break()
  586. self.increase_indent(flow=False, sequence=False)
  587. self.state = self.expect_first_block_mapping_key
  588. def expect_first_block_mapping_key(self):
  589. # type: () -> None
  590. return self.expect_block_mapping_key(first=True)
  591. def expect_block_mapping_key(self, first=False):
  592. # type: (Any) -> None
  593. if not first and isinstance(self.event, MappingEndEvent):
  594. if self.event.comment and self.event.comment[1]:
  595. # final comments from a doc
  596. self.write_pre_comment(self.event)
  597. self.indent = self.indents.pop()
  598. self.state = self.states.pop()
  599. else:
  600. if self.event.comment and self.event.comment[1]:
  601. # final comments from a doc
  602. self.write_pre_comment(self.event)
  603. self.write_indent()
  604. if self.check_simple_key():
  605. if not isinstance(
  606. self.event, (SequenceStartEvent, MappingStartEvent)
  607. ): # sequence keys
  608. try:
  609. if self.event.style == '?':
  610. self.write_indicator(u'?', True, indention=True)
  611. except AttributeError: # aliases have no style
  612. pass
  613. self.states.append(self.expect_block_mapping_simple_value)
  614. self.expect_node(mapping=True, simple_key=True)
  615. if isinstance(self.event, AliasEvent):
  616. self.stream.write(u' ')
  617. else:
  618. self.write_indicator(u'?', True, indention=True)
  619. self.states.append(self.expect_block_mapping_value)
  620. self.expect_node(mapping=True)
  621. def expect_block_mapping_simple_value(self):
  622. # type: () -> None
  623. if getattr(self.event, 'style', None) != '?':
  624. # prefix = u''
  625. if self.indent == 0 and self.top_level_colon_align is not None:
  626. # write non-prefixed colon
  627. c = u' ' * (self.top_level_colon_align - self.column) + self.colon
  628. else:
  629. c = self.prefixed_colon
  630. self.write_indicator(c, False)
  631. self.states.append(self.expect_block_mapping_key)
  632. self.expect_node(mapping=True)
  633. def expect_block_mapping_value(self):
  634. # type: () -> None
  635. self.write_indent()
  636. self.write_indicator(self.prefixed_colon, True, indention=True)
  637. self.states.append(self.expect_block_mapping_key)
  638. self.expect_node(mapping=True)
  639. # Checkers.
  640. def check_empty_sequence(self):
  641. # type: () -> bool
  642. return (
  643. isinstance(self.event, SequenceStartEvent)
  644. and bool(self.events)
  645. and isinstance(self.events[0], SequenceEndEvent)
  646. )
  647. def check_empty_mapping(self):
  648. # type: () -> bool
  649. return (
  650. isinstance(self.event, MappingStartEvent)
  651. and bool(self.events)
  652. and isinstance(self.events[0], MappingEndEvent)
  653. )
  654. def check_empty_document(self):
  655. # type: () -> bool
  656. if not isinstance(self.event, DocumentStartEvent) or not self.events:
  657. return False
  658. event = self.events[0]
  659. return (
  660. isinstance(event, ScalarEvent)
  661. and event.anchor is None
  662. and event.tag is None
  663. and event.implicit
  664. and event.value == ""
  665. )
  666. def check_simple_key(self):
  667. # type: () -> bool
  668. length = 0
  669. if isinstance(self.event, NodeEvent) and self.event.anchor is not None:
  670. if self.prepared_anchor is None:
  671. self.prepared_anchor = self.prepare_anchor(self.event.anchor)
  672. length += len(self.prepared_anchor)
  673. if (
  674. isinstance(self.event, (ScalarEvent, CollectionStartEvent))
  675. and self.event.tag is not None
  676. ):
  677. if self.prepared_tag is None:
  678. self.prepared_tag = self.prepare_tag(self.event.tag)
  679. length += len(self.prepared_tag)
  680. if isinstance(self.event, ScalarEvent):
  681. if self.analysis is None:
  682. self.analysis = self.analyze_scalar(self.event.value)
  683. length += len(self.analysis.scalar)
  684. return length < self.MAX_SIMPLE_KEY_LENGTH and (
  685. isinstance(self.event, AliasEvent)
  686. or (isinstance(self.event, SequenceStartEvent) and self.event.flow_style is True)
  687. or (isinstance(self.event, MappingStartEvent) and self.event.flow_style is True)
  688. or (
  689. isinstance(self.event, ScalarEvent)
  690. # if there is an explicit style for an empty string, it is a simple key
  691. and not (self.analysis.empty and self.style and self.style not in '\'"')
  692. and not self.analysis.multiline
  693. )
  694. or self.check_empty_sequence()
  695. or self.check_empty_mapping()
  696. )
  697. # Anchor, Tag, and Scalar processors.
  698. def process_anchor(self, indicator):
  699. # type: (Any) -> bool
  700. if self.event.anchor is None:
  701. self.prepared_anchor = None
  702. return False
  703. if self.prepared_anchor is None:
  704. self.prepared_anchor = self.prepare_anchor(self.event.anchor)
  705. if self.prepared_anchor:
  706. self.write_indicator(indicator + self.prepared_anchor, True)
  707. # issue 288
  708. self.no_newline = False
  709. self.prepared_anchor = None
  710. return True
  711. def process_tag(self):
  712. # type: () -> None
  713. tag = self.event.tag
  714. if isinstance(self.event, ScalarEvent):
  715. if self.style is None:
  716. self.style = self.choose_scalar_style()
  717. if self.event.value == '' and self.style == "'" and \
  718. tag == 'tag:yaml.org,2002:null' and self.alt_null is not None:
  719. self.event.value = self.alt_null
  720. self.analysis = None
  721. self.style = self.choose_scalar_style()
  722. if (not self.canonical or tag is None) and (
  723. (self.style == "" and self.event.implicit[0])
  724. or (self.style != "" and self.event.implicit[1])
  725. ):
  726. self.prepared_tag = None
  727. return
  728. if self.event.implicit[0] and tag is None:
  729. tag = u'!'
  730. self.prepared_tag = None
  731. else:
  732. if (not self.canonical or tag is None) and self.event.implicit:
  733. self.prepared_tag = None
  734. return
  735. if tag is None:
  736. raise EmitterError('tag is not specified')
  737. if self.prepared_tag is None:
  738. self.prepared_tag = self.prepare_tag(tag)
  739. if self.prepared_tag:
  740. self.write_indicator(self.prepared_tag, True)
  741. if (
  742. self.sequence_context
  743. and not self.flow_level
  744. and isinstance(self.event, ScalarEvent)
  745. ):
  746. self.no_newline = True
  747. self.prepared_tag = None
  748. def choose_scalar_style(self):
  749. # type: () -> Any
  750. if self.analysis is None:
  751. self.analysis = self.analyze_scalar(self.event.value)
  752. if self.event.style == '"' or self.canonical:
  753. return '"'
  754. if (not self.event.style or self.event.style == '?') and (
  755. self.event.implicit[0] or not self.event.implicit[2]
  756. ):
  757. if not (
  758. self.simple_key_context and (self.analysis.empty or self.analysis.multiline)
  759. ) and (
  760. self.flow_level
  761. and self.analysis.allow_flow_plain
  762. or (not self.flow_level and self.analysis.allow_block_plain)
  763. ):
  764. return ""
  765. self.analysis.allow_block = True
  766. if self.event.style and self.event.style in '|>':
  767. if (
  768. not self.flow_level
  769. and not self.simple_key_context
  770. and self.analysis.allow_block
  771. ):
  772. return self.event.style
  773. if not self.event.style and self.analysis.allow_double_quoted:
  774. if "'" in self.event.value or '\n' in self.event.value:
  775. return '"'
  776. if not self.event.style or self.event.style == "'":
  777. if self.analysis.allow_single_quoted and not (
  778. self.simple_key_context and self.analysis.multiline
  779. ):
  780. return "'"
  781. return '"'
  782. def process_scalar(self):
  783. # type: () -> None
  784. if self.analysis is None:
  785. self.analysis = self.analyze_scalar(self.event.value)
  786. if self.style is None:
  787. self.style = self.choose_scalar_style()
  788. split = not self.simple_key_context
  789. # if self.analysis.multiline and split \
  790. # and (not self.style or self.style in '\'\"'):
  791. # self.write_indent()
  792. # nprint('xx', self.sequence_context, self.flow_level)
  793. if self.sequence_context and not self.flow_level:
  794. self.write_indent()
  795. if self.style == '"':
  796. self.write_double_quoted(self.analysis.scalar, split)
  797. elif self.style == "'":
  798. self.write_single_quoted(self.analysis.scalar, split)
  799. elif self.style == '>':
  800. self.write_folded(self.analysis.scalar)
  801. elif self.style == '|':
  802. self.write_literal(self.analysis.scalar, self.event.comment)
  803. else:
  804. self.write_plain(self.analysis.scalar, split)
  805. self.analysis = None
  806. self.style = None
  807. if self.event.comment:
  808. self.write_post_comment(self.event)
  809. # Analyzers.
  810. def prepare_version(self, version):
  811. # type: (Any) -> Any
  812. major, minor = version
  813. if major != 1:
  814. raise EmitterError('unsupported YAML version: %d.%d' % (major, minor))
  815. return u'%d.%d' % (major, minor)
  816. def prepare_tag_handle(self, handle):
  817. # type: (Any) -> Any
  818. if not handle:
  819. raise EmitterError('tag handle must not be empty')
  820. if handle[0] != u'!' or handle[-1] != u'!':
  821. raise EmitterError("tag handle must start and end with '!': %r" % (utf8(handle)))
  822. for ch in handle[1:-1]:
  823. if not (
  824. u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' or ch in u'-_'
  825. ):
  826. raise EmitterError(
  827. 'invalid character %r in the tag handle: %r' % (utf8(ch), utf8(handle))
  828. )
  829. return handle
  830. def prepare_tag_prefix(self, prefix):
  831. # type: (Any) -> Any
  832. if not prefix:
  833. raise EmitterError('tag prefix must not be empty')
  834. chunks = [] # type: List[Any]
  835. start = end = 0
  836. if prefix[0] == u'!':
  837. end = 1
  838. ch_set = u"-;/?:@&=+$,_.~*'()[]"
  839. if self.dumper:
  840. version = getattr(self.dumper, 'version', (1, 2))
  841. if version is None or version >= (1, 2):
  842. ch_set += u'#'
  843. while end < len(prefix):
  844. ch = prefix[end]
  845. if u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' or ch in ch_set:
  846. end += 1
  847. else:
  848. if start < end:
  849. chunks.append(prefix[start:end])
  850. start = end = end + 1
  851. data = utf8(ch)
  852. for ch in data:
  853. chunks.append(u'%%%02X' % ord(ch))
  854. if start < end:
  855. chunks.append(prefix[start:end])
  856. return "".join(chunks)
  857. def prepare_tag(self, tag):
  858. # type: (Any) -> Any
  859. if not tag:
  860. raise EmitterError('tag must not be empty')
  861. if tag == u'!':
  862. return tag
  863. handle = None
  864. suffix = tag
  865. prefixes = sorted(self.tag_prefixes.keys())
  866. for prefix in prefixes:
  867. if tag.startswith(prefix) and (prefix == u'!' or len(prefix) < len(tag)):
  868. handle = self.tag_prefixes[prefix]
  869. suffix = tag[len(prefix) :]
  870. chunks = [] # type: List[Any]
  871. start = end = 0
  872. ch_set = u"-;/?:@&=+$,_.~*'()[]"
  873. if self.dumper:
  874. version = getattr(self.dumper, 'version', (1, 2))
  875. if version is None or version >= (1, 2):
  876. ch_set += u'#'
  877. while end < len(suffix):
  878. ch = suffix[end]
  879. if (
  880. u'0' <= ch <= u'9'
  881. or u'A' <= ch <= u'Z'
  882. or u'a' <= ch <= u'z'
  883. or ch in ch_set
  884. or (ch == u'!' and handle != u'!')
  885. ):
  886. end += 1
  887. else:
  888. if start < end:
  889. chunks.append(suffix[start:end])
  890. start = end = end + 1
  891. data = utf8(ch)
  892. for ch in data:
  893. chunks.append(u'%%%02X' % ord(ch))
  894. if start < end:
  895. chunks.append(suffix[start:end])
  896. suffix_text = "".join(chunks)
  897. if handle:
  898. return u'%s%s' % (handle, suffix_text)
  899. else:
  900. return u'!<%s>' % suffix_text
  901. def prepare_anchor(self, anchor):
  902. # type: (Any) -> Any
  903. if not anchor:
  904. raise EmitterError('anchor must not be empty')
  905. for ch in anchor:
  906. if not check_anchorname_char(ch):
  907. raise EmitterError(
  908. 'invalid character %r in the anchor: %r' % (utf8(ch), utf8(anchor))
  909. )
  910. return anchor
  911. def analyze_scalar(self, scalar):
  912. # type: (Any) -> Any
  913. # Empty scalar is a special case.
  914. if not scalar:
  915. return ScalarAnalysis(
  916. scalar=scalar,
  917. empty=True,
  918. multiline=False,
  919. allow_flow_plain=False,
  920. allow_block_plain=True,
  921. allow_single_quoted=True,
  922. allow_double_quoted=True,
  923. allow_block=False,
  924. )
  925. # Indicators and special characters.
  926. block_indicators = False
  927. flow_indicators = False
  928. line_breaks = False
  929. special_characters = False
  930. # Important whitespace combinations.
  931. leading_space = False
  932. leading_break = False
  933. trailing_space = False
  934. trailing_break = False
  935. break_space = False
  936. space_break = False
  937. # Check document indicators.
  938. if scalar.startswith(u'---') or scalar.startswith(u'...'):
  939. block_indicators = True
  940. flow_indicators = True
  941. # First character or preceded by a whitespace.
  942. preceeded_by_whitespace = True
  943. # Last character or followed by a whitespace.
  944. followed_by_whitespace = len(scalar) == 1 or scalar[1] in u'\0 \t\r\n\x85\u2028\u2029'
  945. # The previous character is a space.
  946. previous_space = False
  947. # The previous character is a break.
  948. previous_break = False
  949. index = 0
  950. while index < len(scalar):
  951. ch = scalar[index]
  952. # Check for indicators.
  953. if index == 0:
  954. # Leading indicators are special characters.
  955. if ch in u'#,[]{}&*!|>\'"%@`':
  956. flow_indicators = True
  957. block_indicators = True
  958. if ch in u'?:': # ToDo
  959. if self.serializer.use_version == (1, 1):
  960. flow_indicators = True
  961. elif len(scalar) == 1: # single character
  962. flow_indicators = True
  963. if followed_by_whitespace:
  964. block_indicators = True
  965. if ch == u'-' and followed_by_whitespace:
  966. flow_indicators = True
  967. block_indicators = True
  968. else:
  969. # Some indicators cannot appear within a scalar as well.
  970. if ch in u',[]{}': # http://yaml.org/spec/1.2/spec.html#id2788859
  971. flow_indicators = True
  972. if ch == u'?' and self.serializer.use_version == (1, 1):
  973. flow_indicators = True
  974. if ch == u':':
  975. if followed_by_whitespace:
  976. flow_indicators = True
  977. block_indicators = True
  978. if ch == u'#' and preceeded_by_whitespace:
  979. flow_indicators = True
  980. block_indicators = True
  981. # Check for line breaks, special, and unicode characters.
  982. if ch in u'\n\x85\u2028\u2029':
  983. line_breaks = True
  984. if not (ch == u'\n' or u'\x20' <= ch <= u'\x7E'):
  985. if (
  986. ch == u'\x85'
  987. or u'\xA0' <= ch <= u'\uD7FF'
  988. or u'\uE000' <= ch <= u'\uFFFD'
  989. or (self.unicode_supplementary and (u'\U00010000' <= ch <= u'\U0010FFFF'))
  990. ) and ch != u'\uFEFF':
  991. # unicode_characters = True
  992. if not self.allow_unicode:
  993. special_characters = True
  994. else:
  995. special_characters = True
  996. # Detect important whitespace combinations.
  997. if ch == u' ':
  998. if index == 0:
  999. leading_space = True
  1000. if index == len(scalar) - 1:
  1001. trailing_space = True
  1002. if previous_break:
  1003. break_space = True
  1004. previous_space = True
  1005. previous_break = False
  1006. elif ch in u'\n\x85\u2028\u2029':
  1007. if index == 0:
  1008. leading_break = True
  1009. if index == len(scalar) - 1:
  1010. trailing_break = True
  1011. if previous_space:
  1012. space_break = True
  1013. previous_space = False
  1014. previous_break = True
  1015. else:
  1016. previous_space = False
  1017. previous_break = False
  1018. # Prepare for the next character.
  1019. index += 1
  1020. preceeded_by_whitespace = ch in u'\0 \t\r\n\x85\u2028\u2029'
  1021. followed_by_whitespace = (
  1022. index + 1 >= len(scalar) or scalar[index + 1] in u'\0 \t\r\n\x85\u2028\u2029'
  1023. )
  1024. # Let's decide what styles are allowed.
  1025. allow_flow_plain = True
  1026. allow_block_plain = True
  1027. allow_single_quoted = True
  1028. allow_double_quoted = True
  1029. allow_block = True
  1030. # Leading and trailing whitespaces are bad for plain scalars.
  1031. if leading_space or leading_break or trailing_space or trailing_break:
  1032. allow_flow_plain = allow_block_plain = False
  1033. # We do not permit trailing spaces for block scalars.
  1034. if trailing_space:
  1035. allow_block = False
  1036. # Spaces at the beginning of a new line are only acceptable for block
  1037. # scalars.
  1038. if break_space:
  1039. allow_flow_plain = allow_block_plain = allow_single_quoted = False
  1040. # Spaces followed by breaks, as well as special character are only
  1041. # allowed for double quoted scalars.
  1042. if special_characters:
  1043. allow_flow_plain = allow_block_plain = allow_single_quoted = allow_block = False
  1044. elif space_break:
  1045. allow_flow_plain = allow_block_plain = allow_single_quoted = False
  1046. if not self.allow_space_break:
  1047. allow_block = False
  1048. # Although the plain scalar writer supports breaks, we never emit
  1049. # multiline plain scalars.
  1050. if line_breaks:
  1051. allow_flow_plain = allow_block_plain = False
  1052. # Flow indicators are forbidden for flow plain scalars.
  1053. if flow_indicators:
  1054. allow_flow_plain = False
  1055. # Block indicators are forbidden for block plain scalars.
  1056. if block_indicators:
  1057. allow_block_plain = False
  1058. return ScalarAnalysis(
  1059. scalar=scalar,
  1060. empty=False,
  1061. multiline=line_breaks,
  1062. allow_flow_plain=allow_flow_plain,
  1063. allow_block_plain=allow_block_plain,
  1064. allow_single_quoted=allow_single_quoted,
  1065. allow_double_quoted=allow_double_quoted,
  1066. allow_block=allow_block,
  1067. )
  1068. # Writers.
  1069. def flush_stream(self):
  1070. # type: () -> None
  1071. if hasattr(self.stream, 'flush'):
  1072. self.stream.flush()
  1073. def write_stream_start(self):
  1074. # type: () -> None
  1075. # Write BOM if needed.
  1076. if self.encoding and self.encoding.startswith('utf-16'):
  1077. self.stream.write(u'\uFEFF'.encode(self.encoding))
  1078. def write_stream_end(self):
  1079. # type: () -> None
  1080. self.flush_stream()
  1081. def write_indicator(self, indicator, need_whitespace, whitespace=False, indention=False):
  1082. # type: (Any, Any, bool, bool) -> None
  1083. if self.whitespace or not need_whitespace:
  1084. data = indicator
  1085. else:
  1086. data = u' ' + indicator
  1087. self.whitespace = whitespace
  1088. self.indention = self.indention and indention
  1089. self.column += len(data)
  1090. self.open_ended = False
  1091. if bool(self.encoding):
  1092. data = data.encode(self.encoding)
  1093. self.stream.write(data)
  1094. def write_indent(self):
  1095. # type: () -> None
  1096. indent = self.indent or 0
  1097. if (
  1098. not self.indention
  1099. or self.column > indent
  1100. or (self.column == indent and not self.whitespace)
  1101. ):
  1102. if bool(self.no_newline):
  1103. self.no_newline = False
  1104. else:
  1105. self.write_line_break()
  1106. if self.column < indent:
  1107. self.whitespace = True
  1108. data = u' ' * (indent - self.column)
  1109. self.column = indent
  1110. if self.encoding:
  1111. data = data.encode(self.encoding)
  1112. self.stream.write(data)
  1113. def write_line_break(self, data=None):
  1114. # type: (Any) -> None
  1115. if data is None:
  1116. data = self.best_line_break
  1117. self.whitespace = True
  1118. self.indention = True
  1119. self.line += 1
  1120. self.column = 0
  1121. if bool(self.encoding):
  1122. data = data.encode(self.encoding)
  1123. self.stream.write(data)
  1124. def write_version_directive(self, version_text):
  1125. # type: (Any) -> None
  1126. data = u'%%YAML %s' % version_text
  1127. if self.encoding:
  1128. data = data.encode(self.encoding)
  1129. self.stream.write(data)
  1130. self.write_line_break()
  1131. def write_tag_directive(self, handle_text, prefix_text):
  1132. # type: (Any, Any) -> None
  1133. data = u'%%TAG %s %s' % (handle_text, prefix_text)
  1134. if self.encoding:
  1135. data = data.encode(self.encoding)
  1136. self.stream.write(data)
  1137. self.write_line_break()
  1138. # Scalar streams.
  1139. def write_single_quoted(self, text, split=True):
  1140. # type: (Any, Any) -> None
  1141. if self.root_context:
  1142. if self.requested_indent is not None:
  1143. self.write_line_break()
  1144. if self.requested_indent != 0:
  1145. self.write_indent()
  1146. self.write_indicator(u"'", True)
  1147. spaces = False
  1148. breaks = False
  1149. start = end = 0
  1150. while end <= len(text):
  1151. ch = None
  1152. if end < len(text):
  1153. ch = text[end]
  1154. if spaces:
  1155. if ch is None or ch != u' ':
  1156. if (
  1157. start + 1 == end
  1158. and self.column > self.best_width
  1159. and split
  1160. and start != 0
  1161. and end != len(text)
  1162. ):
  1163. self.write_indent()
  1164. else:
  1165. data = text[start:end]
  1166. self.column += len(data)
  1167. if bool(self.encoding):
  1168. data = data.encode(self.encoding)
  1169. self.stream.write(data)
  1170. start = end
  1171. elif breaks:
  1172. if ch is None or ch not in u'\n\x85\u2028\u2029':
  1173. if text[start] == u'\n':
  1174. self.write_line_break()
  1175. for br in text[start:end]:
  1176. if br == u'\n':
  1177. self.write_line_break()
  1178. else:
  1179. self.write_line_break(br)
  1180. self.write_indent()
  1181. start = end
  1182. else:
  1183. if ch is None or ch in u' \n\x85\u2028\u2029' or ch == u"'":
  1184. if start < end:
  1185. data = text[start:end]
  1186. self.column += len(data)
  1187. if bool(self.encoding):
  1188. data = data.encode(self.encoding)
  1189. self.stream.write(data)
  1190. start = end
  1191. if ch == u"'":
  1192. data = u"''"
  1193. self.column += 2
  1194. if bool(self.encoding):
  1195. data = data.encode(self.encoding)
  1196. self.stream.write(data)
  1197. start = end + 1
  1198. if ch is not None:
  1199. spaces = ch == u' '
  1200. breaks = ch in u'\n\x85\u2028\u2029'
  1201. end += 1
  1202. self.write_indicator(u"'", False)
  1203. ESCAPE_REPLACEMENTS = {
  1204. u'\0': u'0',
  1205. u'\x07': u'a',
  1206. u'\x08': u'b',
  1207. u'\x09': u't',
  1208. u'\x0A': u'n',
  1209. u'\x0B': u'v',
  1210. u'\x0C': u'f',
  1211. u'\x0D': u'r',
  1212. u'\x1B': u'e',
  1213. u'"': u'"',
  1214. u'\\': u'\\',
  1215. u'\x85': u'N',
  1216. u'\xA0': u'_',
  1217. u'\u2028': u'L',
  1218. u'\u2029': u'P',
  1219. }
  1220. def write_double_quoted(self, text, split=True):
  1221. # type: (Any, Any) -> None
  1222. if self.root_context:
  1223. if self.requested_indent is not None:
  1224. self.write_line_break()
  1225. if self.requested_indent != 0:
  1226. self.write_indent()
  1227. self.write_indicator(u'"', True)
  1228. start = end = 0
  1229. while end <= len(text):
  1230. ch = None
  1231. if end < len(text):
  1232. ch = text[end]
  1233. if (
  1234. ch is None
  1235. or ch in u'"\\\x85\u2028\u2029\uFEFF'
  1236. or not (
  1237. u'\x20' <= ch <= u'\x7E'
  1238. or (
  1239. self.allow_unicode
  1240. and (u'\xA0' <= ch <= u'\uD7FF' or u'\uE000' <= ch <= u'\uFFFD')
  1241. )
  1242. )
  1243. ):
  1244. if start < end:
  1245. data = text[start:end]
  1246. self.column += len(data)
  1247. if bool(self.encoding):
  1248. data = data.encode(self.encoding)
  1249. self.stream.write(data)
  1250. start = end
  1251. if ch is not None:
  1252. if ch in self.ESCAPE_REPLACEMENTS:
  1253. data = u'\\' + self.ESCAPE_REPLACEMENTS[ch]
  1254. elif ch <= u'\xFF':
  1255. data = u'\\x%02X' % ord(ch)
  1256. elif ch <= u'\uFFFF':
  1257. data = u'\\u%04X' % ord(ch)
  1258. else:
  1259. data = u'\\U%08X' % ord(ch)
  1260. self.column += len(data)
  1261. if bool(self.encoding):
  1262. data = data.encode(self.encoding)
  1263. self.stream.write(data)
  1264. start = end + 1
  1265. if (
  1266. 0 < end < len(text) - 1
  1267. and (ch == u' ' or start >= end)
  1268. and self.column + (end - start) > self.best_width
  1269. and split
  1270. ):
  1271. data = text[start:end] + u'\\'
  1272. if start < end:
  1273. start = end
  1274. self.column += len(data)
  1275. if bool(self.encoding):
  1276. data = data.encode(self.encoding)
  1277. self.stream.write(data)
  1278. self.write_indent()
  1279. self.whitespace = False
  1280. self.indention = False
  1281. if text[start] == u' ':
  1282. data = u'\\'
  1283. self.column += len(data)
  1284. if bool(self.encoding):
  1285. data = data.encode(self.encoding)
  1286. self.stream.write(data)
  1287. end += 1
  1288. self.write_indicator(u'"', False)
  1289. def determine_block_hints(self, text):
  1290. # type: (Any) -> Any
  1291. indent = 0
  1292. indicator = u''
  1293. hints = u''
  1294. if text:
  1295. if text[0] in u' \n\x85\u2028\u2029':
  1296. indent = self.best_sequence_indent
  1297. hints += text_type(indent)
  1298. elif self.root_context:
  1299. for end in ['\n---', '\n...']:
  1300. pos = 0
  1301. while True:
  1302. pos = text.find(end, pos)
  1303. if pos == -1:
  1304. break
  1305. try:
  1306. if text[pos + 4] in ' \r\n':
  1307. break
  1308. except IndexError:
  1309. pass
  1310. pos += 1
  1311. if pos > -1:
  1312. break
  1313. if pos > 0:
  1314. indent = self.best_sequence_indent
  1315. if text[-1] not in u'\n\x85\u2028\u2029':
  1316. indicator = u'-'
  1317. elif len(text) == 1 or text[-2] in u'\n\x85\u2028\u2029':
  1318. indicator = u'+'
  1319. hints += indicator
  1320. return hints, indent, indicator
  1321. def write_folded(self, text):
  1322. # type: (Any) -> None
  1323. hints, _indent, _indicator = self.determine_block_hints(text)
  1324. self.write_indicator(u'>' + hints, True)
  1325. if _indicator == u'+':
  1326. self.open_ended = True
  1327. self.write_line_break()
  1328. leading_space = True
  1329. spaces = False
  1330. breaks = True
  1331. start = end = 0
  1332. while end <= len(text):
  1333. ch = None
  1334. if end < len(text):
  1335. ch = text[end]
  1336. if breaks:
  1337. if ch is None or ch not in u'\n\x85\u2028\u2029\a':
  1338. if (
  1339. not leading_space
  1340. and ch is not None
  1341. and ch != u' '
  1342. and text[start] == u'\n'
  1343. ):
  1344. self.write_line_break()
  1345. leading_space = ch == u' '
  1346. for br in text[start:end]:
  1347. if br == u'\n':
  1348. self.write_line_break()
  1349. else:
  1350. self.write_line_break(br)
  1351. if ch is not None:
  1352. self.write_indent()
  1353. start = end
  1354. elif spaces:
  1355. if ch != u' ':
  1356. if start + 1 == end and self.column > self.best_width:
  1357. self.write_indent()
  1358. else:
  1359. data = text[start:end]
  1360. self.column += len(data)
  1361. if bool(self.encoding):
  1362. data = data.encode(self.encoding)
  1363. self.stream.write(data)
  1364. start = end
  1365. else:
  1366. if ch is None or ch in u' \n\x85\u2028\u2029\a':
  1367. data = text[start:end]
  1368. self.column += len(data)
  1369. if bool(self.encoding):
  1370. data = data.encode(self.encoding)
  1371. self.stream.write(data)
  1372. if ch == u'\a':
  1373. if end < (len(text) - 1) and not text[end + 2].isspace():
  1374. self.write_line_break()
  1375. self.write_indent()
  1376. end += 2 # \a and the space that is inserted on the fold
  1377. else:
  1378. raise EmitterError('unexcpected fold indicator \\a before space')
  1379. if ch is None:
  1380. self.write_line_break()
  1381. start = end
  1382. if ch is not None:
  1383. breaks = ch in u'\n\x85\u2028\u2029'
  1384. spaces = ch == u' '
  1385. end += 1
  1386. def write_literal(self, text, comment=None):
  1387. # type: (Any, Any) -> None
  1388. hints, _indent, _indicator = self.determine_block_hints(text)
  1389. self.write_indicator(u'|' + hints, True)
  1390. try:
  1391. comment = comment[1][0]
  1392. if comment:
  1393. self.stream.write(comment)
  1394. except (TypeError, IndexError):
  1395. pass
  1396. if _indicator == u'+':
  1397. self.open_ended = True
  1398. self.write_line_break()
  1399. breaks = True
  1400. start = end = 0
  1401. while end <= len(text):
  1402. ch = None
  1403. if end < len(text):
  1404. ch = text[end]
  1405. if breaks:
  1406. if ch is None or ch not in u'\n\x85\u2028\u2029':
  1407. for br in text[start:end]:
  1408. if br == u'\n':
  1409. self.write_line_break()
  1410. else:
  1411. self.write_line_break(br)
  1412. if ch is not None:
  1413. if self.root_context:
  1414. idnx = self.indent if self.indent is not None else 0
  1415. self.stream.write(u' ' * (_indent + idnx))
  1416. else:
  1417. self.write_indent()
  1418. start = end
  1419. else:
  1420. if ch is None or ch in u'\n\x85\u2028\u2029':
  1421. data = text[start:end]
  1422. if bool(self.encoding):
  1423. data = data.encode(self.encoding)
  1424. self.stream.write(data)
  1425. if ch is None:
  1426. self.write_line_break()
  1427. start = end
  1428. if ch is not None:
  1429. breaks = ch in u'\n\x85\u2028\u2029'
  1430. end += 1
  1431. def write_plain(self, text, split=True):
  1432. # type: (Any, Any) -> None
  1433. if self.root_context:
  1434. if self.requested_indent is not None:
  1435. self.write_line_break()
  1436. if self.requested_indent != 0:
  1437. self.write_indent()
  1438. else:
  1439. self.open_ended = True
  1440. if not text:
  1441. return
  1442. if not self.whitespace:
  1443. data = u' '
  1444. self.column += len(data)
  1445. if self.encoding:
  1446. data = data.encode(self.encoding)
  1447. self.stream.write(data)
  1448. self.whitespace = False
  1449. self.indention = False
  1450. spaces = False
  1451. breaks = False
  1452. start = end = 0
  1453. while end <= len(text):
  1454. ch = None
  1455. if end < len(text):
  1456. ch = text[end]
  1457. if spaces:
  1458. if ch != u' ':
  1459. if start + 1 == end and self.column > self.best_width and split:
  1460. self.write_indent()
  1461. self.whitespace = False
  1462. self.indention = False
  1463. else:
  1464. data = text[start:end]
  1465. self.column += len(data)
  1466. if self.encoding:
  1467. data = data.encode(self.encoding)
  1468. self.stream.write(data)
  1469. start = end
  1470. elif breaks:
  1471. if ch not in u'\n\x85\u2028\u2029': # type: ignore
  1472. if text[start] == u'\n':
  1473. self.write_line_break()
  1474. for br in text[start:end]:
  1475. if br == u'\n':
  1476. self.write_line_break()
  1477. else:
  1478. self.write_line_break(br)
  1479. self.write_indent()
  1480. self.whitespace = False
  1481. self.indention = False
  1482. start = end
  1483. else:
  1484. if ch is None or ch in u' \n\x85\u2028\u2029':
  1485. data = text[start:end]
  1486. self.column += len(data)
  1487. if self.encoding:
  1488. data = data.encode(self.encoding)
  1489. try:
  1490. self.stream.write(data)
  1491. except: # NOQA
  1492. sys.stdout.write(repr(data) + '\n')
  1493. raise
  1494. start = end
  1495. if ch is not None:
  1496. spaces = ch == u' '
  1497. breaks = ch in u'\n\x85\u2028\u2029'
  1498. end += 1
  1499. def write_comment(self, comment, pre=False):
  1500. # type: (Any, bool) -> None
  1501. value = comment.value
  1502. # nprintf('{:02d} {:02d} {!r}'.format(self.column, comment.start_mark.column, value))
  1503. if not pre and value[-1] == '\n':
  1504. value = value[:-1]
  1505. try:
  1506. # get original column position
  1507. col = comment.start_mark.column
  1508. if comment.value and comment.value.startswith('\n'):
  1509. # never inject extra spaces if the comment starts with a newline
  1510. # and not a real comment (e.g. if you have an empty line following a key-value
  1511. col = self.column
  1512. elif col < self.column + 1:
  1513. ValueError
  1514. except ValueError:
  1515. col = self.column + 1
  1516. # nprint('post_comment', self.line, self.column, value)
  1517. try:
  1518. # at least one space if the current column >= the start column of the comment
  1519. # but not at the start of a line
  1520. nr_spaces = col - self.column
  1521. if self.column and value.strip() and nr_spaces < 1 and value[0] != '\n':
  1522. nr_spaces = 1
  1523. value = ' ' * nr_spaces + value
  1524. try:
  1525. if bool(self.encoding):
  1526. value = value.encode(self.encoding)
  1527. except UnicodeDecodeError:
  1528. pass
  1529. self.stream.write(value)
  1530. except TypeError:
  1531. raise
  1532. if not pre:
  1533. self.write_line_break()
  1534. def write_pre_comment(self, event):
  1535. # type: (Any) -> bool
  1536. comments = event.comment[1]
  1537. if comments is None:
  1538. return False
  1539. try:
  1540. start_events = (MappingStartEvent, SequenceStartEvent)
  1541. for comment in comments:
  1542. if isinstance(event, start_events) and getattr(comment, 'pre_done', None):
  1543. continue
  1544. if self.column != 0:
  1545. self.write_line_break()
  1546. self.write_comment(comment, pre=True)
  1547. if isinstance(event, start_events):
  1548. comment.pre_done = True
  1549. except TypeError:
  1550. sys.stdout.write('eventtt {} {}'.format(type(event), event))
  1551. raise
  1552. return True
  1553. def write_post_comment(self, event):
  1554. # type: (Any) -> bool
  1555. if self.event.comment[0] is None:
  1556. return False
  1557. comment = event.comment[0]
  1558. self.write_comment(comment)
  1559. return True