Skip to content

Commit bdaa222

Browse files
authored
Allow spin --version without configuration present (#173)
xref: conda-forge/spin-feedstock#6 (comment) xref: conda-forge/spin-feedstock#6 (comment)
2 parents 22e70de + a197bba commit bdaa222

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

spin/__main__.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ def main():
2525
if (len(sys.argv) == 2) and (sys.argv[1] == "help"):
2626
sys.argv[1] = "--help"
2727

28-
def error(message):
29-
print(f"Error: {message}", file=sys.stderr)
30-
sys.exit(1)
31-
3228
def load_toml(filename):
3329
if not os.path.exists(filename):
3430
return None
3531
with open(filename, "rb") as f:
3632
try:
3733
return tomllib.load(f)
3834
except tomllib.TOMLDecodeError:
39-
error("cannot parse [{filename}]")
35+
print(f"Error: cannot parse [{filename}]", file=sys.stderr)
4036

4137
toml_config = collections.ChainMap()
4238
toml_config.maps.extend(
@@ -50,18 +46,28 @@ def load_toml(filename):
5046
)
5147

5248
# Basic configuration validation
53-
if "tool.spin" not in toml_config:
54-
error(
55-
"needs valid configuration in [.spin.toml], [spin.toml] or [pyproject.toml]"
56-
)
57-
if "tool.spin.commands" not in toml_config:
58-
error("configuration is missing section [tool.spin.commands]")
49+
version_query = len(sys.argv) == 2 and (sys.argv[1] == "--version")
50+
51+
spin_config = {}
52+
if not version_query:
53+
if "tool.spin" in toml_config:
54+
spin_config = toml_config["tool.spin"]
55+
if "tool.spin.commands" not in toml_config:
56+
print(
57+
"Error: configuration is missing section [tool.spin.commands]\n",
58+
file=sys.stderr,
59+
)
60+
else:
61+
print(
62+
"Error: need valid configuration in [.spin.toml], [spin.toml], or [pyproject.toml]\n"
63+
"See https://github.com/scientific-python/spin/blob/main/README.md\n",
64+
file=sys.stderr,
65+
)
5966

60-
spin_config = toml_config["tool.spin"]
6167
proj_name = (
6268
toml_config.get("project.name")
6369
or spin_config.get("package")
64-
or "unknown project"
70+
or "[unknown project]"
6571
)
6672

6773
@click.group(help=f"Developer tool for {proj_name}", cls=SectionedHelpGroup)
@@ -72,7 +78,7 @@ def group(ctx):
7278
ctx.meta["commands"] = ctx.command.section_commands
7379
ctx.show_default = True
7480

75-
config_cmds = spin_config["commands"]
81+
config_cmds = spin_config.get("commands", [])
7682
# Commands can be provided as a list, or as a dictionary
7783
# so that they can be sorted into sections
7884
if isinstance(config_cmds, list):
@@ -134,7 +140,8 @@ def group(ctx):
134140
try:
135141
group()
136142
except Exception as e:
137-
error(f"{e}; aborting.")
143+
print(f"Error: {e}; aborting.", file=sys.stderr)
144+
sys.exit(1)
138145

139146

140147
if __name__ == "__main__":

0 commit comments

Comments
 (0)