Agenten-Plattform/ums/utils/functions.py

56 lines
1.4 KiB
Python

# Agenten Plattform
#
# (c) 2024 Magnus Bender
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
# Universitaet Hamburg
# https://www.chai.uni-hamburg.de/~bender
#
# source code released under the terms of GNU Public License Version 3
# https://www.gnu.org/licenses/gpl-3.0.txt
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")