1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2023 EfficiOS Inc.
5 # pyright: strict, reportTypeCommentUsage=false
11 from typing
import Any
, List
, Union
18 def __init__(self
, what
: str, line_no
: int, col_no
: int):
20 self
._line
_no
= line_no
36 class Error(RuntimeError):
37 def __init__(self
, causes
: List
[ErrorCause
]):
46 name
: str, base_dir
: str, content
: Union
[str, bytearray
], verbose
: bool
48 path
= os
.path
.join(base_dir
, name
)
51 print("Writing `{}`.".format(os
.path
.normpath(path
)))
53 os
.makedirs(os
.path
.normpath(os
.path
.dirname(path
)), exist_ok
=True)
55 with
open(path
, "w" if isinstance(content
, str) else "wb") as f
:
60 part
: moultipart
.Part
, init_vars
: normand
.VariablesT
, init_labels
: normand
.LabelsT
64 part
.content
, init_variables
=init_vars
, init_labels
=init_labels
66 except normand
.ParseError
as e
:
71 msg
.text_location
.line_no
+ part
.first_content_line_no
- 1,
72 msg
.text_location
.col_no
,
79 def _generate_from_part(
80 part
: moultipart
.Part
,
83 normand_vars
: normand
.VariablesT
,
84 normand_labels
: normand
.LabelsT
,
86 content
= part
.content
88 if part
.header_info
!= "metadata":
89 res
= _normand_parse(part
, normand_vars
, normand_labels
)
91 normand_vars
= res
.variables
92 normand_labels
= res
.labels
94 _write_file(part
.header_info
, base_dir
, content
, verbose
)
95 return normand_vars
, normand_labels
98 def generate(input_path
: str, base_dir
: str, verbose
: bool):
99 with
open(input_path
) as input_file
:
100 variables
= {} # type: normand.VariablesT
101 labels
= {} # type: normand.LabelsT
103 for part
in moultipart
.parse(input_file
):
104 variables
, labels
= _generate_from_part(
105 part
, base_dir
, verbose
, variables
, labels
109 def _parse_cli_args():
110 argparser
= argparse
.ArgumentParser()
111 argparser
.add_argument(
112 "input_path", metavar
="PATH", type=str, help="moultipart input file name"
114 argparser
.add_argument(
115 "--base-dir", type=str, help="base directory of generated files", default
=""
117 argparser
.add_argument(
118 "--verbose", "-v", action
="store_true", help="increase verbosity"
120 return argparser
.parse_args()
123 def _run_cli(args
: Any
):
125 typing
.cast(str, args
.input_path
),
126 typing
.cast(str, args
.base_dir
),
127 typing
.cast(bool, args
.verbose
),
132 args
= _parse_cli_args()
137 print("Failed to process Normand part:", file=sys
.stderr
)
139 for cause
in reversed(exc
.causes
):
141 " {}:{}:{} - {}{}".format(
142 os
.path
.abspath(args
.input_path
),
146 "." if cause
.what
[-1] not in ".:;" else "",
152 if __name__
== "__main__":
This page took 0.032682 seconds and 4 git commands to generate.