Skip to content

Commit

Permalink
Add option compress
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 28, 2024
1 parent 0359b4d commit 2dfa5a6
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 34 deletions.
28 changes: 17 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ CLI Usage
usage: musescore-manager [-h] [--print-completion {bash,zsh,tcsh}] [-V] [-b]
[--bail] [-k] [-C GENERAL_CONFIG_FILE] [-d] [-m]
[--diff] [-e FILE_PATH] [-v] [-E <extension>]
[-c META_CLEAN] [-D]
[--compress] [-c META_CLEAN] [-D]
[-i <source-fields> <format-string>] [-j]
[-l <log-file> <format-string>] [-y]
[-S DESTINATION_FIELD FORMAT_STRING]
[--metatag <field> <value>] [--vbox <field> <value>]
[-x LYRICS_EXTRACT] [-r LYRICS_REMAP] [-F] [--rename]
[-f RENAME_FORMAT] [-A] [-a] [-n] [-K FIELDS]
[-t RENAME_TARGET] [-L]
[-f <path-template>] [-A] [-a] [-n] [-K <fields>]
[-t <directory>] [-L]
[-g <glob-pattern> | --mscz | --mscx]
[-s <style-name> <value>] [--clean] [-Y <file>] [--s3]
[--s4] [--list-fonts] [--text-font <font-face>]
Expand Down Expand Up @@ -346,7 +346,8 @@ CLI Usage
-C GENERAL_CONFIG_FILE, --config-file GENERAL_CONFIG_FILE
Specify a configuration file in the INI format.
-d, --dry-run Simulate the actions.
-m, --mscore Open and save the XML file in MuseScore after manipulating
-m, --mscore, --save-in-mscore
Open and save the XML file in MuseScore after manipulating
the XML with lxml to avoid differences in the XML structure.
--diff Show a diff of the XML file before and after the
manipulation.
Expand All @@ -363,6 +364,8 @@ CLI Usage
https://musescore.org/en/handbook/4/file-export. MuseScore
must be installed and the script must know the location of
the binary file.
--compress Save an uncompressed MuseScore file (*.mscx) as a compressed
file (*.mscz).

meta:
Deal with meta data informations stored in the MuseScore file. MuseScore can store meta data informations in different places:
Expand Down Expand Up @@ -633,16 +636,19 @@ CLI Usage
%upper{text}
Convert “text” to UPPERCASE.

--rename Format string.
-f RENAME_FORMAT, --format RENAME_FORMAT
Format string.
--rename Flag to activate the renaming. If you omit this flag, the
renaming will not be performed.
-f <path-template>, --format <path-template>
A path template string to set the destination location
(Default is “$title ($composer)”).
-A, --alphanum Use only alphanumeric characters.
-a, --ascii Use only ASCII characters.
-n, --no-whitespace Replace all whitespaces with dashes or sometimes underlines.
-K FIELDS, --skip-if-empty FIELDS
Skip rename action if FIELDS are empty. Separate FIELDS
using commas: composer,title
-t RENAME_TARGET, --target RENAME_TARGET
-K <fields>, --skip-if-empty <fields>
Skip the rename action if the fields specified in <fields>
are empty. Multiple fields can be separated by commas, e.
g.: composer,title
-t <directory>, --target <directory>
Target directory

selection:
Expand Down
3 changes: 2 additions & 1 deletion autocomplete.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@



_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '--bail' '-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' '-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' '--clean' '-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' '--a4' '--din-a4' '--letter' '--margin' '--header' '--no-header' '--footer' '--no-footer')
_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '--bail' '-k' '--colorize' '-C' '--config-file' '-d' '--dry-run' '-m' '--mscore' '--save-in-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' '-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' '--clean' '-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' '--a4' '--din-a4' '--letter' '--margin' '--header' '--no-header' '--footer' '--no-footer')

_shtab_musescore_manager_pos_0_COMPGEN=_shtab_compgen_files
_shtab_musescore_manager__C_COMPGEN=_shtab_compgen_files
Expand Down Expand Up @@ -32,6 +32,7 @@ _shtab_musescore_manager__d_nargs=0
_shtab_musescore_manager___dry_run_nargs=0
_shtab_musescore_manager__m_nargs=0
_shtab_musescore_manager___mscore_nargs=0
_shtab_musescore_manager___save_in_mscore_nargs=0
_shtab_musescore_manager___diff_nargs=0
_shtab_musescore_manager__v_nargs=0
_shtab_musescore_manager___verbose_nargs=0
Expand Down
2 changes: 1 addition & 1 deletion autocomplete.tcsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


complete musescore-manager \
'c/--/(a4 alphanum ascii backup bail clean clean-meta colorize config-file delete-duplicates diff din-a4 distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json letter 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/--/(a4 alphanum ascii backup bail clean clean-meta colorize config-file delete-duplicates diff din-a4 distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json letter 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 save-in-mscore 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/' \
Expand Down
8 changes: 4 additions & 4 deletions autocomplete.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _shtab_musescore_manager_options=(
{-k,--colorize}"[Colorize the command line print statements.]"
{-C,--config-file}"[Specify a configuration file in the INI format.]:general_config_file:_files"
{-d,--dry-run}"[Simulate the actions.]"
{-m,--mscore}"[Open and save the XML file in MuseScore after manipulating the XML with lxml to avoid differences in the XML structure.]"
{-m,--mscore,--save-in-mscore}"[Open and save the XML file in MuseScore after manipulating the XML with lxml to avoid differences in the XML structure.]"
"--diff[Show a diff of the XML file before and after the manipulation.]"
{-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.]"
Expand All @@ -36,12 +36,12 @@ _shtab_musescore_manager_options=(
{-x,--extract,--extract-lyrics}"[Extract each lyrics verse into a separate MuseScore file. Specify \”all\” to extract all lyrics verses. The old verse number is appended to the file name, e. g.\: score_1.mscx.]:lyrics_extract:"
{-r,--remap,--remap-lyrics}"[Remap lyrics. Example\: \"--remap 3\:2,5\:3\". This example remaps lyrics verse 3 to verse 2 and verse 5 to 3. Use commas to specify multiple remap pairs. One remap pair is separated by a colon in this form\: \"old\:new\"\: \"old\" stands for the old verse number. \"new\" stands for the new verse number.]:lyrics_remap:"
{-F,--fix,--fix-lyrics}"[Fix lyrics\: Convert trailing hyphens (\"la- la- la\") to a correct hyphenation (\"la - la - la\")]"
"--rename[Format string.]"
{-f,--format}"[Format string.]:rename_format:"
"--rename[Flag to activate the renaming. If you omit this flag, the renaming will not be performed.]"
{-f,--format}"[A path template string to set the destination location (Default is \“\$title (\$composer)\”).]:rename_format:"
{-A,--alphanum}"[Use only alphanumeric characters.]"
{-a,--ascii}"[Use only ASCII characters.]"
{-n,--no-whitespace}"[Replace all whitespaces with dashes or sometimes underlines.]"
{-K,--skip-if-empty}"[Skip rename action if FIELDS are empty. Separate FIELDS using commas\: composer,title]:rename_skip:"
{-K,--skip-if-empty}"[Skip the rename action if the fields specified in \<fields\> are empty. Multiple fields can be separated by commas, e. g.\: composer,title]:rename_skip:"
{-t,--target}"[Target directory]:rename_target:"
{-L,--list-files}"[Only list files and do nothing else.]"
{-g,--glob}"[Handle only files which matches against Unix style glob patterns (e. g. \"\*.mscx\", \"\* - \*\"). If you omit this option, the standard glob pattern \"\*.msc\[xz\]\" is used.]:selection_glob:"
Expand Down
28 changes: 17 additions & 11 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ Comande line interface
usage: musescore-manager [-h] [--print-completion {bash,zsh,tcsh}] [-V] [-b]
[--bail] [-k] [-C GENERAL_CONFIG_FILE] [-d] [-m]
[--diff] [-e FILE_PATH] [-v] [-E <extension>]
[-c META_CLEAN] [-D]
[--compress] [-c META_CLEAN] [-D]
[-i <source-fields> <format-string>] [-j]
[-l <log-file> <format-string>] [-y]
[-S DESTINATION_FIELD FORMAT_STRING]
[--metatag <field> <value>] [--vbox <field> <value>]
[-x LYRICS_EXTRACT] [-r LYRICS_REMAP] [-F] [--rename]
[-f RENAME_FORMAT] [-A] [-a] [-n] [-K FIELDS]
[-t RENAME_TARGET] [-L]
[-f <path-template>] [-A] [-a] [-n] [-K <fields>]
[-t <directory>] [-L]
[-g <glob-pattern> | --mscz | --mscx]
[-s <style-name> <value>] [--clean] [-Y <file>] [--s3]
[--s4] [--list-fonts] [--text-font <font-face>]
Expand Down Expand Up @@ -44,7 +44,8 @@ Comande line interface
-C GENERAL_CONFIG_FILE, --config-file GENERAL_CONFIG_FILE
Specify a configuration file in the INI format.
-d, --dry-run Simulate the actions.
-m, --mscore Open and save the XML file in MuseScore after manipulating
-m, --mscore, --save-in-mscore
Open and save the XML file in MuseScore after manipulating
the XML with lxml to avoid differences in the XML structure.
--diff Show a diff of the XML file before and after the
manipulation.
Expand All @@ -61,6 +62,8 @@ Comande line interface
https://musescore.org/en/handbook/4/file-export. MuseScore
must be installed and the script must know the location of
the binary file.
--compress Save an uncompressed MuseScore file (*.mscx) as a compressed
file (*.mscz).

meta:
Deal with meta data informations stored in the MuseScore file. MuseScore can store meta data informations in different places:
Expand Down Expand Up @@ -331,16 +334,19 @@ Comande line interface
%upper{text}
Convert “text” to UPPERCASE.

--rename Format string.
-f RENAME_FORMAT, --format RENAME_FORMAT
Format string.
--rename Flag to activate the renaming. If you omit this flag, the
renaming will not be performed.
-f <path-template>, --format <path-template>
A path template string to set the destination location
(Default is “$title ($composer)”).
-A, --alphanum Use only alphanumeric characters.
-a, --ascii Use only ASCII characters.
-n, --no-whitespace Replace all whitespaces with dashes or sometimes underlines.
-K FIELDS, --skip-if-empty FIELDS
Skip rename action if FIELDS are empty. Separate FIELDS
using commas: composer,title
-t RENAME_TARGET, --target RENAME_TARGET
-K <fields>, --skip-if-empty <fields>
Skip the rename action if the fields specified in <fields>
are empty. Multiple fields can be separated by commas, e.
g.: composer,title
-t <directory>, --target <directory>
Target directory

selection:
Expand Down
1 change: 1 addition & 0 deletions docs/stubs/mscxyz.Score.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
~Score.extension
~Score.fields
~Score.filename
~Score.is_uncompressed
~Score.json_file
~Score.lyrics
~Score.meta
Expand Down
23 changes: 18 additions & 5 deletions mscxyz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
parser.add_argument(
"-m",
"--mscore",
"--save-in-mscore",
action="store_true",
dest="general_mscore",
help="Open and save the XML file in MuseScore after manipulating the XML "
Expand Down Expand Up @@ -163,6 +164,13 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
"binary file.",
)

parser.add_argument(
"--compress",
dest="export_compress",
action="store_true",
help="Save an uncompressed MuseScore file (*.mscx) as a compressed file (*.mscz).",
)

###############################################################################
# meta
###############################################################################
Expand Down Expand Up @@ -379,15 +387,16 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
"--rename",
dest="rename_rename",
action="store_true",
help="Format string.",
help="Flag to activate the renaming. If you omit this flag, the renaming will not be performed.",
)

group_rename.add_argument(
"-f",
"--format",
dest="rename_format",
default="$title ($composer)",
help="Format string.",
metavar="<path-template>",
help="A path template string to set the destination location (Default is “$title ($composer)”).",
)

group_rename.add_argument(
Expand Down Expand Up @@ -418,15 +427,16 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
"-K",
"--skip-if-empty",
dest="rename_skip",
metavar="FIELDS",
help="Skip rename action if FIELDS are empty. Separate FIELDS using "
"commas: composer,title",
metavar="<fields>",
help="Skip the rename action if the fields specified in <fields> are empty. "
"Multiple fields can be separated by commas, e. g.: composer,title",
)

group_rename.add_argument(
"-t",
"--target",
dest="rename_target",
metavar="<directory>",
help="Target directory",
)

Expand Down Expand Up @@ -693,6 +703,9 @@ def list_styles(version: int) -> None:
if args.general_diff:
score.make_snapshot()

if args.export_compress:
score = Score(score.export.compress())

# style

if args.style_clean:
Expand Down
16 changes: 15 additions & 1 deletion mscxyz/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class Export:
def __init__(self, score: "Score") -> None:
self.score = score

def to_extension(self, extension: str = "pdf") -> None:
def to_extension(self, extension: str = "pdf") -> Path:
"""Export the score to the specifed file type.
:param extension: The extension (default: pdf)
:return: The path of the exported file.
"""

extension = extension.lower()
Expand All @@ -65,6 +67,18 @@ def to_extension(self, extension: str = "pdf") -> None:
str(self.score.path),
]
)
return dest

def compress(self) -> Path:
"""Compress the score.
:return: The path of the new compress score.
"""

if not self.score.is_uncompressed:
raise ValueError("The score is already compressed!")

return self.to_extension("mscz")

def reload(self, save: bool = False) -> Export:
"""
Expand Down
5 changes: 5 additions & 0 deletions mscxyz/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def extension(self) -> str:
"""The extension (``mscx`` or ``mscz``) of the score file."""
return self.filename.split(".")[-1].lower()

@property
def is_uncompressed(self) -> bool:
"""Whether the MuseScore file is uncompressed , i.e. it is a ``*.mscx`` file"""
return self.extension != "mscz"

def change_path(
self, suffix: Optional[Any] = None, extension: Optional[str] = None
) -> Path:
Expand Down
1 change: 1 addition & 0 deletions mscxyz/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DefaultArguments:

# export
export_extension: Optional[str] = None
export_compress: bool = False

# help
help_markdown: bool = False
Expand Down
10 changes: 10 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Test the command line interface using subprocess."""
from __future__ import annotations

from pathlib import Path
from unittest import mock

import pytest

from tests import helper
from tests.helper import Cli

Expand All @@ -22,3 +25,10 @@ def test_export_fails(self) -> None:
"musescore-manager: error: argument -E/--export: invalid choice: 'xxx' (choose from 'mscz', 'mscx', 'spos', 'mpos', 'pdf', 'svg', 'png', 'wav', 'mp3', 'ogg', 'flac', 'mid', 'midi', 'kar', 'musicxml', 'xml', 'mxl', 'brf', 'mei')"
in Cli("--export", "xxx").sysexit()
)


@pytest.mark.slow
def test_compress() -> None:
score = Cli("--compress").append_score("simple.mscx", 3).score()
dest = str(score.path).replace(".mscx", ".mscz")
assert Path(dest).exists()

0 comments on commit 2dfa5a6

Please sign in to comment.