Example #1
0
def get_local_dir_info_from_thing_node_id(
    thing_node_id: str, ) -> Tuple[str, Optional[str]]:
    dir_path = from_url_representation(thing_node_id)

    top_dir = get_valid_top_dir(dir_path)

    if top_dir is None or not os.path.isdir(dir_path):
        return (dir_path, None)

    return dir_path, top_dir
Example #2
0
async def get_single_sink(sink_id: str) -> LocalFileStructureSink:
    possible_sink = get_sink_by_id(sink_id)

    if possible_sink is None:
        raise HTTPException(
            status_code=404,
            detail="Could not find writable local file at path " +
            from_url_representation(sink_id) +
            " or path not in configured local_dirs",
        )

    return possible_sink
Example #3
0
async def get_single_thingNode(
        id: str,  # pylint: disable=redefined-builtin
) -> StructureThingNode:
    possible_thing_node = get_thing_node_by_id(id)

    if possible_thing_node is None:
        raise HTTPException(
            status_code=404,
            detail="Could not find directory at path " +
            from_url_representation(id) +
            " or path not in configured local_dirs",
        )

    return possible_thing_node
Example #4
0
def get_local_file_by_id(
        id: str,  # pylint: disable=redefined-builtin
) -> Optional[LocalFile]:
    """Get a specific file by id

    Rudimentarily checks for existence and returns None if local file could not be found
    """
    local_file_path = from_url_representation(id)

    top_dir = get_valid_top_dir(local_file_path)

    if top_dir is None:
        return None

    local_file = local_file_from_path(local_file_path, top_dir=top_dir)

    if local_file is None:
        return None

    if not (os.path.exists(local_file.path)
            or os.path.exists(local_file.path + ".settings.json")):
        return None

    return local_file
Example #5
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)
        ],
    )