Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 3b9e15d

Browse files
committed
update
1 parent 3e31b9c commit 3b9e15d

File tree

6 files changed

+204
-1
lines changed

6 files changed

+204
-1
lines changed

ecml_tools/__main__.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
# (C) Copyright 2024 ECMWF.
3+
#
4+
# This software is licensed under the terms of the Apache Licence Version 2.0
5+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
# In applying this licence, ECMWF does not waive the privileges and immunities
7+
# granted to it by virtue of its status as an intergovernmental organisation
8+
# nor does it submit to any jurisdiction.
9+
#
10+
11+
12+
import argparse
13+
import logging
14+
import sys
15+
import traceback
16+
17+
from . import __version__
18+
from .commands import COMMANDS
19+
20+
LOG = logging.getLogger(__name__)
21+
22+
23+
def main():
24+
parser = argparse.ArgumentParser(
25+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
26+
)
27+
28+
parser.add_argument(
29+
"--version",
30+
"-V",
31+
action="store_true",
32+
help="show the version and exit",
33+
)
34+
parser.add_argument(
35+
"--debug",
36+
"-d",
37+
action="store_true",
38+
help="Debug mode",
39+
)
40+
parser.add_argument(
41+
"--force",
42+
"-f",
43+
action="store_true",
44+
help="Don't ask for confirmation",
45+
)
46+
subparsers = parser.add_subparsers(help="commands:", dest="command")
47+
for name, command in COMMANDS.items():
48+
command_parser = subparsers.add_parser(name, help=command.__doc__)
49+
command.add_arguments(command_parser)
50+
51+
args = parser.parse_args()
52+
53+
if args.version:
54+
print(__version__)
55+
return
56+
57+
if args.command is None:
58+
parser.print_help()
59+
return
60+
61+
cmd = COMMANDS[args.command]
62+
63+
logging.basicConfig(
64+
format="%(asctime)s %(levelname)s %(message)s",
65+
datefmt="%Y-%m-%d %H:%M:%S",
66+
level=logging.DEBUG if args.debug else logging.INFO,
67+
)
68+
69+
try:
70+
cmd.run(args)
71+
except ValueError as e:
72+
traceback.print_exc()
73+
LOG.error("\n💣 %s", str(e).lstrip())
74+
LOG.error("💣 Exiting")
75+
sys.exit(1)
76+
77+
78+
if __name__ == "__main__":
79+
main()

ecml_tools/commands/__init__.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
# (C) Copyright 2024 ECMWF.
3+
#
4+
# This software is licensed under the terms of the Apache Licence Version 2.0
5+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
# In applying this licence, ECMWF does not waive the privileges and immunities
7+
# granted to it by virtue of its status as an intergovernmental organisation
8+
# nor does it submit to any jurisdiction.
9+
#
10+
11+
import argparse
12+
import importlib
13+
import logging
14+
import os
15+
import sys
16+
17+
LOG = logging.getLogger(__name__)
18+
19+
20+
def register(here, package, select, fail=None):
21+
result = {}
22+
not_available = {}
23+
24+
for p in os.listdir(here):
25+
full = os.path.join(here, p)
26+
if p.startswith("_"):
27+
continue
28+
if not (
29+
p.endswith(".py")
30+
or (
31+
os.path.isdir(full)
32+
and os.path.exists(os.path.join(full, "__init__.py"))
33+
)
34+
):
35+
continue
36+
37+
name, _ = os.path.splitext(p)
38+
39+
try:
40+
imported = importlib.import_module(
41+
f".{name}",
42+
package=package,
43+
)
44+
except ImportError as e:
45+
not_available[name] = e
46+
continue
47+
48+
obj = select(imported)
49+
if obj is not None:
50+
result[name] = obj
51+
52+
for name, e in not_available.items():
53+
if fail is None:
54+
pass
55+
if callable(fail):
56+
result[name] = fail(name, e)
57+
58+
return result
59+
60+
61+
class Command:
62+
def run(self, args):
63+
raise NotImplementedError(f"Command not implemented: {args.command}")
64+
65+
66+
class Failed(Command):
67+
def __init__(self, name, error):
68+
self.name = name
69+
self.error = error
70+
71+
def add_arguments(self, command_parser):
72+
command_parser.add_argument("x", nargs=argparse.REMAINDER)
73+
74+
def run(self, args):
75+
print(f"Command '{self.name}' not available: {self.error}")
76+
sys.exit(1)
77+
78+
79+
COMMANDS = register(
80+
os.path.dirname(__file__),
81+
__name__,
82+
lambda x: x.command(),
83+
lambda name, error: Failed(name, error),
84+
)

ecml_tools/commands/create.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from ecml_tools.create import Creator
2+
3+
from . import Command
4+
5+
6+
class Create(Command):
7+
internal = True
8+
timestamp = True
9+
10+
def add_arguments(self, command_parser):
11+
command_parser.add_argument(
12+
"--overwrite", action="store_true", help="Overwrite existing files"
13+
)
14+
command_parser.add_argument("config", help="Configuration file")
15+
command_parser.add_argument("path", help="Path to store the created data")
16+
17+
def run(self, args):
18+
kwargs = vars(args)
19+
20+
c = Creator(**kwargs)
21+
c.create()
22+
23+
24+
command = Create
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (C) Copyright 2024 ECMWF.
2+
#
3+
# This software is licensed under the terms of the Apache Licence Version 2.0
4+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5+
# In applying this licence, ECMWF does not waive the privileges and immunities
6+
# granted to it by virtue of its status as an intergovernmental organisation
7+
# nor does it submit to any jurisdiction.
8+
#
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (C) Copyright 2024 ECMWF.
2+
#
3+
# This software is licensed under the terms of the Apache Licence Version 2.0
4+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5+
# In applying this licence, ECMWF does not waive the privileges and immunities
6+
# granted to it by virtue of its status as an intergovernmental organisation
7+
# nor does it submit to any jurisdiction.
8+
#

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def read(fname):
8181
},
8282
zip_safe=True,
8383
keywords="tool",
84-
entry_points={},
8584
classifiers=[
8685
"Development Status :: 3 - Alpha",
8786
"Intended Audience :: Developers",
@@ -94,4 +93,5 @@ def read(fname):
9493
"Programming Language :: Python :: Implementation :: PyPy",
9594
"Operating System :: OS Independent",
9695
],
96+
entry_points={"console_scripts": ["anemoi-datasets=ecml_tools.__main__:main"]},
9797
)

0 commit comments

Comments
 (0)