|
13 | 13 | # You should have received a copy of the GNU General Public License
|
14 | 14 | # along with AtomDB. If not, see <http://www.gnu.org/licenses/>.
|
15 | 15 |
|
16 |
| -r"""AtomDB, a database of atomic and ionic properties.""" |
| 16 | +"""AtomDB console script. |
17 | 17 |
|
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 | +""" |
20 | 20 |
|
21 |
| -from atomdb.periodic import Element |
| 21 | +import sys |
| 22 | +from argparse import ArgumentParser, ArgumentTypeError |
| 23 | +from atomdb import compile_species, load |
22 | 24 |
|
23 |
| -from atomdb.species import Species |
24 | 25 |
|
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 |
26 | 35 |
|
27 |
| -from atomdb.periodic import element_number, element_symbol, element_name |
28 | 36 |
|
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 | + ) |
30 | 43 |
|
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 | + ) |
32 | 52 |
|
| 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 | + ) |
33 | 61 |
|
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() |
47 | 64 |
|
| 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) |
48 | 69 |
|
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() |
50 | 87 |
|
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