Complete ~ and ~user strings. :param text: the string prefix we are attempting to match (all matches must begin with it) :param add_trailing_sep_if_dir: whether a trailing separator should be appended to directory completions :return: a Completions object
(text: str, add_trailing_sep_if_dir: bool)
| 2142 | |
| 2143 | @staticmethod |
| 2144 | def _complete_users(text: str, add_trailing_sep_if_dir: bool) -> Completions: |
| 2145 | """Complete ~ and ~user strings. |
| 2146 | |
| 2147 | :param text: the string prefix we are attempting to match (all matches must begin with it) |
| 2148 | :param add_trailing_sep_if_dir: whether a trailing separator should be appended to directory completions |
| 2149 | :return: a Completions object |
| 2150 | """ |
| 2151 | items: list[CompletionItem] = [] |
| 2152 | |
| 2153 | # Windows lacks the pwd module so we can't get a list of users. |
| 2154 | # Instead we will return a result once the user enters text that |
| 2155 | # resolves to an existing home directory. |
| 2156 | if sys.platform.startswith("win"): |
| 2157 | expanded_path = os.path.expanduser(text) |
| 2158 | if os.path.isdir(expanded_path): |
| 2159 | user = text |
| 2160 | if add_trailing_sep_if_dir: |
| 2161 | user += os.path.sep |
| 2162 | items.append(CompletionItem(user)) |
| 2163 | else: |
| 2164 | import pwd |
| 2165 | |
| 2166 | # Iterate through a list of users from the password database |
| 2167 | for cur_pw in pwd.getpwall(): |
| 2168 | # Check if the user has an existing home dir |
| 2169 | if os.path.isdir(cur_pw.pw_dir): |
| 2170 | # Add a ~ to the user to match against text |
| 2171 | cur_user = "~" + cur_pw.pw_name |
| 2172 | if cur_user.startswith(text): |
| 2173 | if add_trailing_sep_if_dir: |
| 2174 | cur_user += os.path.sep |
| 2175 | items.append(CompletionItem(cur_user)) |
| 2176 | |
| 2177 | # Since all ~user matches resolve to directories, set allow_finalization to False |
| 2178 | # so the user can continue into the subdirectory structure. |
| 2179 | return Completions(items=items, allow_finalization=False) |
| 2180 | |
| 2181 | def path_complete( |
| 2182 | self, |
no test coverage detected