Skip to content

Commit

Permalink
Added the ability to convert YAML/path/file.yaml to path/file
Browse files Browse the repository at this point in the history
  • Loading branch information
grahampugh committed Jul 17, 2019
1 parent 1c13459 commit 17d2b12
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ $ ./plistyamlplist.py ~/Downloads/com.something.plist.yaml
# this will output to `~/Downloads/com.something.plist'
```

## YAML folder

If you have a folder named `YAML` in your path, and you do not supply a destination, the script will determine if a
corresponding folder exists in the path without 'YAML'. For example, consider the following file:

/Users/myuser/gitrepo/YAML/product/com.something.plist.yaml

If the folder `/Users/myuser/gitrepo/product` exists, the converted file will be created/overwritten at:

/Users/myuser/gitrepo/product/com.something.plist

If there is no `YAML` folder in the path, the converted file will be placed in the same folder

# Credits

Elements of these scripts come from [chaimleib/ppl](https://github.com/chaimleib/ppl) and [asciidoctor/sublimetext-asciidoc](https://github.com/asciidoctor/sublimetext-asciidoc)
90 changes: 69 additions & 21 deletions plistyamlplist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,89 @@
from yaml_plist import yaml_plist


def usage():
"""print help"""
print("Usage: plistyamlplist.py <input-file> <output-file>\n")
print("If <input-file> is a PLIST and <output-file> is omitted,\n"
"<input-file> is converted to <input-file>.yaml\n")
print("If <input-file> ends in .yaml or .yml and <output-file> is omitted,\n"
"<input-file>.yaml is converted to PLIST format with name <input-file>\n")


def check_if_plist(in_path):
"""rather than restrict by filename, check if the file is a plist by reading
the second line of the file for the PLIST declaration"""
with open(in_path) as fp:
for i, line in enumerate(fp):
if i == 1:
# print line
if line.find('PLIST 1.0') == -1:
is_plist = False
else:
is_plist = True
elif i > 2:
break
return is_plist


def check_for_yaml_folder(check_path):
"""Check folder hierarchy for a YAML folder. Output to same folder structure outwith YAML
folder if it exists,
e.g. /path/to/YAML/folder/subfolder/my.plist.yaml ==> /path/to/folder/subfolder/my.plist
Note there is no reverse option at this time"""
check_abspath = path.abspath(check_path)
if 'YAML' in check_abspath:
print('YAML folder exists : {}'.format(check_abspath))
top_path, base_path = check_abspath.split('YAML/')
out_path = path.dirname(path.join(top_path, base_path))
if path.exists(out_path):
print('Path exists : {}'.format(out_path))
return out_path
else:
print('Path does not exist : {}'.format(out_path))
print('Please create this folder and try again')
exit(1)


def main():
"""Get the command line inputs if running this script directly"""
"""get the command line inputs if running this script directly"""
if len(sys.argv) < 2:
print("Usage: plistyamlplist.py <input-file> <output-file>")
usage()
exit(1)

in_path = sys.argv[1]
try:
sys.argv[2]
except Exception as e:
except IndexError:
if in_path.endswith('.yaml') or in_path.endswith('.yml'):
filename, file_extension = path.splitext(in_path)
out_path = filename
out_dir = check_for_yaml_folder(in_path)
if out_dir:
filename, file_extension = path.splitext(path.basename(in_path))
out_path = path.join(out_dir, filename)
else:
filename, file_extension = path.splitext(path.abspath(in_path))
out_path = filename
else:
# rather than restrict by filename, check if the file is a plist by reading
# the second line of the file for the PLIST declaration
with open(in_path) as fp:
for i, line in enumerate(fp):
if i == 1:
print line
if line.find('PLIST 1.0') == -1:
print("Usage: plistyamlplist.py <input-file> <output-file>")
exit(1)
else:
out_path = "{}.yaml".format(in_path)
elif i > 2:
break
if check_if_plist(in_path):
out_path = "{}.yaml".format(in_path)
else:
print("\nERROR: File is neither PLIST nor YAML format.\n")
usage()
exit(1)
else:
out_path = sys.argv[2]

# auto-determine which direction the conversion should go
if out_path.endswith('.yaml'):
plist_yaml(in_path, out_path)
else:
if in_path.endswith('.yaml'):
yaml_plist(in_path, out_path)
else:
if check_if_plist(in_path):
plist_yaml(in_path, out_path)
else:
print("\nERROR: Input File is neither PLIST nor YAML format.\n")
usage()
exit(1)
print('Wrote to : {}'.format(out_path))


if __name__ == '__main__':
Expand Down

0 comments on commit 17d2b12

Please sign in to comment.