Skip to content

Commit

Permalink
imports pyyaml if not installed already
Browse files Browse the repository at this point in the history
  • Loading branch information
grahampugh committed Feb 10, 2021
1 parent 26bb4d5 commit 473ccb5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
11 changes: 10 additions & 1 deletion plistyamlplist.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_out_path(in_path, filetype):
else:
filename, _ = os.path.splitext(os.path.abspath(in_path))
out_path = filename
if filetype == "json":
elif filetype == "json":
out_dir = check_for_json_folder(in_path)
if out_dir:
filename, _ = os.path.splitext(os.path.basename(in_path))
Expand Down Expand Up @@ -146,8 +146,10 @@ def main():
for glob_file in glob_files:
out_path = get_out_path(glob_file, filetype)
if filetype == "yaml":
print("Processing YAML folder with globs...")
yaml_plist(glob_file, out_path)
elif filetype == "json":
print("Processing JSON folder with globs...")
json_plist(glob_file, out_path)
else:
try:
Expand All @@ -157,17 +159,23 @@ def main():
else:
out_path = sys.argv[2]
if filetype == "yaml":
print("Processing yaml file...")
yaml_plist(in_path, out_path)
elif filetype == "json":
print("Processing json file...")
json_plist(in_path, out_path)
# allow for converting whole folders if 'YAML' or 'JSON' is in the path
# and the path supplied is a folder
elif os.path.isdir(in_path) and "YAML" in in_path:
print("Processing YAML folder...")
filetype = "yaml"
for in_file in os.listdir(in_path):
in_file_path = os.path.join(in_path, in_file)
out_path = get_out_path(in_file_path, filetype)
yaml_plist(in_file_path, out_path)
elif os.path.isdir(in_path) and "JSON" in in_path:
print("Processing JSON folder...")
filetype = "json"
for in_file in os.listdir(in_path):
in_file_path = os.path.join(in_path, in_file)
out_path = get_out_path(in_file_path, filetype)
Expand All @@ -180,6 +188,7 @@ def main():
out_path = get_out_path(in_path, filetype)
else:
out_path = sys.argv[2]
print("Processing plist file...")
plist_yaml(in_path, out_path)
else:
print("\nERROR: Input File is not PLIST, JSON or YAML format.\n")
Expand Down
10 changes: 8 additions & 2 deletions plistyamlplist_lib/plist_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
taken from the input file, with .yaml added to the end.
"""

import subprocess
import sys
from collections import OrderedDict

Expand All @@ -21,7 +22,12 @@
from plistlib import Data # Python 2
from plistlib import readPlist as load_plist

import yaml
try:
import yaml
except ImportError:
subprocess.check_call([sys.executable, "-m", "ensurepip", "--user"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyyaml", "--user"])
import yaml


def represent_ordereddict(dumper, data):
Expand Down Expand Up @@ -107,7 +113,7 @@ def main():

try:
sys.argv[2]
except Exception as e:
except Exception:
out_path = "%s.yaml" % in_path
else:
out_path = sys.argv[2]
Expand Down
12 changes: 9 additions & 3 deletions plistyamlplist_lib/yaml_plist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
For best results, the input file should therefore be named with
"""

import subprocess
import sys
import yaml
import os.path

try: # python 3
from plistlib import dumps as write_plist
except ImportError: # python 2
from plistlib import writePlistToString as write_plist
import yaml

try:
import yaml
except ImportError:
subprocess.check_call([sys.executable, "-m", "ensurepip", "--user"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyyaml", "--user"])
import yaml


def convert(data):
Expand Down Expand Up @@ -60,7 +66,7 @@ def main():
in_path = sys.argv[1]
try:
sys.argv[2]
except Exception as e:
except Exception:
if in_path.endswith(".yaml"):
filename, _ = os.path.splitext(in_path)
out_path = filename
Expand Down
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from setuptools import setup, find_packages

setup(
name="plistyamlplist",
version="0.4.0",
packages=find_packages(include=["plistyamlplist_lib", "plistyamlplist_lib.*"]),
install_requires=["pyyaml"],
)

0 comments on commit 473ccb5

Please sign in to comment.