Skip to content

Commit 404fa8e

Browse files
committed
[cli] Add xpp -s <module>
1 parent aeaef2b commit 404fa8e

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

xpp/__main__.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Modules
44
import os
55
import sys
6+
import json
67
from . import (
78
__version__,
89
load_sections, config, Interpreter
@@ -19,8 +20,10 @@ def __init__(self) -> None:
1920
{"args": ["-h", "--help"], "fn": self.show_help, "desc": "Displays the help menu"},
2021
{"args": ["-hl", "--helplong"], "fn": self.show_help_long, "desc": "Displays a more detailed help menu"},
2122
{"args": ["-v", "--ver", "--version"], "fn": self.show_version, "desc": "Prints the x++ version"},
22-
{"args": ["-i", "--installation"], "fn": self.show_install_path, "desc": "Prints the installation path"}
23+
{"args": ["-i", "--installation"], "fn": self.show_install_path, "desc": "Prints the installation path"},
24+
{"args": ["-s", "--show"], "fn": self.show_module, "desc": "Provides information about an installed x++ module"}
2325
]
26+
self.install_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
2427

2528
self.usage = f"""x++ (x{__version__}) Interpreter
2629
(c) 2021-23 iiPython; (c) 2022-23 Dm123321_31mD "DmmD" Gaming
@@ -35,16 +38,16 @@ def __init__(self) -> None:
3538
for flag in self.flags:
3639
self.vals[flag["name"]] = any([a in self.argv for a in flag["args"]])
3740

38-
# Handle options
39-
for opt in self.options:
40-
if any([a in self.argv for a in opt["args"]]):
41-
opt["fn"]()
42-
4341
# Load filepath
4442
self.filepath = None
4543
if self.argv:
4644
self.filepath = ([a for a in self.argv if a[0] != "-"] or [None])[-1]
4745

46+
# Handle options
47+
for opt in self.options:
48+
if any([a in self.argv for a in opt["args"]]):
49+
opt["fn"]()
50+
4851
def show_help(self) -> None:
4952
return sys.exit(self.usage)
5053

@@ -64,7 +67,30 @@ def show_version(self) -> None:
6467
return sys.exit(__version__)
6568

6669
def show_install_path(self) -> None:
67-
return sys.exit(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
70+
return sys.exit(self.install_path)
71+
72+
def show_module(self) -> None:
73+
module_path = os.path.join(self.install_path, "pkgs", self.filepath.replace(".", os.sep))
74+
if not os.path.isdir(module_path):
75+
return exit("no such module: " + self.filepath)
76+
77+
# Load .xconfig
78+
xconfig = os.path.join(module_path, ".xconfig")
79+
if not os.path.isfile(xconfig) and "." in self.filepath:
80+
xconfig = os.path.join(self.install_path, "pkgs", self.filepath.split(".")[-1], ".xconfig")
81+
82+
metadata = {}
83+
if os.path.isfile(xconfig):
84+
with open(xconfig, "r") as fh:
85+
metadata = json.loads(fh.read())
86+
87+
# Print out information
88+
exit(f"""Name: {self.filepath}
89+
Version: {metadata.get('version', 'N/A')}
90+
Summary: {metadata.get('summary', 'N/A')}
91+
Author: {metadata.get('author', 'N/A')}
92+
License: {metadata.get('license', 'N/A')}
93+
Location: {module_path}""")
6894

6995
# Initialization
7096
cli = CLI()

0 commit comments

Comments
 (0)