connectors/PYRO_dialog.py
author Schlumpf <schlumpf@kr-ll.de>
Fri, 15 Mar 2019 17:34:24 +0100
branchgenerate-button
changeset 2531 f5891ce3228e
parent 2521 48ebcbe7f19b
child 2537 eb4a4cc41914
permissions -rw-r--r--
Add "Generate Program" to toolbar in PLCOpenEditor standalone mode.

Currently you can find the "Generate Program" action only in the file menu,
but this action is very often used in standalone mode, so I added toolbuffon.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# See COPYING file for copyrights details.

from __future__ import absolute_import

from itertools import repeat, islice, chain
import wx

from connectors.SchemeEditor import SchemeEditor


model = [('host', _("Host:")),
         ('port', _("Port:"))]

# (scheme, model, secure)
models = [("LOCAL", [], False), ("PYRO", model, False), ("PYROS", model, True)]

Schemes = list(zip(*models)[0])

_PerSchemeConf = {sch: (mod, sec) for sch, mod, sec in models}


class PYRO_dialog(SchemeEditor):
    def __init__(self, scheme, *args, **kwargs):
        # ID selector is enabled only on PYROS (secure)
        self.model, self.EnableIDSelector = _PerSchemeConf[scheme]

        SchemeEditor.__init__(self, scheme, *args, **kwargs)

    # pylint: disable=unused-variable
    def SetLoc(self, loc):
        hostport, ID = list(islice(chain(loc.split("#"), repeat("")), 2))
        host, port = list(islice(chain(hostport.split(":"), repeat("")), 2))
        self.SetFields(locals())

    def GetLoc(self):
        if self.model:
            fields = self.GetFields()
            template = "{host}"
            if fields['port']:
                template += ":{port}"
            if fields['ID']:
                template += "#{ID}"

            return template.format(**fields)
        return ''