Beispiel #1
0
def save(
        dataset, path = None, filetype = None, workspace = None,
        base = 'user', **kwds):
    """Export dataset to file.

    Args:
        dataset (object): nemoa dataset instance
        path (str, optional): path of export file
        filetype (str, optional): filetype of export file
        workspace (str, optional): workspace to use for file export

    Returns:
        Boolean value which is True if file export was successful

    """

    if not entity.has_base(dataset, 'Dataset'):
        raise TypeError("dataset is not valid")

    from nemoa.base import env
    import nemoa

    # get directory, filename and fileextension
    if isinstance(workspace, str) and not workspace == 'None':
        dname = nemoa.path('datasets', workspace=workspace, base=base)
    elif isinstance(path, str):
        dname = env.get_dirname(path)
    else:
        dname = env.get_dirname(dataset.path)
    if isinstance(path, str):
        fbase = env.basename(path)
    else:
        fbase = dataset.fullname
    if isinstance(filetype, str):
        fext = filetype
    elif isinstance(path, str):
        fext = env.fileext(path) or env.fileext(dataset.path)
    else:
        fext = env.fileext(dataset.path)
    path = str(env.join_path(dname, fbase + '.' + fext))

    # get filetype from file extension if not given
    # and test if filetype is supported
    if not filetype:
        filetype = fext.lower()
    if filetype not in filetypes():
        raise ValueError(f"filetype '{filetype}' is not supported.")

    # export to file
    mname = filetypes(filetype)[0]
    if mname == 'text':
        return text.save(dataset, path, filetype, **kwds)
    if mname == 'archive':
        return archive.save(dataset, path, filetype, **kwds)
    if mname == 'image':
        return image.save(dataset, path, filetype, **kwds)

    return False
Beispiel #2
0
def save(network,
         path=None,
         filetype=None,
         workspace=None,
         base='user',
         **kwds):
    """Export network to file.

    Args:
        network (object): nemoa network instance
        path (str, optional): path of export file
        filetype (str, optional): filetype of export file
        workspace (str, optional): workspace to use for file export

    Returns:
        Boolean value which is True if file export was successful

    """

    from nemoa.base import env

    if not entity.has_base(network, 'Network'):
        raise ValueError("network is not valid")

    # get directory, filename and fileextension
    if isinstance(workspace, str) and not workspace == 'None':
        directory = nemoa.path('networks', workspace=workspace, base=base)
    elif isinstance(path, str):
        directory = env.get_dirname(path)
    else:
        directory = env.get_dirname(network.path)
    if isinstance(path, str): name = env.basename(path)
    else: name = network.fullname
    if isinstance(filetype, str): fileext = filetype
    elif isinstance(path, str):
        fileext = env.fileext(path) or env.fileext(network.path)
    else:
        fileext = env.fileext(network.path)
    path = str(env.join_path(directory, name + '.' + fileext))

    # get filetype from file extension if not given
    # and test if filetype is supported
    if not filetype: filetype = fileext.lower()
    if filetype not in filetypes():
        raise ValueError(f"filetype '{filetype}' is not supported")

    # export to file
    module_name = filetypes(filetype)[0]
    if module_name == 'graph':
        return graph.save(network, path, filetype, **kwds)
    if module_name == 'archive':
        return archive.save(network, path, filetype, **kwds)
    if module_name == 'image':
        return image.save(network, path, filetype, **kwds)

    return False
Beispiel #3
0
def load(path, filetype=None, **kwds):
    """Import network dictionary from file or workspace."""

    import os
    from nemoa.base import env
    import nemoa

    # get path (if necessary)
    if 'workspace' in kwds or not os.path.isfile(path):
        name = path
        pathkwds = {}
        if 'workspace' in kwds:
            pathkwds['workspace'] = kwds.pop('workspace')
        if 'base' in kwds:
            pathkwds['base'] = kwds.pop('base')
        path = nemoa.path('network', name, **pathkwds)
        if not path:
            raise Warning("""could not import network:
                invalid network name.""") or {}
        if not os.path.isfile(path):
            raise Warning("""could not import network:
                file '%s' does not exist.""" % path) or {}

    # get filetype (from file extension if not given)
    # and check if filetype is supported
    if not filetype: filetype = env.fileext(path).lower()
    if filetype not in filetypes():
        raise ValueError(f"filetype '{filetype}' is not supported")

    # import, check and update dictionary
    mname = filetypes(filetype)[0]
    if mname == 'archive':
        network = archive.load(path, **kwds)
    elif mname == 'graph':
        network = graph.load(path, **kwds)
    elif mname == 'text':
        network = text.load(path, **kwds)
    else:
        network = None
    if not network:
        raise ValueError("""could not import network:
            file '%s' is not valid.""" % path) or {}

    # update path
    network['config']['path'] = path

    return network
Beispiel #4
0
    def _get_path_default(self):
        """Get default filepath.

        Path to a potential file containing or referencing the resource.

        Returns:
            Basestring containing the potential path of the resource.

        """

        module = self.__module__.split('.')[1]
        fileext = nemoa.get('default', 'filetype', module)
        path = nemoa.path(module + 's') or nemoa.common.ospath.getcwd()
        filename = '%s.%s' % (nemoa.common.ospath.get_clean_filename(
            self._get_fullname()), fileext)

        return nemoa.common.ospath.get_norm_path(path, filename)
Beispiel #5
0
def load(path, filetype=None, **kwds):
    """Import dataset dictionary from file or workspace."""

    import os

    from nemoa.base import env

    # get path (if necessary)
    if 'workspace' in kwds or not os.path.isfile(path):
        name = path
        pathkwds = {}
        if 'workspace' in kwds:
            pathkwds['workspace'] = kwds.pop('workspace')
        if 'base' in kwds:
            pathkwds['base'] = kwds.pop('base')
        path = nemoa.path('dataset', name, **pathkwds)
        if not path:
            raise Warning("""could not import dataset:
                invalid dataset name.""") or {}
        if not os.path.isfile(path):
            raise Warning("""could not import dataset:
                file '%s' does not exist.""" % path) or {}

    # get filtype from file extension if not given
    # and check if filetype is supported
    if not filetype: filetype = env.fileext(path).lower()
    if filetype not in filetypes():
        raise ValueError(f"filetype '{filetype}' is not supported")

    # import and check dictionary
    mname = filetypes(filetype)[0]
    if mname == 'archive': dataset = archive.load(path, **kwds)
    elif mname == 'text': dataset = text.load(path, **kwds)
    else: dataset = None
    if not dataset:
        raise ValueError("""could not import dataset:
            file '%s' is not valid.""" % path) or {}

    # update path
    dataset['config']['path'] = path

    return dataset
Beispiel #6
0
 def path(*args, **kwargs):
     """Wrapping function to nemoa.path()."""
     nemoa.log('note', nemoa.path(*args, **kwargs))
     return None
Beispiel #7
0
def save(dataset,
         path=None,
         filetype=None,
         workspace=None,
         base='user',
         **kwds):
    """Export dataset to file.

    Args:
        dataset (object): nemoa dataset instance
        path (str, optional): path of export file
        filetype (str, optional): filetype of export file
        workspace (str, optional): workspace to use for file export

    Returns:
        Boolean value which is True if file export was successful

    """

    if not entity.has_base(dataset, 'Dataset'):
        raise TypeError("dataset is not valid")

    from nemoa.base import env
    import nemoa

    # get directory, filename and fileextension
    if isinstance(workspace, str) and not workspace == 'None':
        dname = nemoa.path('datasets', workspace=workspace, base=base)
    elif isinstance(path, str):
        dname = env.get_dirname(path)
    else:
        dname = env.get_dirname(dataset.path)
    if isinstance(path, str):
        fbase = env.basename(path)
    else:
        fbase = dataset.fullname
    if isinstance(filetype, str):
        fext = filetype
    elif isinstance(path, str):
        fext = env.fileext(path) or env.fileext(dataset.path)
    else:
        fext = env.fileext(dataset.path)
    path = str(env.join_path(dname, fbase + '.' + fext))

    # get filetype from file extension if not given
    # and test if filetype is supported
    if not filetype:
        filetype = fext.lower()
    if filetype not in filetypes():
        raise ValueError(f"filetype '{filetype}' is not supported.")

    # export to file
    mname = filetypes(filetype)[0]
    if mname == 'text':
        return text.save(dataset, path, filetype, **kwds)
    if mname == 'archive':
        return archive.save(dataset, path, filetype, **kwds)
    if mname == 'image':
        return image.save(dataset, path, filetype, **kwds)

    return False
Beispiel #8
0
    def test_session_path(self):

        with self.subTest(cmd="nemoa.path('basepath')"):
            test = isinstance(nemoa.path('basepath'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('baseconf')"):
            test = isinstance(nemoa.path('baseconf'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('datasets')"):
            test = isinstance(nemoa.path('datasets'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('networks')"):
            test = isinstance(nemoa.path('networks'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('systems')"):
            test = isinstance(nemoa.path('systems'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('models')"):
            test = isinstance(nemoa.path('models'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('scripts')"):
            test = isinstance(nemoa.path('scripts'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('cache')"):
            test = isinstance(nemoa.path('cache'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('inifile')"):
            test = isinstance(nemoa.path('inifile'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('logfile')"):
            test = isinstance(nemoa.path('logfile'), str)
            self.assertTrue(test)

        with self.subTest(cmd="nemoa.path('expand', )"):
            valid = nemoa.path('expand',
                               '%basepath%',
                               '%workspace%',
                               check=True)

            invalid = nemoa.path('expand',
                                 '%basepath%',
                                 '%workspace%',
                                 'invalid_path_name',
                                 check=True)
            test = valid and not invalid
            self.assertTrue(test)

        # test paths of configured objects
        objtypes = ['dataset', 'network', 'system', 'model', 'script']
        for objtype in objtypes:
            for name in nemoa.list(objtype + 's'):
                cmd = "nemoa.path('%s', '%s')" % (objtype, name)
                with self.subTest(cmd=cmd):
                    path = nemoa.path(objtype, name)
                    self.assertIsInstance(path, str)