microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/qsharp/_fs.py
100lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | """ |
| 5 | _fs.py |
| 6 | |
| 7 | This module provides file system utility functions for working with the file |
| 8 | system as Python sees it. These are used as callbacks passed into native code |
| 9 | to allow the native code to interact with the file system in an |
| 10 | environment-specific way. |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | from typing import Dict, List, Tuple |
| 15 | |
| 16 | |
| 17 | def read_file(path: str) -> Tuple[str, str]: |
| 18 | """ |
| 19 | Read the contents of a file. |
| 20 | |
| 21 | Args: |
| 22 | path (str): The path to the file. |
| 23 | |
| 24 | Returns: |
| 25 | Tuple[str, str]: A tuple containing the path and the file contents. |
| 26 | """ |
| 27 | with open(path, mode="r", encoding="utf-8-sig") as f: |
| 28 | return (path, f.read()) |
| 29 | |
| 30 | |
| 31 | def list_directory(dir_path: str) -> List[Dict[str, str]]: |
| 32 | """ |
| 33 | Lists the contents of a directory and returns a list of dictionaries, |
| 34 | where each dictionary represents an entry in the directory. |
| 35 | |
| 36 | Args: |
| 37 | dir_path (str): The path of the directory to list. |
| 38 | |
| 39 | Returns: |
| 40 | List[Dict[str, str]]: A list of dictionaries representing the entries |
| 41 | in the directory. Each dictionary contains the following keys: |
| 42 | - "path": The full path of the entry. |
| 43 | - "entry_name": The name of the entry. |
| 44 | - "type": The type of the entry: "file", "folder", or "unknown". |
| 45 | """ |
| 46 | |
| 47 | def map_dir(e: str) -> Dict[str, str]: |
| 48 | path = os.path.join(dir_path, e) |
| 49 | return { |
| 50 | "path": path, |
| 51 | "entry_name": e, |
| 52 | "type": ( |
| 53 | "file" |
| 54 | if os.path.isfile(path) |
| 55 | else "folder" if os.path.isdir(path) else "unknown" |
| 56 | ), |
| 57 | } |
| 58 | |
| 59 | return list(map(map_dir, os.listdir(dir_path))) |
| 60 | |
| 61 | |
| 62 | def resolve(base: str, path: str) -> str: |
| 63 | """ |
| 64 | Resolves a relative path with respect to a base path. |
| 65 | |
| 66 | Args: |
| 67 | base (str): The base path. |
| 68 | path (str): The relative path. |
| 69 | |
| 70 | Returns: |
| 71 | str: The resolved path. |
| 72 | """ |
| 73 | return os.path.normpath(join(base, path)) |
| 74 | |
| 75 | |
| 76 | def exists(path) -> bool: |
| 77 | """ |
| 78 | Check if a file or directory exists at the given path. |
| 79 | |
| 80 | Args: |
| 81 | path (str): The path to the file or directory. |
| 82 | |
| 83 | Returns: |
| 84 | bool: True if the file or directory exists, False otherwise. |
| 85 | """ |
| 86 | return os.path.exists(path) |
| 87 | |
| 88 | |
| 89 | def join(path: str, *paths) -> str: |
| 90 | """ |
| 91 | Joins one or more path components intelligently. |
| 92 | |
| 93 | Args: |
| 94 | path (str): The base path. |
| 95 | *paths (str): Additional path components to be joined. |
| 96 | |
| 97 | Returns: |
| 98 | str: The concatenated path. |
| 99 | """ |
| 100 | return os.path.join(path, *paths) |