query: Move 'file' command to a function

This commit is contained in:
Franciszek Stachura 2025-02-25 23:31:56 +01:00 committed by Théo Lebrun
parent 9bc76f0d8d
commit 53b1ffe208
4 changed files with 38 additions and 36 deletions

View file

@ -2,7 +2,8 @@ import re
from .utils import Filter, FilterContext, encode_number, decode_number, extension_matches
# Filter for DT compatible strings in code (C family) files
# Finds assigments to properties and variables named 'compatible' and recognized by the Query.query('file')
# Finds assigments to properties and variables named 'compatible' and recognized by
# Query.get_tokenized_file()
# .compatible = "device"
# Example: u-boot/v2023.10/source/drivers/phy/nop-phy.c#L84
class DtsCompCodeFilter(Filter):

View file

@ -2,8 +2,8 @@ import re
from .utils import Filter, FilterContext, encode_number, decode_number
# Filter for identifier links
# Replaces identifiers marked by Query.query('file') with links to ident page.
# If Query.query('file') detects that a file belongs to a family that can contain
# Replaces identifiers marked by Query.get_tokenized_file() with links to ident page.
# If Query.get_tokenized_file() detects that a file belongs to a family that can contain
# indexed identifiers, it processes the file by adding unprintable markers
# ('\033[31m' + token + b'\033[0m') to tokens that have an entry in the definitions
# database. This filter replaces these marked tokens with links to their ident pages,

View file

@ -120,39 +120,9 @@ class Query:
return entries_str.split("\n")[:-1]
elif cmd == 'file':
# Returns the contents of the specified file
# Tokens are marked for further processing
# Example: ./query.py file v3.1-rc10 /Makefile
version = args[0]
path = args[1]
filename = os.path.basename(path)
family = lib.getFileFamily(filename)
if family != None:
assert family in lib.CACHED_DEFINITIONS_FAMILIES, f"family {family} must have its definitions cached"
buffer = BytesIO()
tokens = self.scriptLines('tokenize-file', version, path, family)
even = True
prefix = b''
if family == 'K':
prefix = b'CONFIG_'
for tok in tokens:
even = not even
tok2 = prefix + tok
if even and self.db.defs_cache[family].exists(tok2):
tok = b'\033[31m' + tok2 + b'\033[0m'
else:
tok = lib.unescape(tok)
buffer.write(tok)
return decode(buffer.getvalue())
else:
return decode(self.script('get-file', version, path))
return self.get_tokenized_file(version, path)
elif cmd == 'family':
# Get the family of a given file
@ -214,6 +184,37 @@ class Query:
else:
return 'Unknown subcommand: ' + cmd + '\n'
# Returns the contents of the specified file
# Tokens are marked for further processing
# Example: v3.1-rc10 /Makefile
def get_tokenized_file(self, version, path):
filename = os.path.basename(path)
family = lib.getFileFamily(filename)
if family != None:
assert family in lib.CACHED_DEFINITIONS_FAMILIES, f"family {family} must have its definitions cached"
buffer = BytesIO()
tokens = self.scriptLines('tokenize-file', version, path, family)
even = True
prefix = b''
if family == 'K':
prefix = b'CONFIG_'
for tok in tokens:
even = not even
tok2 = prefix + tok
if even and self.db.defs_cache[family].exists(tok2):
tok = b'\033[31m' + tok2 + b'\033[0m'
else:
tok = lib.unescape(tok)
buffer.write(tok)
return decode(buffer.getvalue())
else:
return decode(self.script('get-file', version, path))
# Returns the list of indexed versions in the following format:
# topmenu submenu tag
# Example: v3 v3.1 v3.1-rc10
@ -425,7 +426,7 @@ def cmd_ident(q, version, ident, family, **kwargs):
print(symbol_doccomment)
def cmd_file(q, version, path, **kwargs):
code = q.query("file", version, path)
code = q.get_tokenized_file(version, path)
print(code)
if __name__ == "__main__":

View file

@ -515,7 +515,7 @@ def format_code(filename: str, code: str) -> str:
# version: requested version of the project
# path: path to the file in the repository
def generate_source(q: Query, project: str, version: str, path: str) -> str:
code = q.query('file', version, path)
code = q.get_tokenized_file(version, path)
_, fname = os.path.split(path)
_, extension = os.path.splitext(fname)