From e403d1dce19cf1c39f49a3c6511787445be26b1e Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sat, 9 Jul 2022 23:08:08 +0200 Subject: [PATCH] added Layer, AbstractLayer, IDLayer and RegexLayer for bmg patch operation (unimplemented) --- source/mkw/Patch/PatchOperation.py | 54 ++++++++++++++++++++++++++++-- source/mkw/Patch/__init__.py | 10 ++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/source/mkw/Patch/PatchOperation.py b/source/mkw/Patch/PatchOperation.py index 3173cf5..8ae57b6 100644 --- a/source/mkw/Patch/PatchOperation.py +++ b/source/mkw/Patch/PatchOperation.py @@ -251,11 +251,61 @@ class PatchOperation: def __init__(self, layers: list[dict]): """ - :param layers: all the bmg patch to apply + :param layers: layers """ self.layers = layers def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): - print(file_content) + for layer in self.layers: + file_content = self.Layer(layer).patch_bmg(patch, file_content) return file_name, file_content + + class Layer: + """ + represent a layer for a bmg-edit + """ + + def __new__(cls, layer: dict) -> "Layer": + """ + return the correct type of layer corresponding to the layer mode + :param layer: the layer to load + """ + for subclass in filter(lambda subclass: subclass.mode == layer["mode"], + cls.AbstractLayer.__subclasses__()): + layer.pop("mode") + return subclass(**layer) + raise InvalidBmgLayerMode(layer["mode"]) + + class AbstractLayer(ABC): + @abstractmethod + def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: + """ + Patch a bmg with the actual layer. Return the new bmg content. + """ + + class IDLayer(AbstractLayer): + """ + Represent a layer that replace bmg entry by their ID + """ + + mode = "id" + + def __init__(self, template: dict[str, str]): + self.template = template + + def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: + return file_content + + class RegexLayer(AbstractLayer): + """ + Represent a layer that replace bmg entry by matching them with a regex + """ + + mode = "regex" + + def __init__(self, template: dict[str, str]): + self.template = template + + def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: + return file_content diff --git a/source/mkw/Patch/__init__.py b/source/mkw/Patch/__init__.py index fc305f2..aa22bb3 100644 --- a/source/mkw/Patch/__init__.py +++ b/source/mkw/Patch/__init__.py @@ -18,14 +18,20 @@ class InvalidPatchOperation(Exception): class InvalidImageLayerType(Exception): def __init__(self, layer_type: str): - super().__init__(f"Error : layer type \"{layer_type}\" is not implemented") + super().__init__(f"Error : image layer type \"{layer_type}\" is not implemented") + + +class InvalidBmgLayerMode(Exception): + def __init__(self, layer_mode: str): + super().__init__(f"Error : bmg layer mode \"{layer_mode}\" is not implemented") + # TODO : implement BMG # TODO : recreate SZS configuration_example = { "operation": { # other operation for the file - "bmg-replace": { + "bmg-edit": { "mode": "regex", # regex or id "template": { "CWF": "{{ ONLINE_SERVICE }}", # regex type expression