implemented getting metadata from focused zathura
This commit is contained in:
parent
0fdd723b71
commit
d433754132
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.venv/
|
||||
__pycache__
|
1
copy_ref.py
Normal file
1
copy_ref.py
Normal file
@ -0,0 +1 @@
|
||||
#!/usr/bin/env python3
|
17
datatypes.py
Normal file
17
datatypes.py
Normal file
@ -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
|
71
pdf_data.py
Normal file
71
pdf_data.py
Normal file
@ -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)
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
pycairo==1.24.0
|
||||
pydbus==0.6.0
|
||||
PyGObject==3.44.1
|
Loading…
Reference in New Issue
Block a user