|
| 1 | +import os |
| 2 | +import json |
| 3 | +import sys |
| 4 | +import ctypes, time, subprocess |
| 5 | +import getpass |
| 6 | +import webbrowser |
| 7 | +from win32com.client import Dispatch |
| 8 | + |
| 9 | +# file in which all configurations will be stored |
| 10 | +FILE = 'config.json' |
| 11 | + |
| 12 | +# return value used in case of success for some functions |
| 13 | +# it is set to '' so as to append error messages if error happen |
| 14 | +SUCCESS = '' |
| 15 | + |
| 16 | +# set of commands used to check user input in the main method |
| 17 | +RESET = ['r', 'reset', 'rm', 'remove'] |
| 18 | +CREATE = ['c', 'create', 'cr'] |
| 19 | +YES = ['yes', 'y', 'ya', 'ye'] |
| 20 | + |
| 21 | +# gets the username of C:\Users\username |
| 22 | +USER = getpass.getuser() |
| 23 | + |
| 24 | +isFile = lambda x : os.path.isfile(x) |
| 25 | +rm = lambda x : os.remove(x) |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +class Config: |
| 30 | + ''' |
| 31 | + This class represents a configuration object |
| 32 | + such configuration has a list of strings, each string in this list should be the path to the |
| 33 | + executable file of a program. |
| 34 | + the n_desktops attribute states how many desktops will be used to start such programs |
| 35 | + the name of the configuration is the way this configuration is identified, it should be unique for |
| 36 | + each configuration |
| 37 | + ''' |
| 38 | + |
| 39 | + def __init__(self, programs = None, n_desktops = 1, name = 'main'): |
| 40 | + self.programs = programs |
| 41 | + self.n_desktops = n_desktops |
| 42 | + self.name = name |
| 43 | + |
| 44 | + def __str__(self): |
| 45 | + return str(self.__dict__) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +def reset_config(filename): |
| 50 | + if isFile(filename): rm(filename) |
| 51 | + |
| 52 | + |
| 53 | +def save_configs(configs, filename): |
| 54 | + json_str = json.dumps([c.__dict__ for c in configs]) |
| 55 | + with open(filename, 'w') as f: |
| 56 | + f.write(json_str) |
| 57 | + |
| 58 | + |
| 59 | +def load_configs(filename): |
| 60 | + if not isFile(filename) : return None |
| 61 | + |
| 62 | + with open(filename, 'r') as f: |
| 63 | + lines = json.load(f) |
| 64 | + |
| 65 | + return [Config(d['programs'], d['n_desktops'], d['name']) for d in lines] |
| 66 | + |
| 67 | + |
| 68 | +def find_config(configs, name): |
| 69 | + for c in configs: |
| 70 | + if c.name == name: return c |
| 71 | + return None |
| 72 | + |
| 73 | +def isUrl(program): |
| 74 | + if "http" in program or "www." in program: |
| 75 | + return True |
| 76 | + return False |
| 77 | + |
| 78 | +def isValid(program): |
| 79 | + return isUrl(program) or isFile(program) |
| 80 | + |
| 81 | +def run_config(configs, name): |
| 82 | + config = find_config(configs, name) |
| 83 | + err_code = SUCCESS |
| 84 | + if config : |
| 85 | + virtual_desktop_accessor = ctypes.WinDLL("./VirtualDesktopAccessor.dll") |
| 86 | + for i in range(len(config.programs)): |
| 87 | + program = config.programs[i] |
| 88 | + if (i < config.n_desktops): |
| 89 | + virtual_desktop_accessor.GoToDesktopNumber(i) |
| 90 | + print("Go to screen ", i) |
| 91 | + |
| 92 | + if isUrl(program): |
| 93 | + webbrowser.open(program) |
| 94 | + else: |
| 95 | + subprocess.Popen(program, close_fds=True) |
| 96 | + |
| 97 | + time.sleep(1) |
| 98 | + print("Run ", program) |
| 99 | + else : |
| 100 | + err_code = "No config {0} found".format(name) |
| 101 | + return err_code |
| 102 | + |
| 103 | +def create_config(programs, nScreen, name, filename = FILE): |
| 104 | + configs = load_configs(FILE) |
| 105 | + if name.strip() == "": |
| 106 | + name = 'main' |
| 107 | + |
| 108 | + config = Config(programs, nScreen, name) |
| 109 | + if configs : |
| 110 | + configs.append(config) |
| 111 | + save_configs(configs, filename) |
| 112 | + else : |
| 113 | + save_configs([config], filename) |
| 114 | + |
| 115 | + create_executable(config) |
| 116 | + |
| 117 | +def create_executable(config): |
| 118 | + print(config) |
| 119 | + filename = config.name + ".bat" |
| 120 | + print(filename) |
| 121 | + f = open(filename, "w") |
| 122 | + f.write("python configurer.py {0}".format(config.name)) |
| 123 | + f.close() |
| 124 | + |
| 125 | + createShortcut(filename, config.name, icon="C:\\Users\\Ugo\\Documents\\Projet\\FastRun\\icone\\beeboop.ico") |
| 126 | + |
| 127 | + |
| 128 | +def createShortcut(filename, name, icon=''): |
| 129 | + desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') |
| 130 | + shell = Dispatch('WScript.Shell') |
| 131 | + shortcut = shell.CreateShortCut(desktop + "\\" + name + ".lnk") |
| 132 | + shortcut.Targetpath = os.path.abspath(os.getcwd()) + "\\" + filename |
| 133 | + shortcut.WorkingDirectory = os.path.abspath(os.getcwd()) |
| 134 | + if icon == '': |
| 135 | + pass |
| 136 | + else: |
| 137 | + shortcut.IconLocation = icon |
| 138 | + shortcut.save() |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + configs = load_configs(FILE) |
| 143 | + argv = sys.argv |
| 144 | + |
| 145 | + if configs : |
| 146 | + if len(argv) == 1: |
| 147 | + error = run_config(configs, configs[0].name) |
| 148 | + if error != SUCCESS: print(error) |
| 149 | + |
| 150 | + elif argv[1].lower() in RESET: |
| 151 | + reset_config(FILE) |
| 152 | + |
| 153 | + else : |
| 154 | + error = run_config(configs, argv[1]) |
| 155 | + if error != SUCCESS: print(error) |
| 156 | + |
| 157 | + else : |
| 158 | + create_config(argv[1], argv[2], argv[3]) |
0 commit comments