Last active
August 1, 2017 20:29
-
-
Save davidfraser/3d4b19ad41d7a7d0409eb92cd5907016 to your computer and use it in GitHub Desktop.
Script for making cut-down version of pyyaml that is a single-file yaml loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Command-line utility to combine the necessary modules to make a single-file yaml_decoder.py""" | |
import shutil | |
import os | |
from os.path import abspath, dirname, exists, join | |
include_modules = 'loader reader parser scanner composer constructor resolver nodes events tokens error'.split() | |
# this should be placed in the pyyaml source tree | |
root_dir = dirname(abspath(__file__)) | |
src_dir = join(root_dir, 'lib', 'yaml') | |
dist_dir = join(root_dir, 'dist') | |
if not exists(dist_dir): | |
os.makedirs(dist_dir) | |
output_file = join(dist_dir, 'yaml_decoder.py') | |
header = '''#!/usr/bin/env python | |
"""yaml_decoder is a stripped-down bundle of pyyaml that offers only decoding/loading (not encoding/dumping) in a single pure-python module""" | |
# See https://gist.github.com/davidfraser/3d4b19ad41d7a7d0409eb92cd5907016 for updated versions | |
''' | |
with open(output_file, 'w') as out_f: | |
out_f.write(header) | |
with open(join(root_dir, 'LICENSE')) as in_f: | |
for line in in_f: | |
out_f.write(('# %s' % line).rstrip() + '\n') | |
# include the first part of the file, before the functions | |
with open(join(src_dir, '__init__.py')) as in_f: | |
for line in in_f: | |
if line.startswith('from '): | |
module_name = line.split()[1] | |
if module_name in include_modules: | |
continue | |
elif line.startswith('def '): | |
break | |
else: | |
out_f.write(line) | |
out_f.write('\n\n') | |
for module in reversed(include_modules): | |
filename = '%s.py' % module | |
out_f.write('\n\n# including %s\n\n' % filename) | |
with open(join(src_dir, filename)) as in_f: | |
await_closebracket = False | |
for line in in_f: | |
if line.startswith('from '): | |
module_name = line.split()[1] | |
if module_name in include_modules: | |
continue | |
elif line.startswith('__all__'): | |
if '[' in line and ']' not in line: | |
await_closebracket = True | |
elif await_closebracket and ']' in line: | |
await_closebracket = False | |
elif not await_closebracket: | |
out_f.write(line.replace('pureyaml.grammar.', 'pureyaml_grammar_')) | |
# include the load functions | |
out_f.write('\n\n# load functions from __init__.py\n\n') | |
with open(join(src_dir, '__init__.py')) as in_f: | |
in_load, post_load = False, False | |
for line in in_f: | |
if line.startswith('def '): | |
in_load = True | |
if line.startswith('def emit'): | |
post_load = True | |
if in_load and not post_load: | |
out_f.write(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment