From a946415b37231faef05ed64063480743b4fb72fa Mon Sep 17 00:00:00 2001 From: Josef Friedrich Date: Tue, 16 Jan 2024 22:12:21 +0100 Subject: [PATCH] Implement --clean-meta in ng-cli --- README.rst | 5 ++-- autocomplete.bash | 2 +- autocomplete.tcsh | 2 +- autocomplete.zsh | 2 +- docs/cli.rst | 5 ++-- mscxyz/cli.py | 11 +++++---- tests/test_meta.py | 59 ++++++++++++++++++++++++++++++++++++++-------- 7 files changed, 65 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index cc8fdfc..50dd604 100644 --- a/README.rst +++ b/README.rst @@ -386,8 +386,9 @@ CLI Usage - vbox_subtitle - vbox_title - -c META_CLEAN, --clean META_CLEAN - Clean the meta data fields. Possible values: „all“ or + -c META_CLEAN, --clean-meta META_CLEAN + Clean the meta data fields. Possible values: „all“ or a + comma separated list of fields, for example: „field_one,field_two“. -D, --delete-duplicates Deletes combined_lyricist if this field is equal to diff --git a/autocomplete.bash b/autocomplete.bash index 0a6a299..a658f71 100644 --- a/autocomplete.bash +++ b/autocomplete.bash @@ -2,7 +2,7 @@ -_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '-k' '--colorize' '-C' '--config-file' '-d' '--dry-run' '-m' '--mscore' '--diff' '-e' '--executable' '-v' '--verbose' '-E' '--export' '-c' '--clean' '-D' '--delete-duplicates' '-i' '--distribute-fields' '-j' '--json' '-l' '--log' '-y' '--synchronize' '-S' '--set-field' '--metatag' '--metatag-meta' '--vbox' '--vbox-meta' '--combined' '--combined-meta' '-x' '--extract' '--extract-lyrics' '-r' '--remap' '--remap-lyrics' '-F' '--fix' '--fix-lyrics' '--rename' '-f' '--format' '-A' '--alphanum' '-a' '--ascii' '-n' '--no-whitespace' '-K' '--skip-if-empty' '-t' '--target' '-L' '--list-files' '-g' '--glob' '--mscz' '--mscx' '-s' '--style' '-Y' '--style-file' '--s3' '--styles-v3' '--s4' '--styles-v4' '--list-fonts' '--text-font' '--title-font' '--musical-symbol-font' '--musical-text-font' '--staff-space' '--page-size' '--margin' '--header' '--no-header' '--footer' '--no-footer') +_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '-k' '--colorize' '-C' '--config-file' '-d' '--dry-run' '-m' '--mscore' '--diff' '-e' '--executable' '-v' '--verbose' '-E' '--export' '-c' '--clean-meta' '-D' '--delete-duplicates' '-i' '--distribute-fields' '-j' '--json' '-l' '--log' '-y' '--synchronize' '-S' '--set-field' '--metatag' '--metatag-meta' '--vbox' '--vbox-meta' '--combined' '--combined-meta' '-x' '--extract' '--extract-lyrics' '-r' '--remap' '--remap-lyrics' '-F' '--fix' '--fix-lyrics' '--rename' '-f' '--format' '-A' '--alphanum' '-a' '--ascii' '-n' '--no-whitespace' '-K' '--skip-if-empty' '-t' '--target' '-L' '--list-files' '-g' '--glob' '--mscz' '--mscx' '-s' '--style' '-Y' '--style-file' '--s3' '--styles-v3' '--s4' '--styles-v4' '--list-fonts' '--text-font' '--title-font' '--musical-symbol-font' '--musical-text-font' '--staff-space' '--page-size' '--margin' '--header' '--no-header' '--footer' '--no-footer') _shtab_musescore_manager_pos_0_COMPGEN=_shtab_compgen_files _shtab_musescore_manager__C_COMPGEN=_shtab_compgen_files diff --git a/autocomplete.tcsh b/autocomplete.tcsh index 6166235..1c92821 100644 --- a/autocomplete.tcsh +++ b/autocomplete.tcsh @@ -3,7 +3,7 @@ complete musescore-manager \ - 'c/--/(alphanum ascii backup clean colorize combined combined-meta config-file delete-duplicates diff distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json list-files list-fonts log margin metatag metatag-meta mscore mscx mscz musical-symbol-font musical-text-font no-footer no-header no-whitespace page-size print-completion remap remap-lyrics rename s3 s4 set-field skip-if-empty staff-space style style-file styles-v3 styles-v4 synchronize target text-font title-font vbox vbox-meta verbose version)/' \ + 'c/--/(alphanum ascii backup clean-meta colorize combined combined-meta config-file delete-duplicates diff distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json list-files list-fonts log margin metatag metatag-meta mscore mscx mscz musical-symbol-font musical-text-font no-footer no-header no-whitespace page-size print-completion remap remap-lyrics rename s3 s4 set-field skip-if-empty staff-space style style-file styles-v3 styles-v4 synchronize target text-font title-font vbox vbox-meta verbose version)/' \ 'c/-/(- A C D E F K L S V Y a b c d e f g h i j k l m n r s t v x y)/' \ 'n/--print-completion/(bash zsh tcsh)/' \ 'n/-C/f/' \ diff --git a/autocomplete.zsh b/autocomplete.zsh index 8073c5d..85fe549 100644 --- a/autocomplete.zsh +++ b/autocomplete.zsh @@ -23,7 +23,7 @@ _shtab_musescore_manager_options=( {-e,--executable}"[Path of the musescore executable.]:general_executable:_files" "*"{-v,--verbose}"[Make commands more verbose. You can specifiy multiple arguments (. g.\: -vvv) to make the command more verbose.]" {-E,--export}"[Export the scores in a format defined by the extension. The exported file has the same path, only the file extension is different. Further information can be found at the MuseScore website\: https\:\/\/musescore.org\/en\/handbook\/2\/file-formats, https\:\/\/musescore.org\/en\/handbook\/3\/file-export, https\:\/\/musescore.org\/en\/handbook\/4\/file-export. MuseScore must be installed and the script must know the location of the binary file.]:export_extension:(mscz mscx spos mpos pdf svg png wav mp3 ogg flac mid midi kar musicxml xml mxl brf mei)" - {-c,--clean}"[Clean the meta data fields. Possible values\: \„all\“ or \„field_one,field_two\“.]:meta_clean:" + {-c,--clean-meta}"[Clean the meta data fields. Possible values\: \„all\“ or a comma separated list of fields, for example\: \„field_one,field_two\“.]:meta_clean:" {-D,--delete-duplicates}"[Deletes combined_lyricist if this field is equal to combined_composer. Deletes combined_subtitle if this field is equal tocombined_title. Move combined_subtitle to combimed_title if combined_title is empty.]" "*"{-i,--distribute-fields}"[Distribute source fields to target fields by applying a format string on the source fields. It is possible to apply multiple --distribute-fields options. \ can be a single field or a comma separated list of fields\: field_one,field_two. The program tries first to match the \ on the first source field. If thisfails, it tries the second source field ... and so on.]:meta_dist:" {-j,--json}"[Additionally write the meta data to a json file.]" diff --git a/docs/cli.rst b/docs/cli.rst index b62b9f6..c7f3411 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -150,8 +150,9 @@ Comande line interface - vbox_subtitle - vbox_title - -c META_CLEAN, --clean META_CLEAN - Clean the meta data fields. Possible values: „all“ or + -c META_CLEAN, --clean-meta META_CLEAN + Clean the meta data fields. Possible values: „all“ or a + comma separated list of fields, for example: „field_one,field_two“. -D, --delete-duplicates Deletes combined_lyricist if this field is equal to diff --git a/mscxyz/cli.py b/mscxyz/cli.py index 65a3309..ce818ae 100644 --- a/mscxyz/cli.py +++ b/mscxyz/cli.py @@ -255,9 +255,10 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]: group_meta.add_argument( "-c", - "--clean", + "--clean-meta", dest="meta_clean", - help="Clean the meta data fields. Possible values: „all“ or " + help="Clean the meta data fields. Possible values: „all“ or a comma separated " + "list of fields, for example: " "„field_one,field_two“.", ) @@ -810,8 +811,10 @@ def list_styles(version: int) -> None: # score = Score(file) # if __no_error(lxml.etree.XMLSyntaxError, score.errors): # pre: dict[str, str] = score.meta.interface.export_to_dict() - # if args.meta_clean: - # score.meta.clean_metadata(fields_spec=args.meta_clean) + + if args.meta_clean: + score.meta.clean_metadata(fields_spec=args.meta_clean) + # if args.meta_json: # score.meta.export_json() if args.meta_dist: diff --git a/tests/test_meta.py b/tests/test_meta.py index a9554ea..8dd612a 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -576,25 +576,63 @@ def test_distribute_field_exception_unmatched(self) -> None: class TestOptionClean: + @pytest.mark.legacy + def test_clean_all_legacy(self) -> None: + c = ( + Cli("meta", "--clean", "all", legacy=True) + .append_score("meta-all-values.mscz") + .execute() + ) + i = c.post.meta.interface_read_write + for field in i.fields: + assert getattr(i, field) is None, field + def test_clean_all(self) -> None: - tmp = helper.get_score("meta-all-values.mscx") - Cli("meta", "--clean", "all", tmp, legacy=True).execute() - score = helper.reload(tmp) - i = score.meta.interface_read_write + c = Cli("--clean-meta", "all").append_score("meta-all-values.mscz").execute() + i = c.post.meta.interface_read_write for field in i.fields: assert getattr(i, field) is None, field + @pytest.mark.legacy + def test_clean_single_field_legacy(self) -> None: + c = ( + Cli("meta", "--clean", "vbox_title", legacy=True) + .append_score("meta-all-values.mscz") + .execute() + ) + i = c.post.meta.interface_read_write + assert i.vbox_title is None, "vbox_title" + assert i.vbox_composer == "vbox_composer", "vbox_composer" + def test_clean_single_field(self) -> None: - tmp = helper.get_score("meta-all-values.mscx") - Cli("meta", "--clean", "vbox_title", tmp, legacy=True).execute() - i = reload(tmp) + c = ( + Cli("--clean-meta", "vbox_title") + .append_score("meta-all-values.mscz") + .execute() + ) + i = c.post.meta.interface_read_write assert i.vbox_title is None, "vbox_title" assert i.vbox_composer == "vbox_composer", "vbox_composer" + @pytest.mark.legacy + def test_clean_some_fields_legacy(self) -> None: + c = ( + Cli("meta", "--clean", "vbox_title,vbox_composer", legacy=True) + .append_score("meta-all-values.mscz") + .execute() + ) + i = c.post.meta.interface_read_write + assert i.vbox_title is None, "vbox_title" + assert i.vbox_composer is None, "vbox_composer" + assert i.vbox_subtitle == "vbox_subtitle", "vbox_subtitle" + def test_clean_some_fields(self) -> None: - tmp = helper.get_score("meta-all-values.mscx") - Cli("meta", "--clean", "vbox_title,vbox_composer", tmp, legacy=True).execute() - i = reload(tmp) + c = ( + Cli("--clean-meta", "vbox_title,vbox_composer") + .append_score("meta-all-values.mscz") + .execute() + ) + i = c.post.meta.interface_read_write assert i.vbox_title is None, "vbox_title" assert i.vbox_composer is None, "vbox_composer" assert i.vbox_subtitle == "vbox_subtitle", "vbox_subtitle" @@ -879,6 +917,7 @@ def test_multiple_times(self) -> None: assert i.vbox_title == "vt" assert i.vbox_composer == "vc" + @pytest.mark.legacy def test_with_templating_legacy(self) -> None: c = ( Cli(