示例#1
0
class ExampleFile(DataContentBase):
    def ingest(self, src):
        if not isinstance(src, list) and os.path.isfile(src):
            src = ingest_yaml_list(src)

        for doc in src:
            self.add(doc)

        if self.collection is None:
            m = 'all examples must have a collection'
            logger.error(m)
            raise InheritableContentError(m)

    @property
    def collection(self):
        if 'collection' not in self.state:
            return None
        else:
            return self.content[self.state['collection']]

    @collection.setter
    def collection(self, value):
        if 'collection' in self.state:
            m = 'example spec "{0}" already exists'.format(
                self.collection.name)
            logger.error(m)
            raise ExampleError(m)
        elif isinstance(value, ExampleData):
            self.content[value.name] = value
            self.state['collection'] = value.name
        else:
            raise TypeError

    @property
    def examples(self):
        return [
            example for example in self.content.values()
            if 'collection' not in example
        ]

    def add(self, doc):
        if ('edition' in doc and 'edition' in self.conf.project
                and doc['edition'] != self.conf.project.edition):
            return

        if 'collection' in doc:
            self.collection = ExampleData(doc, self.conf)

            if not self.collection.is_resolved():
                self.collection.resolve(self.data)
        elif 'operation' in doc:
            op = ExampleCase(doc, self.conf)
            if op.ref in self.content:
                m = 'example named {0} already exists'.format(op.ref)
                logger.error(m)
                raise ExampleError(m)
            else:
                self.content[op.ref] = op
                if not op.is_resolved():
                    op.resolve(self.data)

                logger.debug('added operation {0}'.format(op.name))

    def fetch(self, ref):
        if ref == self.collection.name:
            if not self.collection.is_resolved():
                self.collection.resolve(self.data)

            return self.collection

        elif ref in self.content:
            content = self.content[ref]

            if not content.is_resolved():
                content.resolve(self.data)

            return content

        else:
            m = 'content with ref "{0}" not found'.format(ref)
            logger.error(m)
            raise ExampleError(m)
示例#2
0
class ExampleFile(DataContentBase):

    """
    There is a one to one mapping of example files and output examples. Each
    example file has some "starting data," or a "collection" and then a sequence
    of operation and result pairs (i.e. "examples") that contain both a sequence
    of operations *and* an expected result. Each example *must* have a
    collection, and typically has 1 or more output examples.

    The ``ingest()`` method adds a check to ensure to produce an error to ensure
    that there's a "collection" value, while ``add()`` and ``fetch()`` capture
    collection and examples separately.
    """

    @property
    def collection(self):
        if 'collection' not in self.state:
            return None
        else:
            return self.content[self.state['collection']]

    @collection.setter
    def collection(self, value):
        if 'collection' in self.state:
            m = 'example spec "{0}" already exists'.format(self.collection.name)
            logger.error(m)
            raise ExampleError(m)
        elif isinstance(value, ExampleData):
            self.content[value.name] = value
            self.state['collection'] = value.name
        else:
            raise TypeError

    @property
    def examples(self):
        l = []
        for ref in self.ordering:
            example = self.fetch(ref)
            if 'collection' not in example:
                l.append(example)

        return l

    def add(self, doc):
        if 'collection' in doc:
            self.collection = ExampleData(doc, self.conf)

            if not self.collection.is_resolved():
                self.collection.resolve(self.data)

            return self.collection
        else:
            op = ExampleCase(doc, self.conf)
            if 'edition' in op:
                op.ref = '-'.join((str(op.ref), op.edition))

            if op.ref in self.content:
                m = 'example named {0} already exists'.format(op.ref)
                logger.error(m)
                raise ExampleError(m)
            else:
                self.content[op.ref] = op
                if not op.is_resolved():
                    op.resolve(self.data)
                logger.debug('added operation {0}'.format(op.name))

            return op

    def fetch(self, ref):
        if ref == self.collection.name:
            if not self.collection.is_resolved():
                self.collection.resolve(self.data)

            return self.collection

        elif ref in self.content:
            content = self.content[ref]

            if not content.is_resolved():
                content.resolve(self.data)

            return content

        else:
            m = 'content with ref "{0}" not found'.format(ref)
            logger.error(m)
            raise ExampleError(m)
示例#3
0
class ExampleFile(DataContentBase):
    def ingest(self, src):
        if not isinstance(src, list) and os.path.isfile(src):
            src = ingest_yaml_list(src)

        for doc in src:
            self.add(doc)

        if self.collection is None:
            m = 'all examples must have a collection'
            logger.error(m)
            raise InheritableContentError(m)

    @property
    def collection(self):
        if 'collection' not in self.state:
            return None
        else:
            return self.content[self.state['collection']]

    @collection.setter
    def collection(self, value):
        if 'collection' in self.state:
            m = 'example spec "{0}" already exists'.format(self.collection.name)
            logger.error(m)
            raise ExampleError(m)
        elif isinstance(value, ExampleData):
            self.content[value.name] = value
            self.state['collection'] = value.name
        else:
            raise TypeError

    @property
    def examples(self):
        return [ example
                 for example in self.content.values()
                 if 'collection' not in example
        ]

    def add(self, doc):
        if ('edition' in doc and
            'edition' in self.conf.project and
            doc['edition'] != self.conf.project.edition):
            return

        if 'collection' in doc:
            self.collection = ExampleData(doc, self.conf)

            if not self.collection.is_resolved():
                self.collection.resolve(self.data)
        elif 'operation' in doc:
            op = ExampleCase(doc, self.conf)
            if op.ref in self.content:
                m = 'example named {0} already exists'.format(op.ref)
                logger.error(m)
                raise ExampleError(m)
            else:
                self.content[op.ref] = op
                if not op.is_resolved():
                    op.resolve(self.data)

                logger.debug('added operation {0}'.format(op.name))

    def fetch(self, ref):
        if ref == self.collection.name:
            if not self.collection.is_resolved():
                self.collection.resolve(self.data)

            return self.collection

        elif ref in self.content:
            content = self.content[ref]

            if not content.is_resolved():
                content.resolve(self.data)

            return content

        else:
            m = 'content with ref "{0}" not found'.format(ref)
            logger.error(m)
            raise ExampleError(m)