Skip to content

Commit ec255bf

Browse files
committed
feat(aiv): add stdin stdout for json conversion
1 parent fc86b66 commit ec255bf

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

sourcehold/tool/convert/aiv/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pathlib
1+
import pathlib, sys
22

33
from sourcehold.tool.convert.aiv.exports import to_json
44
from sourcehold.tool.convert.aiv.imports import from_json
@@ -13,7 +13,7 @@ def convert_aiv(args):
1313
return None
1414

1515
inp = args.input
16-
if not pathlib.Path(inp).exists():
16+
if inp != '-' and not pathlib.Path(inp).exists():
1717
raise Exception(f"file does not exist: {inp}")
1818
inp_invert_y = False
1919
inp_format = args.from_format
@@ -45,7 +45,10 @@ def convert_aiv(args):
4545
if out_format == "json":
4646
args.output = "-"
4747
elif out_format == "aiv":
48-
args.output = f"{pathlib.Path(inp).name}.aiv"
48+
if inp == "-":
49+
args.output = "output.aiv"
50+
else:
51+
args.output = f"{pathlib.Path(inp).name}.aiv"
4952
#args.output = f"{pathlib.Path(inp).name}.json"
5053

5154
if args.debug:
@@ -55,13 +58,17 @@ def convert_aiv(args):
5558

5659
conv = to_json(path = inp, include_extra=args.extra, report=args.debug, invert_y=out_invert_y, skip_keep=out_skip_keep)
5760
if args.output == "-":
58-
print(conv)
61+
sys.stdout.write(conv)
62+
sys.stdout.flush()
5963
else:
6064
pathlib.Path(args.output).write_text(conv)
6165
elif inp_format.startswith('json') and out_format.startswith("aiv"):
62-
conv = from_json(path = inp, report=args.debug, invert_y=inp_invert_y)
66+
if inp == "-":
67+
conv = from_json(f = sys.stdin, report=args.debug, invert_y=inp_invert_y)
68+
else:
69+
conv = from_json(path = inp, report=args.debug, invert_y=inp_invert_y)
6370
conv.to_file(args.output)
6471
else:
65-
raise NotImplementedError(f"combination of in-format '{inp_format}' and out-format '{out_format}' not implemented")
72+
raise NotImplementedError(f"combination of from-format '{inp_format}' and to-format '{out_format}' not implemented")
6673

6774
return True

sourcehold/tool/convert/aiv/exports.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121

2222

2323

24-
def to_json(aiv=None, path: str='', include_extra=False, invert_y=True, skip_keep=False, report=False):
25-
if aiv == None and not path:
24+
def to_json(aiv=None, path: str='', f=None, include_extra=False, invert_y=True, skip_keep=False, report=False):
25+
if not aiv and not path and not f:
2626
raise Exception()
2727
if aiv == None:
28-
aiv = AIV().from_file(path)
29-
28+
if path:
29+
aiv = AIV().from_file(path)
30+
else:
31+
aiv = AIV().from_buffer(f) # type: ignore
32+
3033
if report:
3134
print(f"INFO: aiv has version: {aiv.directory.version_number}")
3235

sourcehold/tool/convert/aiv/imports.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ def convert_offsets(offsets, invert_y=False):
2828
for loc in offsets:
2929
yield convert_offset(loc, invert_y=invert_y)
3030

31-
def from_json(path = None, data: Dict | None = None, invert_y = True, report = False):
32-
if not path and not data:
31+
def from_json(path: str | None = None, data: Dict | None = None, f=None, invert_y = True, report = False):
32+
if not path and not data and not f:
3333
raise Exception()
3434

35-
if path and not data:
36-
with open(path, 'rb') as f:
37-
data = json.load(f)
35+
if not data:
36+
if path:
37+
with open(path, 'rb') as f:
38+
data = json.load(f)
39+
elif f:
40+
data = json.load(f)
3841

3942
if data == None:
4043
raise Exception()

tests/aiv/test_aiv_to_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
from sourcehold.aivs.AIV import AIV
6-
from sourcehold.aivs.conversion import to_json
6+
from sourcehold.tool.convert.aiv.exports import to_json
77

88
BASEPATH = pathlib.Path("C:/Program Files (x86)/Steam/steamapps/common/Stronghold Crusader Extreme/aiv")
99

0 commit comments

Comments
 (0)