MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / query_lines

Function query_lines

src/licensedcode/tokenize.py:28–70  ·  view source on GitHub ↗

Return an iterable of tuples (line number, text line) given a file at `location` or a `query string`. Include empty lines. Line numbers start at ``start_line`` which is 1-based by default. If `plain_text` is True treat the file as a plain text file and do not attempt to detect

(
    location=None,
    query_string=None,
    strip=True,
    start_line=1,
    plain_text=False,
)

Source from the content-addressed store, hash-verified

26
27
28def query_lines(
29 location=None,
30 query_string=None,
31 strip=True,
32 start_line=1,
33 plain_text=False,
34):
35 """
36 Return an iterable of tuples (line number, text line) given a file at
37 `location` or a `query string`. Include empty lines.
38 Line numbers start at ``start_line`` which is 1-based by default.
39
40 If `plain_text` is True treat the file as a plain text file and do not
41 attempt to detect its type and extract its content with special procedures.
42 This is used mostly when loading license texts and rules.
43 """
44 # TODO: OPTIMIZE: tokenizing line by line may be rather slow
45 # we could instead get lines and tokens at once in a batch?
46 numbered_lines = []
47 if location:
48 numbered_lines = numbered_text_lines(
49 location,
50 demarkup=False,
51 start_line=start_line,
52 plain_text=plain_text,
53 )
54
55 elif query_string:
56 if strip:
57 keepends = False
58 else:
59 keepends = True
60
61 numbered_lines = enumerate(
62 query_string.splitlines(keepends),
63 start_line,
64 )
65
66 for line_number, line in numbered_lines:
67 if strip:
68 yield line_number, line.strip()
69 else:
70 yield line_number, line.rstrip('\n') + '\n'
71
72# Split on whitespace and punctuations: keep only characters and numbers and +
73# when in the middle or end of a word. Keeping the trailing + is important for

Calls 1

numbered_text_linesFunction · 0.90