-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
example.csv csv json example.json | ||
example.csv csv jsonc example.jsonc | ||
example.csv json jsonc example.jsonc | ||
example.csv jsonc json example.json | ||
example.json jsonc json example.json | ||
example.jsonc json json example.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
from typing import Dict, List | ||
|
||
from jsonmaestro.logger import infof | ||
from jsonmaestro.loader import Loader | ||
from jsonmaestro.converter import Converter | ||
|
||
|
||
def read_input() -> List[Dict[str, str]]: | ||
""" | ||
Reads input from stdin and returns a list of dictionaries. | ||
Each dictionary represents a line in the input file. | ||
The dictionary has three keys: "file", "format_in", and "format_out". | ||
"file_in" is the path to the file to be converted. | ||
"format_in" is the format of the file to be converted. | ||
"format_out" is the format to convert the file to. | ||
"file_out" is the path to the output file. | ||
""" | ||
input_data: List[Dict[str, str]] = [] | ||
|
||
for line in sys.stdin: | ||
line = line.strip() | ||
l: Dict[str, str] = {} | ||
parts = line.split(" ") | ||
l["file_in"] = parts[0] | ||
l["format_in"] = parts[1] | ||
l["format_out"] = parts[2] | ||
l["file_out"] = parts[3] | ||
input_data.append(l) | ||
|
||
return input_data | ||
|
||
|
||
def main() -> None: | ||
input_data: List[Dict[str, str]] = read_input() | ||
|
||
for conversion in input_data: | ||
infof("Converting {} from {} to {}", conversion['file_in'], | ||
conversion['format_in'], conversion['format_out']) | ||
|
||
loader = Loader(conversion['file_in']) | ||
|
||
_converter = Converter(file_path=None, | ||
str_data=None, | ||
data=loader.load_as(conversion['format_in']), | ||
source_format=conversion['format_in'], | ||
target_format=conversion['format_out']) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import sys | ||
from io import StringIO | ||
from typing import Dict, List | ||
from jsonmaestro.apps.converter import read_input, main | ||
|
||
|
||
def test_read_input(): | ||
original_stdin = sys.stdin | ||
test_input = "example.csv csv json example.json\n" | ||
sys.stdin = StringIO(test_input) | ||
|
||
data_out = read_input() | ||
|
||
assert len(data_out) == 1 | ||
assert data_out[0]["file_in"] == "example.csv" | ||
assert data_out[0]["format_in"] == "csv" | ||
assert data_out[0]["format_out"] == "json" | ||
assert data_out[0]["file_out"] == "example.json" | ||
|
||
sys.stdin = original_stdin | ||
|
||
|
||
def test_read_input_from_file(): | ||
original_stdin = sys.stdin | ||
with open("data/test_stdin.txt", "r") as file: | ||
test_input = file.read() | ||
|
||
sys.stdin = StringIO(test_input) | ||
data_out = read_input() | ||
|
||
assert True | ||
|
||
with open("data/test_stdin.txt", "r") as file: | ||
input = file.readlines() | ||
|
||
assert len(data_out) == len(input) | ||
|
||
in_data: List[Dict[str, str]] = [] | ||
|
||
for line in input: | ||
line = line.strip() | ||
l: Dict[str, str] = {} | ||
parts = line.split(" ") | ||
l["file_in"] = parts[0] | ||
l["format_in"] = parts[1] | ||
l["format_out"] = parts[2] | ||
l["file_out"] = parts[3] | ||
in_data.append(l) | ||
|
||
for i in range(len(data_out)): | ||
assert data_out[i]["file_in"] == in_data[i]["file_in"] | ||
assert data_out[i]["format_in"] == in_data[i]["format_in"] | ||
assert data_out[i]["format_out"] == in_data[i]["format_out"] | ||
assert data_out[i]["file_out"] == in_data[i]["file_out"] | ||
|
||
sys.stdin = original_stdin | ||
|
||
|
||
def test_main(): | ||
original_stdin = sys.stdin | ||
test_input = "data/example.csv csv json data/example.json\n" | ||
sys.stdin = StringIO(test_input) | ||
|
||
main() | ||
|
||
assert True | ||
|
||
sys.stdin = original_stdin |