Skip to content

Commit fa902cf

Browse files
author
a.pirogov
committed
start implementing pom.xml support, refactor out lots of code duplication
1 parent 1fd7ad0 commit fa902cf

14 files changed

Lines changed: 281 additions & 331 deletions

File tree

poetry.lock

Lines changed: 13 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ cffconvert = "^2.0.0"
4444
wrapt = "^1.15.0"
4545
packaging = "^23.1"
4646
jinja2 = "^3.1.2"
47+
defusedxml = "^0.7.1"
4748

4849
[tool.poetry.group.dev.dependencies]
4950
poethepoet = "^0.18.1"

src/somesy/cli/fill.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import typer
77
from jinja2 import Environment, FunctionLoader, select_autoescape
88

9-
from .util import resolved_somesy_input, wrap_exceptions
9+
from .util import (
10+
existing_file_arg_config,
11+
file_arg_config,
12+
resolved_somesy_input,
13+
wrap_exceptions,
14+
)
1015

1116
logger = logging.getLogger("somesy")
1217
app = typer.Typer()
@@ -19,37 +24,22 @@ def fill(
1924
None,
2025
"--template",
2126
"-t",
22-
exists=True,
23-
file_okay=True,
24-
dir_okay=False,
25-
writable=False,
26-
readable=True,
27-
resolve_path=False,
2827
help="Path to a Jinja2 template for somesy to fill (default: stdin).",
28+
**existing_file_arg_config,
2929
),
3030
input_file: Path = typer.Option(
3131
None,
3232
"--input-file",
3333
"-i",
34-
exists=True,
35-
file_okay=True,
36-
dir_okay=False,
37-
writable=True,
38-
readable=True,
39-
resolve_path=True,
4034
help="Path of somesy input file (default: try to infer).",
35+
**existing_file_arg_config,
4136
),
4237
output_file: Path = typer.Option(
4338
None,
4439
"--output-file",
4540
"-o",
46-
exists=False,
47-
file_okay=True,
48-
dir_okay=False,
49-
writable=True,
50-
readable=False,
51-
resolve_path=True,
5241
help="Path for target file (default: stdout).",
42+
**file_arg_config,
5343
),
5444
):
5545
"""Fill a Jinja2 template with somesy project metadata (e.g. list authors in project docs)."""

src/somesy/cli/init.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,56 @@ def config():
2323

2424
# prompt for inputs
2525
input_file = typer.prompt("Input file path", default=input_file_default)
26-
input_file = Path(input_file)
27-
options = {
28-
"input_file": input_file,
29-
"no_sync_cff": not typer.confirm(
30-
"Do you want to sync to a CFF file?", default=True
31-
),
32-
}
33-
cff_file = typer.prompt("CFF file path", default="CITATION.cff")
34-
if cff_file is not None or cff_file != "":
26+
options = dict(input_file=Path(input_file))
27+
28+
# ----
29+
30+
options["no_sync_cff"] = not typer.confirm(
31+
"Do you want to sync to a CFF file?", default=True
32+
)
33+
if cff_file := typer.prompt("CFF file path", default="CITATION.cff"):
3534
options["cff_file"] = cff_file
3635

36+
options["no_sync_codemeta"] = not typer.confirm(
37+
"Do you want to sync to a codemeta.json file?", default=True
38+
)
39+
if codemeta_file := typer.prompt(
40+
"codemeta.json file path", default="codemeta.json"
41+
):
42+
options["codemeta_file"] = codemeta_file
43+
3744
options["no_sync_pyproject"] = not typer.confirm(
3845
"Do you want to sync to a pyproject.toml file?", default=True
3946
)
40-
41-
pyproject_file = typer.prompt("pyproject.toml file path", default="pyproject.toml")
42-
if pyproject_file is not None or pyproject_file != "":
47+
if pyproject_file := typer.prompt(
48+
"pyproject.toml file path", default="pyproject.toml"
49+
):
4350
options["pyproject_file"] = pyproject_file
4451

4552
options["sync_package_json"] = typer.confirm(
4653
"Do you want to sync to a package.json file?", default=False
4754
)
48-
package_json_file = typer.prompt("package.json file path", default="package.json")
49-
if package_json_file is not None or package_json_file != "":
55+
if package_json_file := typer.prompt(
56+
"package.json file path", default="package.json"
57+
):
5058
options["package_json_file"] = package_json_file
5159

52-
options["no_sync_codemeta"] = not typer.confirm(
53-
"Do you want to sync to a codemeta.json file?", default=True
54-
)
55-
codemeta_file = typer.prompt("codemeta.json file path", default="codemeta.json")
56-
if codemeta_file is not None or codemeta_file != "":
57-
options["codemeta_file"] = codemeta_file
58-
5960
options["no_sync_julia"] = not typer.confirm(
6061
"Do you want to sync to a Project.toml(Julia) file?", default=True
6162
)
62-
julia_file = typer.prompt("Project.toml(Julia) file path", default="Project.toml")
63-
if julia_file is not None or julia_file != "":
63+
if julia_file := typer.prompt(
64+
"Project.toml (Julia) file path", default="Project.toml"
65+
):
6466
options["julia_file"] = julia_file
6567

68+
options["no_sync_pom_xml"] = not typer.confirm(
69+
"Do you want to sync to a pom.xml file?", default=True
70+
)
71+
if pom_xml_file := typer.prompt("pom.xml file path", default="pom.xml"):
72+
options["pom_xml_file"] = pom_xml_file
73+
74+
# ----
75+
6676
options["show_info"] = typer.confirm(
6777
"Do you want to show info about the sync process?"
6878
)

src/somesy/cli/sync.py

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
from somesy.commands import sync as sync_command
88
from somesy.core.models import SomesyInput
99

10-
from .util import resolved_somesy_input, wrap_exceptions
10+
from .util import (
11+
existing_file_arg_config,
12+
file_arg_config,
13+
resolved_somesy_input,
14+
wrap_exceptions,
15+
)
1116

1217
logger = logging.getLogger("somesy")
1318

@@ -21,13 +26,8 @@ def sync(
2126
None,
2227
"--input-file",
2328
"-i",
24-
exists=False,
25-
file_okay=True,
26-
dir_okay=False,
27-
writable=True,
28-
readable=True,
29-
resolve_path=True,
3029
help="Somesy input file path (default: .somesy.toml)",
30+
**file_arg_config,
3131
),
3232
no_sync_pyproject: bool = typer.Option(
3333
None,
@@ -39,13 +39,8 @@ def sync(
3939
None,
4040
"--pyproject-file",
4141
"-p",
42-
exists=True,
43-
file_okay=True,
44-
dir_okay=False,
45-
writable=True,
46-
readable=True,
47-
resolve_path=True,
4842
help="Existing pyproject.toml file path (default: pyproject.toml)",
43+
**existing_file_arg_config,
4944
),
5045
no_sync_package_json: bool = typer.Option(
5146
None,
@@ -57,13 +52,34 @@ def sync(
5752
None,
5853
"--package-json-file",
5954
"-j",
60-
exists=True,
61-
file_okay=True,
62-
dir_okay=False,
63-
writable=True,
64-
readable=True,
65-
resolve_path=True,
6655
help="Existing package.json file path (default: package.json)",
56+
**existing_file_arg_config,
57+
),
58+
no_sync_julia: bool = typer.Option(
59+
None,
60+
"--no-sync-julia",
61+
"-L",
62+
help="Do not sync Project.toml (Julia) file (default: False)",
63+
),
64+
julia_file: Path = typer.Option(
65+
None,
66+
"--julia-file",
67+
"-l",
68+
help="Custom Project.toml (Julia) file path (default: Project.toml)",
69+
**existing_file_arg_config,
70+
),
71+
no_sync_pom_xml: bool = typer.Option(
72+
None,
73+
"--no-sync-pomxml",
74+
"-X",
75+
help="Do not sync pom.xml (Java Maven) file (default: False)",
76+
),
77+
pom_xml_file: Path = typer.Option(
78+
None,
79+
"--pomxml-file",
80+
"-x",
81+
help="Custom pom.xml (Java Maven) file path (default: pom.xml)",
82+
**existing_file_arg_config,
6783
),
6884
no_sync_cff: bool = typer.Option(
6985
None,
@@ -75,49 +91,21 @@ def sync(
7591
None,
7692
"--cff-file",
7793
"-c",
78-
exists=False,
79-
file_okay=True,
80-
dir_okay=False,
81-
writable=True,
82-
readable=True,
83-
resolve_path=True,
8494
help="CITATION.cff file path (default: CITATION.cff)",
95+
**file_arg_config,
8596
),
8697
no_sync_codemeta: bool = typer.Option(
8798
None,
8899
"--no-sync-codemeta",
89100
"-M",
90-
help="Do not sync codemeta.json file",
101+
help="Do not sync codemeta.json file (default: False)",
91102
),
92103
codemeta_file: Path = typer.Option(
93104
None,
94105
"--codemeta-file",
95106
"-m",
96-
exists=False,
97-
file_okay=True,
98-
dir_okay=False,
99-
writable=True,
100-
readable=True,
101-
resolve_path=True,
102-
help="Custom codemeta.json file path",
103-
),
104-
no_sync_julia: bool = typer.Option(
105-
None,
106-
"--no-sync-julia",
107-
"-M",
108-
help="Do not sync Project.toml(Julia) file",
109-
),
110-
julia_file: Path = typer.Option(
111-
None,
112-
"--julia-file",
113-
"-m",
114-
exists=True,
115-
file_okay=True,
116-
dir_okay=False,
117-
writable=True,
118-
readable=True,
119-
resolve_path=True,
120-
help="Custom Project.toml(Julia) file path",
107+
help="Custom codemeta.json file path (default: codemeta.json)",
108+
**file_arg_config,
121109
),
122110
):
123111
"""Sync project metadata input with metadata files."""
@@ -133,6 +121,8 @@ def sync(
133121
codemeta_file=codemeta_file,
134122
no_sync_julia=no_sync_julia,
135123
julia_file=julia_file,
124+
no_sync_pom_xml=no_sync_pom_xml,
125+
pom_xml_file=pom_xml_file,
136126
)
137127
run_sync(somesy_input)
138128

@@ -150,6 +140,14 @@ def run_sync(somesy_input: SomesyInput):
150140
logger.info(
151141
f" - [italic]package.json[/italic]:\t[grey]{conf.package_json_file}[/grey]"
152142
)
143+
if not conf.no_sync_julia:
144+
logger.info(
145+
f" - [italic]Project.toml[/italic]:\t[grey]{conf.julia_file}[/grey]\n"
146+
)
147+
if not conf.no_sync_pom_xml:
148+
logger.info(
149+
f" - [italic]pom.xml[/italic]:\t[grey]{conf.pom_xml_file}[/grey]\n"
150+
)
153151
if not conf.no_sync_cff:
154152
logger.info(f" - [italic]CITATION.cff[/italic]:\t[grey]{conf.cff_file}[/grey]")
155153
if not conf.no_sync_codemeta:

src/somesy/cli/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
logger = logging.getLogger("somesy")
1515

1616

17+
# configuration dicts for CLI file arguments
18+
file_arg_config = dict(
19+
file_okay=True,
20+
dir_okay=False,
21+
writable=True,
22+
readable=True,
23+
resolve_path=True,
24+
)
25+
existing_file_arg_config = dict(file_arg_config)
26+
existing_file_arg_config.update(dict(exists=True))
27+
28+
1729
@wrapt.decorator
1830
def wrap_exceptions(wrapped, instance, args, kwargs):
1931
"""Format and log exceptions for cli commands."""

src/somesy/codemeta/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Integration with codemeta.json (to re-generate codemeta as part of somesy sync)."""
2-
from .writer import Codemeta
2+
from .writer import CodeMeta
33

4-
__all__ = ["Codemeta"]
4+
__all__ = ["CodeMeta"]

src/somesy/codemeta/writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
logger = logging.getLogger("somesy")
1414

1515

16-
class Codemeta(ProjectMetadataWriter):
16+
class CodeMeta(ProjectMetadataWriter):
1717
"""Codemeta.json parser and saver."""
1818

1919
def __init__(
@@ -71,7 +71,7 @@ def _validate(self) -> None:
7171
config = dict(self._get_property([]))
7272

7373
logger.debug(
74-
f"No validation for codemeta.json files {Codemeta.__name__}: {pretty_repr(config)}"
74+
f"No validation for codemeta.json files {CodeMeta.__name__}: {pretty_repr(config)}"
7575
)
7676

7777
def _init_new_file(self) -> None:

0 commit comments

Comments
 (0)