Skip to content

Commit

Permalink
Added --pattern to CLI, closes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Jul 11, 2020
1 parent 8ed104e commit 522c218
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ The command line tool can also be run via `python -m osxmetadata`. Running it w

```
Usage: osxmetadata [OPTIONS] FILE
Usage: __main__.py [OPTIONS] FILE
Read/write metadata from file(s).
Options:
Expand Down Expand Up @@ -64,13 +63,25 @@ Options:
tags' sets tags and keywords to same values.
-B, --backup Backup FILE attributes. Backup file
'.osxmetadata.json' will be created in same
folder as FILE. Only backs up attributes
folder as FILE. Only backs up attributes
known to osxmetadata.
-R, --restore Restore FILE attributes from backup file.
Restore will look for backup file
'.osxmetadata.json' in same folder as FILE.
-V, --verbose Print verbose output.
-f, --copyfrom SOURCE_FILE Copy attributes from file SOURCE_FILE.
--files-only Do not apply metadata commands to
directories themselves, only files in a
directory.
-p, --pattern PATTERN Only process files matching PATTERN; only
applies to --walk. If specified, only files
matching PATTERN will be processed as each
directory is walked. May be used for than
once to specify multiple patterns. For
example, tag all *.pdf files in projectdir
and subfolders with tag 'project':
osxmetadata --append tags 'project' --walk
projectdir/ --pattern '*.pdf'
Valid attributes for ATTRIBUTE: Each attribute has a short name, a constant
name, and a long constant name. Any of these may be used for ATTRIBUTE
Expand Down
72 changes: 50 additions & 22 deletions osxmetadata/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# /usr/bin/env python3

import datetime
import glob
import itertools
import json
import logging
Expand Down Expand Up @@ -274,6 +275,18 @@ def get_help(self, ctx):
default=False,
required=False,
)
PATTERN_OPTION = click.option(
"--pattern",
"-p",
metavar="PATTERN",
help="Only process files matching PATTERN; only applies to --walk. "
"If specified, only files matching PATTERN will be processed as each directory is walked. "
"May be used for than once to specify multiple patterns. "
"For example, tag all *.pdf files in projectdir and subfolders with tag 'project': "
"osxmetadata --append tags 'project' --walk projectdir/ --pattern '*.pdf'",
multiple=True,
required=False,
)


@click.command(cls=MyClickCommand)
Expand All @@ -297,6 +310,7 @@ def get_help(self, ctx):
@VERBOSE_OPTION
@COPY_FROM_OPTION
@FILES_ONLY_OPTION
@PATTERN_OPTION
@click.pass_context
def cli(
ctx,
Expand All @@ -319,6 +333,7 @@ def cli(
verbose,
copyfrom,
files_only,
pattern,
):
""" Read/write metadata from file(s). """

Expand Down Expand Up @@ -403,30 +418,43 @@ def cli(

# loop through each file, process it, then do backup or restore if needed
for filename in files:
process_files(
ctx,
[filename],
json_,
set_,
append,
update,
remove,
clear,
get,
list_,
mirror,
wipe,
verbose,
copyfrom,
backup,
restore,
walk,
files_only,
)
if not all([os.path.isdir(filename), walk, pattern]):
process_files(
ctx,
[filename],
json_,
set_,
append,
update,
remove,
clear,
get,
list_,
mirror,
wipe,
verbose,
copyfrom,
backup,
restore,
walk,
files_only,
)

if walk and os.path.isdir(filename):
for root, dirnames, filenames in os.walk(filename):
filepaths = [os.path.join(root, fname) for fname in dirnames + filenames]
if pattern:
# only process files matching pattern
filepaths = []
for dirname in dirnames:
for matches in [
glob.glob(os.path.join(os.path.join(root, dirname), pat))
for pat in pattern
]:
filepaths.extend(matches)
else:
filepaths = [
os.path.join(root, fname) for fname in dirnames + filenames
]
process_files(
ctx,
filepaths,
Expand Down Expand Up @@ -481,7 +509,7 @@ def process_files(
if verbose:
click.echo(f"Skipping directory: {fpath}")
continue

if verbose:
click.echo(f"Processing file: {fpath}")

Expand Down
2 changes: 1 addition & 1 deletion osxmetadata/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.99.8"
__version__ = "0.99.9"
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,36 @@ def test_cli_walk_files_only(temp_dir):
assert not md.tags


def test_cli_walk_pattern(temp_dir):
""" test --walk with --pattern """
import os
import pathlib
from osxmetadata import OSXMetaData, Tag
from osxmetadata.__main__ import cli

dirname = pathlib.Path(temp_dir)
os.makedirs(dirname / "temp" / "subfolder1")
os.makedirs(dirname / "temp" / "subfolder2")
create_file(dirname / "temp" / "temp1.txt")
create_file(dirname / "temp" / "subfolder1" / "sub1.txt")
create_file(dirname / "temp" / "subfolder1" / "sub1.pdf")

runner = CliRunner()
result = runner.invoke(
cli, ["--set", "tags", "FOO", "--walk", "--pattern", "*.pdf", temp_dir]
)
assert result.exit_code == 0

md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.pdf")
assert md.tags == [Tag("FOO")]

md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
assert not md.tags

md = OSXMetaData(dirname / "temp" / "subfolder2")
assert not md.tags


def test_cli_files_only(temp_dir):
""" test --files-only without --walk """
import glob
Expand Down

0 comments on commit 522c218

Please sign in to comment.