114 lines
3.1 KiB
Python
Executable File
114 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Based on: https://github.com/r-bar/activity-browser
|
|
import sys
|
|
import os
|
|
import json
|
|
import subprocess
|
|
import shutil
|
|
|
|
distro = "activity-browser"
|
|
configDir = os.path.join(os.environ.get("XDG_CONFIG_DIR",os.path.join(os.environ["HOME"],".config")),distro) # $XDG_CONFIG_DIR/$distro or $HOME/.config/$distro
|
|
configFile = os.path.join(configDir,"config.json")
|
|
|
|
# If the program string contains the string on the right, it is identified as the browser on the left. First match is used.
|
|
programTranslation = [
|
|
# Specific browsers:
|
|
["firefox","seamonkey"],
|
|
["firefox","palemoon"],
|
|
["chromium","vivaldi"],
|
|
["chromium","opera"],
|
|
|
|
# General
|
|
["firefox","fox"],
|
|
["chromium","chrom"]
|
|
]
|
|
|
|
defaults = {
|
|
"program": "firefox",
|
|
"profile": None
|
|
}
|
|
|
|
# COMMANDS
|
|
command = {}
|
|
def f():
|
|
config[activityID]["program"] = args[1]
|
|
saveConfig()
|
|
return 0
|
|
command["ab.set-program"] = f
|
|
|
|
def f():
|
|
config[activityID]["profile"] = args[1]
|
|
saveConfig()
|
|
return 0
|
|
command["ab.set-profile"] = f
|
|
|
|
def f():
|
|
config[activityID]["profile"] = None
|
|
saveConfig()
|
|
return 0
|
|
command["ab.clear-profile"] = f
|
|
|
|
def f():
|
|
print(json.dumps(config,indent=2,sort_keys=True))
|
|
return 0
|
|
command["ab.show-config"] = f
|
|
|
|
# LAUNCHERS
|
|
browserLauncher = {}
|
|
def f():
|
|
global args, programPath
|
|
if config[activityID]["profile"] != None: args = ["-P",config[activityID]["profile"]] + args
|
|
args = [config[activityID]["program"]] + args
|
|
os.execv(programPath,args)
|
|
browserLauncher["firefox"] = f
|
|
|
|
def f():
|
|
global args, programPath
|
|
if config[activityID]["profile"] != None: args = ["--profile-directory=" + config[activityID]["profile"]] + args
|
|
args = [config[activityID]["program"]] + args
|
|
os.execv(programPath,args)
|
|
browserLauncher["chromium"] = f
|
|
|
|
# MISC
|
|
def getProgramType(command):
|
|
command = command.rsplit(os.path.sep,1)[-1]
|
|
for program in programTranslation:
|
|
if program[1] in command: return True,program[0]
|
|
return False,"firefox"
|
|
|
|
def getActivityID():
|
|
cmd = [
|
|
'dbus-send', '--session', '--dest=org.kde.ActivityManager',
|
|
'--type=method_call', '--print-reply=literal',
|
|
'/ActivityManager/Activities',
|
|
'org.kde.ActivityManager.Activities.CurrentActivity',
|
|
]
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
|
if proc.wait() != 0: raise subprocess.CalledProcessError
|
|
return proc.stdout.read().strip().decode("utf-8")
|
|
|
|
def saveConfig():
|
|
with open(configFile,"w") as f:
|
|
f.write(json.dumps(config,indent=2,sort_keys=True))
|
|
|
|
if __name__ == '__main__':
|
|
activityID = getActivityID()
|
|
if not os.path.isdir(configDir): os.makedirs(configDir)
|
|
if not os.path.isfile(configFile):
|
|
with open(configFile,"w") as f: f.write("{}")
|
|
config = json.loads(open(configFile,"r").read())
|
|
if not activityID in config:
|
|
print("Activity not found, saving defaults ...")
|
|
config[activityID] = defaults
|
|
saveConfig()
|
|
|
|
programFound,programType = getProgramType(config[activityID]["program"])
|
|
if programFound == False:
|
|
print(config[activityID]["program"]+ " could not be identified, assuming it to be " +programType+ ".")
|
|
|
|
args = sys.argv[1:]
|
|
if len(args) > 0 and args[0] in command:
|
|
sys.exit(command[args[0]]())
|
|
|
|
programPath = shutil.which(config[activityID]["program"])
|
|
browserLauncher[programType]() |