Beispiel #1
0
    def load_one_layer_at(self, path, sorce_layer: int, target_layer: int):
        # release all macro ids in the about to be replaced tab
        tab = self.tabs[target_layer]
        ids = tab.keymap.get_all_macro_ids_in_use()
        for id in ids:
            MacroIDPool.mark_as_not_in_use(id)

        with open(path, "rb") as f:
            layers = pickle.load(f)
            l = layers[sorce_layer]
            self.tabs[target_layer].set_keymap_data(l)
Beispiel #2
0
 def load_keymap(self, path):
     """
     Loads pickled keymap data.
     :param path:
     :return:
     """
     MacroIDPool.reset()
     with open(path, "rb") as f:
         layers = pickle.load(f)
         for i, l in enumerate(layers):
             self.tabs[i].set_keymap_data(l)
Beispiel #3
0
    def _handle_macro_id_conflict(self, d: dict):
        """
        Should be called only when loading keymap or layer from external source.
        :param d:
        :return:
        """
        # data should be shared but be on the safe side
        # make sure changes are shared.
        p = KeyConfDictParser(d)
        if not p.is_macro():
            return
        try:
            d = d[key_macro]
        except KeyError as e:
            return  # no macro

        if type(d) is str:
            return  # generated by old script

        code = p.macro_code
        ids = p.macro_ids

        if type(ids) is not list:
            raise TypeError("supposed to be a list of ids but got:%s" %
                            str(ids))
        if len(ids) > 2:
            raise ValueError("Too many ids are bound to a single key.")

        # remove all old ids and give them anew
        new_ids = []
        new_code = code
        for id in ids:
            if not MacroIDPool.is_id_in_use(id):
                MacroIDPool.mark_as_in_use([id])
                new_ids.append(ids)
                continue
            new_id = MacroIDPool.get_id()
            new_ids.append(new_id)

            new_code = re.sub("M\(\s*" + str(id) + "\s*\)", "M(%s)" % (new_id),
                              code)
            new_code = re.sub("case \s*" + str(id) + "\s*:",
                              "case %s:" % (new_id), new_code)
            code = new_code

        g = KeyConfDictGenerator()
        g.set_macro_type(p.macro_type)
        g.set_macro_ids(new_ids)
        g.set_code(new_code)
        g.set_macro_name(p.macro_name)
        self.keymap.update_key_data(d, g.to_macro_dict())
 def assign_id_if_no_id_bound(self):
     # to be removed, handle old macro code generation script that
     # do not assign a macro id.
     if self.is_macro() and self.macro_type == "" and len(
             self.macro_ids) == 0:
         id = MacroIDPool.get_id()
         self.macro_ids.append(id)
         case_statement = "case %s:\n" % (id)
         self.macro_code = case_statement + self.macro_code + "\n break;"
Beispiel #5
0
    def set_code_for_clt_receptor(self, code):
        while len(self.macro_ids) < 2:
            self.macro_ids.append(MacroIDPool.get_id())

        self.code = ""
        macro_id = self.macro_ids[1]
        macro_id2 = self.macro_ids[0]

        # macro_id is at 1 position of self.macro_ids
        self.code += "\n case %s:\n %s" % (macro_id, code.strip())

        # macro_id2 is at 0 position of self.macro_ids
        self.code += "\n case %s:\n process_combo_lt_receptor(record,%s,M(%s)); break;" \
                     % (macro_id2,macro_id2,macro_id)
Beispiel #6
0
    def set_code(self, codes: list):
        self.code = ""
        while len(self.macro_ids) < len(codes):
            self.macro_ids.append(MacroIDPool.get_id())

        for i, code in enumerate(codes):
            macro_id = self.macro_ids[i]
            # add macro name as comment to macro code
            macro_comment = "// " + self.macro_name_line_edit.text() + " \n"

            self.code += """
        case %s:
            %s
            %s
            break\n;
        """ % (macro_id, macro_comment, code.strip())
 def release_macro_ids(self):
     if len(self.macro_ids) == 0:
         return
     MacroIDPool.mark_as_not_in_use(self.macro_ids)