示例#1
0
    def get_data(self, **kwargs):
        """
        .. versionchanged:: 0.1.12

        Read the data. This is only possible if a datasource is specified and
        any kind of IOExtension or IOInterface is activated. By default,
        the builtin :class:`IOExtension <metacatalog.ext.io.extension.IOExtension>`
        is activated since version 0.1.12.

        """
        if self.datasource is None:
            raise MetadataMissingError('Entry need datasource information')

        try:
            # check if an io_extension is set
            if self.io_extension is not None:
                return self.io_extension.read(**kwargs)

            # if no extension instance, maybe an interface class is set
            elif self.io_interface is not None:
                reader = self.io_interface.get_reader(self.datasource)
                return reader(self, self.datasource, **kwargs)
            else:
                raise IOOperationNotFoundError(
                    "No IO interface activated. Run metacatalog.ext.extension('io', InterfaceClass) to register"
                )
        except IOOperationNotFoundError as e:
            print('[ERROR]: Operation not possible.\n%s' % str(e))
            return None
示例#2
0
    def append_data(self, data, **kwargs):
        """
        .. versionadded:: 0.1.12

        Append data. This is only possible if a datasource is specified and 
        any kind of IOExtension or IOInterface is activated. By default, 
        the builtin :class:`IOExtension <metacatalog.ext.io.extension.IOExtension>` 
        is activated since version 0.1.12.

        For the default interface, the datasource type and data type determine 
        where the data will be stored and how the data has to look like. 
        You can easily inherit from the 
        :class:`IOExtension <metacatalog.ext.io.extension.IOExtension>` to 
        customize read and write behaviour. If you import i.e. a timeseries to 
        the same database as metacatalog, you will need to prepared data to 
        to only hold an datetime index and the data to be stored.

        """
        if self.datasource is None:
            raise MetadataMissingError('Entry need datasource information')
        
        try:
            # check if an io_extension is set
            if self.io_extension is not None:
                return self.io_extension.append(data, **kwargs)    
            
            # if no extension instance, maybe an interface class is set
            elif self.io_interface is not None:
                appender = self.io_interface.get_appender(self.datasource)
                return appender(self, self.datasource, data, **kwargs)
            else:
                raise IOOperationNotFoundError("No IO interface activated. Run metacatalog.ext.extension('io', InterfaceClass) to register")
        except IOOperationNotFoundError as e:
            print('[ERROR]: Operation not possible.\n%s' % str(e))
            return None
示例#3
0
    def create_datasource(self,
                          path: str,
                          type,
                          datatype,
                          commit=False,
                          **args):
        """
        """
        #
        if self.datasource is not None:
            raise MetadataMissingError(
                'Datasource already exists. You can edit that one.')

        # get a session
        session = object_session(self)

        # load the datasource type
        if isinstance(type, int):
            ds_type = api.find_datasource_type(session=session,
                                               id=type,
                                               return_iterator=True).one()
        elif isinstance(type, str):
            ds_type = api.find_datasource_type(session=session,
                                               name=type,
                                               return_iterator=True).first()
        else:
            raise AttributeError('type has to be of type int or str')

        # TODO need the API for DataTypes here!!
        dtype = session.query(
            models.DataType).filter(models.DataType.name == datatype).one()

        # build the datasource object
        ds = models.DataSource(type=ds_type, datatype=dtype, path=path)

        # add the args
        ds.save_args_from_dict(args)

        # append to self
        self.datasource = ds

        if commit:
            try:
                session.add(self)
                session.commit()
            except Exception as e:
                session.rollback()
                raise e

        # return
        return ds
示例#4
0
    def delete_data(self, delete_source=False, **kwargs):
        """
        .. versionadded:: 0.1.12

        Delete data. This is only possible if a datasource is specified and
        any kind of IOExtension or IOInterface is activated. By default,
        the builtin :class:`IOExtension <metacatalog.ext.io.extension.IOExtension>`
        is activated since version 0.1.12.

        For the default interface, the datasource type and data type determine
        where the data is stored and how the data will be delted.
        You can easily inherit from the
        :class:`IOExtension <metacatalog.ext.io.extension.IOExtension>` to
        customize read and write behaviour.

        Parameters
        ----------
        delete_source : bool
            If True, the DataSource will be deleted as well after the data
            has been deleted.

        """
        if self.datasource is None:
            raise MetadataMissingError('Entry need datasource information')

        kwargs['delete_source'] = delete_source
        try:
            # check if an io_extension is set
            if self.io_extension is not None:
                return self.io_extension.delete(**kwargs)

            # if no extension instance, maybe an interface class is set
            elif self.io_interface is not None:
                deleter = self.io_interface.get_deleter(self.datasource)
                return deleter(self, self.datasource, **kwargs)
            else:
                raise IOOperationNotFoundError(
                    "No IO interface activated. Run metacatalog.ext.extension('io', InterfaceClass) to register"
                )
        except IOOperationNotFoundError as e:
            print('[ERROR]: Operation not possible.\n%s' % str(e))
            return None