From d4337541321112774abebc1d8e17602aec0f594a Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Mon, 10 Jul 2023 11:20:55 -0400 Subject: [PATCH] implemented getting metadata from focused zathura --- .gitignore | 2 ++ copy_ref.py | 1 + datatypes.py | 17 ++++++++++++ pdf_data.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ 5 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 copy_ref.py create mode 100644 datatypes.py create mode 100644 pdf_data.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40505c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ +__pycache__ diff --git a/copy_ref.py b/copy_ref.py new file mode 100644 index 0000000..e5a0d9b --- /dev/null +++ b/copy_ref.py @@ -0,0 +1 @@ +#!/usr/bin/env python3 diff --git a/datatypes.py b/datatypes.py new file mode 100644 index 0000000..b167764 --- /dev/null +++ b/datatypes.py @@ -0,0 +1,17 @@ +from typing import NewType +from os import PathLike +from dataclasses import dataclass + +# X11 window id +WindowId = NewType("WindowId", int) +# PID int +ProcessId = NewType("ProcessId", int) +# page number +PageNumber = NewType("PageNumber", int) + + +# reference to a specific page in a specific pdf +@dataclass +class Reference: + filename: PathLike + page: PageNumber diff --git a/pdf_data.py b/pdf_data.py new file mode 100644 index 0000000..38f0aa2 --- /dev/null +++ b/pdf_data.py @@ -0,0 +1,71 @@ +from pathlib import Path +from datatypes import * +import pydbus +import subprocess + + +def get_metadata_pdf() -> Reference: + """Find current page of focused PDF reader window. + + Returns + ------- + `Reference` to the current page, or None if not found. + """ + try: + res = subprocess.run( + ["xdotool", "getactivewindow"], capture_output=True, text=True + ) + window_id: WindowId = WindowId(int(res.stdout)) + res = subprocess.run( + ["xdotool", "getactivewindow", "getwindowpid"], + capture_output=True, + text=True, + ) + pid: ProcessId = ProcessId(int(res.stdout)) + except subprocess.CalledProcessError as e: + raise Exception( + "Could not get currently focused window. Check that `xdotool` is installed." + ) from e + + try: + res = subprocess.run( + ["xprop", "-id", str(window_id), "WM_CLASS"], capture_output=True, text=True + ) + # WM_CLASS(STRING) = "org.pwmt.zathura", "Zathura" + wm_class: list[str] = [ + i.strip('"') for i in res.stdout.split(" = ")[-1].split(", ") + ] + except subprocess.CalledProcessError as e: + raise Exception( + "Could not get focused window class. Check that `xprop` is installed." + ) from e + + match wm_class[0]: + case "Zathura": + return get_metadata_zathura(pid) + case "org.pwmt.zathura": + return get_metadata_zathura(pid) + case _: + raise NotImplemented("Can not retrieve pdf data from this type of window.") + + +def get_metadata_zathura(pid: ProcessId) -> Reference: + """Given the PID of a Zathura instance, find which page of which file it's on. + + Parameters + ---------- + pid + Process ID of the Zathura instance to retrieve the reference from. + + Returns + ------- + `Reference` that the Zathura instance is currently on + """ + + bus = pydbus.SessionBus() + obj = bus.get(f"org.pwmt.zathura.PID-{pid}", "/org/pwmt/zathura") + + filename: str = obj.filename + pagenumber: PageNumber = obj.pagenumber + + return Reference(filename=Path(filename), page=pagenumber) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..12c7d9a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pycairo==1.24.0 +pydbus==0.6.0 +PyGObject==3.44.1