diff --git a/launch.py b/launch.py index eafb441..7ca4923 100755 --- a/launch.py +++ b/launch.py @@ -1,26 +1,26 @@ #!/usr/bin/python import pickle -import commands import sys import os import subprocess +import collections from operator import itemgetter CACHE_FILE = os.getenv('HOME')+'/.launch' 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: @@ -34,14 +34,13 @@ 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) 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,'rb') as f: obj = pickle.load(f) f.close() return(obj) @@ -69,19 +68,20 @@ def update(): store(cache_new,CACHE_FILE) def dmenu(pgm_list): - p = subprocess.Popen([DMENU,"-l","10"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) - out = p.communicate("\n".join(pgm_list))[0] + 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.iteritems(), key=itemgetter(1), reverse=True) - pgm_list = [ x[0] for x in sorted_list ] - out = dmenu(pgm_list) + pgm_list = cache.keys() + 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): @@ -100,10 +100,12 @@ def run(): 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) + sorted_cache = sorted(cache.items(), key=itemgetter(1), reverse=True) + ordered_cache = collections.OrderedDict(sorted_cache) + store(ordered_cache,CACHE_FILE) run()