Skip to content

Commit c488bf4

Browse files
authored
Update __init__.py
1 parent d23ec4d commit c488bf4

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

atomdb/__init__.py

+60-28
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,75 @@
1313
# You should have received a copy of the GNU General Public License
1414
# along with AtomDB. If not, see <http://www.gnu.org/licenses/>.
1515

16-
r"""AtomDB, a database of atomic and ionic properties."""
16+
"""AtomDB console script.
1717
18-
import importlib
19-
from importlib.metadata import PackageNotFoundError
18+
This script provides a command-line interface to compile or query entries in the AtomDB database.
19+
"""
2020

21-
from atomdb.periodic import Element
21+
import sys
22+
from argparse import ArgumentParser, ArgumentTypeError
23+
from atomdb import compile_species, load
2224

23-
from atomdb.species import Species
2425

25-
from atomdb.promolecule import Promolecule
26+
def positive_int(value):
27+
"""Validate if the argument is a positive integer."""
28+
try:
29+
ivalue = int(value)
30+
if ivalue <= 0:
31+
raise ArgumentTypeError(f"{value} is not a positive integer.")
32+
return ivalue
33+
except ValueError as e:
34+
raise ArgumentTypeError(f"Invalid integer value: {value}") from e
2635

27-
from atomdb.periodic import element_number, element_symbol, element_name
2836

29-
from atomdb.species import compile_species, load, dump, raw_datafile
37+
def main():
38+
"""Main function for the AtomDB CLI."""
39+
parser = ArgumentParser(
40+
prog="atomdb",
41+
description="Command-line tool to compile or query an AtomDB entry.",
42+
)
3043

31-
from atomdb.promolecule import make_promolecule
44+
# Define mutually exclusive commands
45+
command_group = parser.add_mutually_exclusive_group(required=True)
46+
command_group.add_argument(
47+
"-c", "--compile_species", action="store_true", help="Compile a species into the database."
48+
)
49+
command_group.add_argument(
50+
"-q", "--query", action="store_true", help="Query a species from the database."
51+
)
3252

53+
# Add arguments
54+
parser.add_argument("dataset", type=str, help="Name of the dataset.")
55+
parser.add_argument("elem", type=str, help="Element symbol (e.g., H, He, Li).")
56+
parser.add_argument("charge", type=positive_int, help="Charge of the species (positive integer).")
57+
parser.add_argument("mult", type=positive_int, help="Multiplicity of the species (positive integer).")
58+
parser.add_argument(
59+
"-e", "--exc", type=int, default=0, help="Excitation level (default: 0). Must be non-negative."
60+
)
3361

34-
__all__ = [
35-
"Element",
36-
"Species",
37-
"Promolecule",
38-
"element_number",
39-
"element_symbol",
40-
"element_name",
41-
"compile_species",
42-
"load",
43-
"dump",
44-
"raw_datafile",
45-
"make_promolecule",
46-
]
62+
# Parse arguments
63+
args = parser.parse_args()
4764

65+
# Input validation
66+
if args.exc < 0:
67+
print("Error: Excitation level (-e) must be a non-negative integer.", file=sys.stderr)
68+
sys.exit(1)
4869

49-
r"""AtomDB version string."""
70+
try:
71+
# Handle commands
72+
if args.compile_species:
73+
print(f"Compiling species: {args.elem}, Charge: {args.charge}, Multiplicity: {args.mult}, Excitation: {args.exc}")
74+
compile_species(args.elem, args.charge, args.mult, args.exc, args.dataset)
75+
print("Compilation successful.")
76+
elif args.query:
77+
print(f"Querying species: {args.elem}, Charge: {args.charge}, Multiplicity: {args.mult}, Excitation: {args.exc}")
78+
species = load(args.elem, args.charge, args.mult, args.exc, args.dataset)
79+
print("Query result:", species.to_json())
80+
except Exception as e:
81+
print(f"An error occurred: {e}", file=sys.stderr)
82+
sys.exit(1)
83+
84+
85+
if __name__ == "__main__":
86+
main()
5087

51-
try:
52-
__version__ = importlib.metadata.version("qc-AtomDB")
53-
except PackageNotFoundError:
54-
# Package is not installed
55-
print("Package 'qc-AtomDB' is not installed.")

0 commit comments

Comments
 (0)