Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 27, 2024
1 parent a46cacb commit 8566e25
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
18 changes: 9 additions & 9 deletions mscxyz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
from mscxyz.utils import Dimension


def __mm(value: str) -> float:
def _mm(value: str) -> float:
return Dimension(value).to("mm")


def __inch(value: str) -> float:
def _inch(value: str) -> float:
return Dimension(value).to("in")


def __embed_fields(
def _embed_fields(
fields: Sequence[str], prefix: str = " Available fields: ", suffix: str = "."
) -> str:
joined_fields: str = ", ".join(fields)
Expand Down Expand Up @@ -318,7 +318,7 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
action="append",
metavar=("<field>", "<value>"),
dest="meta_metatag",
help="Define the metadata in MetaTag elements." + __embed_fields(Metatag.fields),
help="Define the metadata in MetaTag elements." + _embed_fields(Metatag.fields),
)

group_meta.add_argument(
Expand All @@ -328,7 +328,7 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
action="append",
metavar=("<field>", "<value>"),
dest="meta_vbox",
help="Define the metadata in VBox elements." + __embed_fields(Vbox.fields),
help="Define the metadata in VBox elements." + _embed_fields(Vbox.fields),
)

###############################################################################
Expand Down Expand Up @@ -573,7 +573,7 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
group_style.add_argument(
"--staff-space",
dest="style_staff_space",
type=__mm,
type=_mm,
metavar="<dimension>",
help="Set the staff space or spatium. This is the vertical distance between "
"two lines of a music staff.",
Expand Down Expand Up @@ -711,11 +711,11 @@ def list_styles(version: int) -> None:
score.style.staff_space = args.style_staff_space

if args.style_page_size is not None:
score.style.page_width = __inch(args.style_page_size[0])
score.style.page_height = __inch(args.style_page_size[1])
score.style.page_width = _inch(args.style_page_size[0])
score.style.page_height = _inch(args.style_page_size[1])

if args.style_margin is not None:
score.style.margin = __inch(args.style_margin)
score.style.margin = _inch(args.style_margin)

if args.style_show_header is not None:
score.style.show_header = args.style_show_header
Expand Down
22 changes: 4 additions & 18 deletions mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@


class ReadOnlyFieldError(Exception):
"""TODO remove: unused"""

def __init__(self, field: str) -> None:
self.msg = "The field “{}” is read only!".format(field)
Exception.__init__(self, self.msg)


class UnkownFieldError(Exception):
"""TODO remove: unused"""

def __init__(self, field: str, valid_fields: typing.Sequence[str]) -> None:
self.msg = "Unkown field of name “{}”! Valid field names are: {}".format(
field, ", ".join(valid_fields)
Expand All @@ -40,24 +44,6 @@ def __init__(self, format_string: str) -> None:
Exception.__init__(self, self.msg)


def export_to_dict(obj: object, fields: typing.Iterable[str]) -> dict[str, str]:
"""
Export the specified fields of an object to a dictionary.
:param obj: The object to export.
:param fields: The fields to include in the dictionary.
:return: A dictionary containing the specified fields and their values.
"""
out: dict[str, str] = {}
for field in fields:
value = getattr(obj, field)
if not value:
value = ""
out[field] = value
return out


class Metatag:
"""
The class provides access to the MuseScore metadata fields.
Expand Down
11 changes: 0 additions & 11 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Meta,
Metatag,
Vbox,
export_to_dict,
)
from mscxyz.score import Score
from tests import helper
Expand Down Expand Up @@ -47,16 +46,6 @@ def test_format_string_no_field_error(self) -> None:
assert e.value.args[0] == "No fields found in your " "format string “test”!"


class TestFunctions:
def test_export_to_dict(self) -> None:
class Data:
a = "a"
b = "b"

data = Data()
assert export_to_dict(data, ("a")) == {"a": "a"}


def get_meta_tag(filename: str, version: int) -> Metatag:
score = helper.get_score(filename, version)
return score.meta.metatag
Expand Down

0 comments on commit 8566e25

Please sign in to comment.