Example #1
0
    def update(self, digest, resource):
        """Update resource specified by digest.

        Args:
            digest (str): Content digest that is udpated.
            resource (Resource): A single Resource() container that contains updates.
        """

        self._logger.debug('update content')
        resource.updated = Config.utcnow()
        collection = self._database.update(digest, resource)

        return collection
Example #2
0
    def dump(cls, collection, filename):
        """Dump collection into file."""

        if not Config.is_supported_file_format():
            cls._logger.debug('file format not supported for file %s',
                              filename)

            return

        if not collection:
            Cause.push(Cause.HTTP_NOT_FOUND, 'no content found to be exported')

            return

        cls._logger.debug('exporting contents %s', filename)
        with open(filename, 'w') as outfile:
            try:
                dictionary = {
                    'meta': {
                        'updated': Config.utcnow(),
                        'version': __version__,
                        'homepage': __homepage__
                    },
                    'data': collection.dump_dict(Config.remove_fields)
                }
                if Config.is_operation_file_text:
                    outfile.write(collection.dump_text(Config.templates))
                elif Config.is_operation_file_json:
                    json.dump(dictionary, outfile)
                    outfile.write(Const.NEWLINE)
                elif Config.is_operation_file_mkdn:
                    outfile.write(collection.dump_mkdn(Config.templates))
                elif Config.is_operation_file_yaml:
                    yaml.safe_dump(dictionary,
                                   outfile,
                                   default_flow_style=False)
                else:
                    cls._logger.debug('unknown export file format')
            except (IOError, TypeError, ValueError, yaml.YAMLError) as error:
                cls._logger.exception(
                    'fatal failure to generate formatted export file "%s"',
                    error)
                Cause.push(Cause.HTTP_INTERNAL_SERVER_ERROR,
                           'fatal failure while exporting content to file')
Example #3
0
    def dump_template(cls, category):
        """Dump content template into file."""

        filename = Config.get_operation_file()
        resource = Collection.get_resource(category, Config.utcnow())
        template = resource.get_template(category, Config.template_format,
                                         Config.templates)
        cls._logger.debug('exporting content template %s', filename)
        with open(filename, 'w') as outfile:
            try:
                outfile.write(template)
            except IOError as error:
                cls._logger.exception(
                    'fatal failure in creating %s template file "%s"',
                    category, error)
                Cause.push(
                    Cause.HTTP_INTERNAL_SERVER_ERROR,
                    'fatal failure while exporting template {}'.format(
                        filename))
Example #4
0
    def load(cls, filename):
        """Load dictionary from file."""

        collection = Collection()
        if not Config.is_supported_file_format():
            cls._logger.debug('file format not supported for file %s',
                              filename)

            return collection

        cls._logger.debug('importing contents from file %s', filename)
        if os.path.isfile(filename):
            with open(filename, 'r') as infile:
                try:
                    timestamp = Config.utcnow()
                    if Config.is_operation_file_text:
                        collection.load_text(timestamp, infile.read())
                    elif Config.is_operation_file_mkdn:
                        collection.load_mkdn(timestamp, infile.read())
                    elif Config.is_operation_file_json:
                        dictionary = json.load(infile)
                        collection.load_dict(timestamp, dictionary)
                    elif Config.is_operation_file_yaml:
                        dictionary = yaml.safe_load(infile)
                        collection.load_dict(timestamp, dictionary)
                    else:
                        cls._logger.debug('unknown import file format')
                except (TypeError, ValueError, yaml.YAMLError) as error:
                    cls._logger.exception(
                        'fatal exception while loading file "%s"', error)
                    Cause.push(
                        Cause.HTTP_INTERNAL_SERVER_ERROR,
                        'fatal failure while importing content from file')

        else:
            Cause.push(Cause.HTTP_NOT_FOUND,
                       'cannot read file {}'.format(filename))

        return collection