tir-csv 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tir_csv-0.1.0/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyrigh (c) 2026 OGAWA Keiji
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tir_csv-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: tir-csv
3
+ Version: 0.1.0
4
+ Summary: CSV/TSV <-> TIR converter backend for tirenvi
5
+ Author: OGAWA Keiji
6
+ License: MIT
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # tir-csv
13
+
14
+ CSV/TSV <-> TIR converter backend for tirenvi.
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ pip install tir-csv
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ tir-csv parse file.csv
@@ -0,0 +1,13 @@
1
+ # tir-csv
2
+
3
+ CSV/TSV <-> TIR converter backend for tirenvi.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install tir-csv
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ tir-csv parse file.csv
@@ -0,0 +1,16 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tir-csv"
7
+ version = "0.1.0"
8
+ description = "CSV/TSV <-> TIR converter backend for tirenvi"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "OGAWA Keiji" }]
13
+ dependencies = []
14
+
15
+ [project.scripts]
16
+ tir-csv = "tir_csv.cli:main"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,10 @@
1
+ import sys
2
+ from .parser import run
3
+
4
+
5
+ def main() -> int:
6
+ return run(sys.argv[1:])
7
+
8
+
9
+ if __name__ == "__main__":
10
+ sys.exit(main())
@@ -0,0 +1,50 @@
1
+ from contextlib import contextmanager
2
+ from typing import cast, Optional, TextIO, Iterator, ContextManager
3
+ import sys
4
+ import io
5
+
6
+
7
+ def _stream(
8
+ path: Optional[str],
9
+ *,
10
+ mode: str,
11
+ stdio,
12
+ newline: Optional[str],
13
+ ) -> ContextManager[TextIO]:
14
+ @contextmanager
15
+ def cm() -> Iterator[TextIO]:
16
+ if path is None or path == "-":
17
+ yield io.TextIOWrapper(
18
+ stdio.buffer,
19
+ encoding="utf-8",
20
+ newline=newline,
21
+ )
22
+ else:
23
+ with open(path, mode, encoding="utf-8", newline=newline) as f:
24
+ yield cast(TextIO, f)
25
+
26
+ return cm()
27
+
28
+
29
+ @contextmanager
30
+ def input_stream(path: Optional[str]) -> Iterator[TextIO]:
31
+ with _stream(path, mode="r", stdio=sys.stdin, newline=None) as f:
32
+ yield f
33
+
34
+
35
+ @contextmanager
36
+ def output_stream(path: Optional[str]) -> Iterator[TextIO]:
37
+ with _stream(path, mode="w", stdio=sys.stdout, newline=None) as f:
38
+ yield f
39
+
40
+
41
+ @contextmanager
42
+ def input_stream_csv(path: Optional[str]) -> Iterator[TextIO]:
43
+ with _stream(path, mode="r", stdio=sys.stdin, newline="") as f:
44
+ yield f
45
+
46
+
47
+ @contextmanager
48
+ def output_stream_csv(path: Optional[str]) -> Iterator[TextIO]:
49
+ with _stream(path, mode="w", stdio=sys.stdout, newline="") as f:
50
+ yield f
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import csv
4
+ import json
5
+ import sys
6
+ import os
7
+ from typing import Optional
8
+ from tir_csv.io_utils import input_stream_csv, output_stream_csv
9
+
10
+ __version__ = "0.1.0"
11
+ FORMAT_VERSION = "tir/0.1"
12
+
13
+ # ------------------------------------------------------------
14
+ # parse : CSV -> TIR (NDJSON)
15
+ # ------------------------------------------------------------
16
+
17
+
18
+ def parse(input_file_path: Optional[str], delimiter: str = ",") -> None:
19
+ with input_stream_csv(input_file_path) as input_stream:
20
+ if input_file_path is None or input_file_path == "-":
21
+ absolute_path = None
22
+ else:
23
+ absolute_path = os.path.abspath(input_file_path)
24
+ file_attr_record = {
25
+ "kind": "file_attr",
26
+ "version": FORMAT_VERSION,
27
+ "file_path": absolute_path,
28
+ }
29
+ print(json.dumps(file_attr_record, ensure_ascii=False))
30
+ csv_reader = csv.reader(input_stream, delimiter=delimiter)
31
+ for row in csv_reader:
32
+ grid_record = {
33
+ "kind": "grid",
34
+ "row": [
35
+ normalize_cell(cell) if cell is not None else "" for cell in row
36
+ ],
37
+ }
38
+ print(json.dumps(grid_record, ensure_ascii=False))
39
+
40
+
41
+ # ------------------------------------------------------------
42
+ # unparse : TIR (NDJSON) -> CSV
43
+ # ------------------------------------------------------------
44
+
45
+
46
+ def unparse(output_file_path: Optional[str], delimiter: str = ",") -> None:
47
+ with output_stream_csv(output_file_path) as output_stream:
48
+ csv_writer = csv.writer(output_stream, delimiter=delimiter)
49
+ for line in sys.stdin:
50
+ if not line.strip():
51
+ continue
52
+ record = json.loads(line)
53
+ record_kind = record.get("kind")
54
+ if record_kind in ("file_attr", "block_start"):
55
+ continue
56
+ if record_kind == "plain":
57
+ cell_value = record.get("line", "")
58
+ csv_writer.writerow([cell_value])
59
+ elif record_kind == "grid":
60
+ row = record.get("row", [])
61
+ csv_writer.writerow(row)
62
+
63
+
64
+ # ------------------------------------------------------------
65
+ # utilities
66
+ # ------------------------------------------------------------
67
+
68
+
69
+ def normalize_cell(cell: str) -> str:
70
+ return cell.replace("\r\n", "\n").replace("\r", "\n")
71
+
72
+
73
+ def usage() -> None:
74
+ print(
75
+ f"""tir-csv {__version__}
76
+
77
+ usage:
78
+ tir-csv parse [--delimiter DELIM] [file|-]
79
+ tir-csv unparse [--delimiter DELIM] [file|-]
80
+ tir-csv --version
81
+
82
+ Options:
83
+ --delimiter DELIM Field delimiter (default: ',')
84
+ Use '\\t' for TSV.
85
+
86
+ If file is omitted or '-', parse reads from stdin.
87
+ If file is omitted or '-', unparse writes to stdout.
88
+ """,
89
+ file=sys.stderr,
90
+ )
91
+
92
+
93
+ def parse_args(argv):
94
+ delimiter = ","
95
+ remaining_args = []
96
+
97
+ argument_iterator = iter(argv)
98
+
99
+ for argument in argument_iterator:
100
+ if argument == "--delimiter":
101
+ try:
102
+ delimiter = next(argument_iterator)
103
+ except StopIteration:
104
+ raise ValueError("--delimiter requires an argument")
105
+
106
+ if delimiter == r"\t":
107
+ delimiter = "\t"
108
+ else:
109
+ remaining_args.append(argument)
110
+
111
+ return delimiter, remaining_args
112
+
113
+
114
+ # ------------------------------------------------------------
115
+ # pip entry point
116
+ # ------------------------------------------------------------
117
+
118
+
119
+ def run(argv) -> int:
120
+ try:
121
+ delimiter, args = parse_args(argv)
122
+ except Exception as error:
123
+ print(str(error), file=sys.stderr)
124
+ usage()
125
+ return 1
126
+
127
+ if not args:
128
+ usage()
129
+ return 1
130
+
131
+ if args[0] == "--version":
132
+ print(__version__)
133
+ return 0
134
+
135
+ if len(args) not in (1, 2):
136
+ usage()
137
+ return 1
138
+
139
+ command = args[0]
140
+ file_argument = args[1] if len(args) == 2 else None
141
+
142
+ try:
143
+ if command == "parse":
144
+ parse(file_argument, delimiter=delimiter)
145
+ elif command == "unparse":
146
+ unparse(file_argument, delimiter=delimiter)
147
+ else:
148
+ print(f"unknown sub command: {command}", file=sys.stderr)
149
+ usage()
150
+ return 1
151
+
152
+ except Exception as error:
153
+ print(str(error), file=sys.stderr)
154
+ return 1
155
+
156
+ return 0
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: tir-csv
3
+ Version: 0.1.0
4
+ Summary: CSV/TSV <-> TIR converter backend for tirenvi
5
+ Author: OGAWA Keiji
6
+ License: MIT
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # tir-csv
13
+
14
+ CSV/TSV <-> TIR converter backend for tirenvi.
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ pip install tir-csv
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ tir-csv parse file.csv
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/tir_csv/__init__.py
5
+ src/tir_csv/cli.py
6
+ src/tir_csv/io_utils.py
7
+ src/tir_csv/parser.py
8
+ src/tir_csv.egg-info/PKG-INFO
9
+ src/tir_csv.egg-info/SOURCES.txt
10
+ src/tir_csv.egg-info/dependency_links.txt
11
+ src/tir_csv.egg-info/entry_points.txt
12
+ src/tir_csv.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ tir-csv = tir_csv.cli:main
@@ -0,0 +1 @@
1
+ tir_csv