Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@o-jasper
Created May 24, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save o-jasper/8de084491023d9fe4d5a4c3be07793cd to your computer and use it in GitHub Desktop.
Save o-jasper/8de084491023d9fe4d5a4c3be07793cd to your computer and use it in GitHub Desktop.
Ln -s trick which am using. Could possibly use some features like supporting quotes, perhaps "patterns"
#!/bin/python
import sys
import subprocess
import os
import sys
from pathlib import Path
def figure_path_list():
got = os.getenv('LNS_TRICK_PATH')
got = got and got.split(';') or []
if len(got) == 0 or got[-1] == '+':
return [os.getenv('HOME') + "/.config/lns-trick/", "/etc/lns-trick/",
"/usr/share/lib/lns-trick/defaults/", ""] + got
else:
return got
def find_file(path_list, file):
for p in path_list:
uf = p + "/" + file + ".args"
if Path(uf).is_file():
return uf
# TODO kindah want quotations..
def parse_file(fd, path_list):
for line in fd.readlines():
if line.find('#') >= 0:
line = line[:line.find('#')]
if line.startswith(':inc'):
file = find_file(path_list, line[5:-1])
if file:
with open(file) as sfd:
yield parse_file(sfd, path_list)
# elif line.startswith(':as line'): # Better allow quotes?
# yield line[9:-1]
# elif line.startswith(':just print'): # TODO maybe allow disabling..
# assert line == ":just print\n"
# yield 1
elif not line.startswith('#') and len(line)>0:
assert not line.startswith(':'), "Dont have this command; " + line
if line.startswith('\\'): line = line[1:]
for el in line[:-1].split(" "): # TODO quotation..
yield el
def flatten(gen):
for el in gen:
if type(el) == str:
yield el
else:
for sel in flatten(el):
yield sel
prog_name = 'bwrap'
def prog():
path_list = figure_path_list()
argv = sys.argv
run_prog_name = argv[0].split('/')[-1]
profile_name = run_prog_name
if run_prog_name == 'lns-trick': # It was manually called.
profile_name = argv[1] # In this case, you can say the profile.
run_prog_name = argv[2].split('/')[-1]
if profile_name == 'n': profile_name = run_prog_name
argv = argv[3:]
file = find_file(path_list, profile_name)
if file:
with open(file) as fd:
#print("\n".join(list(flatten(parse_file(fd, path_list)))
# + sys.argv[1:]))
# TODO instead do $PATH, ignoring this program itself?
ua = [prog_name] + list(flatten(parse_file(fd, path_list))) + \
["/usr/bin/" + run_prog_name] + argv[1:]
print(ua)
subprocess.run(ua)
else:
print("Didn't find file for:", run_prog_name)
prog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment