From 34269381d839a449e940c0eeb8c18bad043cdb9f Mon Sep 17 00:00:00 2001 From: Donovan Date: Wed, 6 Mar 2024 22:15:20 +0900 Subject: [PATCH 1/9] Added dump to JSON format. Batch edit with JSON format. --- src/livemaker/cli/lmlsb.py | 257 +++++++++++++++++++++++++++++++++---- 1 file changed, 229 insertions(+), 28 deletions(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 8ea9561..4f5f7ea 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -39,6 +39,8 @@ from .cli import __version__, _version +#dreamsavior edit +import json @click.group() @click.version_option(version=__version__, message=_version) @@ -152,7 +154,7 @@ def validate(input_file): @lmlsb.command() @click.option( - "-m", "--mode", type=click.Choice(["text", "xml", "lines"]), default="text", help="Output mode (defaults to text)" + "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json"]), default="text", help="Output mode (defaults to text)" ) @click.option( "-e", @@ -170,12 +172,29 @@ def validate(input_file): @click.argument("input_file", required=True, nargs=-1, type=click.Path(exists=True, dir_okay="False")) def dump(mode, encoding, output_file, input_file): """Dump the contents of the specified LSB file(s) to stdout in a human-readable format. - - For text mode, the full LSB will be output as human-readable text. - - For xml mode, the full LSB file will be output as an XML document. - - For lines mode, only text lines will be output. + + \b + MODE: + text + The full LSB will be output as human-readable text. + + \b + xml + The full LSB file will be output as an XML document. + + \b + lines + only text lines will be output. + + \b + JSON + The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). + You can edit the JSON and import it back using the lmlsb edit command. + Note: Don't forget to set the "modified" flag to true for each line you edit. + + \b + Example: + lmlsb.exe dump 00000001.lsb -m json -o 00000001.json """ if output_file: outf = open(output_file, mode="w", encoding=encoding) @@ -210,6 +229,36 @@ def dump(mode, encoding, output_file, input_file): etree.tostring(root, encoding=encoding, pretty_print=True, xml_declaration=True).decode(encoding), file=outf, ) + elif mode == "json": + jsonData = {} + #print("command param", lsb.command_params) + for cmd in lsb.commands: + thisData = {} + #print("text", str(cmd)) + thisData["text"] = str(cmd) + thisData["type"] = cmd.type.name + thisData["mute"] = cmd.Mute + thisData["modified"] = False + + #print(vars(cmd)) + #print(cmd.LineNo, "command", cmd) + compKeys = None + + try: + compKeys = cmd._component_keys + except: + #do nothing + continue + + if compKeys != None: + params = {} + for key in cmd._component_keys: + params[key] = str(cmd[key]) + #print("Key", key, "default", cmd[key]) + thisData["params"] = params + jsonData[cmd.LineNo] = thisData + print(json.dumps(jsonData, ensure_ascii=False, indent=4), file=outf) + elif mode == "lines": lsb_path = Path(path) for line, name, scenario in lsb.text_scenarios(): @@ -221,7 +270,7 @@ def dump(mode, encoding, output_file, input_file): print("------", file=outf) for block in scenario.get_text_blocks(): for line in block.text.splitlines(): - print(line, file=outf) + print(line, file=outf) else: for c in lsb.commands: if c.Mute: @@ -765,11 +814,78 @@ def _edit_calc(cmd): print("Editing Calc expression") _edit_parser(parser) +# issues/126 +# Dreamsavior: edit component with predetermined settings +def _edit_component_auto(cmd, setting): + """Edit a BaseComponent (or subclass) command with predetermined settings.""" + print() + print("setting", setting) + + # paramId = -1 + edited = False + + for key in setting: + if key not in cmd._component_keys: + print(f"Warning: Cannot find {key} in command") + continue + + # paramId += 1 + parser = cmd[key] + + if key not in setting: + continue + else: + print() + print("{} -> Default value: {}".format(key, parser)) + + # TODO: editing complex fields and adding values for empty fields will + # require full LiveParser expression parsing, for now we can only edit + # simple scalar values. + if ( + len(parser.entries) > 1 + or (len(parser.entries) == 1 and parser.entries[0].type != OpeDataType.To) + or (len(parser.entries) == 0 and key not in EDITABLE_PROPERTY_TYPES) + ): + print(f"{key} [{parser}]: ") + continue + + value = setting[key] + if parser.entries: + e = parser.entries[0] + op = e.operands[-1] + if op: + if value != op.value: + if op.type == ParamType.Int or op.type == ParamType.Flag: + op.value = int(value) + elif op.type == ParamType.Float: + op.value = numpy.longdouble(value) + else: + op.value = value + edited = True + else: + if value: + param_type = EDITABLE_PROPERTY_TYPES[key] + try: + if param_type == ParamType.Int or param_type == ParamType.Flag: + value = int(value) + elif param_type == ParamType.Float: + value = numpy.longdouble(value) + op = Param(value, param_type) + e = OpeData(type=OpeDataType.To, name="____arg", operands=[op]) + parser.entries.append(e) + edited = True + except ValueError: + print(f"Invalid datatype for {key}, skipping.") + continue + return edited + def _edit_component(cmd): """Edit a BaseComponent (or subclass) command.""" print() print("Enter new value for each field (or keep existing value)") + + edited = False for key in cmd._component_keys: parser = cmd[key] # TODO: editing complex fields and adding values for empty fields will @@ -780,7 +896,7 @@ def _edit_component(cmd): or (len(parser.entries) == 1 and parser.entries[0].type != OpeDataType.To) or (len(parser.entries) == 0 and key not in EDITABLE_PROPERTY_TYPES) ): - print(f"{key} [{parser}]: ") + print("{} [{}]: ".format(key, parser)) continue if parser.entries: e = parser.entries[0] @@ -794,6 +910,7 @@ def _edit_component(cmd): op.value = numpy.longdouble(value) else: op.value = value + edited = True else: value = click.prompt(key, default="") if value: @@ -806,8 +923,12 @@ def _edit_component(cmd): op = Param(value, param_type) e = OpeData(type=OpeDataType.To, name="____arg", operands=[op]) parser.entries.append(e) + edited = True except ValueError: - print(f"Invalid datatype for {key}, skipping.") + print("Invalid datatype for {}, skipping.".format(key)) + continue + + return edited def _edit_jump(cmd): @@ -830,10 +951,33 @@ def _edit_jump(cmd): _edit_parser(parser) +# issues/126 +def _main_edit(cmd, setting, line_number): + if line_number != None: + print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) + + if isinstance(cmd, BaseComponentCommand): + if setting != None: + return _edit_component_auto(cmd, setting) + else: + return _edit_component(cmd) + elif isinstance(cmd, Calc): + _edit_calc(cmd) + elif isinstance(cmd, Jump): + _edit_jump(cmd) + else: + print(f"Cannot edit {cmd.type.name} commands.") + return False + return True + + @lmlsb.command() @click.argument("lsb_file", required=True, type=click.Path(exists=True, dir_okay=False)) -@click.argument("line_number", required=True, type=int) -def edit(lsb_file, line_number): +# issues/126 +@click.argument("line_number", required=False, type=int) +@click.option("-p", "--param", type=str, help="Parameter in JSON format.") +@click.option("-b", "--batch", type=str, help="Edit with parameter with JSON formatted file.") +def edit(lsb_file, line_number, param, batch): """Edit the specified command within an LSB file. Only specific command types and specific fields can be edited. @@ -849,30 +993,87 @@ def edit(lsb_file, line_number): the data type of the new value is assumed to be the same as the original data type. + \b + Batch mode: + You can batch edit several line and paramaters with JSON file. + The format of JSON file is as follow: + { + "36" : { + "modified": true, + "params": { + "PR_LEFT": "20", + "PR_TOP": "12" + } + } + } + + You can generate the JSON via "lmlsb.exe dump" command with JSON mode. + + \b + Example: + - To edit line 33 with prompt: + lmlsb.exe edit 00000001.lsb 33 + + \b + - To set the value of PR_LEFT parameter on line 33 to 20: + lmlsb.exe edit 00000001.lsb 33 -p '{\\"PR_LEFT\\": 20}' + + \b + - To import back the value from lmlsb dump: + lmlsb.exe edit 00000001.lsb -b 00000001.json """ + batchData = None + if batch != None: + with open(batch, "rb") as f: + try: + batchData = json.load(f) + except LiveMakerException as e: + sys.exit(f"Could not read JSON file : {batch}\nWith error {e}") + + + if batch == None and line_number == None: + sys.exit("One of the parameter line number or -b must exist") + with open(lsb_file, "rb") as f: try: lsb = LMScript.from_file(f) except LiveMakerException as e: sys.exit(f"Could not open LSB file: {e}") - cmd = None - for c in lsb.commands: - if c.LineNo == line_number: - cmd = c - break - else: - sys.exit(f"Command {line_number} does not exist in the specified LSB") + # handling setting + setting = None + if (param != None): + print("Parsing param") + try: + setting = json.loads(param) + except LiveMakerException as e: + sys.exit("Cannot JSON parse parameter : {param}\nWith error {e}") + + + + writeData = False + if line_number != None: + for c in lsb.commands: + if c.LineNo == line_number: + writeData = _main_edit(c, setting, line_number) + break + else: + sys.exit("Command {line_number} does not exist in the specified LSB") + elif batchData != None: + if setting != None: + print("Found both batchData and parameter. Parameter will be ignored.") + for c in lsb.commands: + key = str(c.LineNo) + if key in batchData: + if batchData[key]["modified"]: + print("Found modified line no", c.LineNo) + writeData = _main_edit(c, batchData[key]["params"], c.LineNo) or writeData + print("Batch edit completed") + + + if not writeData: + sys.exit("Nothing to write") - print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) - if isinstance(cmd, BaseComponentCommand): - _edit_component(cmd) - elif isinstance(cmd, Calc): - _edit_calc(cmd) - elif isinstance(cmd, Jump): - _edit_jump(cmd) - else: - sys.exit(f"Cannot edit {cmd.type.name} commands.") print("Backing up original LSB.") shutil.copyfile(str(lsb_file), f"{str(lsb_file)}.bak") From a454ca630ce0779342f2d866fc84e1671b502070 Mon Sep 17 00:00:00 2001 From: Donovan Date: Tue, 12 Mar 2024 12:00:30 +0900 Subject: [PATCH 2/9] add: documentation about the changes on lmlsb's edit and dump command --- .gitignore | 3 ++ docs/cli.rst | 74 ++++++++++++++++++++++++++------ src/livemaker/cli/lmlsb.py | 88 ++++++++++++++++++++++++-------------- 3 files changed, 119 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 84229f4..01e7655 100644 --- a/.gitignore +++ b/.gitignore @@ -100,3 +100,6 @@ ENV/ # mypy .mypy_cache/ + + +deploy.bat \ No newline at end of file diff --git a/docs/cli.rst b/docs/cli.rst index f974468..6cc8fb1 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -274,21 +274,40 @@ lines $ lmlsb dump --help Usage: lmlsb dump [OPTIONS] INPUT_FILE... - Dump the contents of the specified LSB file(s) to stdout in a human- - readable format. + Dump the contents of the specified LSB file(s) to stdout in a human-readable + format. - For text mode, the full LSB will be output as human-readable text. + MODE: + text + The full LSB will be output as human-readable text. - For xml mode, the full LSB file will be output as an XML document. + xml + The full LSB file will be output as an XML document. - For lines mode, only text lines will be output. + lines + only text lines will be output. + + json + The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). + You can edit the JSON and import it back using the lmlsb edit command. + Use "jsonfull" option if you want to output all line. + Note: - Don't forget to set the "modified" flag to true for each line you edit. + - This mode will only output the editable lines. + + jsonfull + Will output the complete line instead of only editable lines + + + Example: + lmlsb.exe dump 00000001.lsb -m json -o 00000001.json Options: - -m, --mode [text|xml|lines] Output mode (defaults to text) - -e, --encoding [cp932|utf-8] Output text encoding (defaults to utf-8). - -o, --output-file FILE Output file. If unspecified, output will be - dumped to stdout. - --help Show this message and exit. + -m, --mode [text|xml|lines|json|jsonfull] + Output mode (defaults to text) + -e, --encoding [cp932|utf-8] Output text encoding (defaults to utf-8). + -o, --output-file FILE Output file. If unspecified, output will be + dumped to stdout. + --help Show this message and exit. edit ^^^^ @@ -311,7 +330,7 @@ For more specific usage/implementation details refer to the thread in `issue #9 :: $ lmlsb edit --help - Usage: lmlsb edit [OPTIONS] LSB_FILE LINE_NUMBER + Usage: lmlsb edit [OPTIONS] LSB_FILE [LINE_NUMBER] Edit the specified command within an LSB file. @@ -324,11 +343,38 @@ For more specific usage/implementation details refer to the thread in `issue #9 behavior (or a complete crash) in the LiveMaker engine during runtime. Note: Setting empty fields to improper data types may cause undefined - behavior in the LiveMaker engine. When editing a field, the data type of - the new value is assumed to be the same as the original data type. + behavior in the LiveMaker engine. When editing a field, the data type of the + new value is assumed to be the same as the original data type. + + Batch mode: + You can batch edit several line and paramaters with JSON file. + The format of JSON file is as follow: + { + "36" : { + "modified": true, + "params": { + "PR_LEFT": "20", + "PR_TOP": "12" + } + } + } + + You can generate the JSON via "lmlsb.exe dump" command with JSON mode. + + Example: + - To edit line 33 with input prompt: + lmlsb.exe edit 00000001.lsb 33 + + - To set the value of PR_LEFT parameter on line 33 to 20: + lmlsb.exe edit 00000001.lsb 33 -p '{\"PR_LEFT\": 20}' + + - To import back the value from lmlsb dump: + lmlsb.exe edit 00000001.lsb -b 00000001.json Options: - --help Show this message and exit. + -p, --param TEXT Parameter in JSON format. + -b, --batch TEXT Edit with parameter with JSON formatted file. + --help Show this message and exit. extract ^^^^^^^ diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 4f5f7ea..b0c313d 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -152,9 +152,54 @@ def validate(input_file): print(f" script mismatch, {len(orig_bytes)} {len(new_bytes)}") +def _dump_json(lsb, pylm, jsonmode): + jsonData = {} + #print("command param", lsb.command_params) + for cmd in lsb.commands: + thisData = {} + #print("text", str(cmd)) + thisData["text"] = str(cmd) + thisData["type"] = cmd.type.name + thisData["mute"] = cmd.Mute + thisData["modified"] = False + thisData["indent"] = cmd.Indent + + compKeys = None + + try: + compKeys = cmd._component_keys + thisData["editable"] = True + except: + thisData["editable"] = False + if jsonmode != "jsonfull": + continue + + if compKeys != None: + params = {} + for key in cmd._component_keys: + params[key] = str(cmd[key]) + thisData["params"] = params + + if cmd.type == CommandType.TextIns: + dec = LNSDecompiler() + thisData["script"] = str(dec.decompile(cmd.get("Text"))) + + ref = cmd.get("Page") + if ref and isinstance(ref, LabelReference): + if ref.Page.endswith("lsb") and pylm: + # resolve lsb refs + line_no, name = pylm.resolve_label(ref) + if line_no is not None: + thisData["target"] = {} + thisData["target"]["line"] = str(line_no) + thisData["target"]["label"] = str(name) + + jsonData[cmd.LineNo] = thisData + return jsonData + @lmlsb.command() @click.option( - "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json"]), default="text", help="Output mode (defaults to text)" + "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), default="text", help="Output mode (defaults to text)" ) @click.option( "-e", @@ -187,11 +232,16 @@ def dump(mode, encoding, output_file, input_file): only text lines will be output. \b - JSON + json The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). You can edit the JSON and import it back using the lmlsb edit command. - Note: Don't forget to set the "modified" flag to true for each line you edit. - + Use "jsonfull" option if you want to output all line. + Note: - Don't forget to set the "modified" flag to true for each line you edit. + - This mode will only output the editable lines. + \b + jsonfull + Will output the complete line instead of only editable lines + \b Example: lmlsb.exe dump 00000001.lsb -m json -o 00000001.json @@ -229,34 +279,8 @@ def dump(mode, encoding, output_file, input_file): etree.tostring(root, encoding=encoding, pretty_print=True, xml_declaration=True).decode(encoding), file=outf, ) - elif mode == "json": - jsonData = {} - #print("command param", lsb.command_params) - for cmd in lsb.commands: - thisData = {} - #print("text", str(cmd)) - thisData["text"] = str(cmd) - thisData["type"] = cmd.type.name - thisData["mute"] = cmd.Mute - thisData["modified"] = False - - #print(vars(cmd)) - #print(cmd.LineNo, "command", cmd) - compKeys = None - - try: - compKeys = cmd._component_keys - except: - #do nothing - continue - - if compKeys != None: - params = {} - for key in cmd._component_keys: - params[key] = str(cmd[key]) - #print("Key", key, "default", cmd[key]) - thisData["params"] = params - jsonData[cmd.LineNo] = thisData + elif mode == "json" or mode == "jsonfull": + jsonData = _dump_json(lsb, pylm, mode) print(json.dumps(jsonData, ensure_ascii=False, indent=4), file=outf) elif mode == "lines": From e92ba4f4c42abd2d7e3531042b4c5539e28f1cff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Mar 2024 03:27:53 +0000 Subject: [PATCH 3/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/livemaker/cli/lmlsb.py | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index b0c313d..f61a507 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -19,6 +19,8 @@ import csv import hashlib +# dreamsavior edit +import json import re import shutil import sys @@ -39,8 +41,6 @@ from .cli import __version__, _version -#dreamsavior edit -import json @click.group() @click.version_option(version=__version__, message=_version) @@ -154,10 +154,10 @@ def validate(input_file): def _dump_json(lsb, pylm, jsonmode): jsonData = {} - #print("command param", lsb.command_params) + # print("command param", lsb.command_params) for cmd in lsb.commands: thisData = {} - #print("text", str(cmd)) + # print("text", str(cmd)) thisData["text"] = str(cmd) thisData["type"] = cmd.type.name thisData["mute"] = cmd.Mute @@ -165,7 +165,7 @@ def _dump_json(lsb, pylm, jsonmode): thisData["indent"] = cmd.Indent compKeys = None - + try: compKeys = cmd._component_keys thisData["editable"] = True @@ -197,9 +197,14 @@ def _dump_json(lsb, pylm, jsonmode): jsonData[cmd.LineNo] = thisData return jsonData + @lmlsb.command() @click.option( - "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), default="text", help="Output mode (defaults to text)" + "-m", + "--mode", + type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), + default="text", + help="Output mode (defaults to text)", ) @click.option( "-e", @@ -217,7 +222,7 @@ def _dump_json(lsb, pylm, jsonmode): @click.argument("input_file", required=True, nargs=-1, type=click.Path(exists=True, dir_okay="False")) def dump(mode, encoding, output_file, input_file): """Dump the contents of the specified LSB file(s) to stdout in a human-readable format. - + \b MODE: text @@ -234,14 +239,14 @@ def dump(mode, encoding, output_file, input_file): \b json The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). - You can edit the JSON and import it back using the lmlsb edit command. + You can edit the JSON and import it back using the lmlsb edit command. Use "jsonfull" option if you want to output all line. Note: - Don't forget to set the "modified" flag to true for each line you edit. - - This mode will only output the editable lines. + - This mode will only output the editable lines. \b jsonfull Will output the complete line instead of only editable lines - + \b Example: lmlsb.exe dump 00000001.lsb -m json -o 00000001.json @@ -294,7 +299,7 @@ def dump(mode, encoding, output_file, input_file): print("------", file=outf) for block in scenario.get_text_blocks(): for line in block.text.splitlines(): - print(line, file=outf) + print(line, file=outf) else: for c in lsb.commands: if c.Mute: @@ -838,7 +843,8 @@ def _edit_calc(cmd): print("Editing Calc expression") _edit_parser(parser) -# issues/126 + +# issues/126 # Dreamsavior: edit component with predetermined settings def _edit_component_auto(cmd, setting): """Edit a BaseComponent (or subclass) command with predetermined settings.""" @@ -903,7 +909,8 @@ def _edit_component_auto(cmd, setting): print(f"Invalid datatype for {key}, skipping.") continue return edited - + + def _edit_component(cmd): """Edit a BaseComponent (or subclass) command.""" print() @@ -975,11 +982,11 @@ def _edit_jump(cmd): _edit_parser(parser) -# issues/126 +# issues/126 def _main_edit(cmd, setting, line_number): if line_number != None: print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) - + if isinstance(cmd, BaseComponentCommand): if setting != None: return _edit_component_auto(cmd, setting) @@ -997,7 +1004,7 @@ def _main_edit(cmd, setting, line_number): @lmlsb.command() @click.argument("lsb_file", required=True, type=click.Path(exists=True, dir_okay=False)) -# issues/126 +# issues/126 @click.argument("line_number", required=False, type=int) @click.option("-p", "--param", type=str, help="Parameter in JSON format.") @click.option("-b", "--batch", type=str, help="Edit with parameter with JSON formatted file.") @@ -1032,7 +1039,7 @@ def edit(lsb_file, line_number, param, batch): } You can generate the JSON via "lmlsb.exe dump" command with JSON mode. - + \b Example: - To edit line 33 with prompt: @@ -1054,7 +1061,6 @@ def edit(lsb_file, line_number, param, batch): except LiveMakerException as e: sys.exit(f"Could not read JSON file : {batch}\nWith error {e}") - if batch == None and line_number == None: sys.exit("One of the parameter line number or -b must exist") @@ -1066,15 +1072,13 @@ def edit(lsb_file, line_number, param, batch): # handling setting setting = None - if (param != None): + if param != None: print("Parsing param") try: setting = json.loads(param) except LiveMakerException as e: sys.exit("Cannot JSON parse parameter : {param}\nWith error {e}") - - writeData = False if line_number != None: for c in lsb.commands: @@ -1094,11 +1098,9 @@ def edit(lsb_file, line_number, param, batch): writeData = _main_edit(c, batchData[key]["params"], c.LineNo) or writeData print("Batch edit completed") - if not writeData: sys.exit("Nothing to write") - print("Backing up original LSB.") shutil.copyfile(str(lsb_file), f"{str(lsb_file)}.bak") try: From aa72c73669c5e26a176467df71f1a295fe768c15 Mon Sep 17 00:00:00 2001 From: Donovan Date: Tue, 12 Mar 2024 13:44:39 +0900 Subject: [PATCH 4/9] Fix: Linter issue --- src/livemaker/cli/lmlsb.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index b0c313d..3054160 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -169,12 +169,12 @@ def _dump_json(lsb, pylm, jsonmode): try: compKeys = cmd._component_keys thisData["editable"] = True - except: + except AttributeError: thisData["editable"] = False if jsonmode != "jsonfull": continue - if compKeys != None: + if compKeys is not None: params = {} for key in cmd._component_keys: params[key] = str(cmd[key]) @@ -977,11 +977,11 @@ def _edit_jump(cmd): # issues/126 def _main_edit(cmd, setting, line_number): - if line_number != None: + if line_number is not None: print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) if isinstance(cmd, BaseComponentCommand): - if setting != None: + if setting is not None: return _edit_component_auto(cmd, setting) else: return _edit_component(cmd) @@ -1047,7 +1047,7 @@ def edit(lsb_file, line_number, param, batch): lmlsb.exe edit 00000001.lsb -b 00000001.json """ batchData = None - if batch != None: + if batch is not None: with open(batch, "rb") as f: try: batchData = json.load(f) @@ -1055,7 +1055,7 @@ def edit(lsb_file, line_number, param, batch): sys.exit(f"Could not read JSON file : {batch}\nWith error {e}") - if batch == None and line_number == None: + if batch is None and line_number is None: sys.exit("One of the parameter line number or -b must exist") with open(lsb_file, "rb") as f: @@ -1066,25 +1066,25 @@ def edit(lsb_file, line_number, param, batch): # handling setting setting = None - if (param != None): + if (param is not None): print("Parsing param") try: setting = json.loads(param) except LiveMakerException as e: - sys.exit("Cannot JSON parse parameter : {param}\nWith error {e}") + sys.exit(f"Cannot JSON parse parameter : {param}\nWith error {e}") writeData = False - if line_number != None: + if line_number is not None: for c in lsb.commands: if c.LineNo == line_number: writeData = _main_edit(c, setting, line_number) break else: sys.exit("Command {line_number} does not exist in the specified LSB") - elif batchData != None: - if setting != None: + elif batchData is not None: + if setting is not None: print("Found both batchData and parameter. Parameter will be ignored.") for c in lsb.commands: key = str(c.LineNo) From f8c48a961a59f1fa29dda392563f5b50c0510294 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Mar 2024 04:48:10 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/livemaker/cli/lmlsb.py | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 3054160..8025521 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -19,6 +19,8 @@ import csv import hashlib +# dreamsavior edit +import json import re import shutil import sys @@ -39,8 +41,6 @@ from .cli import __version__, _version -#dreamsavior edit -import json @click.group() @click.version_option(version=__version__, message=_version) @@ -154,10 +154,10 @@ def validate(input_file): def _dump_json(lsb, pylm, jsonmode): jsonData = {} - #print("command param", lsb.command_params) + # print("command param", lsb.command_params) for cmd in lsb.commands: thisData = {} - #print("text", str(cmd)) + # print("text", str(cmd)) thisData["text"] = str(cmd) thisData["type"] = cmd.type.name thisData["mute"] = cmd.Mute @@ -165,7 +165,7 @@ def _dump_json(lsb, pylm, jsonmode): thisData["indent"] = cmd.Indent compKeys = None - + try: compKeys = cmd._component_keys thisData["editable"] = True @@ -197,9 +197,14 @@ def _dump_json(lsb, pylm, jsonmode): jsonData[cmd.LineNo] = thisData return jsonData + @lmlsb.command() @click.option( - "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), default="text", help="Output mode (defaults to text)" + "-m", + "--mode", + type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), + default="text", + help="Output mode (defaults to text)", ) @click.option( "-e", @@ -217,7 +222,7 @@ def _dump_json(lsb, pylm, jsonmode): @click.argument("input_file", required=True, nargs=-1, type=click.Path(exists=True, dir_okay="False")) def dump(mode, encoding, output_file, input_file): """Dump the contents of the specified LSB file(s) to stdout in a human-readable format. - + \b MODE: text @@ -234,14 +239,14 @@ def dump(mode, encoding, output_file, input_file): \b json The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). - You can edit the JSON and import it back using the lmlsb edit command. + You can edit the JSON and import it back using the lmlsb edit command. Use "jsonfull" option if you want to output all line. Note: - Don't forget to set the "modified" flag to true for each line you edit. - - This mode will only output the editable lines. + - This mode will only output the editable lines. \b jsonfull Will output the complete line instead of only editable lines - + \b Example: lmlsb.exe dump 00000001.lsb -m json -o 00000001.json @@ -294,7 +299,7 @@ def dump(mode, encoding, output_file, input_file): print("------", file=outf) for block in scenario.get_text_blocks(): for line in block.text.splitlines(): - print(line, file=outf) + print(line, file=outf) else: for c in lsb.commands: if c.Mute: @@ -838,7 +843,8 @@ def _edit_calc(cmd): print("Editing Calc expression") _edit_parser(parser) -# issues/126 + +# issues/126 # Dreamsavior: edit component with predetermined settings def _edit_component_auto(cmd, setting): """Edit a BaseComponent (or subclass) command with predetermined settings.""" @@ -903,7 +909,8 @@ def _edit_component_auto(cmd, setting): print(f"Invalid datatype for {key}, skipping.") continue return edited - + + def _edit_component(cmd): """Edit a BaseComponent (or subclass) command.""" print() @@ -975,11 +982,11 @@ def _edit_jump(cmd): _edit_parser(parser) -# issues/126 +# issues/126 def _main_edit(cmd, setting, line_number): if line_number is not None: print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) - + if isinstance(cmd, BaseComponentCommand): if setting is not None: return _edit_component_auto(cmd, setting) @@ -997,7 +1004,7 @@ def _main_edit(cmd, setting, line_number): @lmlsb.command() @click.argument("lsb_file", required=True, type=click.Path(exists=True, dir_okay=False)) -# issues/126 +# issues/126 @click.argument("line_number", required=False, type=int) @click.option("-p", "--param", type=str, help="Parameter in JSON format.") @click.option("-b", "--batch", type=str, help="Edit with parameter with JSON formatted file.") @@ -1032,7 +1039,7 @@ def edit(lsb_file, line_number, param, batch): } You can generate the JSON via "lmlsb.exe dump" command with JSON mode. - + \b Example: - To edit line 33 with prompt: @@ -1054,7 +1061,6 @@ def edit(lsb_file, line_number, param, batch): except LiveMakerException as e: sys.exit(f"Could not read JSON file : {batch}\nWith error {e}") - if batch is None and line_number is None: sys.exit("One of the parameter line number or -b must exist") @@ -1066,15 +1072,13 @@ def edit(lsb_file, line_number, param, batch): # handling setting setting = None - if (param is not None): + if param is not None: print("Parsing param") try: setting = json.loads(param) except LiveMakerException as e: sys.exit(f"Cannot JSON parse parameter : {param}\nWith error {e}") - - writeData = False if line_number is not None: for c in lsb.commands: @@ -1094,11 +1098,9 @@ def edit(lsb_file, line_number, param, batch): writeData = _main_edit(c, batchData[key]["params"], c.LineNo) or writeData print("Batch edit completed") - if not writeData: sys.exit("Nothing to write") - print("Backing up original LSB.") shutil.copyfile(str(lsb_file), f"{str(lsb_file)}.bak") try: From d395f765e324fb8b96525fb0ffe63bfd549464eb Mon Sep 17 00:00:00 2001 From: Donovan Date: Tue, 12 Mar 2024 14:05:36 +0900 Subject: [PATCH 6/9] Format code with black and isort --- src/livemaker/cli/lmlsb.py | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 3054160..8025521 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -19,6 +19,8 @@ import csv import hashlib +# dreamsavior edit +import json import re import shutil import sys @@ -39,8 +41,6 @@ from .cli import __version__, _version -#dreamsavior edit -import json @click.group() @click.version_option(version=__version__, message=_version) @@ -154,10 +154,10 @@ def validate(input_file): def _dump_json(lsb, pylm, jsonmode): jsonData = {} - #print("command param", lsb.command_params) + # print("command param", lsb.command_params) for cmd in lsb.commands: thisData = {} - #print("text", str(cmd)) + # print("text", str(cmd)) thisData["text"] = str(cmd) thisData["type"] = cmd.type.name thisData["mute"] = cmd.Mute @@ -165,7 +165,7 @@ def _dump_json(lsb, pylm, jsonmode): thisData["indent"] = cmd.Indent compKeys = None - + try: compKeys = cmd._component_keys thisData["editable"] = True @@ -197,9 +197,14 @@ def _dump_json(lsb, pylm, jsonmode): jsonData[cmd.LineNo] = thisData return jsonData + @lmlsb.command() @click.option( - "-m", "--mode", type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), default="text", help="Output mode (defaults to text)" + "-m", + "--mode", + type=click.Choice(["text", "xml", "lines", "json", "jsonfull"]), + default="text", + help="Output mode (defaults to text)", ) @click.option( "-e", @@ -217,7 +222,7 @@ def _dump_json(lsb, pylm, jsonmode): @click.argument("input_file", required=True, nargs=-1, type=click.Path(exists=True, dir_okay="False")) def dump(mode, encoding, output_file, input_file): """Dump the contents of the specified LSB file(s) to stdout in a human-readable format. - + \b MODE: text @@ -234,14 +239,14 @@ def dump(mode, encoding, output_file, input_file): \b json The output will be a JSON-formatted LSB command (JSON will always be in UTF-8 format). - You can edit the JSON and import it back using the lmlsb edit command. + You can edit the JSON and import it back using the lmlsb edit command. Use "jsonfull" option if you want to output all line. Note: - Don't forget to set the "modified" flag to true for each line you edit. - - This mode will only output the editable lines. + - This mode will only output the editable lines. \b jsonfull Will output the complete line instead of only editable lines - + \b Example: lmlsb.exe dump 00000001.lsb -m json -o 00000001.json @@ -294,7 +299,7 @@ def dump(mode, encoding, output_file, input_file): print("------", file=outf) for block in scenario.get_text_blocks(): for line in block.text.splitlines(): - print(line, file=outf) + print(line, file=outf) else: for c in lsb.commands: if c.Mute: @@ -838,7 +843,8 @@ def _edit_calc(cmd): print("Editing Calc expression") _edit_parser(parser) -# issues/126 + +# issues/126 # Dreamsavior: edit component with predetermined settings def _edit_component_auto(cmd, setting): """Edit a BaseComponent (or subclass) command with predetermined settings.""" @@ -903,7 +909,8 @@ def _edit_component_auto(cmd, setting): print(f"Invalid datatype for {key}, skipping.") continue return edited - + + def _edit_component(cmd): """Edit a BaseComponent (or subclass) command.""" print() @@ -975,11 +982,11 @@ def _edit_jump(cmd): _edit_parser(parser) -# issues/126 +# issues/126 def _main_edit(cmd, setting, line_number): if line_number is not None: print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) - + if isinstance(cmd, BaseComponentCommand): if setting is not None: return _edit_component_auto(cmd, setting) @@ -997,7 +1004,7 @@ def _main_edit(cmd, setting, line_number): @lmlsb.command() @click.argument("lsb_file", required=True, type=click.Path(exists=True, dir_okay=False)) -# issues/126 +# issues/126 @click.argument("line_number", required=False, type=int) @click.option("-p", "--param", type=str, help="Parameter in JSON format.") @click.option("-b", "--batch", type=str, help="Edit with parameter with JSON formatted file.") @@ -1032,7 +1039,7 @@ def edit(lsb_file, line_number, param, batch): } You can generate the JSON via "lmlsb.exe dump" command with JSON mode. - + \b Example: - To edit line 33 with prompt: @@ -1054,7 +1061,6 @@ def edit(lsb_file, line_number, param, batch): except LiveMakerException as e: sys.exit(f"Could not read JSON file : {batch}\nWith error {e}") - if batch is None and line_number is None: sys.exit("One of the parameter line number or -b must exist") @@ -1066,15 +1072,13 @@ def edit(lsb_file, line_number, param, batch): # handling setting setting = None - if (param is not None): + if param is not None: print("Parsing param") try: setting = json.loads(param) except LiveMakerException as e: sys.exit(f"Cannot JSON parse parameter : {param}\nWith error {e}") - - writeData = False if line_number is not None: for c in lsb.commands: @@ -1094,11 +1098,9 @@ def edit(lsb_file, line_number, param, batch): writeData = _main_edit(c, batchData[key]["params"], c.LineNo) or writeData print("Batch edit completed") - if not writeData: sys.exit("Nothing to write") - print("Backing up original LSB.") shutil.copyfile(str(lsb_file), f"{str(lsb_file)}.bak") try: From b85ba441fc88f74881ed3022f98fd6e102dc6de7 Mon Sep 17 00:00:00 2001 From: Dreamsavior Date: Wed, 13 Mar 2024 13:10:56 +0900 Subject: [PATCH 7/9] Update src/livemaker/cli/lmlsb.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Rowlands (변기호) --- src/livemaker/cli/lmlsb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 8025521..e9170ad 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -154,7 +154,6 @@ def validate(input_file): def _dump_json(lsb, pylm, jsonmode): jsonData = {} - # print("command param", lsb.command_params) for cmd in lsb.commands: thisData = {} # print("text", str(cmd)) From 808a6e73176e420a2987b93cdb1acbb29bf1e584 Mon Sep 17 00:00:00 2001 From: Dreamsavior Date: Wed, 13 Mar 2024 13:11:11 +0900 Subject: [PATCH 8/9] Update src/livemaker/cli/lmlsb.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Rowlands (변기호) --- src/livemaker/cli/lmlsb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index e9170ad..5e07740 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -19,7 +19,6 @@ import csv import hashlib -# dreamsavior edit import json import re import shutil From 92195f2db3547383483f6455a921a6e1e563b58d Mon Sep 17 00:00:00 2001 From: Donovan Date: Tue, 19 Mar 2024 11:02:55 +0900 Subject: [PATCH 9/9] code cleanup --- .gitignore | 3 --- src/livemaker/cli/lmlsb.py | 53 ++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 01e7655..84229f4 100644 --- a/.gitignore +++ b/.gitignore @@ -100,6 +100,3 @@ ENV/ # mypy .mypy_cache/ - - -deploy.bat \ No newline at end of file diff --git a/src/livemaker/cli/lmlsb.py b/src/livemaker/cli/lmlsb.py index 8025521..6eb3a43 100644 --- a/src/livemaker/cli/lmlsb.py +++ b/src/livemaker/cli/lmlsb.py @@ -19,7 +19,6 @@ import csv import hashlib -# dreamsavior edit import json import re import shutil @@ -153,36 +152,34 @@ def validate(input_file): def _dump_json(lsb, pylm, jsonmode): - jsonData = {} - # print("command param", lsb.command_params) + json_data = {} for cmd in lsb.commands: - thisData = {} - # print("text", str(cmd)) - thisData["text"] = str(cmd) - thisData["type"] = cmd.type.name - thisData["mute"] = cmd.Mute - thisData["modified"] = False - thisData["indent"] = cmd.Indent + this_data = {} + this_data["text"] = str(cmd) + this_data["type"] = cmd.type.name + this_data["mute"] = cmd.Mute + this_data["modified"] = False + this_data["indent"] = cmd.Indent - compKeys = None + comp_keys = None try: - compKeys = cmd._component_keys - thisData["editable"] = True + comp_keys = cmd._component_keys + this_data["editable"] = True except AttributeError: - thisData["editable"] = False + this_data["editable"] = False if jsonmode != "jsonfull": continue - if compKeys is not None: + if comp_keys is not None: params = {} for key in cmd._component_keys: params[key] = str(cmd[key]) - thisData["params"] = params + this_data["params"] = params if cmd.type == CommandType.TextIns: dec = LNSDecompiler() - thisData["script"] = str(dec.decompile(cmd.get("Text"))) + this_data["script"] = str(dec.decompile(cmd.get("Text"))) ref = cmd.get("Page") if ref and isinstance(ref, LabelReference): @@ -190,12 +187,12 @@ def _dump_json(lsb, pylm, jsonmode): # resolve lsb refs line_no, name = pylm.resolve_label(ref) if line_no is not None: - thisData["target"] = {} - thisData["target"]["line"] = str(line_no) - thisData["target"]["label"] = str(name) + this_data["target"] = {} + this_data["target"]["line"] = str(line_no) + this_data["target"]["label"] = str(name) - jsonData[cmd.LineNo] = thisData - return jsonData + json_data[cmd.LineNo] = this_data + return json_data @lmlsb.command() @@ -844,14 +841,12 @@ def _edit_calc(cmd): _edit_parser(parser) -# issues/126 -# Dreamsavior: edit component with predetermined settings def _edit_component_auto(cmd, setting): """Edit a BaseComponent (or subclass) command with predetermined settings.""" print() - print("setting", setting) + print("Apply setting :", setting) + print() - # paramId = -1 edited = False for key in setting: @@ -859,14 +854,12 @@ def _edit_component_auto(cmd, setting): print(f"Warning: Cannot find {key} in command") continue - # paramId += 1 parser = cmd[key] if key not in setting: continue else: - print() - print("{} -> Default value: {}".format(key, parser)) + print(f"{key} : {parser} -> {setting[key]}") # TODO: editing complex fields and adding values for empty fields will # require full LiveParser expression parsing, for now we can only edit @@ -982,7 +975,6 @@ def _edit_jump(cmd): _edit_parser(parser) -# issues/126 def _main_edit(cmd, setting, line_number): if line_number is not None: print("{}: {}".format(line_number, str(cmd).replace("\r", "\\r").replace("\n", "\\n"))) @@ -1004,7 +996,6 @@ def _main_edit(cmd, setting, line_number): @lmlsb.command() @click.argument("lsb_file", required=True, type=click.Path(exists=True, dir_okay=False)) -# issues/126 @click.argument("line_number", required=False, type=int) @click.option("-p", "--param", type=str, help="Parameter in JSON format.") @click.option("-b", "--batch", type=str, help="Edit with parameter with JSON formatted file.")