示例#1
0
 def addHotKeysFromFile(self, filename):
     print("[Config] Parsing hotkeys config file: " +
           Parser.getShortFilePath(filename))
     if not os.path.exists(filename) or not os.path.isfile(filename):
         print("Error! %s file are not exists!" % filename)
         return
     stopParsing = False
     addedActions = list()
     for line in open(filename, "r"):
         command = su.trimComments(line)
         if not command:
             continue
         if command == TOKEN.END:
             self.key_bindings.append("#IfWinActive")
         elif command.startswith('[') and command.endswith(']'):
             if Parser.gotIgnoreToken(command, addedActions, filename):
                 break
             self.key_bindings.append("\n#IfWinActive " +
                                      Parser.getApp(command))
         elif command.startswith(TOKEN.BIND):
             if len(command.split(' ', 3)) != 4:
                 print("Error! failed to parse line: " + command)
                 continue
             (bind_token, key, action, data) = command.split(' ', 3)
             self.setHotKeyAction(key, action, data)
             addedActions.append(action)
         else:
             print("[addHotKeysFromFile] Error: Failed to parse string: " +
                   command)
 def addHotKeysFromFile(self, filename):
     print("[Config] Parsing hotkeys config file: " + Parser.getShortFilePath(filename))
     if not os.path.exists(filename) or not os.path.isfile(filename):
         print("Error! %s file are not exists!" % filename)
         return
     stopParsing = False
     addedActions = list()
     for line in open(filename, "r"):
         command = su.trimComments(line)
         if not command:
             continue
         if command == TOKEN.END:
             self.key_bindings.append("#IfWinActive")
         elif command.startswith('[') and command.endswith(']'):
             if Parser.gotIgnoreToken(command, addedActions, filename):
                 break
             self.key_bindings.append("\n#IfWinActive " + Parser.getApp(command))
         elif command.startswith(TOKEN.BIND):
             if len(command.split(' ', 3)) != 4:
                 print("Error! failed to parse line: " + command)
                 continue
             (bind_token, key, action, data) = command.split(' ', 3)
             self.setHotKeyAction(key, action, data)
             addedActions.append(action)
         else:
             print("[addHotKeysFromFile] Error: Failed to parse string: " + command)
    def createPrintTextMenuFromFile(filename, builder):
        print("[Config] Parsing menu config file: " + Parser.getShortFilePath(filename))
        if not os.path.exists(filename) or not os.path.isfile(filename):
            print("Error! %s file are not exists!" % filename)
            return

        menu = None
        deleteOnClick = False
        clipSave = True
        method = None
        import json
        data = json.loads(Parser.readFileAsString(filename))
        if not len(data['MenuItems']):
            error("Invalid JSON file [%s] MenuItems dictionary is empty or invalid!" % filename)

        if data['MenuSettings'] and data['MenuSettings']['Hotkey']:
            hotKey = data['MenuSettings']['Hotkey']
            menu = Menu(hotKey, hotKey, builder)
        else:
            error("Invalid JSON file [%s] MenuSettings.Hotkey value is not provided!" % filename)

        if data['MenuSettings']['clipSave'] == 'false':
            clipSave = False
        elif data['MenuSettings']['clipSave'] == 'true':
            clipSave = True

        if data['MenuSettings']['deleteOnClick'] == 'true':
            deleteOnClick = True
        elif data['MenuSettings']['deleteOnClick'] == 'false':
            deleteOnClick = False
        else:
            error("Invalid JSON file [%s] MenuSettings.deleteOnClick value is not provided!")

        if data['MenuSettings']['mode'] == 'print':
            method = menu.addPrintText
        elif data['MenuSettings']['mode'] == 'clipboard' or data['MenuSettings']['mode'] == 'paste':
            method = menu.addPasteText
        else:
            error("Invalid JSON file [%s] MenuSettings.mode value is not provided!")

        for key, subMenu in data['MenuItems'].iteritems():
            if key == 'MenuSettings':
                continue
            if isinstance(subMenu, dict):
                for itemName, itemValue in subMenu.iteritems():
                    method(itemName, itemValue, subMenuName=key, deleteOnClick=deleteOnClick, clipSave=clipSave)
                menu.attachSubMenu(hotKey, key, key)
            if isinstance(subMenu, list):
                for item in subMenu:
                    #If got empty json element ("",) - skip it
                    if not item:
                        continue
                    itemName = item
                    if len(item) > 80:
                        itemName = item[:80]
                    method(itemName, item, subMenuName=key, deleteOnClick=deleteOnClick, clipSave=clipSave)
                menu.attachSubMenu(hotKey, key, key)
        if menu:
            menu.assignMenuHotKey()
示例#4
0
    def addAutoCompleteFromFile(self, filename):
        print("[Config] Parsing autocomplete config file: " +
              Parser.getShortFilePath(filename))
        if not os.path.exists(filename) or not os.path.isfile(filename):
            print("Error! %s file are not exists!" % filename)
            return
        stopParsing = False
        applicationChosen = None
        autoCompleteAppData = None
        for line in open(filename, "r"):
            command = su.trimComments(line)
            if command:
                if command.startswith('[') and command.endswith(
                        ']'):  #Works only for selected application
                    if Parser.gotIgnoreToken(command, autoCompleteAppData,
                                             filename):
                        stopParsing = True
                    if applicationChosen and len(
                            autoCompleteAppData) or stopParsing:
                        print("[addAutoCompleteForApp] writing %s entries" %
                              len(autoCompleteAppData))
                        self.addAutoCompleteForApp(
                            application=applicationChosen,
                            data=autoCompleteAppData)
                        if stopParsing:
                            break
                    if command == TOKEN.END:
                        applicationChosen = None
                        continue

                    applicationChosen = Parser.getApp(command)
                    autoCompleteAppData = dict()
                    print("[addAutoCompleteForApp] Section for: " +
                          applicationChosen)
                    continue
                if applicationChosen:
                    if '==' in command:
                        keyAndCommand = command.split('==')
                        if len(keyAndCommand) != 2:
                            print("Error! failed to parse; " + command)
                            continue
                        autoCompleteAppData[
                            keyAndCommand[0]] = keyAndCommand[1]
                    else:
                        name = self.createTextAlias(command)
                        if name:
                            autoCompleteAppData[name] = command
                elif '==' in command:  #Works in all applications
                    keyAndCommand = command.split('==')
                    if len(keyAndCommand) != 2:
                        print("Error! failed to parse; " + command)
                        continue
                    status = self.addAutoComplete(keyAndCommand[0],
                                                  keyAndCommand[1])
                else:
                    status = self.addAutoCompleteSmart(command)
    def addAutoCompleteFromFile(self, filename):
        print("[Config] Parsing autocomplete config file: " + Parser.getShortFilePath(filename))
        if not os.path.exists(filename) or not os.path.isfile(filename):
            print("Error! %s file are not exists!" % filename)
            return
        stopParsing = False
        applicationChosen = None
        autoCompleteAppData = None
        for line in open(filename, "r"):
            command = su.trimComments(line)
            if command:
                if command.startswith('[') and command.endswith(']'): #Works only for selected application
                    if Parser.gotIgnoreToken(command, autoCompleteAppData, filename):
                        stopParsing = True
                    if applicationChosen and len(autoCompleteAppData) or stopParsing:
                        print("[addAutoCompleteForApp] writing %s entries" % len(autoCompleteAppData))
                        self.addAutoCompleteForApp(application=applicationChosen, data=autoCompleteAppData)
                        if stopParsing:
                            break
                    if command == TOKEN.END:
                        applicationChosen = None
                        continue

                    applicationChosen = Parser.getApp(command)
                    autoCompleteAppData = dict()
                    print("[addAutoCompleteForApp] Section for: " + applicationChosen)
                    continue
                if applicationChosen:
                    if '==' in command:
                        keyAndCommand = command.split('==')
                        if len(keyAndCommand) != 2:
                            print("Error! failed to parse; " + command)
                            continue
                        autoCompleteAppData[keyAndCommand[0]] = keyAndCommand[1]
                    else:
                        name = self.createTextAlias(command)
                        if name:
                            autoCompleteAppData[name] = command
                elif '==' in command: #Works in all applications
                    keyAndCommand = command.split('==')
                    if len(keyAndCommand) != 2:
                        print("Error! failed to parse; " + command)
                        continue
                    status = self.addAutoComplete(keyAndCommand[0], keyAndCommand[1])
                else:
                    status = self.addAutoCompleteSmart(command)
示例#6
0
    def createPrintTextMenuFromFile(filename, builder):
        print("[Config] Parsing menu config file: " +
              Parser.getShortFilePath(filename))
        if not os.path.exists(filename) or not os.path.isfile(filename):
            print("Error! %s file are not exists!" % filename)
            return

        menu = None
        deleteOnClick = False
        clipSave = True
        method = None
        import json
        data = json.loads(Parser.readFileAsString(filename))
        if not len(data['MenuItems']):
            error(
                "Invalid JSON file [%s] MenuItems dictionary is empty or invalid!"
                % filename)

        if data['MenuSettings'] and data['MenuSettings']['Hotkey']:
            hotKey = data['MenuSettings']['Hotkey']
            menu = Menu(hotKey, hotKey, builder)
        else:
            error(
                "Invalid JSON file [%s] MenuSettings.Hotkey value is not provided!"
                % filename)

        if data['MenuSettings']['clipSave'] == 'false':
            clipSave = False
        elif data['MenuSettings']['clipSave'] == 'true':
            clipSave = True

        if data['MenuSettings']['deleteOnClick'] == 'true':
            deleteOnClick = True
        elif data['MenuSettings']['deleteOnClick'] == 'false':
            deleteOnClick = False
        else:
            error(
                "Invalid JSON file [%s] MenuSettings.deleteOnClick value is not provided!"
            )

        if data['MenuSettings']['mode'] == 'print':
            method = menu.addPrintText
        elif data['MenuSettings']['mode'] == 'clipboard' or data[
                'MenuSettings']['mode'] == 'paste':
            method = menu.addPasteText
        else:
            error(
                "Invalid JSON file [%s] MenuSettings.mode value is not provided!"
            )

        for key, subMenu in data['MenuItems'].iteritems():
            if key == 'MenuSettings':
                continue
            if isinstance(subMenu, dict):
                for itemName, itemValue in subMenu.iteritems():
                    method(itemName,
                           itemValue,
                           subMenuName=key,
                           deleteOnClick=deleteOnClick,
                           clipSave=clipSave)
                menu.attachSubMenu(hotKey, key, key)
            if isinstance(subMenu, list):
                for item in subMenu:
                    #If got empty json element ("",) - skip it
                    if not item:
                        continue
                    itemName = item
                    if len(item) > 80:
                        itemName = item[:80]
                    method(itemName,
                           item,
                           subMenuName=key,
                           deleteOnClick=deleteOnClick,
                           clipSave=clipSave)
                menu.attachSubMenu(hotKey, key, key)
        if menu:
            menu.assignMenuHotKey()