From 2689977f88ba29d2b2d707f9c832ac5dc0b3f4cc Mon Sep 17 00:00:00 2001 From: Alexander Akhmetov Date: Tue, 25 Sep 2018 23:31:49 +0200 Subject: [PATCH] Removed plutil requirement --- CHANGELOG.md | 1 + README.md | 5 ----- docs/python_tutorial.md | 7 +------ shortcuts/cli.py | 11 ++--------- shortcuts/dump.py | 11 +++++++++-- shortcuts/utils.py | 9 --------- 6 files changed, 13 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91d608d..4987e88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New example: [shields.toml](/examples/shields.toml) - New action: SpeakTextAction - Added `default=True` to field `SetLowPowerModeAction.on`. +- Removed `plutil` requirement ## [0.7.0] - 25.09.2018 diff --git a/README.md b/README.md index 492325e..4641991 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,6 @@ sc.actions = [ ## How to use -### Requirements - -This library requires `plutil` tool, which should be installed on MacOS by default. -On Linux, you should be able to use `plistutil` instead. - ### Installation ```bash diff --git a/docs/python_tutorial.md b/docs/python_tutorial.md index a2c17b4..84613a3 100644 --- a/docs/python_tutorial.md +++ b/docs/python_tutorial.md @@ -9,8 +9,6 @@ How to use this package from your python code: ```python from shortcuts import Shortcut, actions -from shortcuts.utils import convert_plist_to_binary - sc = Shortcut() @@ -23,11 +21,8 @@ sc.actions = [ file_path = 's.shortcut' -with open(file_path, 'w') as f: +with open(file_path, 'wb') as f: sc.dump(f, file_format='plist') - -convert_plist_to_binary(file_path) - ``` Now you can upload `s.shortcut` to your phone and open it with Shortcuts app. diff --git a/shortcuts/cli.py b/shortcuts/cli.py index 9470059..a0c3341 100644 --- a/shortcuts/cli.py +++ b/shortcuts/cli.py @@ -2,24 +2,17 @@ import os.path import shortcuts -from shortcuts.utils import convert_plist_to_binary, convert_plist_to_xml def convert_shortcut(input_filepath, out_filepath): input_format = _get_format(input_filepath) out_format = _get_format(out_filepath) - if input_format == 'plist': - convert_plist_to_xml(input_filepath) - with open(input_filepath, 'rb') as f: sc = shortcuts.Shortcut.load(f, file_format=input_format) - with open(out_filepath, 'w') as f: + with open(out_filepath, 'wb') as f: sc.dump(f, file_format=out_format) - if out_format == 'plist': - convert_plist_to_binary(out_filepath) - def _get_format(filepath): _, ext = os.path.splitext(filepath) @@ -34,7 +27,7 @@ def _get_format(filepath): def main(): parser = argparse.ArgumentParser(description='Shortcuts: Siri shortcuts creator') - parser.add_argument('file', nargs='?', help='Input file: *.(toml|shortcut)') + parser.add_argument('file', nargs='?', help='Input file: *.(toml|shortcut|itunes url)') parser.add_argument('output', nargs='?', help='Output file: *.(toml|shortcut)') parser.add_argument('--version', action='store_true', help='Version information') diff --git a/shortcuts/dump.py b/shortcuts/dump.py index 5194344..3648bfd 100644 --- a/shortcuts/dump.py +++ b/shortcuts/dump.py @@ -1,5 +1,5 @@ import plistlib -from typing import TYPE_CHECKING, Any, Dict, TextIO, Type +from typing import TYPE_CHECKING, Any, BinaryIO, Dict, Type import toml @@ -13,7 +13,7 @@ class BaseDumper: def __init__(self, shortcut: 'Shortcut') -> None: self.shortcut = shortcut - def dump(self, file_obj: TextIO) -> None: + def dump(self, file_obj: BinaryIO) -> None: file_obj.write(self.dumps()) def dumps(self) -> str: @@ -21,6 +21,13 @@ def dumps(self) -> str: class PListDumper(BaseDumper): + def dump(self, file_obj: BinaryIO) -> None: + binary = plistlib.dumps( # todo: change dumps to binary and remove this + plistlib.loads(self.dumps().encode('utf-8')), + fmt=plistlib.FMT_BINARY, + ) + file_obj.write(binary) + def dumps(self) -> str: data = { 'WFWorkflowActions': self.shortcut._get_actions(), diff --git a/shortcuts/utils.py b/shortcuts/utils.py index 8bb8afa..e69de29 100644 --- a/shortcuts/utils.py +++ b/shortcuts/utils.py @@ -1,9 +0,0 @@ -from subprocess import call - - -def convert_plist_to_binary(filepath): - call(['plutil', '-convert', 'binary1', filepath]) - - -def convert_plist_to_xml(filepath): - call(['plutil', '-convert', 'xml1', filepath])