示例#1
0
    def _get_list_workspaces(self, base=None):
        """Get list of workspaces in given searchpath."""

        if not base:
            wsdict = {}
            for base in self._config['default']['basepath']:
                wsdict[base] = self._get_list_workspaces(base=base)
            return wsdict
        if base not in self._config['default']['basepath']:
            raise ValueError("unknown workspace base '%s'" % base)

        basepath = self._config['default']['basepath'][base]
        baseglob = self._get_path_expand((basepath, '*'))

        wslist = []
        fname = self._config['default']['path']['ini'][-1]
        for subdir in glob.iglob(baseglob):
            if not os.path.isdir(subdir):
                continue
            fpath = str(env.join_path(subdir, fname))
            if not os.path.isfile(fpath):
                continue
            wslist.append(os.path.basename(subdir))

        return sorted(wslist)
示例#2
0
文件: nbase.py 项目: fd0livar/rian
    def _get_path(self) -> OptStr:
        """Get filepath.

        Path to a potential file containing or referencing the resource.

        Returns:
            String containing the (potential) path of the resource.

        """
        if 'path' in self._config:
            path = str(self._config['path'])
            if path:
                return path

        from rian import session

        mname = self.__module__.rsplit('.', 1)[-1]
        dname = session.path(mname + 's')
        if not dname:
            return None
        fbase = env.clear_filename(self._get_fullname())
        if not fbase:
            return None
        fext = session.get('default', 'filetype', mname)
        if not fext:
            return None
        return str(env.join_path(dname, fbase + '.' + fext))
示例#3
0
文件: __init__.py 项目: fd0livar/rian
def save(system,
         path=None,
         filetype=None,
         workspace=None,
         base='user',
         **kwds):
    """Export system to file.

    Args:
        system (object): rian system 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 hup.base import otree

    if not otree.has_base(system, 'System'):
        raise ValueError("system is not valid")

    from hup.base import env

    # get directory, filename and fileextension
    if isinstance(workspace, str) and not workspace == 'None':
        directory = rian.path('systems', workspace=workspace, base=base)
    elif isinstance(path, str):
        directory = env.get_dirname(path)
    else:
        directory = env.get_dirname(system.path)
    if isinstance(path, str):
        name = env.basename(path)
    else:
        name = system.fullname
    if isinstance(filetype, str):
        fileext = filetype
    elif isinstance(path, str):
        fileext = env.fileext(path) or env.fileext(system.path)
    else:
        fileext = env.fileext(system.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 == 'archive':
        return rian.system.exports.archive.save(system, path, filetype, **kwds)

    return False
示例#4
0
    def _get_path_expand(self,
                         *args,
                         check: bool = False,
                         create: bool = False):
        """Get expanded path.

        Args:
            path (string or tuple or list):
            check (bool):
            create (bool):

        Returns:
            String containing expanded path.

        """
        path = str(env.join_path(args))

        # expand rian environment variables
        base = self._get_base()
        udict = {
            'workspace':
            self._get_workspace() or 'none',
            'base':
            self._get_base() or 'none',
            'basepath':
            str(env.join_path(self._config['default']['basepath'][base]))
        }

        for key, val in self._config['default']['basepath'].items():
            udict[key] = str(env.join_path(val))
        for key, val in self._config['current']['path'].items():
            udict[key] = str(env.join_path(val))

        path = str(env.expand(path, udict=udict))

        # (optional) create directory
        if create:
            env.mkdir(path)

        # (optional) check path
        if check and not os.path.exists(path):
            return None

        return path
示例#5
0
文件: __init__.py 项目: fd0livar/rian
def save(dataset,
         path=None,
         filetype=None,
         workspace=None,
         base='user',
         **kwds):
    """Export dataset to file.

    Args:
        dataset (object): rian 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 otree.has_base(dataset, 'Dataset'):
        raise TypeError("dataset is not valid")

    from hup.base import env
    import rian

    # get directory, filename and fileextension
    if isinstance(workspace, str) and not workspace == 'None':
        dname = rian.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
示例#6
0
 def test_join_path(self) -> None:
     val = env.join_path(('a', ('b', 'c')), 'd')
     self.assertEqual(val, pathlib.Path('a/b/c/d'))