Improve/simplify error handling.
This commit is contained in:
parent
3e31a8b74f
commit
1d936552ce
@ -149,60 +149,37 @@ class SearchPassService(dbus.service.Object):
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
def send_password_to_gpaste(self, base_args, name, field=None):
|
def send_password_to_gpaste(self, base_args, name, field=None):
|
||||||
|
gpaste = self.session_bus.get_object(
|
||||||
|
"org.gnome.GPaste.Daemon", "/org/gnome/GPaste"
|
||||||
|
)
|
||||||
|
|
||||||
|
output = subprocess.check_output(
|
||||||
|
base_args + [name], stderr=subprocess.STDOUT, universal_newlines=True
|
||||||
|
)
|
||||||
|
if field is not None:
|
||||||
|
match = re.search(
|
||||||
|
fr"^{field}:\s*(?P<value>.+?)$", output, flags=re.I | re.M
|
||||||
|
)
|
||||||
|
if match:
|
||||||
|
password = match.group("value")
|
||||||
|
else:
|
||||||
|
raise RuntimeError(f"The field {field} was not found in the pass file.")
|
||||||
|
else:
|
||||||
|
password = output.split("\n", 1)[0]
|
||||||
try:
|
try:
|
||||||
gpaste = self.session_bus.get_object(
|
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste1")
|
||||||
"org.gnome.GPaste.Daemon", "/org/gnome/GPaste"
|
except dbus.DBusException:
|
||||||
)
|
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste2")
|
||||||
|
|
||||||
output = subprocess.check_output(
|
|
||||||
base_args + [name], stderr=subprocess.STDOUT, universal_newlines=True
|
|
||||||
)
|
|
||||||
if field is not None:
|
|
||||||
match = re.search(
|
|
||||||
fr"^{field}:\s*(?P<value>.+?)$", output, flags=re.I | re.M
|
|
||||||
)
|
|
||||||
if match:
|
|
||||||
password = match.group("value")
|
|
||||||
else:
|
|
||||||
raise RuntimeError(
|
|
||||||
f"The field {field} was not found in " + "the pass file."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
password = output.split("\n", 1)[0]
|
|
||||||
try:
|
|
||||||
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste1")
|
|
||||||
except dbus.DBusException:
|
|
||||||
gpaste.AddPassword(name, password, dbus_interface="org.gnome.GPaste2")
|
|
||||||
|
|
||||||
if "otp" in base_args:
|
|
||||||
self.notify("Copied OTP password to clipboard:", body=f"<b>{name}</b>")
|
|
||||||
elif field is not None:
|
|
||||||
self.notify(
|
|
||||||
f"Copied field {field} to clipboard:", body=f"<b>{name}</b>"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self.notify("Copied password to clipboard:", body=f"<b>{name}</b>")
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
self.notify("Failed to copy password!", body=e.output, error=True)
|
|
||||||
except RuntimeError as e:
|
|
||||||
self.notify("Failed to copy field!", body=e.output, error=True)
|
|
||||||
|
|
||||||
def send_password_to_native_clipboard(self, base_args, name, field=None):
|
def send_password_to_native_clipboard(self, base_args, name, field=None):
|
||||||
if field is not None:
|
if field is not None or self.use_bw:
|
||||||
self.notify(
|
raise RuntimeError("This feature requires GPaste.")
|
||||||
"Cannot copy field values.",
|
|
||||||
body="This feature requires GPaste.",
|
|
||||||
error=True,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
pass_cmd = subprocess.run(base_args + ["-c", name])
|
result = subprocess.run(base_args + ["-c", name])
|
||||||
if pass_cmd.returncode:
|
if result.returncode:
|
||||||
self.notify("Failed to copy password!", error=True)
|
raise RuntimeError(
|
||||||
elif "otp" in base_args:
|
f"Error while running pass: got return code {result.returncode}."
|
||||||
self.notify("Copied OTP password to clipboard:", body=f"<b>{name}</b>")
|
)
|
||||||
else:
|
|
||||||
self.notify("Copied password to clipboard:", body=f"<b>{name}</b>")
|
|
||||||
|
|
||||||
def send_password_to_clipboard(self, name):
|
def send_password_to_clipboard(self, name):
|
||||||
field = None
|
field = None
|
||||||
@ -216,13 +193,22 @@ class SearchPassService(dbus.service.Object):
|
|||||||
if name.startswith(":"):
|
if name.startswith(":"):
|
||||||
field, name = name.split(" ", 1)
|
field, name = name.split(" ", 1)
|
||||||
field = field[1:]
|
field = field[1:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.send_password_to_gpaste(base_args, name, field)
|
try:
|
||||||
except dbus.DBusException:
|
self.send_password_to_gpaste(base_args, name, field)
|
||||||
# We couldn't join GPaste over D-Bus,
|
except dbus.DBusException:
|
||||||
# use pass native clipboard copy
|
# We couldn't join GPaste over D-Bus, use native clipboard
|
||||||
self.send_password_to_native_clipboard(base_args, name, field)
|
self.send_password_to_native_clipboard(base_args, name, field)
|
||||||
|
if "otp" in base_args:
|
||||||
|
self.notify("Copied OTP password to clipboard:", body=f"<b>{name}</b>")
|
||||||
|
elif field is not None:
|
||||||
|
self.notify(
|
||||||
|
f"Copied field {field} to clipboard:", body=f"<b>{name}</b>"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.notify("Copied password to clipboard:", body=f"<b>{name}</b>")
|
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError, RuntimeError) as e:
|
||||||
|
self.notify("Failed to copy password or field!", body=e.output, error=True)
|
||||||
|
|
||||||
def notify(self, message, body="", error=False):
|
def notify(self, message, body="", error=False):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user