This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__main__.py
101 lines (82 loc) · 3.62 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import argparse
from pathlib import Path
from typing import Optional, Union
from io import BytesIO
from rsi import (
Rsi,
HyphenSplitter,
NumberSplitter,
SimpleSplitter,
UnderscoreSplitter,
)
def main() -> int:
"""
Entry point for the command line rsi utility script.
"""
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest="command")
_from_dmi = subparser.add_parser("from_dmi", help="Will create an RSI from a BYOND DMI file.")
_from_dmi.add_argument("input", help="The DMI file to read from.")
_from_dmi.add_argument("output", help="The RSI to output to.", type=Path)
_from_dmi.add_argument("-c", "--copyright", help="Specifies the copyright of the new RSI file.")
_from_dmi.add_argument("-i", "--indents", help="Indents to use for the meta.json file.", type=int)
_from_dmi.add_argument("-l", "--license", help="Specifies the license of the new RSI file.")
_from_dmi.add_argument("-s", "--splitter", help="Splitter to use for the rsi if applicable.")
_new_rsi = subparser.add_parser("new", help="Will create a new RSI at the provided directory.")
_new_rsi.add_argument("rsi", help="The location of the new RSI. Must not exist yet.", type=Path)
_new_rsi.add_argument("dimensions", help="The dimensions of the new rsi, in <width>x<height> format, like so: 32x32.")
_new_rsi.add_argument("-c", "--copyright", action="store", help="Copyright info for this RSI file such as author.", nargs="?")
_new_rsi.add_argument("-l", "--license", action="store", help="The license of this RSI file, as valid SPDX License Identifier (Google it).", nargs="?")
_new_rsi.add_argument("--dont-make-parent-dirs", action="store_true", help="Do not create parent directories if they do not exist, instead throw an error.", dest="no_parents")
args = parser.parse_args()
if args.command == "from_dmi":
from_dmi(args.input, args.output, args.license, args.copyright, args.indents, args.splitter)
return 0
if args.command == "new":
return new_rsi(args.rsi, args.dimensions, args.copyright, args.license, not args.no_parents)
print("No command specified!")
return 1
def from_dmi(inputf: Union[BytesIO, Path, str],
output: Path,
new_license: Optional[str],
new_copyright: Optional[str],
indents: Optional[int] = None,
splitter: Optional[str] = "",) -> None:
rsi = Rsi.from_dmi(inputf)
if new_license:
rsi.license = new_license
if new_copyright:
rsi.copyright = new_copyright
splitter_class = {
"hyphen": HyphenSplitter,
"number": NumberSplitter,
"simple": SimpleSplitter,
"underscore": UnderscoreSplitter,
}.get(splitter, None) # type: ignore
if splitter_class is None:
rsi.write(output)
else:
splitter_class(rsi).split_to(output, indents)
def new_rsi(loc: Path,
dimensions: str,
rsi_copyright: Optional[str],
rsi_license: Optional[str],
make_parents: bool) -> int:
try:
dimsplit = dimensions.split("x")
if len(dimsplit) != 2:
print("Incorrect amount of dimensions passed, expected exactly 2.")
return 1
x = int(dimsplit[0])
y = int(dimsplit[1])
except ValueError:
print("Invalid dimensions passed.")
return 1
if not loc.parent.exists() and not make_parents:
print("Parent directories do not exist. Aborting.")
return 1
rsi = Rsi((x, y))
rsi.license = rsi_license
rsi.copyright = rsi_copyright
rsi.write(loc, make_parents)
return 0