def main(a2, mod):
    """
    :param a2: Main A2 object instance.
    :param mod: Current a2 module instance.
    """
    print('Import Hotstrings ... %s' % __name__)

    from PySide2 import QtWidgets

    file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
        None, 'Import Hotstrings Data', a2.paths.a2, '(*.ahk *.json)')

    if not file_path:
        return

    import os
    import a2util
    import pprint
    import hotstrings_io
    name = 'hotstrings'
    base, ext = os.path.splitext(file_path)
    ext = ext.lower()
    if ext == '.ahk':
        hs_input = hotstrings_io.file_to_dict(file_path)
    elif ext == '.json':
        hs_input = a2util.json_read(file_path)

    hs_current = mod.get_user_cfg()[name]
    hs_collisions = {}
    for mode, scope, hstring, hs_cfg in hotstrings_io.iterate(hs_input):
        if mode == '':
            if hstring not in hs_current.get('', {}):
                target = hs_current
            else:
                target = hs_collisions
            target.setdefault('', {})[hstring] = hs_cfg
        else:
            if hstring not in hs_current.get(mode, {}).get(scope, {}):
                target = hs_current
            else:
                target = hs_collisions
            target.setdefault(mode, {}).setdefault(scope, {})[hstring] = hs_cfg

    mod.set_user_cfg({'name': name}, hs_current)
    a2.win.load_runtime_and_ui()
    a2.win.check_element(name)

    if hs_collisions:
        print('There were collisions:\n%s' % pprint.pformat(hs_collisions))
        backup_path = base + '_collisions.json'
        a2util.json_write(backup_path, hs_collisions)
        a2util.explore(backup_path)
Beispiel #2
0
    def create_element(self, NAME):
        # TODO: This needs to goto the a2element module
        name = NAME.lower()
        template = os.path.join(self.a2.paths.defaults, 'element.template.py')
        self.a2.db.set('new_element_cfg', self.element_cfg)
        with open(template) as fobj:
            new_element_code = fobj.read().format(
                description='Some element description ...',
                creation_date=a2util.get_date(),
                author_name=self.main.devset.author_name,
                element_name=name.title())

        if self.element_cfg['target'] == 'global':
            new_path = os.path.join(self.a2.paths.elements, name + '.py')

            if self.element_cfg['enlist']:
                a2element_init = os.path.join(self.a2.paths.elements, '__init__.py')
                with open(a2element_init) as fobj:
                    lines = fobj.read().split('\n')
                for i, line in enumerate(lines):
                    if line.startswith(DISPLAY_ELEMENTS_LABEL):
                        values = json.loads(line.split(' = ', 1)[1].replace("'", '"'))
                        values.append(name)
                        lines[i] = '%s = %s' % (DISPLAY_ELEMENTS_LABEL, str(sorted(values)).replace("u'", "'"))
                with open(a2element_init, 'w') as fobj:
                    fobj.write('\n'.join(lines))

                import a2element
                importlib.reload(a2element)
                a2element.get_list(force=True)
        else:
            new_path = os.path.join(self.main.mod.path, '%s_%s.py' %
                                    (a2ctrl.LOCAL_ELEMENT_ID, name))

        with open(new_path, 'w') as fobj:
            fobj.write(new_element_code)

        a2util.explore(new_path)
Beispiel #3
0
 def explore_a2data(self):
     a2util.explore(self.a2.paths.data)
Beispiel #4
0
 def explore_a2(self):
     a2util.explore(self.a2.paths.a2)
Beispiel #5
0
 def explore_mod(self):
     if self.mod is not None:
         a2util.explore(self.mod.path)
     else:
         self.explore_a2()
Beispiel #6
0
 def explore_path(self):
     a2util.explore(self.value)
Beispiel #7
0
 def explore_a2data(self):
     a2util.explore(self.a2.paths.data)
Beispiel #8
0
 def explore_a2(self):
     a2util.explore(self.a2.paths.a2)
Beispiel #9
0
 def explore_mod(self):
     if self.mod is not None:
         a2util.explore(self.mod.path)
     else:
         self.explore_a2()
Beispiel #10
0
 def explore_path(self):
     a2util.explore(self.value)