Skip to content

Instantly share code, notes, and snippets.

@GuillermoBlasco
Last active August 11, 2019 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GuillermoBlasco/9586426 to your computer and use it in GitHub Desktop.
Save GuillermoBlasco/9586426 to your computer and use it in GitHub Desktop.
open
{
"file" : {
"py":"subl",
"java":"subl",
"cpp":"subl",
"cxx":"subl",
"hpp":"subl",
"h":"subl",
"c":"subl",
"sh":"subl",
"xml":"subl",
"properties":"subl",
"json":"subl",
"dia":"dia",
"png":"eog",
"jpeg":"eog",
"jpg":"eog",
"pdf":"evince",
"doc":"libreoffice",
"html":"chromium"
},
"dir" : "nautilus"
}
#!/usr/bin/python
import sys
import os
import subprocess
import json
MAP_FILE_NAME = "./map.json"
def main(arguments):
commands = processCommands(arguments)
executeCommands(commands)
sys.exit(0)
def processCommands(values):
extensionMap = getMap()
values = values[1:] # remove script invokation
assignation = {value : getProgramOf(value, extensionMap) for value in values}
commands = [toCommand(value, program) for value, program in assignation.iteritems()]
return commands
def executeCommands(commands):
for command in commands:
print command
subprocess.call(command, shell=True)
def toCommand(value, program):
return program + " " + value
def getProgramOf(value, extensionMap):
if os.path.isdir(value):
return extensionMap['dir']
elif os.path.isfile(value) or not os.path.exists(value):
if os.sep in value:
split = value.split(os.sep)
filepath, filenameComplete = split[:-1], split[-1]
else:
filepath, filenameComplete = "", value
if "." in filenameComplete:
split = value.split(".")
filename, extension = split[:-1], split[-1]
else:
filename, extension = value, ""
program = extensionMap['file'][extension]
return program
else:
assert False
def getMap():
file = getMapFile()
content = file.read()
extensionMap = json.loads(content)
return extensionMap
def getMapFile():
return open(MAP_FILE_NAME, 'r')
if __name__ == "__main__":
main(sys.argv)
#!/bin/sh
output=$(python ./open.py "$@")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment