ums.utils.functions
1# Agenten Plattform 2# 3# (c) 2024 Magnus Bender 4# Institute of Humanities-Centered Artificial Intelligence (CHAI) 5# Universitaet Hamburg 6# https://www.chai.uni-hamburg.de/~bender 7# 8# source code released under the terms of GNU Public License Version 3 9# https://www.gnu.org/licenses/gpl-3.0.txt 10 11import os 12 13from typing import List, Callable 14 15from ums.utils.const import SHARE_PATH 16 17def list_path(path:str) -> List[str]: 18 if os.path.isdir(path): 19 items = [] 20 for item in os.listdir(path): 21 full = os.path.join(path, item) 22 if os.path.isdir(full): 23 items.extend(list_path(full)) 24 elif os.path.isfile(full): 25 items.append(full) 26 return items 27 else: 28 return [] 29 30def list_shared(filter:Callable=lambda p,n,e: True) -> List[str]: 31 files = [] 32 for f in list_path(SHARE_PATH): 33 r = f[len(SHARE_PATH)+1:] 34 35 if r[0] == '.' or '/.' in r: 36 # hidden files 37 continue 38 39 if '/' in r and '.' in r: 40 path, name, ending = r[:r.rfind('/')], r[r.rfind('/')+1:r.rfind('.')], r[r.rfind('.')+1:] 41 elif '/' in r: 42 path, name, ending = r[:r.rfind('/')], r[r.rfind('/')+1:], "" 43 elif '.' in r: 44 path, name, ending = "", r[:r.rfind('.')], r[r.rfind('.')+1:] 45 else: 46 path, name, ending = "", r, "" 47 48 if filter(path, name, ending): 49 files.append(r) 50 return files 51 52def list_shared_data(): 53 return list_shared(lambda p,n,e: e != "json") 54 55def list_shared_schema(): 56 return list_shared(lambda p,n,e: e == "json")
def
list_path(path: str) -> List[str]: