2009-05-13 05:36:17 +01:00
|
|
|
import troggle.settings as settings
|
|
|
|
import os
|
2020-05-24 01:57:06 +01:00
|
|
|
import urllib.request, urllib.parse, urllib.error
|
|
|
|
from functools import reduce
|
2009-05-13 05:36:17 +01:00
|
|
|
|
|
|
|
def urljoin(x, y): return x + "/" + y
|
|
|
|
|
|
|
|
def listdir(*path):
|
|
|
|
try:
|
|
|
|
strippedpath = [p for p in path if p]
|
|
|
|
root = os.path.join(settings.FILES, *strippedpath )
|
|
|
|
l = ""
|
|
|
|
#l = root + "\n"
|
2009-05-13 05:39:05 +01:00
|
|
|
isdir = os.path.isdir(root) #This seems to be required for os.path.isdir to work...
|
2009-05-13 05:36:17 +01:00
|
|
|
#l += str(isdir) + "\n"
|
|
|
|
for p in os.listdir(root):
|
|
|
|
if os.path.isdir(os.path.join(root, p)):
|
|
|
|
l += p + "/\n"
|
|
|
|
|
|
|
|
elif os.path.isfile(os.path.join(root, p)):
|
|
|
|
l += p + "\n"
|
|
|
|
#Ignore non-files and non-directories
|
|
|
|
return l
|
|
|
|
except:
|
2009-05-13 05:37:24 +01:00
|
|
|
if strippedpath:
|
|
|
|
c = reduce(urljoin, strippedpath)
|
|
|
|
else:
|
|
|
|
c = ""
|
2009-05-13 05:38:37 +01:00
|
|
|
c = c.replace("#", "%23")
|
2020-05-24 01:57:06 +01:00
|
|
|
print(("FILE: ", settings.FILES + "listdir/" + c))
|
|
|
|
return urllib.request.urlopen(settings.FILES + "listdir/" + c).read()
|
2009-05-13 05:38:37 +01:00
|
|
|
|
|
|
|
def dirsAsList(*path):
|
|
|
|
return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] == "/"]
|
2009-05-13 05:36:17 +01:00
|
|
|
|
2009-05-13 05:38:37 +01:00
|
|
|
def filesAsList(*path):
|
|
|
|
return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] != "/"]
|
2009-05-13 05:36:17 +01:00
|
|
|
|
|
|
|
def readFile(*path):
|
|
|
|
try:
|
|
|
|
f = open(os.path.join(settings.FILES, *path))
|
|
|
|
except:
|
2020-05-24 01:57:06 +01:00
|
|
|
f = urllib.request.urlopen(settings.FILES + "download/" + reduce(urljoin, path))
|
2009-05-13 05:34:52 +01:00
|
|
|
return f.read()
|