query: Move dts commands to functions

This commit is contained in:
Franciszek Stachura 2025-02-25 23:46:13 +01:00 committed by Théo Lebrun
parent ac5a64b972
commit 5563feaf78
4 changed files with 17 additions and 13 deletions

View file

@ -13,7 +13,7 @@ class DtsCompCodeFilter(Filter):
def check_if_applies(self, ctx) -> bool:
return super().check_if_applies(ctx) and \
ctx.query.query('dts-comp') and \
ctx.query.supports_dts_comp() and \
extension_matches(ctx.filepath, {'c', 'cc', 'cpp', 'c++', 'cxx', 'h', 's'})
def transform_raw_code(self, ctx, code: str) -> str:

View file

@ -13,14 +13,14 @@ class DtsCompDocsFilter(Filter):
def check_if_applies(self, ctx) -> bool:
return super().check_if_applies(ctx) and \
ctx.query.query('dts-comp') and \
ctx.query.supports_dts_comp() and \
ctx.filepath.startswith('/Documentation/devicetree/bindings')
def transform_raw_code(self, ctx, code: str) -> str:
def keep_dtscompB(m):
text = m.group(1)
if ctx.query.query('dts-comp-exists', quote(text)):
if ctx.query.dts_comp_exists(quote(text)):
self.dtscompB.append(text)
return f'__KEEPDTSCOMPB__{ encode_number(len(self.dtscompB)) }'
else:

View file

@ -11,7 +11,7 @@ class DtsCompDtsFilter(Filter):
def check_if_applies(self, ctx) -> bool:
return super().check_if_applies(ctx) and \
ctx.query.query('dts-comp') and \
ctx.query.supports_dts_comp() and \
extension_matches(ctx.filepath, {'dts', 'dtsi'})
def transform_raw_code(self, ctx, code: str) -> str:

View file

@ -113,18 +113,11 @@ class Query:
return lib.getFileFamily(filename)
elif cmd == 'dts-comp':
# Get state of dts_comp_support
return self.dts_comp_support
return self.supports_dts_comp()
elif cmd == 'dts-comp-exists':
# Check if a dts compatible string exists
ident = args[0]
if self.dts_comp_support:
return self.db.comps.exists(ident)
else:
return False
return self.dts_comp_exists(ident)
elif cmd == 'keys':
# Return all keys of a given database
@ -165,6 +158,17 @@ class Query:
else:
return 'Unknown subcommand: ' + cmd + '\n'
# Get state of dts_comp_support
def supports_dts_comp(self):
return self.dts_comp_support
# Check if a dts compatible string exists
def dts_comp_exists(self, ident):
if self.dts_comp_support:
return self.db.comps.exists(ident)
else:
return False
# Returns True if file exists
def file_exists(self, version, path):
if version not in self.file_cache: