Exemplo n.º 1
0
    def testChecksumAfterSaveRestore(self):
        """
        A backend that has a sequence added to it, which is then saved and
        restored, and then has a second sequence is added to it must have the
        same checksum as a backend that simply has the two sequences added to
        it without interruption.
        """
        seq1 = 'FRRRFRRRFASAASA'
        seq2 = 'MMMMMMMMMFRRRFR'
        dbParams1 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks, Troughs])
        be1 = Backend()
        be1.configure(dbParams1, 'name1', 0)
        be1.addSubject(AARead('id1', seq1), '0')
        fp = StringIO()
        be1.save(fp)
        fp.seek(0)
        be1 = Backend.restore(fp)
        be1.addSubject(AARead('id2', seq2), '1')

        dbParams2 = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                       trigPoints=[Peaks, Troughs])
        be2 = Backend()
        be2.configure(dbParams2, 'name2', 0)
        be2.addSubject(AARead('id1', seq1), '0')
        be2.addSubject(AARead('id2', seq2), '1')

        self.assertEqual(be1.checksum(), be2.checksum())
Exemplo n.º 2
0
 def testSaveRestoreWithNonDefaultParameters(self):
     """
     When asked to save and then restore a backend with non-default
     parameters, a backend with the correct parameters must result.
     """
     dbParams = DatabaseParameters(landmarks=[], trigPoints=[],
                                   limitPerLandmark=16, maxDistance=17,
                                   minDistance=18, distanceBase=19.0)
     be = Backend()
     be.configure(dbParams)
     fp = StringIO()
     be.save(fp)
     fp.seek(0)
     result = be.restore(fp)
     self.assertIs(None, dbParams.compare(result.dbParams))
Exemplo n.º 3
0
 def testSaveRestoreNonEmpty(self):
     """
     When asked to save and then restore a non-empty backend, the correct
     backend must result.
     """
     dbParams = DatabaseParameters(landmarks=[AlphaHelix, BetaStrand],
                                   trigPoints=[Peaks, Troughs])
     be = Backend()
     be.configure(dbParams)
     be.addSubject(AARead('id', 'FRRRFRRRFASAASA'), '0')
     fp = StringIO()
     be.save(fp)
     fp.seek(0)
     result = Backend.restore(fp)
     self.assertEqual(be.subjectCount(), result.subjectCount())
     self.assertEqual(be.d, result.d)
     self.assertEqual(be.checksum(), result.checksum())
     self.assertIs(None, be.dbParams.compare(result.dbParams))
Exemplo n.º 4
0
    def restore(cls, fpOrFilePrefix):
        """
        Restore state from a file.

        @param fpOrFilePrefix: A file pointer or the C{str} prefix of a file
            name. If a C{str}, self.SAVE_SUFFIX is appended to get the full
            file name.
        @return: An instance of L{SimpleConnector}.
        @raises ValueError: If valid JSON cannot be loaded from C{fp}.
        """
        if isinstance(fpOrFilePrefix, str):
            saveFile = fpOrFilePrefix + cls.SAVE_SUFFIX
            filePrefix = fpOrFilePrefix
        else:
            saveFile = fpOrFilePrefix
            filePrefix = None

        with as_handle(saveFile) as fp:
            dbParams = DatabaseParameters.restore(fp)

        return cls(dbParams,
                   backend=Backend.restore(fpOrFilePrefix),
                   filePrefix=filePrefix)
Exemplo n.º 5
0
    def onJoin(self, details):
        self._sessionId = details.session
        logging.info('Joined with WAMP server, session id %s', self._sessionId)
        self._ApiConfigured = False
        args = self.config.extra['args']

        if args.filePrefix:
            saveFile = args.filePrefix + Backend.SAVE_SUFFIX
            if os.path.exists(saveFile):
                self._backend = Backend.restore(saveFile)
        else:
            # Note that the backend will be configured by the WAMP connector
            # via a call to our configure method (below).
            self._backend = Backend()

        @asyncio.coroutine
        def configure(paramsStr, suggestedName, suggestedChecksum):
            """
            Configure our backend and register its API methods.

            @param paramsStr: The C{str} 'save' of a C{DatabaseParameters}
                instance.
            @param suggestedName: The C{str} suggested name for this backend.
                If the backend has already been configured (from a file
                restore) with a different name, the suggested name is ignored.
            @param suggestedChecksum: The C{int} suggested checksum for the
                backend, or C{None} if there is no initial value.
            @return: A 2-tuple consisting of the C{str} name of the backend and
                its checksum. These will either be the suggested values or
                those that were already in use (if the backend was already
                configured).
            """
            fp = StringIO(paramsStr)
            dbParams = DatabaseParameters.restore(fp)
            name, checksum, subjectCount = self._backend.configure(
                dbParams, suggestedName, suggestedChecksum)

            if not self._ApiConfigured:
                self._ApiConfigured = True
                yield from self.registerAPIMethods()

            return name, checksum, subjectCount

        # Register our configure command.
        yield from self.register(configure, 'configure-%s' % self._sessionId)
        logging.info('Registered configure method.')

        # Register our shutdown command.
        def shutdown(save, filePrefix):
            """
            Shut down the backend.

            @param save: If C{True}, save the backend state.
            @param filePrefix: When saving, use this C{str} as a file name
                prefix.
            """
            logging.info('Shutdown called.')
            self._backend.shutdown(save, filePrefix)
            self.leave('goodbye!')

        yield from self.register(shutdown, 'shutdown-%s' % self._sessionId)
        logging.info('Registered shutdown method.')