3 changed files with 77 additions and 52 deletions
-
63ext_open.py
-
52opener.py
-
14opts.py
@ -1,52 +1,11 @@ |
|||||
import subprocess |
|
||||
from binaryornot.check import is_binary |
|
||||
import os |
|
||||
import re |
|
||||
EDITOR_PATH = os.path.expandvars(r'%programfiles%\Notepad++\notepad++.exe') |
|
||||
EXT = re.compile(r'\.\w+$') |
|
||||
def get_ext(file): |
|
||||
match = EXT.search(file) |
|
||||
if match: |
|
||||
return match.group(0) |
|
||||
|
|
||||
handlers = { |
|
||||
'.py' : { |
|
||||
'editor': EDITOR_PATH, |
|
||||
'execute': 'python', |
|
||||
'idle_edit': 'pythonw -m idlelib -e', |
|
||||
'windowless': 'pythonw' |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
def edit(path,params=[]): |
|
||||
call_chain = [EDITOR_PATH] + params |
|
||||
if not is_binary(path): |
|
||||
call_chain.append(path) |
|
||||
subprocess.call(call_chain) |
|
||||
|
|
||||
def standalone(path,params=[],windowless =False,cwd = None): |
|
||||
|
|
||||
path2windowless = os.path.join(os.path.dirname(__file__),'windowless.vbs') |
|
||||
params = list(map(os.path.expandvars,params)) |
|
||||
if not cwd: |
|
||||
cwd = os.path.dirname(path) |
|
||||
else: |
|
||||
cwd = os.path.expandvars(cwd) |
|
||||
if windowless: |
|
||||
cmd = ' '.join('~{}~'.format(arg) if ' ' in arg else arg for arg in [fullpath]+params) |
|
||||
print(cmd) |
|
||||
subprocess.check_output(['cscript.exe',path2windowless,cmd,cwd]) |
|
||||
else: |
|
||||
|
|
||||
subprocess.Popen([fullpath]+params,cwd=cwd) |
|
||||
|
|
||||
def dependant(path,params=[],mode = 'execute'): |
|
||||
ext = get_ext(path) |
|
||||
if ext: |
|
||||
handler = handlers[ext][mode] |
|
||||
if ' ' in handler: |
|
||||
handler = handler.split(' ') |
|
||||
else: |
|
||||
handler = [handler] |
|
||||
args = handler+[path]+params |
|
||||
subprocess.Popen(args) |
|
||||
|
from opts import * |
||||
|
import argparse |
||||
|
parser = argparse.ArgumentParser(prog="ext-open",prefix_chars = "+") |
||||
|
for opt in prefix: |
||||
|
parser.add_argument(**opt) |
||||
|
for ext in exts: |
||||
|
sub_parser = parser.add_subparser(ext["ext"]) |
||||
|
for opt in ext["opts"]+common: |
||||
|
sub_parser.add_argument(**opt) |
||||
|
if name == "__main__": |
||||
|
args = parser.parse_args() |
||||
@ -0,0 +1,52 @@ |
|||||
|
import subprocess |
||||
|
from binaryornot.check import is_binary |
||||
|
import os |
||||
|
import re |
||||
|
EDITOR_PATH = os.path.expandvars(r'%programfiles%\Notepad++\notepad++.exe') |
||||
|
EXT = re.compile(r'\.\w+$') |
||||
|
def get_ext(file): |
||||
|
match = EXT.search(file) |
||||
|
if match: |
||||
|
return match.group(0) |
||||
|
|
||||
|
handlers = { |
||||
|
'.py' : { |
||||
|
'editor': EDITOR_PATH, |
||||
|
'execute': 'python', |
||||
|
'idle_edit': 'pythonw -m idlelib -e', |
||||
|
'windowless': 'pythonw' |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
def edit(path,params=[]): |
||||
|
call_chain = [EDITOR_PATH] + params |
||||
|
if not is_binary(path): |
||||
|
call_chain.append(path) |
||||
|
subprocess.call(call_chain) |
||||
|
|
||||
|
def standalone(path,params=[],windowless =False,cwd = None): |
||||
|
|
||||
|
path2windowless = os.path.join(os.path.dirname(__file__),'windowless.vbs') |
||||
|
params = list(map(os.path.expandvars,params)) |
||||
|
if not cwd: |
||||
|
cwd = os.path.dirname(path) |
||||
|
else: |
||||
|
cwd = os.path.expandvars(cwd) |
||||
|
if windowless: |
||||
|
cmd = ' '.join('~{}~'.format(arg) if ' ' in arg else arg for arg in [fullpath]+params) |
||||
|
print(cmd) |
||||
|
subprocess.check_output(['cscript.exe',path2windowless,cmd,cwd]) |
||||
|
else: |
||||
|
|
||||
|
subprocess.Popen([fullpath]+params,cwd=cwd) |
||||
|
|
||||
|
def dependant(path,params=[],mode = 'execute'): |
||||
|
ext = get_ext(path) |
||||
|
if ext: |
||||
|
handler = handlers[ext][mode] |
||||
|
if ' ' in handler: |
||||
|
handler = handler.split(' ') |
||||
|
else: |
||||
|
handler = [handler] |
||||
|
args = handler+[path]+params |
||||
|
subprocess.Popen(args) |
||||
@ -0,0 +1,14 @@ |
|||||
|
prefix = [ |
||||
|
] |
||||
|
common [ |
||||
|
] |
||||
|
exts = [ |
||||
|
{ |
||||
|
"ext" : "py", |
||||
|
"opts" : [] |
||||
|
}, |
||||
|
{ |
||||
|
"ext" : "java", |
||||
|
"opts": [] |
||||
|
} |
||||
|
] |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue