Last active
August 11, 2019 21:56
-
-
Save GuillermoBlasco/9586426 to your computer and use it in GitHub Desktop.
open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
output=$(python ./open.py "$@") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment