2019-07-10 17:34:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
class Url2File:
|
2019-12-03 11:30:15 +08:00
|
|
|
def __init__(self,path:str,prefix: str,
|
|
|
|
indexes: list, inherit: bool=False):
|
|
|
|
self.path = path
|
|
|
|
self.starts = prefix
|
2019-07-10 17:34:45 +08:00
|
|
|
self.indexes = indexes
|
|
|
|
self.inherit = inherit
|
|
|
|
|
2019-12-03 11:30:15 +08:00
|
|
|
def realurl(self,url:str) -> str :
|
2019-07-10 17:34:45 +08:00
|
|
|
items = url.split('/')
|
|
|
|
items = [ i for i in items if i != '.' ]
|
|
|
|
while '..' in items:
|
|
|
|
for i,v in enumerate(items):
|
|
|
|
if v=='..' and i > 0:
|
|
|
|
del items[i]
|
|
|
|
del items[i-1]
|
|
|
|
break
|
|
|
|
return '/'.join(items)
|
|
|
|
|
2021-05-06 10:09:01 +08:00
|
|
|
def url2ospath(self, url: str) -> str:
|
|
|
|
url = url.split('?')[0]
|
|
|
|
if len(url) > 0 and url[-1] == '/':
|
2019-07-10 17:34:45 +08:00
|
|
|
url = url[:-1]
|
2020-12-20 11:43:00 +08:00
|
|
|
paths = url.split('/')
|
|
|
|
if url.startswith('http://') or url.startswith('https://'):
|
|
|
|
paths = paths[3:]
|
2020-12-18 14:25:46 +08:00
|
|
|
f = os.path.join(self.path,*paths)
|
|
|
|
real_path = os.path.abspath(f)
|
2021-05-06 10:09:01 +08:00
|
|
|
return real_path
|
|
|
|
|
|
|
|
def url2file(self, url: str) -> str:
|
|
|
|
url = url.split('?')[0]
|
|
|
|
real_path = self.url2ospath(url)
|
2020-12-18 13:46:35 +08:00
|
|
|
if os.path.isdir(real_path):
|
2020-12-18 14:25:46 +08:00
|
|
|
for idx in self.indexes:
|
|
|
|
p = os.path.join(real_path,idx)
|
2020-12-18 13:46:35 +08:00
|
|
|
if os.path.isfile(p):
|
|
|
|
return p
|
|
|
|
|
|
|
|
if os.path.isfile(real_path):
|
|
|
|
return real_path
|
2019-12-03 11:30:15 +08:00
|
|
|
|
2021-05-06 10:09:01 +08:00
|
|
|
if not os.path.isdir(os.path.dirname(real_path)):
|
2023-02-01 11:04:01 +08:00
|
|
|
print(f'url2file() return None, {real_path:}')
|
2021-05-06 10:09:01 +08:00
|
|
|
return None
|
|
|
|
|
2019-07-10 17:34:45 +08:00
|
|
|
if not self.inherit:
|
2023-02-01 11:04:01 +08:00
|
|
|
print(f'url2file() return None, self.inherit is false')
|
2019-07-10 17:34:45 +08:00
|
|
|
return None
|
2021-05-06 10:09:01 +08:00
|
|
|
|
2019-07-10 17:34:45 +08:00
|
|
|
items = url.split('/')
|
|
|
|
if len(items) > 2:
|
|
|
|
del items[-2]
|
|
|
|
url = '/'.join(items)
|
|
|
|
return self.url2file(url)
|
2023-02-01 11:04:01 +08:00
|
|
|
print(f'url2file() return None finally, {items:}')
|
2019-07-10 17:34:45 +08:00
|
|
|
return None
|
|
|
|
|
2019-12-03 11:30:15 +08:00
|
|
|
def relatedurl(self,url: str, name: str) -> str:
|
2021-05-06 10:09:01 +08:00
|
|
|
if len(url) > 0 and url[-1] == '/':
|
2019-07-10 17:34:45 +08:00
|
|
|
url = url[:-1]
|
|
|
|
|
2021-05-06 10:09:01 +08:00
|
|
|
fp = self.url2ospath(url)
|
2020-12-20 11:43:00 +08:00
|
|
|
if os.path.isfile(fp):
|
2019-07-10 17:34:45 +08:00
|
|
|
items = url.split('/')
|
|
|
|
del items[-1]
|
|
|
|
url = '/'.join(items)
|
|
|
|
url = url + '/' + name
|
|
|
|
return self.realurl(url)
|
|
|
|
|
|
|
|
def relatedurl2file(self,url: str, name: str):
|
|
|
|
url = self.relatedurl(url,name)
|
|
|
|
return self.url2file(url)
|
|
|
|
|
|
|
|
class TmplUrl2File(Url2File):
|
2020-12-20 11:43:00 +08:00
|
|
|
def __init__(self,paths,indexes, subffixes=['.tmpl','.ui' ],inherit=False):
|
2019-12-05 06:04:38 +08:00
|
|
|
self.paths = paths
|
|
|
|
self.u2fs = [ Url2File(p,prefix,indexes,inherit=inherit) \
|
|
|
|
for p,prefix in paths ]
|
2019-07-10 17:34:45 +08:00
|
|
|
self.subffixes = subffixes
|
|
|
|
|
2019-12-05 06:04:38 +08:00
|
|
|
def url2file(self,url):
|
|
|
|
for u2f in self.u2fs:
|
|
|
|
fp = u2f.url2file(url)
|
|
|
|
if fp:
|
|
|
|
return fp
|
|
|
|
return None
|
|
|
|
|
2019-07-10 17:34:45 +08:00
|
|
|
def list_tmpl(self):
|
|
|
|
ret = []
|
2019-12-05 06:04:38 +08:00
|
|
|
for rp,_ in self.paths:
|
2019-07-10 17:34:45 +08:00
|
|
|
p = os.path.abspath(rp)
|
|
|
|
[ ret.append(i) for i in listFile(p,suffixs=self.subffixes,rescursive=True) ]
|
|
|
|
return sorted(ret)
|
|
|
|
|