From 2558c6b887dbf96c2725d540d26d14efc90c72a6 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Mon, 24 Aug 2015 21:29:36 +0200 Subject: [PATCH 1/3] Python 3 compatibility Quickly done, probably dirty... --- launch.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/launch.py b/launch.py index eafb441..3f1502e 100755 --- a/launch.py +++ b/launch.py @@ -1,6 +1,5 @@ #!/usr/bin/python import pickle -import commands import sys import os import subprocess @@ -10,17 +9,17 @@ DMENU = "dmenu" TERM = "urxvt -e " CD_TERM = "urxvt -e zsh -c 'cd " -FOLDERS = ["/home/shankar"] +FOLDERS = [os.getenv('HOME')] EXT = { - '.txt':'urxvt -e vim', - '.sh':'urxvt -e vim', - '.py':'urxvt -e vim' + '.txt':TERM + 'nano', + '.sh':TERM + 'nano', + '.py':TERM + 'nano' } -FILEBROWSER = "pcmanfm" +FILEBROWSER = TERM + "ranger" def create_cache(): - prog_list = commands.getoutput("dmenu_path").split("\n") + prog_list = subprocess.getoutput("dmenu_path").split("\n") dirlist = [] for watchdir in FOLDERS: @@ -41,7 +40,7 @@ def create_cache(): def store(object,file): - with open(file, 'w') as f: + with open(file, 'wb') as f: pickle.dump(object,f) f.close() @@ -52,7 +51,7 @@ def create_new(file): def retrieve(file,ifnotfound): try: - with open(file,'r+') as f: + with open(file,'r') as f: obj = pickle.load(f) f.close() return(obj) @@ -70,18 +69,20 @@ def update(): def dmenu(pgm_list): p = subprocess.Popen([DMENU,"-l","10"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) - out = p.communicate("\n".join(pgm_list))[0] + out = p.communicate(bytes("\n".join(pgm_list), "utf-8"))[0] return out def run(): cache = retrieve(CACHE_FILE,create_new) - sorted_list = sorted(cache.iteritems(), key=itemgetter(1), reverse=True) + sorted_list = sorted(cache.items(), key=itemgetter(1), reverse=True) pgm_list = [ x[0] for x in sorted_list ] - out = dmenu(pgm_list) + bytes = dmenu(pgm_list) + out = bytes.decode("utf-8")[:-1] if len(out) > 0 : - if out == "update_dmen": + if out == ";": update() + run() elif out.endswith(';'): out = out[:-1] if os.path.isdir(out): @@ -96,11 +97,12 @@ def run(): if ext == '': os.system(out + " &") else: + print(EXT[ext] + " " + out + " &") os.system(EXT[ext] + " " + out + " &") else: os.system(out + " &") - if not cache.has_key(out): + if out not in cache: cache[out] = 0 cache[out] += 1 store(cache,CACHE_FILE) From ceec494cac7482cddbec7597068fb2e1367a9204 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Mon, 24 Aug 2015 21:30:56 +0200 Subject: [PATCH 2/3] Update launch.py --- launch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launch.py b/launch.py index 3f1502e..b73f691 100755 --- a/launch.py +++ b/launch.py @@ -51,7 +51,7 @@ def create_new(file): def retrieve(file,ifnotfound): try: - with open(file,'r') as f: + with open(file,'r+') as f: obj = pickle.load(f) f.close() return(obj) From d1eafd8d162ede12a6cf1b5dcd9a84f88b185cc3 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Mon, 24 Aug 2015 23:31:35 +0200 Subject: [PATCH 3/3] Small fixes and speed up The cache is stored as sorted (and the sorting is done on exit) --- launch.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/launch.py b/launch.py index b73f691..7ca4923 100755 --- a/launch.py +++ b/launch.py @@ -3,6 +3,7 @@ import sys import os import subprocess +import collections from operator import itemgetter CACHE_FILE = os.getenv('HOME')+'/.launch' @@ -33,7 +34,6 @@ def create_cache(): if not name.startswith('.'): dirlist.append(os.path.join(root,name)) - dirlist.append("update_dmen") dirlist.sort() return dict.fromkeys(prog_list + dirlist,0) @@ -51,7 +51,7 @@ def create_new(file): def retrieve(file,ifnotfound): try: - with open(file,'r+') as f: + with open(file,'rb') as f: obj = pickle.load(f) f.close() return(obj) @@ -68,15 +68,14 @@ def update(): store(cache_new,CACHE_FILE) def dmenu(pgm_list): - p = subprocess.Popen([DMENU,"-l","10"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) + p = subprocess.Popen([DMENU], stdin=subprocess.PIPE, stdout=subprocess.PIPE) out = p.communicate(bytes("\n".join(pgm_list), "utf-8"))[0] return out def run(): cache = retrieve(CACHE_FILE,create_new) - sorted_list = sorted(cache.items(), key=itemgetter(1), reverse=True) - pgm_list = [ x[0] for x in sorted_list ] + pgm_list = cache.keys() bytes = dmenu(pgm_list) out = bytes.decode("utf-8")[:-1] if len(out) > 0 : @@ -97,7 +96,6 @@ def run(): if ext == '': os.system(out + " &") else: - print(EXT[ext] + " " + out + " &") os.system(EXT[ext] + " " + out + " &") else: os.system(out + " &") @@ -105,7 +103,9 @@ def run(): if out not in cache: cache[out] = 0 cache[out] += 1 - store(cache,CACHE_FILE) + sorted_cache = sorted(cache.items(), key=itemgetter(1), reverse=True) + ordered_cache = collections.OrderedDict(sorted_cache) + store(ordered_cache,CACHE_FILE) run()