Exemple #1
0
def loadGsd(name, debug=0):
    exceptions = []
    # Try name as a direct file name.
    try:
        return GsdInterp.fromFile(name, debug=(debug > 0))
    except GsdError as e:
        exceptions.append(str(e))
    # Try to interpret the name as a module name.
    try:
        if "/" not in name:
            mod = name.replace(".gsd", "_gsd")
            return GsdInterp.fromPy(mod, debug=(debug > 0))
    except GsdError as e:
        exceptions.append(str(e))
    # Try to interpret the name as a module name with leading fs path components stripped.
    try:
        slashIdx = name.rfind("/")
        if slashIdx >= 0:
            mod = name[slashIdx + 1:].replace(".gsd", "_gsd")
            return GsdInterp.fromPy(mod, debug=(debug > 0))
    except GsdError as e:
        exceptions.append(str(e))
    raise GsdError("\n".join(exceptions))
 def __interpErr(self, errorText):
     raise GsdError("GSD '%s': %s" %
                    (self.getFileName() or "<data>", errorText))
Exemple #3
0
    def __init__(self, fd, filename=None):
        def get(section, option, fallback=None):
            if p.has_option(section, option):
                return p.get(section, option)
            if fallback is None:
                raise ValueError("Option [%s] '%s' does not exist." %
                                 (section, option))
            return fallback

        def getboolean(section, option, fallback=None):
            if p.has_option(section, option):
                return p.getboolean(section, option)
            if fallback is None:
                raise ValueError("Option [%s] '%s' does not exist." %
                                 (section, option))
            return fallback

        def getint(section, option, fallback=None):
            if p.has_option(section, option):
                return p.getint(section, option)
            if fallback is None:
                raise ValueError("Option [%s] '%s' does not exist." %
                                 (section, option))
            return fallback

        try:
            p = _ConfigParser()
            if hasattr(p, "read_file"):
                p.read_file(fd, filename)
            else:
                p.readfp(fd, filename)

            # [PROFIBUS]
            self.debug = getint("PROFIBUS", "debug", fallback=0)

            # [PHY]
            self.phyType = get("PHY", "type", fallback="serial")
            self.phyDev = get("PHY", "dev", fallback="/dev/ttyS0")
            self.phyBaud = getint("PHY", "baud", fallback=9600)
            self.phyRtsCts = getboolean("PHY", "rtscts", fallback=False)
            self.phyDsrDtr = getboolean("PHY", "dsrdtr", fallback=False)
            self.phySpiBus = getint("PHY", "spiBus", fallback=0)
            self.phySpiCS = getint("PHY", "spiCS", fallback=0)
            self.phySpiSpeedHz = getint("PHY", "spiSpeedHz", fallback=1000000)

            # [DP]
            self.dpMasterClass = getint("DP", "master_class", fallback=1)
            if self.dpMasterClass not in {1, 2}:
                raise ValueError("Invalid master_class")
            self.dpMasterAddr = getint("DP", "master_addr", fallback=0x02)
            if self.dpMasterAddr < 0 or self.dpMasterAddr > 127:
                raise ValueError("Invalid master_addr")

            self.slaveConfs = []
            for section in p.sections():
                m = self.__reSlave.match(section)
                if not m:
                    continue
                index = int(m.group(1))
                s = self._SlaveConf()
                s.index = index
                s.name = get(section, "name", section)
                s.addr = getint(section, "addr")
                try:
                    s.gsd = GsdInterp.fromFile(get(section, "gsd"),
                                               debug=(self.debug > 0))
                except GsdError as origExc:
                    try:
                        s.gsd = GsdInterp.fromPy(get(section,
                                                     "gsd").replace(".", "_"),
                                                 debug=(self.debug > 0))
                    except GsdError as e:
                        raise GsdError("%s\n%s" % (str(origExc), str(e)))
                s.syncMode = getboolean(section, "sync_mode", fallback=False)
                s.freezeMode = getboolean(section,
                                          "freeze_mode",
                                          fallback=False)
                s.groupMask = getboolean(section, "group_mask", fallback=1)
                if s.groupMask < 0 or s.groupMask > 0xFF:
                    raise ValueError("Invalid group_mask")
                s.watchdogMs = getint(section, "watchdog_ms", fallback=5000)
                if s.watchdogMs < 0 or s.watchdogMs > 255 * 255:
                    raise ValueError("Invalid watchdog_ms")
                s.inputSize = getint(section, "input_size")
                if s.inputSize < 0 or s.inputSize > 246:
                    raise ValueError("Invalid input_size")
                s.outputSize = getint(section, "output_size")
                if s.outputSize < 0 or s.outputSize > 246:
                    raise ValueError("Invalid output_size")

                mods = [o for o in p.options(section) if self.__reMod.match(o)]
                mods.sort(key=lambda o: self.__reMod.match(o).group(1))
                if s.gsd.isModular():
                    for option in mods:
                        s.gsd.setConfiguredModule(get(section, option))
                elif mods:
                    print(
                        "Warning: Some modules are specified in the config file, "
                        "but the station is 'Compact': Modular_Station=0.",
                        file=sys.stderr)

                self.slaveConfs.append(s)

        except (IOError, UnicodeError) as e:
            raise PbConfError("Failed to read '%s': %s" %\
             (filename, str(e)))
        except (_ConfigParserError, ValueError) as e:
            raise PbConfError("Profibus config file parse "
                              "error:\n%s" % str(e))
        except GsdError as e:
            raise PbConfError("Failed to parse GSD file:\n%s" % str(e))