From 5b571c4f74781a1e6c86ee22e743e153be729a13 Mon Sep 17 00:00:00 2001 From: Phill Date: Fri, 21 Jun 2019 21:53:42 +0100 Subject: [PATCH] Fixes for path issues --- openlp/core/common/json.py | 17 ++++++++++---- openlp/core/common/settings.py | 2 +- openlp/core/display/html/display.js | 22 ++++--------------- openlp/core/display/window.py | 4 ++-- openlp/core/lib/theme.py | 9 ++++---- openlp/core/ui/media/mediacontroller.py | 2 +- .../lib/presentationcontroller.py | 2 +- 7 files changed, 27 insertions(+), 31 deletions(-) diff --git a/openlp/core/common/json.py b/openlp/core/common/json.py index 4a54e41b3..c323479e6 100644 --- a/openlp/core/common/json.py +++ b/openlp/core/common/json.py @@ -111,6 +111,8 @@ class OpenLPJSONDecoder(JSONDecoder): :param dict obj: A decoded JSON object :return: The custom object from the serialized data if the custom object is registered, else obj """ + if '__Path__' in obj: + return PathSerializer.encode_json(obj, **self.kwargs) try: key = obj['json_meta']['class'] except KeyError: @@ -150,8 +152,8 @@ class OpenLPJSONEncoder(JSONEncoder): if isinstance(obj, JSONMixin): return obj.json_object() elif obj.__class__.__name__ in _registered_classes: - return _registered_classes[obj.__class__.__name__].json_object(obj) - return super().default(obj) + return _registered_classes[obj.__class__.__name__].json_object(obj, **self.kwargs) + return super().default(obj, **self.kwargs) def is_serializable(obj): @@ -174,17 +176,22 @@ class PathSerializer(JSONMixin, register_names=('Path', 'PosixPath', 'WindowsPat :param kwargs: Contains any extra parameters. Not used! :return Path: The deserialized Path object """ - path = Path(*obj['parts']) + if '__Path__' in obj: + parts = obj['__Path__'] + else: + parts = obj['parts'] + path = Path(*parts) if base_path and not path.is_absolute(): return base_path / path return path @classmethod - def json_object(cls, obj, base_path=None, **kwargs): + def json_object(cls, obj, base_path=None, js_use=False, **kwargs): """ Create a dictionary that can be JSON decoded. :param Path base_path: If specified, an absolute path to make a relative path from. + :param bool js_use: Encode the path as a uri. For example for use in the js rendering code. :param kwargs: Contains any extra parameters. Not used! :return: The dictionary representation of this Path object. :rtype: dict[tuple] @@ -193,6 +200,8 @@ class PathSerializer(JSONMixin, register_names=('Path', 'PosixPath', 'WindowsPat if base_path: with suppress(ValueError): path = path.relative_to(base_path) + if js_use is True: + return path.as_uri() json_dict = {'parts': path.parts} cls.attach_meta(json_dict) return json_dict diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 312afcd21..6fbb8a8ed 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -612,7 +612,7 @@ class Settings(QtCore.QSettings): elif isinstance(default_value, dict): return {} elif isinstance(setting, str): - if 'json_meta' in setting or setting.startswith('{'): + if 'json_meta' in setting or '__Path__' in setting or setting.startswith('{'): return json.loads(setting, cls=OpenLPJSONDecoder) # Convert the setting to the correct type. if isinstance(default_value, bool): diff --git a/openlp/core/display/html/display.js b/openlp/core/display/html/display.js index 02980a393..e198eeee6 100644 --- a/openlp/core/display/html/display.js +++ b/openlp/core/display/html/display.js @@ -117,20 +117,6 @@ function _prepareText(text) { return "

" + _nl2br(text) + "

"; } -/** - * The paths we get are JSON versions of Python Path objects, so let's just fix that. - * @private - * @param {object} path - The Path object - * @returns {string} The actual file path - */ -function _pathToString(path) { - var filename = path.__Path__.join("/").replace("//", "/"); - if (!filename.startsWith("/")) { - filename = "/" + filename; - } - return filename; -} - /** * An audio player with a play list */ @@ -676,13 +662,13 @@ var Display = { } break; case BackgroundType.Image: - background_filename = _pathToString(theme.background_filename); - backgroundStyle["background-image"] = "url('file://" + background_filename + "')"; + backgroundStyle["background-image"] = "url('" + theme.background_filename + "')"; + console.warn(backgroundStyle["background-image"]); break; case BackgroundType.Video: - background_filename = _pathToString(theme.background_filename); backgroundStyle["background-color"] = theme.background_border_color; - backgroundHtml = ""; + backgroundHtml = ""; + console.warn(backgroundHtml); break; default: backgroundStyle["background"] = "#000"; diff --git a/openlp/core/display/window.py b/openlp/core/display/window.py index c8d99fd22..e18437a4a 100644 --- a/openlp/core/display/window.py +++ b/openlp/core/display/window.py @@ -332,9 +332,9 @@ class DisplayWindow(QtWidgets.QWidget): theme_copy = copy.deepcopy(theme) theme_copy.background_type = 'image' theme_copy.background_filename = self.checkerboard_path - exported_theme = theme_copy.export_theme() + exported_theme = theme_copy.export_theme(js_use=True) else: - exported_theme = theme.export_theme() + exported_theme = theme.export_theme(js_use=True) self.run_javascript('Display.setTheme({theme});'.format(theme=exported_theme)) def get_video_types(self): diff --git a/openlp/core/lib/theme.py b/openlp/core/lib/theme.py index d5219a5d1..af40809bd 100644 --- a/openlp/core/lib/theme.py +++ b/openlp/core/lib/theme.py @@ -225,17 +225,18 @@ class Theme(object): jsn = json.loads(theme, cls=OpenLPJSONDecoder) self.expand_json(jsn) - def export_theme(self, theme_path=None): + def export_theme(self, theme_path=None, js_use=False): """ Loop through the fields and build a dictionary of them + :param pathlib.Path | None theme_path: + :param bool js_use: For internal use, for example with the theme js code. + :return str: The json encoded theme object """ theme_data = {} for attr, value in self.__dict__.items(): theme_data["{attr}".format(attr=attr)] = value - if theme_path: - return json.dumps(theme_data, cls=OpenLPJSONEncoder, base_path=theme_path) - return json.dumps(theme_data, cls=OpenLPJSONEncoder) + return json.dumps(theme_data, cls=OpenLPJSONEncoder, base_path=theme_path, js_use=js_use) def parse(self, xml): """ diff --git a/openlp/core/ui/media/mediacontroller.py b/openlp/core/ui/media/mediacontroller.py index 588bf636e..1c3534565 100644 --- a/openlp/core/ui/media/mediacontroller.py +++ b/openlp/core/ui/media/mediacontroller.py @@ -372,7 +372,7 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties): return True for file in controller.media_info.file_info: if file.is_file: - suffix = '*%s' % file.suffix.lower() + suffix = file.suffix.lower()[1:] file = str(file) if suffix in VIDEO_EXT: if not controller.media_info.is_background or controller.media_info.is_background and \ diff --git a/openlp/plugins/presentations/lib/presentationcontroller.py b/openlp/plugins/presentations/lib/presentationcontroller.py index fa0284cea..160b68ac1 100644 --- a/openlp/plugins/presentations/lib/presentationcontroller.py +++ b/openlp/plugins/presentations/lib/presentationcontroller.py @@ -129,7 +129,7 @@ class PresentationDocument(object): thumbnail_folder_path = self.get_thumbnail_folder() temp_folder_path = self.get_temp_folder() if thumbnail_folder_path.exists(): - thumbnail_folder_path.rmtree() + shutil.rmtree(thumbnail_folder_path) if temp_folder_path.exists(): shutil.rmtree(temp_folder_path) except OSError: