-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathshyaml.py
executable file
·835 lines (620 loc) · 24.9 KB
/
shyaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/usr/bin/env python
"""
YAML for command line.
"""
## Note: to launch test, you can use:
## python -m doctest -d shyaml.py
## or
## nosetests
from __future__ import print_function
import sys
import os.path
import re
import textwrap
import locale
import yaml
__version__ = "%%version%%" ## gets filled at release time by ./autogen.sh
__with_libyaml__ = False
if not os.environ.get("FORCE_PYTHON_YAML_IMPLEMENTATION"):
try:
from yaml import CSafeLoader as SafeLoader, CSafeDumper as SafeDumper
__with_libyaml__ = True
except ImportError: ## pragma: no cover
pass
if not __with_libyaml__:
from yaml import SafeLoader, SafeDumper ## noqa: F811
__with_libyaml__ = False
PY3 = sys.version_info[0] >= 3
WIN32 = sys.platform == 'win32'
EXNAME = os.path.basename(__file__ if WIN32 else sys.argv[0])
for ext in (".py", ".pyc", ".exe", "-script.py", "-script.pyc"): ## pragma: no cover
if EXNAME.endswith(ext): ## pragma: no cover
EXNAME = EXNAME[:-len(ext)]
break
USAGE = """\
Usage:
%(exname)s {-h|--help}
%(exname)s {-V|--version}
%(exname)s [-y|--yaml] ACTION KEY [DEFAULT]
""" % {"exname": EXNAME}
HELP = """
Parses and output chosen subpart or values from YAML input.
It reads YAML in stdin and will output on stdout it's return value.
%(usage)s
Options:
-y, --yaml
Output only YAML safe value, more precisely, even
literal values will be YAML quoted. This behavior
is required if you want to output YAML subparts and
further process it. If you know you have are dealing
with safe literal value, then you don't need this.
(Default: no safe YAML output)
ACTION Depending on the type of data you've targetted
thanks to the KEY, ACTION can be:
These ACTIONs applies to any YAML type:
get-type ## returns a short string
get-value ## returns YAML
These ACTIONs applies to 'sequence' and 'struct' YAML type:
get-values{,-0} ## returns list of YAML
get-length ## returns an integer
These ACTION applies to 'struct' YAML type:
keys{,-0} ## returns list of YAML
values{,-0} ## returns list of YAML
key-values,{,-0} ## returns list of YAML
Note that any value returned is returned on stdout, and
when returning ``list of YAML``, it'll be separated by
a newline or ``NUL`` char depending of you've used the
``-0`` suffixed ACTION.
KEY Identifier to browse and target subvalues into YAML
structure. Use ``.`` to parse a subvalue. If you need
to use a literal ``.`` or ``\\``, use ``\\`` to quote it.
Use struct keyword to browse ``struct`` YAML data and use
integers to browse ``sequence`` YAML data.
DEFAULT if not provided and given KEY do not match any value in
the provided YAML, then DEFAULT will be returned. If no
default is provided and the KEY do not match any value
in the provided YAML, %(exname)s will fail with an error
message.
Examples:
## get last grocery
cat recipe.yaml | %(exname)s get-value groceries.-1
## get all words of my french dictionary
cat dictionaries.yaml | %(exname)s keys-0 french.dictionary
## get YAML config part of 'myhost'
cat hosts_config.yaml | %(exname)s get-value cfgs.myhost
""" % {"exname": EXNAME, "usage": USAGE}
class ShyamlSafeLoader(SafeLoader):
"""Shyaml specific safe loader"""
class ShyamlSafeDumper(SafeDumper):
"""Shyaml specific safe dumper"""
## Ugly way to force both the Cython code and the normal code
## to get the output line by line.
class ForcedLineStream(object):
def __init__(self, fileobj):
self._file = fileobj
def read(self, size=-1):
## don't care about size
return self._file.readline()
def close(self):
## XXXvlab: for some reason, ``.close(..)`` doesn't seem to
## be used by any code. I'll keep this to avoid any bad surprise.
return self._file.close() ## pragma: no cover
class LineLoader(ShyamlSafeLoader):
"""Forcing stream in line buffer mode"""
def __init__(self, stream):
stream = ForcedLineStream(stream)
super(LineLoader, self).__init__(stream)
##
## Keep previous order in YAML
##
try:
## included in standard lib from Python 2.7
from collections import OrderedDict
except ImportError: ## pragma: no cover
## try importing the backported drop-in replacement
## it's available on PyPI
from ordereddict import OrderedDict
## Ensure that there are no collision with legacy OrderedDict
## that could be used for omap for instance.
class MyOrderedDict(OrderedDict):
pass
ShyamlSafeDumper.add_representer(
MyOrderedDict,
lambda cls, data: cls.represent_dict(data.items()))
def construct_omap(cls, node):
## Force unfolding reference and merges
## otherwise it would fail on 'merge'
cls.flatten_mapping(node)
return MyOrderedDict(cls.construct_pairs(node))
ShyamlSafeLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_omap)
##
## Support local and global objects
##
class EncapsulatedNode(object):
"""Holds a yaml node"""
def mk_encapsulated_node(s, node):
method = "construct_%s" % (node.id, )
data = getattr(s, method)(node)
class _E(data.__class__, EncapsulatedNode):
pass
_E.__name__ = str(node.tag)
_E._node = node
return _E(data)
def represent_encapsulated_node(s, o):
value = s.represent_data(o.__class__.__bases__[0](o))
value.tag = o.__class__.__name__
return value
ShyamlSafeDumper.add_multi_representer(EncapsulatedNode,
represent_encapsulated_node)
ShyamlSafeLoader.add_constructor(None, mk_encapsulated_node)
##
## Key specifier
##
def tokenize(s):
r"""Returns an iterable through all subparts of string splitted by '.'
So:
>>> list(tokenize('foo.bar.wiz'))
['foo', 'bar', 'wiz']
Contrary to traditional ``.split()`` method, this function has to
deal with any type of data in the string. So it actually
interprets the string. Characters with meaning are '.' and '\'.
Both of these can be included in a token by quoting them with '\'.
So dot of slashes can be contained in token:
>>> print('\n'.join(tokenize(r'foo.dot<\.>.slash<\\>')))
foo
dot<.>
slash<\>
Notice that empty keys are also supported:
>>> list(tokenize(r'foo..bar'))
['foo', '', 'bar']
Given an empty string:
>>> list(tokenize(r''))
['']
And a None value:
>>> list(tokenize(None))
[]
"""
if s is None:
return
tokens = (re.sub(r'\\(\\|\.)', r'\1', m.group(0))
for m in re.finditer(r'((\\.|[^.\\])*)', s))
## an empty string superfluous token is added after all non-empty token
for token in tokens:
if len(token) != 0:
next(tokens)
yield token
def mget(dct, key):
r"""Allow to get values deep in recursive dict with doted keys
Accessing leaf values is quite straightforward:
>>> dct = {'a': {'x': 1, 'b': {'c': 2}}}
>>> mget(dct, 'a.x')
1
>>> mget(dct, 'a.b.c')
2
But you can also get subdict if your key is not targeting a
leaf value:
>>> mget(dct, 'a.b')
{'c': 2}
As a special feature, list access is also supported by providing a
(possibily signed) integer, it'll be interpreted as usual python
sequence access using bracket notation:
>>> mget({'a': {'x': [1, 5], 'b': {'c': 2}}}, 'a.x.-1')
5
>>> mget({'a': {'x': 1, 'b': [{'c': 2}]}}, 'a.b.0.c')
2
Keys that contains '.' can be accessed by escaping them:
>>> dct = {'a': {'x': 1}, 'a.x': 3, 'a.y': 4}
>>> mget(dct, 'a.x')
1
>>> mget(dct, r'a\.x')
3
>>> mget(dct, r'a.y') ## doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
MissingKeyError: missing key 'y' in dict.
>>> mget(dct, r'a\.y')
4
As a consequence, if your key contains a '\', you should also escape it:
>>> dct = {r'a\x': 3, r'a\.x': 4, 'a.x': 5, 'a\\': {'x': 6}}
>>> mget(dct, r'a\\x')
3
>>> mget(dct, r'a\\\.x')
4
>>> mget(dct, r'a\\.x')
6
>>> mget({'a\\': {'b': 1}}, r'a\\.b')
1
>>> mget({r'a.b\.c': 1}, r'a\.b\\\.c')
1
And even empty strings key are supported:
>>> dct = {r'a': {'': {'y': 3}, 'y': 4}, 'b': {'': {'': 1}}, '': 2}
>>> mget(dct, r'a..y')
3
>>> mget(dct, r'a.y')
4
>>> mget(dct, r'')
2
>>> mget(dct, r'b..')
1
It will complain if you are trying to get into a leaf:
>>> mget({'a': 1}, 'a.y') ## doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
NonDictLikeTypeError: can't query subvalue 'y' of a leaf...
if the key is None, the whole dct should be sent back:
>>> mget({'a': 1}, None)
{'a': 1}
"""
return aget(dct, tokenize(key))
class MissingKeyError(KeyError):
"""Raised when querying a dict-like structure on non-existing keys"""
def __str__(self):
return self.args[0]
class NonDictLikeTypeError(TypeError):
"""Raised when attempting to traverse non-dict like structure"""
class IndexNotIntegerError(ValueError):
"""Raised when attempting to traverse sequence without using an integer"""
class IndexOutOfRange(IndexError):
"""Raised when attempting to traverse sequence without using an integer"""
def aget(dct, key):
r"""Allow to get values deep in a dict with iterable keys
Accessing leaf values is quite straightforward:
>>> dct = {'a': {'x': 1, 'b': {'c': 2}}}
>>> aget(dct, ('a', 'x'))
1
>>> aget(dct, ('a', 'b', 'c'))
2
If key is empty, it returns unchanged the ``dct`` value.
>>> aget({'x': 1}, ())
{'x': 1}
"""
key = iter(key)
try:
head = next(key)
except StopIteration:
return dct
if isinstance(dct, list):
try:
idx = int(head)
except ValueError:
raise IndexNotIntegerError(
"non-integer index %r provided on a list."
% head)
try:
value = dct[idx]
except IndexError:
raise IndexOutOfRange(
"index %d is out of range (%d elements in list)."
% (idx, len(dct)))
else:
try:
value = dct[head]
except KeyError:
## Replace with a more informative KeyError
raise MissingKeyError(
"missing key %r in dict."
% (head, ))
except Exception:
raise NonDictLikeTypeError(
"can't query subvalue %r of a leaf%s."
% (head,
(" (leaf value is %r)" % dct)
if len(repr(dct)) < 15 else ""))
return aget(value, key)
def stderr(msg):
"""Convenience function to write short message to stderr."""
sys.stderr.write(msg)
def stdout(value):
"""Convenience function to write short message to stdout."""
sys.stdout.write(value)
def die(msg, errlvl=1, prefix="Error: "):
"""Convenience function to write short message to stderr and quit."""
stderr("%s%s\n" % (prefix, msg))
sys.exit(errlvl)
SIMPLE_TYPES = (str if PY3 else basestring, int, float, type(None))
COMPLEX_TYPES = (list, dict)
if PY3:
STRING_TYPES = (str, )
else:
STRING_TYPES = (unicode, str)
## these are not composite values
ACTION_SUPPORTING_STREAMING=["get-type", "get-length", "get-value"]
def magic_dump(value):
"""Returns a representation of values directly usable by bash.
Literal types are printed as-is (avoiding quotes around string for
instance). But complex type are written in a YAML useable format.
"""
return "%s" % value if isinstance(value, SIMPLE_TYPES) \
else yaml_dump(value)
def yaml_dump(value):
"""Returns a representation of values directly usable by bash.
Literal types are quoted and safe to use as YAML.
"""
return yaml.dump(value, default_flow_style=False,
Dumper=ShyamlSafeDumper)
def type_name(value):
"""Returns pseudo-YAML type name of given value."""
return type(value).__name__ if isinstance(value, EncapsulatedNode) else \
"struct" if isinstance(value, dict) else \
"sequence" if isinstance(value, (tuple, list)) else \
"str" if isinstance(value, STRING_TYPES) else \
type(value).__name__
def get_version_info():
if yaml.__with_libyaml__:
import _yaml
libyaml_version = _yaml.get_version_string()
else:
libyaml_version = False
return ("unreleased" if __version__.startswith('%%') else __version__,
yaml.__version__,
libyaml_version,
__with_libyaml__,
sys.version.replace("\n", " "),
)
def _parse_args(args, USAGE, HELP):
opts = {}
opts["dump"] = magic_dump
for arg in ["-y", "--yaml"]:
if arg in args:
args.remove(arg)
opts["dump"] = yaml_dump
opts["quiet"] = False
for arg in ["-q", "--quiet"]:
if arg in args:
args.remove(arg)
opts["quiet"] = True
for arg in ["-L", "--line-buffer"]:
if arg not in args:
continue
args.remove(arg)
opts["loader"] = LineLoader
if len(args) == 0:
stderr("Error: Bad number of arguments.\n")
die(USAGE, errlvl=1, prefix="")
if len(args) == 1 and args[0] in ("-h", "--help"):
stdout(HELP)
exit(0)
if len(args) == 1 and args[0] in ("-V", "--version"):
version_info = get_version_info()
print("version: %s\nPyYAML: %s\nlibyaml available: %s\nlibyaml used: %s\nPython: %s"
% version_info)
exit(0)
opts["action"] = args[0]
opts["key"] = None if len(args) == 1 else args[1]
opts["default"] = args[2] if len(args) > 2 else None
return opts
class InvalidPath(KeyError):
"""Invalid Path"""
def __str__(self):
return self.args[0]
class InvalidAction(KeyError):
"""Invalid Action"""
def traverse(contents, path, default=None):
try:
try:
value = mget(contents, path)
except (IndexOutOfRange, MissingKeyError):
if default is None:
raise
value = default
except (IndexOutOfRange, MissingKeyError,
NonDictLikeTypeError, IndexNotIntegerError) as exc:
msg = str(exc)
raise InvalidPath(
"invalid path %r, %s"
% (path, msg.replace('list', 'sequence').replace('dict', 'struct')))
return value
class ActionTypeError(Exception):
def __init__(self, action, provided, expected):
self.action = action
self.provided = provided
self.expected = expected
def __str__(self):
return ("%s does not support %r type. "
"Please provide or select a %s."
% (self.action, self.provided,
self.expected[0] if len(self.expected) == 1 else
("%s or %s" % (", ".join(self.expected[:-1]),
self.expected[-1]))))
def act(action, value, dump=yaml_dump):
tvalue = type_name(value)
## Note: ``\n`` will be transformed by ``universal_newlines`` mecanism for
## any platform
termination = "\0" if action.endswith("-0") else "\n"
if action == "get-value":
return "%s" % dump(value)
elif action in ("get-values", "get-values-0"):
if isinstance(value, dict):
return "".join("".join((dump(k), termination,
dump(v), termination))
for k, v in value.items())
elif isinstance(value, list):
return "".join("".join((dump(l), termination))
for l in value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action == "get-type":
return tvalue
elif action == "get-length":
if isinstance(value, (dict, list)):
return len(value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action in ("keys", "keys-0",
"values", "values-0",
"key-values", "key-values-0"):
if isinstance(value, dict):
method = value.keys if action.startswith("keys") else \
value.items if action.startswith("key-values") else \
value.values
output = (lambda x: termination.join("%s" % dump(e) for e in x)) \
if action.startswith("key-values") else \
dump
return "".join("".join((str(output(k)), termination)) for k in method())
else:
raise ActionTypeError(
action=action, provided=tvalue, expected=["struct"])
else:
raise InvalidAction(action)
def do(stream, action, key, default=None, dump=yaml_dump,
loader=ShyamlSafeLoader):
"""Return string representations of target value in stream YAML
The key is used for traversal of the YAML structure to target
the value that will be dumped.
:param stream: file like input yaml content
:param action: string identifying one of the possible supported actions
:param key: string dotted expression to traverse yaml input
:param default: optional default value in case of missing end value when
traversing input yaml. (default is ``None``)
:param dump: callable that will be given python objet to dump in yaml
(default is ``yaml_dump``)
:param loader: PyYAML's *Loader subclass to parse YAML
(default is ShyamlSafeLoader)
:return: generator of string representation of target value per
YAML docs in the given stream.
:raises ActionTypeError: when there's a type mismatch between the
action selected and the type of the targetted value.
(ie: action 'key-values' on non-struct)
:raises InvalidAction: when selected action is not a recognised valid
action identifier.
:raises InvalidPath: upon inexistent content when traversing YAML
input following the key specification.
"""
at_least_one_content = False
for content in yaml.load_all(stream, Loader=loader):
at_least_one_content = True
value = traverse(content, key, default=default)
yield act(action, value, dump=dump)
## In case of empty stream, we consider that it is equivalent
## to one document having the ``null`` value.
if at_least_one_content is False:
value = traverse(None, key, default=default)
yield act(action, value, dump=dump)
def main(args): ## pylint: disable=too-many-branches
"""Entrypoint of the whole commandline application"""
EXNAME = os.path.basename(__file__ if WIN32 else sys.argv[0])
for ext in (".py", ".pyc", ".exe", "-script.py", "-script.pyc"): ## pragma: no cover
if EXNAME.endswith(ext): ## pragma: no cover
EXNAME = EXNAME[:-len(ext)]
break
USAGE = """\
Usage:
%(exname)s {-h|--help}
%(exname)s {-V|--version}
%(exname)s [-y|--yaml] [-q|--quiet] ACTION KEY [DEFAULT]
""" % {"exname": EXNAME}
HELP = """
Parses and output chosen subpart or values from YAML input.
It reads YAML in stdin and will output on stdout it's return value.
%(usage)s
Options:
-y, --yaml
Output only YAML safe value, more precisely, even
literal values will be YAML quoted. This behavior
is required if you want to output YAML subparts and
further process it. If you know you have are dealing
with safe literal value, then you don't need this.
(Default: no safe YAML output)
-q, --quiet
In case KEY value queried is an invalid path, quiet
mode will prevent the writing of an error message on
standard error.
(Default: no quiet mode)
-L, --line-buffer
Force parsing stdin line by line allowing to process
streamed YAML as it is fed instead of buffering
input and treating several YAML streamed document
at once. This is likely to have some small performance
hit if you have a huge stream of YAML document, but
then you probably don't really care about the
line-buffering.
(Default: no line buffering)
ACTION Depending on the type of data you've targetted
thanks to the KEY, ACTION can be:
These ACTIONs applies to any YAML type:
get-type ## returns a short string
get-value ## returns YAML
These ACTIONs applies to 'sequence' and 'struct' YAML type:
get-values{,-0} ## returns list of YAML
get-length ## returns an integer
These ACTION applies to 'struct' YAML type:
keys{,-0} ## returns list of YAML
values{,-0} ## returns list of YAML
key-values,{,-0} ## returns list of YAML
Note that any value returned is returned on stdout, and
when returning ``list of YAML``, it'll be separated by
a newline or ``NUL`` char depending of you've used the
``-0`` suffixed ACTION.
KEY Identifier to browse and target subvalues into YAML
structure. Use ``.`` to parse a subvalue. If you need
to use a literal ``.`` or ``\\``, use ``\\`` to quote it.
Use struct keyword to browse ``struct`` YAML data and use
integers to browse ``sequence`` YAML data.
DEFAULT if not provided and given KEY do not match any value in
the provided YAML, then DEFAULT will be returned. If no
default is provided and the KEY do not match any value
in the provided YAML, %(exname)s will fail with an error
message.
Examples:
## get last grocery
cat recipe.yaml | %(exname)s get-value groceries.-1
## get all words of my french dictionary
cat dictionaries.yaml | %(exname)s keys-0 french.dictionary
## get YAML config part of 'myhost'
cat hosts_config.yaml | %(exname)s get-value cfgs.myhost
""" % {"exname": EXNAME, "usage": USAGE}
USAGE = textwrap.dedent(USAGE)
HELP = textwrap.dedent(HELP)
opts = _parse_args(args, USAGE, HELP)
quiet = opts.pop("quiet")
try:
first = True
for output in do(stream=sys.stdin, **opts):
if first:
first = False
else:
if opts["action"] not in ACTION_SUPPORTING_STREAMING:
die("Source YAML is multi-document, "
"which doesn't support any other action than %s"
% ", ".join(ACTION_SUPPORTING_STREAMING))
if opts["dump"] is yaml_dump:
print("---\n", end="")
else:
print("\0", end="")
if opts.get("loader") is LineLoader:
sys.stdout.flush()
safe_print(output)
if opts.get("loader") is LineLoader:
sys.stdout.flush()
except (InvalidPath, ActionTypeError) as e:
if quiet:
exit(1)
else:
die(str(e))
except InvalidAction as e:
die("'%s' is not a valid action.\n%s"
% (e.args[0], USAGE))
##
## Safe print
##
## Note that locale.getpreferredencoding() does NOT follow
## PYTHONIOENCODING by default, but ``sys.stdout.encoding`` does. In
## PY2, ``sys.stdout.encoding`` without PYTHONIOENCODING set does not
## get any values set in subshells. However, if _preferred_encoding
## is not set to utf-8, it leads to encoding errors.
_preferred_encoding = os.environ.get("PYTHONIOENCODING") or \
locale.getpreferredencoding()
def safe_print(content):
if not PY3:
if isinstance(content, unicode):
content = content.encode(_preferred_encoding)
print(content, end='')
sys.stdout.flush()
def entrypoint():
sys.exit(main(sys.argv[1:]))
if __name__ == "__main__":
entrypoint()