query: Move 'file' command to a function
This commit is contained in:
parent
9bc76f0d8d
commit
53b1ffe208
4 changed files with 38 additions and 36 deletions
|
|
@ -2,7 +2,8 @@ import re
|
||||||
from .utils import Filter, FilterContext, encode_number, decode_number, extension_matches
|
from .utils import Filter, FilterContext, encode_number, decode_number, extension_matches
|
||||||
|
|
||||||
# Filter for DT compatible strings in code (C family) files
|
# 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"
|
# .compatible = "device"
|
||||||
# Example: u-boot/v2023.10/source/drivers/phy/nop-phy.c#L84
|
# Example: u-boot/v2023.10/source/drivers/phy/nop-phy.c#L84
|
||||||
class DtsCompCodeFilter(Filter):
|
class DtsCompCodeFilter(Filter):
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ import re
|
||||||
from .utils import Filter, FilterContext, encode_number, decode_number
|
from .utils import Filter, FilterContext, encode_number, decode_number
|
||||||
|
|
||||||
# Filter for identifier links
|
# Filter for identifier links
|
||||||
# Replaces identifiers marked by Query.query('file') with links to ident page.
|
# Replaces identifiers marked by Query.get_tokenized_file() with links to ident page.
|
||||||
# If Query.query('file') detects that a file belongs to a family that can contain
|
# 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
|
# 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
|
# ('\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,
|
# database. This filter replaces these marked tokens with links to their ident pages,
|
||||||
|
|
|
||||||
|
|
@ -120,39 +120,9 @@ class Query:
|
||||||
return entries_str.split("\n")[:-1]
|
return entries_str.split("\n")[:-1]
|
||||||
|
|
||||||
elif cmd == 'file':
|
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]
|
version = args[0]
|
||||||
path = args[1]
|
path = args[1]
|
||||||
|
return self.get_tokenized_file(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))
|
|
||||||
|
|
||||||
elif cmd == 'family':
|
elif cmd == 'family':
|
||||||
# Get the family of a given file
|
# Get the family of a given file
|
||||||
|
|
@ -214,6 +184,37 @@ class Query:
|
||||||
else:
|
else:
|
||||||
return 'Unknown subcommand: ' + cmd + '\n'
|
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:
|
# Returns the list of indexed versions in the following format:
|
||||||
# topmenu submenu tag
|
# topmenu submenu tag
|
||||||
# Example: v3 v3.1 v3.1-rc10
|
# Example: v3 v3.1 v3.1-rc10
|
||||||
|
|
@ -425,7 +426,7 @@ def cmd_ident(q, version, ident, family, **kwargs):
|
||||||
print(symbol_doccomment)
|
print(symbol_doccomment)
|
||||||
|
|
||||||
def cmd_file(q, version, path, **kwargs):
|
def cmd_file(q, version, path, **kwargs):
|
||||||
code = q.query("file", version, path)
|
code = q.get_tokenized_file(version, path)
|
||||||
print(code)
|
print(code)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -515,7 +515,7 @@ def format_code(filename: str, code: str) -> str:
|
||||||
# version: requested version of the project
|
# version: requested version of the project
|
||||||
# path: path to the file in the repository
|
# path: path to the file in the repository
|
||||||
def generate_source(q: Query, project: str, version: str, path: str) -> str:
|
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)
|
_, fname = os.path.split(path)
|
||||||
_, extension = os.path.splitext(fname)
|
_, extension = os.path.splitext(fname)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue