Skip to content

Commit

Permalink
Rewrote adding and removing entry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkHedleyJones committed Jun 6, 2016
1 parent d0ceb4e commit 5aeafa3
Showing 1 changed file with 203 additions and 97 deletions.
300 changes: 203 additions & 97 deletions dmenu_extended.py
Expand Up @@ -616,7 +616,7 @@ def retrieve_aliased_command(self, alias):
if self.debug:
print("Converting '" + str(alias) + "' into its aliased command")
for item in aliases:
if item[0] == alias:
if self.format_alias(item[0], item[1]) == alias:
if self.debug:
print("Converted " + alias + " to: " + item[1])
return item[1]
Expand Down Expand Up @@ -810,9 +810,8 @@ def cache_build(self):
for item in self.prefs['include_items']:
if type(item) == list:
if len(item) > 1:
title = self.format_alias(item[0], item[1])
aliased_items.append(title)
aliases.append([title, item[1]])
aliased_items.append(self.format_alias(item[0], item[1]))
aliases.append([item[0], item[1]])
else:
if self.debug:
print("There are aliased items in the configuration with no command.")
Expand Down Expand Up @@ -1195,79 +1194,208 @@ def run(debug=False):
if out[0] in "+-":
action = out[0]
out = out[1:]
aliased = False
# Check for aliased command
if action == '+':
aliased = True
tmp = out.split('#')

if d.debug:
print("tmp = " + str(tmp))
# tmp is used to split the input into a command and alias (if any)
tmp = out.split('#')

command = tmp[0].rstrip()
if len(tmp) > 1:
out = d.format_alias(tmp[1].lstrip(), command.replace(';',''))
else:
command = tmp[0]
if len(tmp) > 1:
alias = ' '.join(tmp[1:]).lstrip().rstrip()
else:
alias = None

if d.debug:
print("out = '" + str(out) + "'")
print("tmp = '" + str(tmp) + "'")
print("action = '" + str(action) + "'")
print("command = '" + str(command) + "'")
print("alias '= " + str(alias) + "'")



# aliased = False
# # Check for aliased command
# if action == '+':
# aliased = True
# tmp = out.split('#')

# if d.debug:
# print("tmp = " + str(tmp))

# command = tmp[0].rstrip()
# if len(tmp) > 1:
# out = d.format_alias(tmp[1].lstrip(), command.replace(';',''))
# else:
# if d.debug:
# print("This command is not aliased")
# aliased = False
# out = d.format_alias(None, command.replace(';',''))

# if aliased == 0:
# item = command
# else:
# item = [out, command]

# if d.debug:
# print("Item = " + str(item))
# elif out[:len(d.prefs['indicator_alias'])] == d.prefs['indicator_alias']:
# item = out[len(d.prefs['indicator_alias']):].lstrip()
# aliased = True
# else:
# item = out

# Check to see if the item is in the include_items list
found_in_store = False
if d.debug:
print("Command = '" + str(command) + "', alias = '" + str(alias) + "'")
print("Starting to match given command with store elements")

for item in d.prefs['include_items']:
if action == '+' and type(item) == list:
if d.debug:
print("This command is not aliased")
aliased = False
out = d.format_alias(None, command.replace(';',''))
print("Is (+) " + str(item[0]) + " == " + str(alias) + "?")
if alias == item[0]:
if d.debug:
print("Yes")
found_in_store = True
break
elif d.debug:
print("No")

if aliased == 0:
item = command
else:
item = [out, command]
# If removing a command - an alias would be detected as a command

if action == '-' and type(item) == list:
if d.debug:
print("Is (-) " + str(d.format_alias(item[0], item[1])) + " == " + str(command) + "?")
if command == d.format_alias(item[0], item[1]):
found_in_store = True
alias = command
if d.prefs['indicator_alias'] != '':
alias = alias[len(d.prefs['indicator_alias'])+1:]
command = item[1]
if d.debug:
print("Yes")
print("Command is now: " + str(command))
print("Alias is now: " + str(alias))
break
elif d.debug:
print("No")

if d.debug:
print("Item = " + str(item))
elif out[:len(d.prefs['indicator_alias'])] == d.prefs['indicator_alias']:
item = out[len(d.prefs['indicator_alias']):].lstrip()
aliased = True
else:
item = out
if d.debug:
print("Is (-) " + str(d.format_alias(item[0], item[1])) + " == " + str(out) + "?")
if out == d.format_alias(item[0], item[1]):
found_in_store = True
alias = item[0]
if d.prefs['indicator_alias'] != '':
alias = alias[len(d.prefs['indicator_alias'])+1:]
command = item[1]
if d.debug:
print("Yes")
print("Command is now: " + str(command))
print("Alias is now: " + str(alias))
break
elif d.debug:
print("No")

found_in_store = False
for store_item in d.prefs['include_items']:
if d.debug:
print("is " + str(store_item) + " = " + str(item) + " ?")
if type(store_item) == list and out == store_item[0]:
found_in_store = True
break;
elif item == store_item:
found_in_store = True
break;

if action == '+' and found_in_store:
if d.debug:
print("Is (-) " + str(item[0]) + " == " + str(command) + "?")
if command == item[0]:
found_in_store = True
alias = item[0]
if d.prefs['indicator_alias'] != '':
alias = alias[len(d.prefs['indicator_alias'])+1:]
command = item[1]
if d.debug:
print("Yes")
print("Command is now: " + str(command))
print("Alias is now: " + str(alias))
break
elif d.debug:
print("No")

if type(item) != list:
if d.debug:
print("Is " + str(item) + " == " + str(command) + "?")
if command == item:
if d.debug:
print("Yes")
found_in_store = True
break
elif d.debug:
print("No")

if action == '+' and found_in_store == True:
option = d.prefs['indicator_submenu'] + " Remove from store"
answer = d.menu("Item '" + str(item) + "' already in store\n"+option)
if alias is None:
answer = d.menu("Command '" + str(command) + "' already in store\n"+option)
else:
answer = d.menu("Alias '" + str(alias) + "' already in store\n"+option)
if answer != option:
sys.exit()
action = '-'
elif action == '-' and found_in_store == False:
option = d.prefs['indicator_submenu'] + " Add to store"
answer = d.menu("Item '" + (item) + "' was not found in store\n"+option)
if alias is None:
answer = d.menu("Command '" + str(command) + "' was not found in store\n"+option)
else:
answer = d.menu("Alias '" + str(alias) + "' was not found in store\n"+option)
if answer != option:
sys.exit()
action = '+'
action = '+'


cache_scanned = d.cache_open(file_cache)[:-1]

if cache_scanned == False:
d.cache_regenerate()
d.message_close()
sys.exit()
else:
cache_scanned = cache_scanned.split("\n")



if action == '+':
d.prefs['include_items'].append(item)
# Add the item to the alias lookup file
if aliased:

if alias is None:
if d.debug:
print("Adding '" + str(command) + "' to store")
d.prefs['include_items'].append(command)
d.message_open("Adding item to store: " + str(command))
cache_scanned = [command] + cache_scanned
else:
if d.debug:
print("Adding aliased command '" + str([alias, command]) + "' to store")
d.prefs['include_items'].append([alias, command])

# And add the item to the alias lookup file
aliases = d.load_json(file_cache_aliasesLookup)
if item not in aliases:
aliases.append([
out,
item[1]
])
if [alias, command] not in aliases:
aliases.append([alias, command])
d.save_json(file_cache_aliasesLookup, aliases)

d.message_open("Adding aliased item item to store: " + str(d.format_alias(alias, command)))
cache_scanned = [d.format_alias(alias, command)] + cache_scanned

cache_scanned.sort(key=len)
elif action == '-':
if aliased:
if alias is None:
if d.debug:
print("Will try to remove command: '" + str(command) + "' from the included items")
d.prefs['include_items'].remove(command)
d.message_open("Removing item from store: " + str(command))
try:
cache_scanned.remove(command)
except ValueError:
if d.debug:
print("Couldnt remove item from the cache")
else:
pass
else:
to_remove = None
for include_item in d.prefs['include_items']:
if include_item[0] == out:
to_remove = include_item
for item in d.prefs['include_items']:
if item[0] == alias:
to_remove = item
if to_remove is not None:
if d.debug:
print("Item found and is")
Expand All @@ -1276,57 +1404,35 @@ def run(debug=False):
else:
if d.debug:
print("Couldn't remove the item (item could not be located)")
else:
if d.debug:
print("Will try to remove: '" + str(item) + "' from the included items")
d.prefs['include_items'].remove(item)

d.message_open("Removing aliased item from store: " + str(d.format_alias(alias, command)))
try:
cache_scanned.remove(d.format_alias(alias, command))
except ValueError:
if d.debug:
print("Couldnt remove item from the cache")
else:
pass
else:
d.message_close()
d.menu("An error occured while servicing your request.\nYou may need to delete your configuration file.")
sys.exit()

d.save_preferences()

# Recreate the cache

cache_scanned = d.cache_open(file_cache)[:-1]

if cache_scanned == False:
d.cache_regenerate()
d.message_close()
sys.exit()
else:
cache_scanned = cache_scanned.split("\n")

if action == '+':
if d.debug:
print("Adding item to store: " + out)
d.message_open("Adding item to store: " + out)
cache_scanned = [out] + cache_scanned
cache_scanned.sort(key=len)
else:
to_remove = out
d.message_open("Removing item from store: " + to_remove)
if d.debug:
print("Looking to remove '" + str(to_remove) + "' from the cache")
try:
cache_scanned.remove(to_remove)
except ValueError:
if d.debug:
print("Couldnt actually remove item from the cache")
else:
pass

d.cache_save(cache_scanned, file_cache)

d.cache_save(cache_scanned, file_cache)
d.message_close()

# Give the user some feedback
if action == '+':
if aliased == True:
message = "New item (" + command + " aliased as '" + out + "') added to cache."
if alias is None:
message = "New item (" + command + ") added to cache."
else:
message = "New item (" + out + ") added to cache."
message = "New item (" + command + " aliased as '" + alias + "') added to cache."
else:
message = "Existing item (" + out + ") removed from cache."
if alias is None:
message = "Existing item (" + command + ") removed from cache."
else:
message = "Existing alias (" + alias + ") removed from cache."

d.menu(message)
sys.exit()
Expand Down

0 comments on commit 5aeafa3

Please sign in to comment.