You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

  1. import subprocess
  2. from binaryornot.check import is_binary
  3. import os
  4. import re
  5. EDITOR_PATH = os.path.expandvars(r'%programfiles%\Notepad++\notepad++.exe')
  6. EXT = re.compile(r'\.\w+$')
  7. def get_ext(file):
  8. match = EXT.search(file)
  9. if match:
  10. return match.group(0)
  11. handlers = {
  12. '.py' : {
  13. 'editor': EDITOR_PATH,
  14. 'execute': 'python',
  15. 'idle_edit': 'pythonw -m idlelib -e',
  16. 'windowless': 'pythonw'
  17. }
  18. }
  19. def edit(path,params=[]):
  20. call_chain = [EDITOR_PATH] + params
  21. if not is_binary(path):
  22. call_chain.append(path)
  23. subprocess.call(call_chain)
  24. def standalone(path,params=[],windowless =False,cwd = None):
  25. path2windowless = os.path.join(os.path.dirname(__file__),'windowless.vbs')
  26. params = list(map(os.path.expandvars,params))
  27. if not cwd:
  28. cwd = os.path.dirname(path)
  29. else:
  30. cwd = os.path.expandvars(cwd)
  31. if windowless:
  32. cmd = ' '.join('~{}~'.format(arg) if ' ' in arg else arg for arg in [fullpath]+params)
  33. print(cmd)
  34. subprocess.check_output(['cscript.exe',path2windowless,cmd,cwd])
  35. else:
  36. subprocess.Popen([fullpath]+params,cwd=cwd)
  37. def dependant(path,params=[],mode = 'execute'):
  38. ext = get_ext(path)
  39. if ext:
  40. handler = handlers[ext][mode]
  41. if ' ' in handler:
  42. handler = handler.split(' ')
  43. else:
  44. handler = [handler]
  45. args = handler+[path]+params
  46. subprocess.Popen(args)