Extracts the token a qualifier from the text given the line/colum (see test_extract_token_and_qualifier for examples). :param unicode text: :param int line: 0-based :param int column: 0-based
(text, line=0, column=0)
| 217 | |
| 218 | |
| 219 | def extract_token_and_qualifier(text, line=0, column=0): |
| 220 | """ |
| 221 | Extracts the token a qualifier from the text given the line/colum |
| 222 | (see test_extract_token_and_qualifier for examples). |
| 223 | |
| 224 | :param unicode text: |
| 225 | :param int line: 0-based |
| 226 | :param int column: 0-based |
| 227 | """ |
| 228 | # Note: not using the tokenize module because text should be unicode and |
| 229 | # line/column refer to the unicode text (otherwise we'd have to know |
| 230 | # those ranges after converted to bytes). |
| 231 | if line < 0: |
| 232 | line = 0 |
| 233 | if column < 0: |
| 234 | column = 0 |
| 235 | |
| 236 | if isinstance(text, bytes): |
| 237 | text = text.decode("utf-8") |
| 238 | |
| 239 | lines = text.splitlines() |
| 240 | try: |
| 241 | text = lines[line] |
| 242 | except IndexError: |
| 243 | return TokenAndQualifier("", "") |
| 244 | |
| 245 | if column >= len(text): |
| 246 | column = len(text) |
| 247 | |
| 248 | text = text[:column] |
| 249 | token = "" |
| 250 | qualifier = "" |
| 251 | |
| 252 | temp_token = [] |
| 253 | for i in range(column - 1, -1, -1): |
| 254 | c = text[i] |
| 255 | if c in identifier_part or isidentifier(c) or c == ".": |
| 256 | temp_token.append(c) |
| 257 | else: |
| 258 | break |
| 259 | temp_token = "".join(reversed(temp_token)) |
| 260 | if "." in temp_token: |
| 261 | temp_token = temp_token.split(".") |
| 262 | token = ".".join(temp_token[:-1]) |
| 263 | qualifier = temp_token[-1] |
| 264 | else: |
| 265 | qualifier = temp_token |
| 266 | |
| 267 | return TokenAndQualifier(token, qualifier) |