Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _dynamically_collapsed_pwd():
"""Return the compact current working directory. It respects the
environment variable DYNAMIC_CWD_WIDTH.
"""
original_path = _replace_home_cwd()
target_width, units = builtins.__xonsh_env__["DYNAMIC_CWD_WIDTH"]
elision_char = builtins.__xonsh_env__["DYNAMIC_CWD_ELISION_CHAR"]
if target_width == float("inf"):
return original_path
if units == "%":
cols, _ = shutil.get_terminal_size()
target_width = (cols * target_width) // 100
sep = xt.get_sep()
pwd = original_path.split(sep)
last = pwd.pop()
remaining_space = target_width - len(last)
# Reserve space for separators
remaining_space_for_text = remaining_space - len(pwd)
parts = []
for i in range(len(pwd)):
part = pwd[i]
part_len = int(
min(len(part), max(1, remaining_space_for_text // (len(pwd) - i)))
)
remaining_space_for_text -= part_len
if len(part) > part_len:
reduced_part = part[0 : part_len - len(elision_char)] + elision_char
parts.append(reduced_part)
else:
toks = basetoks * (headlen // baselen)
n = headlen % baselen
count = 0
for tok in basetoks:
slen = len(tok[1])
newcount = slen + count
if slen == 0:
continue
elif newcount <= n:
toks.append(tok)
else:
toks.append((tok[0], tok[1][: n - count]))
count = newcount
if n <= count:
break
toks.append((xt.format_color("{NO_COLOR}", hide=True), tail))
rtn = "".join(itertools.chain.from_iterable(toks))
return rtn
def _collapsed_pwd():
sep = xt.get_sep()
pwd = _replace_home_cwd().split(sep)
l = len(pwd)
leader = sep if l > 0 and len(pwd[0]) == 0 else ""
base = [i[0] if ix != l - 1 else i for ix, i in enumerate(pwd) if len(i) > 0]
return leader + sep.join(base)
def _path_from_partial_string(inp, pos=None):
if pos is None:
pos = len(inp)
partial = inp[:pos]
startix, endix, quote = xt.check_for_partial_string(partial)
_post = ""
if startix is None:
return None
elif endix is None:
string = partial[startix:]
else:
if endix != pos:
_test = partial[endix:pos]
if not any(i == " " for i in _test):
_post = _test
else:
return None
string = partial[startix:endix]
end = xt.RE_STRING_START.sub("", quote)
_string = string
if not _string.endswith(end):
def attr_complete(prefix, ctx, filter_func):
"""Complete attributes of an object."""
attrs = set()
m = RE_ATTR.match(prefix)
if m is None:
return attrs
expr, attr = m.group(1, 3)
expr = xt.subexpr_from_unbalanced(expr, "(", ")")
expr = xt.subexpr_from_unbalanced(expr, "[", "]")
expr = xt.subexpr_from_unbalanced(expr, "{", "}")
val, _ctx = _safe_eval(expr, ctx)
if val is None and _ctx is None:
return attrs
if len(attr) == 0:
opts = [o for o in dir(val) if not o.startswith("_")]
else:
opts = [o for o in dir(val) if filter_func(o, attr)]
prelen = len(prefix)
for opt in opts:
# check whether these options actually work (e.g., disallow 7.imag)
_expr = "{0}.{1}".format(expr, opt)
_val_, _ctx_ = _safe_eval(_expr, _ctx)
if _val_ is None and _ctx_ is None:
continue
a = getattr(val, opt)
def _xh_sqlite_get_file_name():
envs = builtins.__xonsh_env__
file_name = envs.get("XONSH_HISTORY_SQLITE_FILE")
if not file_name:
data_dir = envs.get("XONSH_DATA_DIR")
file_name = os.path.join(data_dir, "xonsh-history.sqlite")
return xt.expanduser_abs_path(file_name)
def complete_command(cmd, line, start, end, ctx):
"""
Returns a list of valid commands starting with the first argument
"""
space = " "
out = {
s + space
for s in builtins.__xonsh__.commands_cache
if get_filter_function()(s, cmd)
}
if xp.ON_WINDOWS:
out |= {i for i in xt.executables_in(".") if i.startswith(cmd)}
base = os.path.basename(cmd)
if os.path.isdir(base):
out |= {
os.path.join(base, i) for i in xt.executables_in(base) if i.startswith(cmd)
}
return out
def run(self):
while self.wait_for_shell:
time.sleep(0.01)
if self.size is not None:
hsize, units = xt.to_history_tuple(self.size)
else:
envs = builtins.__xonsh_env__
hsize, units = envs.get("XONSH_HISTORY_SIZE")
if units != "commands":
print(
"sqlite backed history gc currently only supports "
'"commands" as units',
file=sys.stderr,
)
return
if hsize < 0:
return
xh_sqlite_delete_items(hsize, filename=self.filename)
def python_signature_complete(prefix, line, end, ctx, filter_func):
"""Completes a python function (or other callable) call by completing
argument and keyword argument names.
"""
front = line[:end]
if xt.is_balanced(front, "(", ")"):
return set()
funcname = xt.subexpr_before_unbalanced(front, "(", ")")
val, _ctx = _safe_eval(funcname, ctx)
if val is None:
return set()
try:
sig = inspect.signature(val)
except ValueError:
return set()
args = {p + "=" for p in sig.parameters if filter_func(p, prefix)}
return args