Web Interface ok
This commit is contained in:
@ -22,4 +22,6 @@ from ums.utils.types import (
|
||||
|
||||
from ums.utils.const import *
|
||||
|
||||
from ums.utils.request import ManagementRequest
|
||||
from ums.utils.request import ManagementRequest
|
||||
|
||||
from ums.utils.functions import list_shared_data, list_shared_schema
|
46
ums/utils/functions.py
Normal file
46
ums/utils/functions.py
Normal file
@ -0,0 +1,46 @@
|
||||
import os
|
||||
|
||||
from typing import List, Callable
|
||||
|
||||
from ums.utils.const import SHARE_PATH
|
||||
|
||||
def list_path(path:str) -> List[str]:
|
||||
if os.path.isdir(path):
|
||||
items = []
|
||||
for item in os.listdir(path):
|
||||
full = os.path.join(path, item)
|
||||
if os.path.isdir(full):
|
||||
items.extend(list_path(full))
|
||||
elif os.path.isfile(full):
|
||||
items.append(full)
|
||||
return items
|
||||
else:
|
||||
return []
|
||||
|
||||
def list_shared(filter:Callable=lambda p,n,e: True) -> List[str]:
|
||||
files = []
|
||||
for f in list_path(SHARE_PATH):
|
||||
r = f[len(SHARE_PATH)+1:]
|
||||
|
||||
if r[0] == '.' or '/.' in r:
|
||||
# hidden files
|
||||
continue
|
||||
|
||||
if '/' in r and '.' in r:
|
||||
path, name, ending = r[:r.rfind('/')], r[r.rfind('/')+1:r.rfind('.')], r[r.rfind('.')+1:]
|
||||
elif '/' in r:
|
||||
path, name, ending = r[:r.rfind('/')], r[r.rfind('/')+1:], ""
|
||||
elif '.' in r:
|
||||
path, name, ending = "", r[:r.rfind('.')], r[r.rfind('.')+1:]
|
||||
else:
|
||||
path, name, ending = "", r, ""
|
||||
|
||||
if filter(path, name, ending):
|
||||
files.append(r)
|
||||
return files
|
||||
|
||||
def list_shared_data():
|
||||
return list_shared(lambda p,n,e: e != "json")
|
||||
|
||||
def list_shared_schema():
|
||||
return list_shared(lambda p,n,e: e == "json")
|
@ -92,11 +92,15 @@ import os
|
||||
|
||||
from enum import Enum
|
||||
|
||||
from typing import List
|
||||
from typing import List, Any
|
||||
from typing_extensions import Annotated
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic.functional_validators import AfterValidator
|
||||
from pydantic import (
|
||||
BaseModel,
|
||||
ValidationError, ValidationInfo,
|
||||
ValidatorFunctionWrapHandler
|
||||
)
|
||||
from pydantic.functional_validators import WrapValidator, AfterValidator
|
||||
|
||||
from ums.utils.const import SHARE_PATH
|
||||
|
||||
@ -127,7 +131,22 @@ def _check_data_file(file_name:str) -> str:
|
||||
|
||||
return file_name
|
||||
|
||||
def _ignore_file_missing(
|
||||
v: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
|
||||
) -> str:
|
||||
try:
|
||||
return handler(v)
|
||||
except ValidationError:
|
||||
if not info.context is None and \
|
||||
"require_file_exists" in info.context and \
|
||||
info.context["require_file_exists"] == False and \
|
||||
isinstance(v, str):
|
||||
return "missing:{}".format(v)
|
||||
else:
|
||||
raise
|
||||
|
||||
class RiddleData(RiddleInformation):
|
||||
|
||||
"""
|
||||
A data item to be used to solve the riddle
|
||||
"""
|
||||
@ -137,7 +156,7 @@ class RiddleData(RiddleInformation):
|
||||
The type of the data item.
|
||||
"""
|
||||
|
||||
file_plain: Annotated[str, AfterValidator(_check_data_file)]
|
||||
file_plain: Annotated[str, AfterValidator(_check_data_file), WrapValidator(_ignore_file_missing)]
|
||||
"""
|
||||
The plain file (as path to file system) without any processing.
|
||||
|
||||
@ -145,7 +164,7 @@ class RiddleData(RiddleInformation):
|
||||
The file must exist.
|
||||
"""
|
||||
|
||||
file_extracted: Annotated[str, AfterValidator(_check_data_file)] | None = None
|
||||
file_extracted: Annotated[str, AfterValidator(_check_data_file), WrapValidator(_ignore_file_missing)] | None = None
|
||||
"""
|
||||
The processed files (as path to file system), i.e., a schematic file containing all extracted informations.
|
||||
|
||||
@ -153,6 +172,8 @@ class RiddleData(RiddleInformation):
|
||||
The file must exist.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
class RiddleSolution(RiddleInformation):
|
||||
"""
|
||||
A solution of a riddle.
|
||||
|
Reference in New Issue
Block a user