Example #1
0
def sink_from_local_file(local_file: LocalFile) -> LocalFileStructureSink:
    return LocalFileStructureSink(
        id=to_url_representation(local_file.path),
        thingNodeId=to_url_representation(local_file.dir_path),
        name=os.path.basename(local_file.path),
        type="dataframe",
        visible=True,
        metadataKey=None,
        path=local_file.path,
        filters={},
    )
Example #2
0
def get_thing_node_by_id(
        id: str,  # pylint: disable=redefined-builtin
) -> Optional[StructureThingNode]:
    dir_path, top_dir = get_local_dir_info_from_thing_node_id(id)

    if top_dir is None:
        return None

    is_top_dir = dir_path == top_dir

    return StructureThingNode(
        id=id,
        name=os.path.basename(dir_path),
        parentId=to_url_representation(os.path.dirname(dir_path))
        if not is_top_dir else None,
        description=(("Local file directory at "
                      if not is_top_dir else "Root local file directory at ") +
                     os.path.abspath(os.path.realpath(dir_path))),
    )
Example #3
0
def get_structure(parent_id: Optional[str] = None) -> StructureResponse:
    """Obtain structure for corresponding adapter web service endpoint

    parent_id is a local path encoded via to_url_representation from the utils module of this
    adapter, or None.
    """

    local_root_dirs = local_file_adapter_config.local_dirs

    if parent_id is None:  # get root Nodes

        return StructureResponse(
            id="local-file-adapter",
            name="Local File Adapter",
            thingNodes=[
                StructureThingNode(
                    id=to_url_representation(dir_path),
                    name=os.path.basename(dir_path),
                    parentId=None,
                    description="Root local file directory at " +
                    os.path.abspath(os.path.realpath(dir_path)),
                ) for dir_path in local_root_dirs
            ],
            sources=[],
            sinks=[],
        )

    # One level in fiel hierarchy
    current_dir = from_url_representation(parent_id)

    if not len(
        [current_dir.startswith(root_dir)
         for root_dir in local_root_dirs]) > 0:
        raise AdapterHandlingException((
            f"Requested local file dir {current_dir} not contained in configured "
            f"root directories {str(local_root_dirs)}"))

    local_files, dirs = get_local_files_and_dirs(current_dir,
                                                 walk_sub_dirs=False)

    return StructureResponse(
        id="local-file-adapter",
        name="Local File Adapter",
        thingNodes=[
            StructureThingNode(
                id=to_url_representation(dir_path),
                name=os.path.basename(dir_path),
                parentId=parent_id,
                description="Local file directory at " +
                os.path.abspath(os.path.realpath(dir_path)),
            ) for dir_path in dirs
        ],
        sources=[
            source_from_local_file(local_file) for local_file in local_files
            if local_file_loadable(local_file)
        ],
        sinks=[
            sink_from_local_file(local_file) for local_file in local_files
            if local_file_writable(local_file)
        ],
    )