forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxontribs.py
178 lines (152 loc) · 5.27 KB
/
xontribs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Tools for helping manage xontributions."""
import os
import sys
import json
import builtins
import argparse
import functools
import importlib
import importlib.util
from xonsh.tools import print_color, unthreadable
@functools.lru_cache(1)
def xontribs_json():
return os.path.join(os.path.dirname(__file__), "xontribs.json")
def find_xontrib(name):
"""Finds a xontribution from its name."""
if name.startswith("."):
spec = importlib.util.find_spec(name, package="xontrib")
else:
spec = importlib.util.find_spec("." + name, package="xontrib")
return spec or importlib.util.find_spec(name)
def xontrib_context(name):
"""Return a context dictionary for a xontrib of a given name."""
spec = find_xontrib(name)
if spec is None:
return None
m = importlib.import_module(spec.name)
pubnames = getattr(m, "__all__", None)
if pubnames is not None:
ctx = {k: getattr(m, k) for k in pubnames}
else:
ctx = {k: getattr(m, k) for k in dir(m) if not k.startswith("_")}
return ctx
def prompt_xontrib_install(names):
"""Returns a formatted string with name of xontrib package to prompt user"""
md = xontrib_metadata()
packages = []
for name in names:
for xontrib in md["xontribs"]:
if xontrib["name"] == name:
packages.append(xontrib["package"])
print(
"The following xontribs are enabled but not installed: \n"
" {xontribs}\n"
"To install them run \n"
" xpip install {packages}".format(
xontribs=" ".join(names), packages=" ".join(packages)
)
)
def update_context(name, ctx=None):
"""Updates a context in place from a xontrib. If ctx is not provided,
then __xonsh__.ctx is updated.
"""
if ctx is None:
ctx = builtins.__xonsh__.ctx
if not hasattr(update_context, "bad_imports"):
update_context.bad_imports = []
modctx = xontrib_context(name)
if modctx is None:
update_context.bad_imports.append(name)
return ctx
return ctx.update(modctx)
@functools.lru_cache()
def xontrib_metadata():
"""Loads and returns the xontribs.json file."""
with open(xontribs_json(), "r") as f:
md = json.load(f)
return md
def xontribs_load(names, verbose=False):
"""Load xontribs from a list of names"""
ctx = builtins.__xonsh__.ctx
for name in names:
if verbose:
print("loading xontrib {0!r}".format(name))
update_context(name, ctx=ctx)
if update_context.bad_imports:
prompt_xontrib_install(update_context.bad_imports)
del update_context.bad_imports
def _load(ns):
"""load xontribs"""
xontribs_load(ns.names, verbose=ns.verbose)
def _list(ns):
"""Lists xontribs."""
meta = xontrib_metadata()
data = []
nname = 6 # ensures some buffer space.
names = None if len(ns.names) == 0 else set(ns.names)
for md in meta["xontribs"]:
name = md["name"]
if names is not None and md["name"] not in names:
continue
nname = max(nname, len(name))
spec = find_xontrib(name)
if spec is None:
installed = loaded = False
else:
installed = True
loaded = spec.name in sys.modules
d = {"name": name, "installed": installed, "loaded": loaded}
data.append(d)
if ns.json:
jdata = {d.pop("name"): d for d in data}
s = json.dumps(jdata)
print(s)
else:
s = ""
for d in data:
name = d["name"]
lname = len(name)
s += "{PURPLE}" + name + "{NO_COLOR} " + " " * (nname - lname)
if d["installed"]:
s += "{GREEN}installed{NO_COLOR} "
else:
s += "{RED}not-installed{NO_COLOR} "
if d["loaded"]:
s += "{GREEN}loaded{NO_COLOR}"
else:
s += "{RED}not-loaded{NO_COLOR}"
s += "\n"
print_color(s[:-1])
@functools.lru_cache()
def _create_xontrib_parser():
# parse command line args
parser = argparse.ArgumentParser(
prog="xontrib", description="Manages xonsh extensions"
)
subp = parser.add_subparsers(title="action", dest="action")
load = subp.add_parser("load", help="loads xontribs")
load.add_argument(
"-v", "--verbose", action="store_true", default=False, dest="verbose"
)
load.add_argument("names", nargs="+", default=(), help="names of xontribs")
lyst = subp.add_parser(
"list", help=("list xontribs, whether they are " "installed, and loaded.")
)
lyst.add_argument(
"--json", action="store_true", default=False, help="reports results as json"
)
lyst.add_argument("names", nargs="*", default=(), help="names of xontribs")
return parser
_MAIN_XONTRIB_ACTIONS = {"load": _load, "list": _list}
@unthreadable
def xontribs_main(args=None, stdin=None):
"""Alias that loads xontribs"""
if not args or (
args[0] not in _MAIN_XONTRIB_ACTIONS and args[0] not in {"-h", "--help"}
):
args.insert(0, "load")
parser = _create_xontrib_parser()
ns = parser.parse_args(args)
if ns.action is None: # apply default action
ns = parser.parse_args(["load"] + args)
return _MAIN_XONTRIB_ACTIONS[ns.action](ns)