-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessionsaver.py
64 lines (47 loc) · 1.68 KB
/
sessionsaver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
# coding=utf-8
import os
import sys
import codecs
import yaml
import time
## Return a formatted timestamp string
def gcm_timestamp(stamp):
return time.strftime("%Y-%m-%d %H:%M:%S", stamp)
## Wrapper for print() that does not break when redirecting stdin/out
## because of piped output not having a defined encoding. We default
## to UTF-8 encoding in output here.
def gcm_print(smsg):
if sys.stdout.encoding != None:
rsmsg = smsg.encode(sys.stdout.encoding)
else:
rsmsg = smsg.encode("UTF-8")
print("{0} | {1}".format(gcm_timestamp(time.localtime()), rsmsg))
## Fatal error handler
def gcm_fatal(smsg):
gcm_print(u"ERROR: "+ smsg)
sys.exit(1)
## YAML load helper
def yaml_loader(filepath):
with open(filepath, "r") as fh:
data = yaml.load(fh)
return data
with open(os.environ['QUTE_FIFO'], "w") as f: f.write('session-save tabs\n')
with open(os.environ['QUTE_FIFO'], "w") as f: f.write('session-delete default\n')
time.sleep(0.1)
with open(os.environ['QUTE_FIFO'], "w") as f: f.write('quit\n')
inFilename = os.environ['HOME']+"/.local/share/qutebrowser/sessions/tabs.yml"
outFilename = os.environ['HOME']+"/scripts/rofi/tabs"
data = yaml_loader(inFilename)
try:
ofh = open(outFilename, "a")
except:
gcm_fatal(u"Could not open output file '{0}'.".format(outFilename))
for cwindow in data.get("windows"):
for ctab in cwindow.get("tabs"):
for chist in ctab.get("history"):
if chist.get("active") != None and chist.get("active") == True:
str = chist.get("title") +" "+ chist.get("url")
gcm_print(str)
ofh.write(str.encode("UTF-8") +"\n")
ofh.close()