Skip to content

Commit

Permalink
Add some types
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Dec 31, 2023
1 parent e862e78 commit c383c50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
9 changes: 3 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ mscxyz
Run "subcommand --help" for more informations.
clean Clean and reset the formating of the "*.mscx" file
meta Deal with meta data informations stored in the MuseScore file.
lyrics Extract lyrics. Without any option this subcommand extracts all lyrics verses into separate mscx files. This generated mscx files contain only one verse. The old verse number is appended to the file name, e. g.:
score_1.mscx.
lyrics Extract lyrics. Without any option this subcommand extracts all lyrics verses into separate mscx files. This generated mscx files contain only one verse. The old verse number is appended to the file name, e. g.: score_1.mscx.
rename Rename the "*.mscx" files.
export Export the scores to PDFs or to the specified extension.
help Show help. Use “mscx-manager help all” to show help messages of all subcommands. Use “mscx-manager help <subcommand>” to show only help messages for the given subcommand.
Expand Down Expand Up @@ -180,8 +179,7 @@ mscx-manager meta
-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.
-d SOURCE_FIELDS FORMAT_STRING, --distribute-fields SOURCE_FIELDS FORMAT_STRING
Distribute source fields to target fields applying a format string on the source fields. It is possible to apply multiple --distribute-fields options. SOURCE_FIELDS can be a single field or a comma separated list
of fields: field_one,field_two. The program tries first to match the FORMAT_STRING on the first source field. If this fails, it tries the second source field ... an so on.
Distribute source fields to target fields applying a format string on the source fields. It is possible to apply multiple --distribute-fields options. SOURCE_FIELDS can be a single field or a comma separated list of fields: field_one,field_two. The program tries first to match the FORMAT_STRING on the first source field. If this fails, it tries the second source field ... an so on.
-j, --json Additionally write the meta data to a json file.
-l DESTINATION FORMAT_STRING, --log DESTINATION FORMAT_STRING
Write one line per file to a text file. e. g. --log /tmp/mscx-manager.log '$title $composer'
Expand All @@ -201,8 +199,7 @@ mscx-manager lyrics
-e LYRICS_EXTRACT, --extract LYRICS_EXTRACT
The lyric verse number to extract or "all".
-r LYRICS_REMAP, --remap LYRICS_REMAP
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.
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.
-f, --fix Fix lyrics: Convert trailing hyphens ("la- la- la") to a correct hyphenation ("la - la - la")
mscx-manager rename
Expand Down
9 changes: 3 additions & 6 deletions doc/source/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ mscxyz
Run "subcommand --help" for more informations.
clean Clean and reset the formating of the "*.mscx" file
meta Deal with meta data informations stored in the MuseScore file.
lyrics Extract lyrics. Without any option this subcommand extracts all lyrics verses into separate mscx files. This generated mscx files contain only one verse. The old verse number is appended to the file name, e. g.:
score_1.mscx.
lyrics Extract lyrics. Without any option this subcommand extracts all lyrics verses into separate mscx files. This generated mscx files contain only one verse. The old verse number is appended to the file name, e. g.: score_1.mscx.
rename Rename the "*.mscx" files.
export Export the scores to PDFs or to the specified extension.
help Show help. Use “mscx-manager help all” to show help messages of all subcommands. Use “mscx-manager help <subcommand>” to show only help messages for the given subcommand.
Expand Down Expand Up @@ -143,8 +142,7 @@ mscx-manager meta
-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.
-d SOURCE_FIELDS FORMAT_STRING, --distribute-fields SOURCE_FIELDS FORMAT_STRING
Distribute source fields to target fields applying a format string on the source fields. It is possible to apply multiple --distribute-fields options. SOURCE_FIELDS can be a single field or a comma separated list
of fields: field_one,field_two. The program tries first to match the FORMAT_STRING on the first source field. If this fails, it tries the second source field ... an so on.
Distribute source fields to target fields applying a format string on the source fields. It is possible to apply multiple --distribute-fields options. SOURCE_FIELDS can be a single field or a comma separated list of fields: field_one,field_two. The program tries first to match the FORMAT_STRING on the first source field. If this fails, it tries the second source field ... an so on.
-j, --json Additionally write the meta data to a json file.
-l DESTINATION FORMAT_STRING, --log DESTINATION FORMAT_STRING
Write one line per file to a text file. e. g. --log /tmp/mscx-manager.log '$title $composer'
Expand All @@ -164,8 +162,7 @@ mscx-manager lyrics
-e LYRICS_EXTRACT, --extract LYRICS_EXTRACT
The lyric verse number to extract or "all".
-r LYRICS_REMAP, --remap LYRICS_REMAP
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.
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.
-f, --fix Fix lyrics: Convert trailing hyphens ("la- la- la") to a correct hyphenation ("la - la - la")
mscx-manager rename
Expand Down
39 changes: 22 additions & 17 deletions mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import lxml
import lxml.etree
import tmep
from lxml.etree import _Element

from mscxyz.score_file_classes import MscoreXmlTree
from mscxyz.utils import color, get_args

if typing.TYPE_CHECKING:
from lxml.etree import _Element
from lxml.etree import _XPathObject


class ReadOnlyFieldError(Exception):
Expand Down Expand Up @@ -227,37 +228,41 @@ def __init__(self, xml_root: _Element):
for element in xml_root.xpath(xpath):
element.insert(0, vbox)

def _get_tag(self, style):
def _get_tag(self, style: str) -> _Element | None:
"""
:param string style: String inside the `<style>` tags
:param style: String inside the `<style>` tags
"""
for element in self.xml_root.xpath("//VBox/Text"):
if element.find("style").text == style:
return element.find("text")

def _get_text(self, style):
x: _XPathObject = self.xml_root.xpath("//VBox/Text")
if isinstance(x, list):
for element in x:
if isinstance(element, _Element):
s: _Element | None = element.find("style")
if s is not None and s.text == style:
return element.find("text")

def _get_text(self, style: str) -> str | None:
"""
:param string style: String inside the `<style>` tags
:param style: String inside the `<style>` tags
"""
element = self._get_tag(style)
if hasattr(element, "text"):
element: _Element | None = self._get_tag(style)
if element is not None and hasattr(element, "text"):
return element.text

def __getattr__(self, field):
def __getattr__(self, field: str) -> str | None:
field = field.title()
if field not in self.fields:
raise UnkownFieldError(field, self.fields)
else:
return self._get_text(field)

def _create_text_tag(self, style, text):
def _create_text_tag(self, style: str, text: str):
"""
:param string style: String inside the `<style>` tags
:param style: String inside the `<style>` tags
"""
Text_tag = lxml.etree.Element("Text")
style_tag = lxml.etree.SubElement(Text_tag, "style")
Text_tag: _Element = lxml.etree.Element("Text")
style_tag: _Element = lxml.etree.SubElement(Text_tag, "style")
style_tag.text = style
text_tag = lxml.etree.SubElement(Text_tag, "text")
text_tag: _Element = lxml.etree.SubElement(Text_tag, "text")
text_tag.text = text
for element in self.xml_root.xpath("//VBox"):
element.append(Text_tag)
Expand Down

0 comments on commit c383c50

Please sign in to comment.