Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if model.count() > 0:
index = model.data(model.first_item())
index_parts = index.split('/', 1)
else:
raise cmdutils.CommandError(
"No matching tab for: {}".format(index))
if len(index_parts) == 2:
win_id = int(index_parts[0])
idx = int(index_parts[1])
elif len(index_parts) == 1:
idx = int(index_parts[0])
active_win = QApplication.activeWindow()
if active_win is None:
# Not sure how you enter a command without an active window...
raise cmdutils.CommandError(
"No window specified and couldn't find active window!")
win_id = active_win.win_id
if win_id not in objreg.window_registry:
raise cmdutils.CommandError(
"There's no window with id {}!".format(win_id))
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
if not 0 < idx <= tabbed_browser.widget.count():
raise cmdutils.CommandError(
"There's no tab with index {}!".format(idx))
return (tabbed_browser, tabbed_browser.widget.widget(idx-1))
Args:
ms: How many milliseconds to wait.
command: The command to run, with optional args.
"""
if ms < 0:
raise cmdutils.CommandError("I can't run something in the past!")
commandrunner = runners.CommandRunner(win_id)
app = objreg.get('app')
timer = usertypes.Timer(name='later', parent=app)
try:
timer.setSingleShot(True)
try:
timer.setInterval(ms)
except OverflowError:
raise cmdutils.CommandError("Numeric argument is too large for "
"internal int representation.")
timer.timeout.connect(
functools.partial(commandrunner.run_safely, command))
timer.timeout.connect(timer.deleteLater)
timer.start()
except:
timer.deleteLater()
raise
def repl_cb(matchobj):
"""Return replacement for given match."""
var = matchobj.group("var")
if var not in values:
values[var] = variables[var]()
return values[var]
repl_pattern = re.compile("{(?P<var>" + "|".join(variables.keys()) + ")}")
try:
for arg in arglist:
# using re.sub with callback function replaces all variables in a
# single pass and avoids expansion of nested variables (e.g.
# "{url}" from clipboard is not expanded)
args.append(repl_pattern.sub(repl_cb, arg))
except utils.ClipboardError as e:
raise cmdutils.CommandError(e)
return args
</var>
def config_list_add(self, option: str, value: str,
temp: bool = False) -> None:
"""Append a value to a config option that is a list.
Args:
option: The name of the option.
value: The value to append to the end of the list.
temp: Add value temporarily until qutebrowser is closed.
"""
opt = self._config.get_opt(option)
valid_list_types = (configtypes.List, configtypes.ListOrValue)
if not isinstance(opt.typ, valid_list_types):
raise cmdutils.CommandError(":config-list-add can only be used "
"for lists")
with self._handle_config_error():
option_value = self._config.get_mutable_obj(option)
option_value.append(value)
self._config.update_mutables(save_yaml=not temp)
def _handle_config_error(self) -> typing.Iterator[None]:
"""Catch errors in set_command and raise CommandError."""
try:
yield
except configexc.Error as e:
raise cmdutils.CommandError(str(e))
def _current_url(tabbed_browser):
"""Convenience method to get the current url."""
try:
return tabbed_browser.current_url()
except qtutils.QtValueError as e:
msg = "Current URL is invalid"
if e.reason:
msg += " ({})".format(e.reason)
msg += "!"
raise cmdutils.CommandError(msg)
def raise_cmdexc_if_invalid(url: QUrl) -> None:
"""Check if the given QUrl is invalid, and if so, raise a CommandError."""
try:
ensure_valid(url)
except InvalidUrlError as e:
raise cmdutils.CommandError(str(e))
def zoom_in(tab: apitypes.Tab, count: int = 1, quiet: bool = False) -> None:
"""Increase the zoom level for the current tab.
Args:
count: How many steps to zoom in.
quiet: Don't show a zoom level message.
"""
try:
perc = tab.zoom.apply_offset(count)
except ValueError as e:
raise cmdutils.CommandError(e)
if not quiet:
message.info("Zoom level: {}%".format(int(perc)), replace=True)
def debug_pyeval(s, file=False, quiet=False):
"""Evaluate a python string and display the results as a web page.
Args:
s: The string to evaluate.
file: Interpret s as a path to file, also implies --quiet.
quiet: Don't show the output in a new tab.
"""
if file:
quiet = True
path = os.path.expanduser(s)
try:
with open(path, 'r', encoding='utf-8') as f:
s = f.read()
except OSError as e:
raise cmdutils.CommandError(str(e))
try:
exec(s)
out = "No error"
except Exception:
out = traceback.format_exc()
else:
try:
r = eval(s)
out = repr(r)
except Exception:
out = traceback.format_exc()
qutescheme.pyeval_output = out
if quiet:
log.misc.debug("pyeval output: {}".format(out))
else:
def log_capacity(capacity: int) -> None:
"""Change the number of log lines to be stored in RAM.
Args:
capacity: Number of lines for the log.
"""
if capacity < 0:
raise cmdutils.CommandError("Can't set a negative log capacity!")
else:
assert log.ram_handler is not None
log.ram_handler.change_log_capacity(capacity)