connectors/ConnectorBase.py
author Edouard Tisserant <edouard@beremiz.fr>
Fri, 30 Aug 2024 11:50:23 +0200
changeset 4008 f30573e98600
parent 3884 34da877021d5
permissions -rw-r--r--
IDE: allow structures to be located.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# See COPYING file for copyrights details.


import hashlib
from runtime import PlcStatus


class ConnectorBase(object):

    chuncksize = 0xfff # 4KB

    PLCObjDefaults = {
        "StartPLC": False,
        "GetTraceVariables": (PlcStatus.Broken, None),
        "GetPLCstatus": (PlcStatus.Broken, None),
        "RemoteExec": (-1, "RemoteExec script failed!"),
        "GetVersions": "*** Unknown ***"
    }

    def BlobFromFile(self, filepath, seed):
        s = hashlib.new('md5')
        s.update(seed.encode())
        blobID = self.SeedBlob(seed.encode())
        with open(filepath, "rb") as f:
            while blobID == s.digest():
                chunk = f.read(self.chuncksize)
                if len(chunk) == 0:
                    return blobID
                blobID = self.AppendChunkToBlob(chunk, blobID)
                s.update(chunk)
        raise IOError("Data corrupted during transfer or connection lost")