Skip to content

Commit 83652d9

Browse files
committed
Pull main function out to __main__
1 parent 845d0e8 commit 83652d9

File tree

3 files changed

+104
-97
lines changed

3 files changed

+104
-97
lines changed

filetagging/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"""
2020

2121
from .filetagging import (
22-
main,
2322
ls_tags,
2423
filter_tags,
2524
add_tag,
@@ -30,7 +29,6 @@
3029
)
3130

3231
__all__ = [
33-
"main",
3432
"ls_tags",
3533
"filter_tags",
3634
"add_tag",

filetagging/__main__.py

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,108 @@
1414
#
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
"""
18+
filetagging script entry point.
19+
"""
1720

1821
import sys
19-
import filetagging
22+
from functools import partial
23+
from inspect import cleandoc
24+
25+
from .filetagging import ls_tags, filter_tags, add_tag, rm_tag, __version__
26+
27+
28+
def print_help(long: bool=False) -> None:
29+
"""Print program help information.
30+
31+
Keyword arguments:
32+
long -- whether to print full usage details (default False)
33+
"""
34+
print(f"Usage: {sys.argv[0]} [OPTIONS] COMMAND")
35+
if long:
36+
print("")
37+
print(cleandoc(
38+
"""
39+
COMMANDS
40+
ls <file>
41+
| List the tags associated with a file.
42+
43+
filter <tag>
44+
| List files tagged with a tag.
45+
46+
add <tag> <file>
47+
| Add a tag to a file.
48+
49+
rm <tag> <file>
50+
| Remove a tag from a file.
51+
"""))
52+
53+
54+
def print_version() -> None:
55+
"""Print program version information."""
56+
print(cleandoc(
57+
f"""Tag {__version__}
58+
Copyright (C) 2022 Trevor Last
59+
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
60+
This is free software: you are free to change and redistribute it.
61+
There is NO WARRANTY, to the extent permitted by law.
62+
"""))
63+
64+
65+
def handle_argv(argv: list[str]) -> list[partial]:
66+
"""Handle argv and return a list of supplied commands, if any."""
67+
COMMANDS = {
68+
"ls":ls_tags,
69+
"filter":filter_tags,
70+
"add":add_tag,
71+
"rm":rm_tag,
72+
}
73+
74+
commands = []
75+
76+
cmd: partial = None
77+
arg_count = -1
78+
79+
for arg in argv:
80+
if cmd is not None:
81+
if len(cmd.args) < arg_count:
82+
cmd = partial(cmd, arg)
83+
elif len(cmd.args) == arg_count:
84+
commands.append(cmd)
85+
cmd = None
86+
arg_count = -1
87+
else:
88+
match arg:
89+
case "--help":
90+
print_help(long=True)
91+
sys.exit()
92+
93+
case "--version":
94+
print_version()
95+
sys.exit()
96+
97+
case unrecognized:
98+
if unrecognized in COMMANDS:
99+
c = COMMANDS[unrecognized]
100+
cmd = partial(c)
101+
arg_count = c.__code__.co_argcount
102+
else:
103+
raise Exception(f"Unrecognized option '{unrecognized}'")
104+
105+
if cmd is not None:
106+
commands.append(cmd)
107+
return commands
108+
109+
110+
def main(argv: list[str]) -> int:
111+
"""Package main."""
112+
if not argv:
113+
print_help(long=False)
114+
sys.exit()
115+
for command in handle_argv(argv):
116+
command()
117+
return 0
118+
20119

21120
if __name__ == "__main__":
22-
filetagging.main(sys.argv[1:])
121+
sys.exit(main(sys.argv[1:]))

filetagging/filetagging.py

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
__version__ = "0.1.0"
1919

2020
import json
21-
import sys
22-
from functools import partial
23-
from inspect import cleandoc
2421
from os import getcwd
2522
from pathlib import PurePath
2623

@@ -82,50 +79,14 @@ def open_tags(filepath: str) -> TagsFile:
8279
return TagsFile(PurePath(filepath).parent)
8380

8481

85-
86-
def print_help(long=False) -> None:
87-
"""Print program help information.
88-
89-
Keyword arguments:
90-
long -- whether to print full usage details (default False)
91-
"""
92-
print(f"Usage: {sys.argv[0]} [OPTIONS] COMMAND")
93-
if long:
94-
print("")
95-
print(cleandoc(
96-
"""
97-
COMMANDS
98-
ls <file>
99-
| List the tags associated with a file.
100-
101-
filter <tag>
102-
| List files tagged with a tag.
103-
104-
add <tag> <file>
105-
| Add a tag to a file.
106-
107-
rm <tag> <file>
108-
| Remove a tag from a file.
109-
"""))
110-
111-
def print_version() -> None:
112-
"""Print program version information."""
113-
print(cleandoc(
114-
f"""Tag {__version__}
115-
Copyright (C) 2022 Trevor Last
116-
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
117-
This is free software: you are free to change and redistribute it.
118-
There is NO WARRANTY, to the extent permitted by law.
119-
"""))
120-
121-
12282
def ls_tags(filepath: str) -> None:
12383
"""Print the tags attached to the file."""
12484
with open_tags(filepath) as tags:
12585
k = PurePath(filepath).name
12686
for tag in tags.get_tags(k):
12787
print(tag)
12888

89+
12990
def filter_tags(tag: str) -> None:
13091
"""Print all files that match the tag."""
13192
with open_tags(getcwd()) as tags:
@@ -137,67 +98,16 @@ def filter_tags(tag: str) -> None:
13798
for match in matches:
13899
print(match)
139100

101+
140102
def add_tag(tag: str, filepath: str) -> None:
141103
"""Add a tag to the file."""
142104
with open_tags(filepath) as tags:
143105
key = PurePath(filepath).name
144106
tags.add_tag(key, tag)
145107

108+
146109
def rm_tag(tag: str, filepath: str) -> None:
147110
"""Remove a tag from the file."""
148111
with open_tags(filepath) as tags:
149112
key = PurePath(filepath).name
150113
tags.remove_tag(key, tag)
151-
152-
153-
def handle_argv(argv: list[str]) -> list:
154-
"""Handle argv and return a list of supplied commands, if any."""
155-
COMMANDS = {
156-
"ls":ls_tags,
157-
"filter":filter_tags,
158-
"add":add_tag,
159-
"rm":rm_tag,
160-
}
161-
162-
commands = []
163-
164-
cmd: partial = None
165-
arg_count = -1
166-
167-
for arg in argv:
168-
if cmd is not None:
169-
if len(cmd.args) < arg_count:
170-
cmd = partial(cmd, arg)
171-
elif len(cmd.args) == arg_count:
172-
commands.append(cmd)
173-
cmd = None
174-
arg_count = -1
175-
else:
176-
match arg:
177-
case "--help":
178-
print_help(long=True)
179-
sys.exit()
180-
181-
case "--version":
182-
print_version()
183-
sys.exit()
184-
185-
case unrecognized:
186-
if unrecognized in COMMANDS:
187-
c = COMMANDS[unrecognized]
188-
cmd = partial(c)
189-
arg_count = c.__code__.co_argcount
190-
else:
191-
raise Exception(f"Unrecognized option '{unrecognized}'")
192-
193-
if cmd is not None:
194-
commands.append(cmd)
195-
return commands
196-
197-
def main(args: list[str]):
198-
if not args:
199-
print_help(long=False)
200-
sys.exit()
201-
commands = handle_argv(args)
202-
for command in commands:
203-
command()

0 commit comments

Comments
 (0)