Skip to content

Commit

Permalink
adds the property Corpus.repo that uses the new utils.get_git_repo() …
Browse files Browse the repository at this point in the history
…(and is None if not a Git repo)
  • Loading branch information
Johannes Hentschel committed Oct 20, 2023
1 parent d752917 commit fb5e39c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/ms3/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
from collections import defaultdict
from typing import List, Literal, Optional, overload

import git
from ms3 import Parse, compute_path_from_file, make_coloring_reports_and_warnings
from ms3 import (
Parse,
compute_path_from_file,
get_git_repo,
make_coloring_reports_and_warnings,
)
from ms3._version import __version__
from ms3.logger import get_logger, inspect_loggers
from ms3.operations import (
Expand Down Expand Up @@ -355,8 +359,9 @@ def precommit_cmd(
)
args.files = args.positional_args # in the future, maybe use args.include instead
test_passes = review_cmd(args, parse_obj=parse_obj, wrapped_by_precommit=True)
repo = git.Repo(args.dir)
repo.git.add(all=True)
repo = get_git_repo(args.dir)
if repo is not None:
repo.git.add(all=True)
deliver_test_result(test_passes, args.fail, logger)


Expand Down
12 changes: 9 additions & 3 deletions src/ms3/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import pandas as pd
import pathos.multiprocessing as mp
from ms3.utils.frictionless_helpers import store_dataframe_resource
from ms3.utils.functions import compute_path_from_file, get_name_of_highest_version_tag
from ms3.utils.functions import (
compute_path_from_file,
get_git_repo,
get_name_of_highest_version_tag,
)

from ._typing import (
AnnotationsFacet,
Expand Down Expand Up @@ -131,6 +135,9 @@ def __init__(
assert os.path.isdir(directory), f"{directory} is not an existing directory."
self.corpus_path: str = directory
"""Path where the corpus is located."""
self.repo: Optional[git.Repo] = None
"""If the corpus is part of a git repository, this attribute holds the corresponding :obj:`git.Repo` object."""
self.repo = get_git_repo(directory, logger=self.logger)
self.name = os.path.basename(directory).strip(r"\/")
"""Folder name of the corpus."""
if (
Expand Down Expand Up @@ -3007,8 +3014,7 @@ def get_facet_at_git_revision(
)
else:
if git_revision == "LATEST_VERSION":
repo = git.Repo(self.corpus_path)
git_info = get_name_of_highest_version_tag(repo)
git_info = get_name_of_highest_version_tag(self.repo)
else:
git_info = git_revision
self.logger.info(
Expand Down
20 changes: 18 additions & 2 deletions src/ms3/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,23 @@ class Piece(LoggedClass):
_deprecated_elements = ["get_dataframe"]

def __init__(
self, pname: str, view: View = None, labels_cfg={}, ms=None, **logger_cfg
self,
pname: str,
view: Optional[View] = None,
labels_cfg: Optional[dict] = None,
ms=None,
**logger_cfg,
):
"""
Args:
pname: Piece name, that is the file name without any suffixes or extensions.
view: :obj:`View` object to be used as default.
labels_cfg:
Configuration dictionary to determine the output format of :py:attr:`~.score.Score.labels`.
ms: MuseScore executable if convertible files (not MSCX or MSCZ) are to be parsed.
**logger_cfg
"""
super().__init__(subclass="Piece", logger_cfg=logger_cfg)
self.name = pname
available_types = ("scores",) + Score.dataframe_types
Expand Down Expand Up @@ -125,7 +140,8 @@ def __init__(
Configuration dictionary to determine the output format of :py:attr:`~.score.Score.labels` and
:py:attr:`~.score.Score.expanded` tables. The dictonary is passed to :py:attr:`~.score.Score` upon parsing.
"""
self.labels_cfg.update(update_labels_cfg(labels_cfg, logger=self.logger))
if labels_cfg is not None:
self.labels_cfg.update(update_labels_cfg(labels_cfg, logger=self.logger))

def all_facets_present(
self, view_name: Optional[str] = None, selected_facets: Optional[Facets] = None
Expand Down
24 changes: 24 additions & 0 deletions src/ms3/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from functools import cache, reduce
from inspect import getfullargspec, stack
from itertools import chain, repeat, takewhile
from pathlib import Path
from shutil import which
from tempfile import NamedTemporaryFile as Temp
from typing import (
Expand Down Expand Up @@ -1751,6 +1752,29 @@ def _fifths2str(fifths: int, steps: Collection[str], inverted: bool = False) ->
return acc + steps[fifths % 7]


def get_git_repo(
directory: str | Path,
search_parent_directories: bool = True,
logger: Optional[logging.Logger | str] = None,
) -> Optional[git.Repo]:
if logger is None:
logger = module_logger
elif isinstance(logger, str):
logger = get_logger(logger)
try:
repo = git.Repo(directory, search_parent_directories=search_parent_directories)
logger.debug(
f"{directory!r} has been recognized to be (part of) the git repository "
f"{repo.working_tree_dir}."
)
except Exception as e:
repo = None
logger.debug(
f"{directory!r} is not (part of) an existing git repository: {e!r}"
)
return repo


def get_ms_version(mscx_file):
with open(mscx_file, encoding="utf-8") as file:
for i, l in enumerate(file):
Expand Down

0 comments on commit fb5e39c

Please sign in to comment.