Skip to content

Commit

Permalink
Multilabel and not-label support added (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasursadikov authored Sep 7, 2024
1 parent c728195 commit 9990295
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ All entries are stored in `.mudconfig` in XML format. After making your first en
- `mud update` - check for available updates.

### Arguments
- `-l=<label>` or `--label=<label>` - filters out repositories by mud labels.
- `-l=<label>` or `--label=<label>` - filters out repositories with provided label.
- `-nl=<label>` or `--not-label=<label>` - filters out repositories excluding provided label.
- `-b=<branch>` or `--branch=<branch>` - filters out repositories by current branch name.
- `-m` or `--modified` - filters out modified repositories.
- `-d` or `--diverged` - filters repositories with diverged branches.
Expand Down
11 changes: 5 additions & 6 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,16 @@ def load(self, file_path: str) -> None:
labels = [label.strip() for label in dir_element.get('label', '').split(',') if label.strip()]
self.data[path] = labels

def all(self) -> Dict[str, List[str]]:
return self.data

def paths(self) -> List[str]:
return list(self.data.keys())

def with_label(self, label: str) -> Dict[str, List[str]]:
def filter_label(self, label: str, repos: Dict[str, List[str]] = None) -> Dict[str, List[str]]:
if repos is None:
repos = self.data
if label == '':
return self.all()
return repos
result = {}
for path, labels in self.data.items():
for path, labels in repos.items():
if label in labels:
result[path] = labels
return result
Expand Down
21 changes: 17 additions & 4 deletions mud.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Filters
TABLE_ATTR = '-t', '--table'
LABEL_PREFIX = '-l=', '--label='
NOT_LABEL_PREFIX = '-nl=', '--not-label='
BRANCH_PREFIX = '-b=', '--branch='
MODIFIED_ATTR = '-m', '--modified'
DIVERGED_ATTR = '-d', '--diverged'
Expand Down Expand Up @@ -65,7 +66,8 @@ def _create_parser() -> ArgumentParser:
remove_parser.add_argument('path', help='Repository to remove (optional).', nargs='?', type=str)

parser.add_argument(*TABLE_ATTR, metavar='TABLE', nargs='?', default='', type=str, help=f'Switches table view, runs in table view it is disabled in {BOLD}.mudsettings{RESET}.')
parser.add_argument(*LABEL_PREFIX, metavar='LABEL', nargs='?', default='', type=str, help='Filters repositories by provided label.')
parser.add_argument(*LABEL_PREFIX, metavar='LABEL', nargs='?', default='', type=str, help='Selects repositories with provided label.')
parser.add_argument(*NOT_LABEL_PREFIX, metavar='NOT_LABEL', nargs='?', default='', type=str, help='Selects repositories without provided label.')
parser.add_argument(*BRANCH_PREFIX, metavar='BRANCH', nargs='?', default='', type=str, help='Filter repositories by provided branch.')
parser.add_argument(*MODIFIED_ATTR, action='store_true', help='Filters modified repositories.')
parser.add_argument(*DIVERGED_ATTR, action='store_true', help='Filters repositories with diverged branches.')
Expand Down Expand Up @@ -158,7 +160,11 @@ def run(self) -> None:

# Filter out repositories if user provided filters
def _filter_repos(self) -> None:
self.repos = self.config.all()
self.repos = self.config.data
for path, labels in self.config.filter_label('ignore', self.config.data).items():
del self.repos[path]
any_filters = False
filtered = {}
branch = None
modified = False
diverged = False
Expand All @@ -168,9 +174,15 @@ def _filter_repos(self) -> None:
arg = sys.argv[index]
if arg.startswith('-'):
arg = sys.argv[1:][index - 1]
if any(arg.startswith(prefix) for prefix in LABEL_PREFIX):
if any(arg.startswith(prefix) for prefix in LABEL_PREFIX) or (arg.startswith(prefix) for prefix in NOT_LABEL_PREFIX):
any_filters = True
label = arg.split('=', 1)[1]
self.repos = self.config.with_label(label)
include = any(arg.startswith(prefix) for prefix in LABEL_PREFIX)
for path, labels in self.config.filter_label(label, self.repos).items():
if include:
filtered[path] = labels
elif path in filtered:
del filtered[path]
elif any(arg.startswith(prefix) for prefix in BRANCH_PREFIX):
branch = arg.split('=', 1)[1]
elif arg in TABLE_ATTR:
Expand All @@ -185,6 +197,7 @@ def _filter_repos(self) -> None:
del sys.argv[index]
continue
break
self.repos = filtered if any_filters else self.repos
directory = os.getcwd()
to_delete = []
for repo in self.repos:
Expand Down

0 comments on commit 9990295

Please sign in to comment.