Last active
June 4, 2020 19:16
-
-
Save zackn9ne/5d67a92fabfb64599680c6356d755d2e to your computer and use it in GitHub Desktop.
yaml_plist_a_dir
This file contains 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 | |
# -*- coding: utf-8 -*- | |
"""If this script is run directly, it takes an input file .yaml file and an outputs a .recipe file in the same directory | |
yaml_plist.py <input path> | |
""" | |
import sys | |
import yaml | |
import os.path | |
from os import walk | |
from plistlib import writePlistToString | |
import yaml | |
def convert(data): | |
"""Do the conversion.""" | |
lines = writePlistToString(data).splitlines() | |
lines.append("") | |
return "\n".join(lines) | |
def get_all_files(in_path): | |
print 'searching in path: ', in_path | |
f = [] | |
y = [] | |
for (dirpath, dirnames, filenames) in walk(in_path): | |
for i in filenames: | |
if i.endswith('yaml'): | |
y.append(i) | |
f.extend(filenames) | |
break | |
return y | |
# returns all the yaml files found in the path | |
def yaml_plist(things_to_convert, where_to_save_them): | |
print (type(things_to_convert)) | |
print 'we found these things to convert: {}'.format(things_to_convert) | |
"""Convert yaml to plist.""" | |
for i in things_to_convert: | |
incoming_fullpath = '{}{}'.format(where_to_save_them,i) | |
if i.endswith('.recipe.yaml'): | |
target_file = i.replace('.recipe.yaml', '.recipe') | |
else: | |
target_file = i.replace('.yaml', '.recipe') | |
target_file = '{}{}'.format(where_to_save_them, target_file) | |
print 'we are going to make: {}'.format(target_file) | |
in_file = open(incoming_fullpath, "r") | |
out_file = open(target_file, "w") | |
input_data = yaml.safe_load(in_file) | |
output = convert(input_data) | |
out_file.writelines(output) | |
# in_file = open(in_path, "r") | |
# out_file = open(out_path, "w") | |
def main(): | |
"""Get the command line inputs if running this script directly.""" | |
if len(sys.argv) < 1: | |
print("Usage: yaml_plist.py <input-dir-path>") | |
sys.exit(1) | |
in_path = sys.argv[1] | |
# get_all_files(in_path) | |
things_to_convert = get_all_files(in_path) | |
yaml_plist(things_to_convert, in_path) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment