Add Filter interace, class for context used by filters

* Move filter utils to a new file
This commit is contained in:
Franciszek Stachura 2024-07-30 21:09:32 +02:00
parent f5f49b6b39
commit afdc1c4512
2 changed files with 88 additions and 26 deletions

View file

@ -1,31 +1,7 @@
# Common filters
from filters.utils import encode_number, decode_number
def encode_number(number):
result = ''
while number != 0:
number, rem = divmod(number, 10)
rem = chr(ord('A') + rem)
result = rem + result
return result
def decode_number(string):
result = ''
while string != '':
string, char = string[:-1], string[-1]
char = str(ord(char) - ord('A'))
result = char + result
return int(result)
new_filters = []
filters = []
exec(open('dtscomp.py').read())
exec(open('ident.py').read())

86
http/filters/utils.py Normal file
View file

@ -0,0 +1,86 @@
import re
import os
from dataclasses import dataclass
from typing import Callable, List
from query import Query
# Context data used by Filters
# tag: browsed version, unqoted
# family: family of file
# path: path of file
# get_ident_url: function that returns URL to identifier passed as argument
# get_absolute_source_url: function that returns a URL to file with absolute path passed as an argument
# get_relative_source_url: function that returns a URL to file in directory of current file
@dataclass
class FilterContext:
query: Query
tag: str
family: str
filepath: str
get_ident_url: str
get_absolute_source_url: Callable[[str], str]
get_relative_source_url: Callable[[str], str]
# Filter interface/base class
class Filter:
def __init__(self, path_exceptions: List[str] = []):
self.path_exceptions = path_exceptions
# Return True if filter can be applied to file with path
def check_if_applies(self, ctx: FilterContext) -> bool:
for p in self.path_exceptions:
if re.match(p, ctx.filepath):
return False
return True
# Add information required by filter by transforming raw source code.
# Known identifiers are marked by '\033[31m' and '\033[0m'
def transform_raw_code(self, ctx: FilterContext, code: str) -> str:
return code
# Replace information left by `transform_raw_code` with target HTML
# html: HTML output from code formatter
def untransform_formatted_code(self, ctx: FilterContext, html: str) -> str:
return html
# Returns true if filename from filepath, with removed extension, is in the
# allowed_filenames_without_ext iterable
def filename_without_ext_matches(filepath: str, allowed_filenames_without_ext) -> bool:
filename = os.path.basename(filepath)
filename_without_ext, _ = os.path.splitext(filename)
return filename_without_ext in allowed_filenames_without_ext
# Returns true if extension of filename from filepath is in the
# allowed_extensions iterable
def extension_matches(filepath: str, allowed_extensions) -> bool:
_, file_ext_dot = os.path.splitext(filepath)
file_ext = file_ext_dot[1:].lower()
return file_ext in allowed_extensions
# Encodes an integer into a string of characters (A-J)
# encode_number(10239) = 'BACDJ'
def encode_number(number):
result = ''
while number != 0:
number, rem = divmod(number, 10)
rem = chr(ord('A') + rem)
result = rem + result
return result
# Decodes a string of characters returned by encode_number into an integer
# decode_number('BACDJ') = 10239
def decode_number(string):
result = ''
while string != '':
string, char = string[:-1], string[-1]
char = str(ord(char) - ord('A'))
result = char + result
return int(result)