diff --git a/troggle/expo/fileAbstraction.py b/troggle/expo/fileAbstraction.py new file mode 100644 index 000000000..deb1a4a68 --- /dev/null +++ b/troggle/expo/fileAbstraction.py @@ -0,0 +1,25 @@ +import troggle.settings as settings +import os + +def urljoin(x, y): return x + "/" + y + +def listdir(*path): + try: + l = "" + root = os.path.join(settings.FILES, *path) + 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: + return urllib.urlopen(settings.FILES + "listdir/" + reduce(urljoin, path)) + +def readFile(*path): + try: + f = open(os.path.join(settings.FILES, *path)) + except: + f = urllib.urlopen(settings.FILES + "download/" + reduce(urljoin, path)) + return f.read() \ No newline at end of file diff --git a/troggle/expo/view_surveys.py b/troggle/expo/view_surveys.py index cd3210b0b..2c5b5a108 100644 --- a/troggle/expo/view_surveys.py +++ b/troggle/expo/view_surveys.py @@ -1,38 +1,22 @@ -import troggle.settings as settings +import fileAbstraction from django.http import HttpResponse, Http404 -import os + def listdir(request, path): - try: - l = [] - print settings.FILES, "t", path, "t" - root = os.path.join(settings.FILES, path) - print root - for p in os.listdir(root): - if os.path.isdir(os.path.join(root, p)): - l.append(p + "/") - elif os.path.isfile(os.path.join(root, p)): - l.append(p) - #Ignore non-files and non-directories - return HttpResponse(str(l), mimetype = "text/plain") - except: - try: - return HttpResponse(urllib.urlopen(settings.FILES + "listdir/" + name), mimetype = "text/plain") - except: - raise Http404 + #try: + return HttpResponse(fileAbstraction.listdir(path), mimetype = "text/plain") + #except: + # raise Http404 def upload(request, path): pass def download(request, path): - try: - f = open(os.path.join(settings.FILES, path)) - except: - try: - f = urllib.urlopen(settings.FILES + "download/" + path) - except: - raise Http404 - return HttpResponse(f.read(), mimetype=getMimeType(path.split(".")[-1])) + #try: + return HttpResponse(fileAbstraction.readFile(path), mimetype=getMimeType(path.split(".")[-1])) + #except: + # raise Http404 + def getMimeType(extension): try: diff --git a/troggle/parsers/surveys.py b/troggle/parsers/surveys.py index 86d7dc6e0..1fc5edb65 100644 --- a/troggle/parsers/surveys.py +++ b/troggle/parsers/surveys.py @@ -7,6 +7,7 @@ from troggle import * os.environ['DJANGO_SETTINGS_MODULE']='troggle.settings' import troggle.settings as settings import troggle.expo.models as models +import fileAbstraction #import settings #import expo.models as models @@ -16,9 +17,9 @@ import datetime def openFileOrWeb(name): try: - f = open(os.path.join(settings.SURVEYS, name)) + f = open(os.path.join(settings.FILES, name)) except: - f = urllib.urlopen(settings.SURVEYS + name) + f = urllib.urlopen(settings.FILES + "download/" + name) return f.readlines() surveytab = openFileOrWeb("Surveys.csv") @@ -57,12 +58,12 @@ for survey in surveyreader: # add survey scans def parseSurveyScans(year): - yearPath=os.path.join(settings.SURVEYS, year.year) - yearFileList=os.listdir(yearPath) - for surveyFolder in yearFileList: + yearDirList = [d for d in fileAbstraction.listdir(year.year).split("\n") if d[-1] == "/"] + for surveyFolder in yearDirList: + print surveyFolder try: surveyNumber=re.match(r'\d\d\d\d#0*(\d+)',surveyFolder).groups() - scanList=os.listdir(os.path.join(yearPath,surveyFolder)) + scanList=fileAbstraction.listdir(yearPath, surveyFolder).split("\n") except AttributeError: print surveyFolder + " ignored" continue