[703] | 1 | #!/usr/bin/python |
---|
| 2 | import os |
---|
[1295] | 3 | import sre |
---|
[739] | 4 | from obsconfig import observatory |
---|
[703] | 5 | |
---|
| 6 | class FileList: |
---|
| 7 | |
---|
[1295] | 8 | def __init__(self, loc=None, projectcode=None): |
---|
[703] | 9 | self.message ="""Content-Type: text/xml |
---|
| 10 | |
---|
| 11 | <?xml version="1.0" encoding="ISO-8859-1"?>""" |
---|
| 12 | self.error = None |
---|
| 13 | if loc is None: |
---|
[712] | 14 | loc = observatory['rpfpath'][0] |
---|
[703] | 15 | else: |
---|
| 16 | loc = int(loc) |
---|
[712] | 17 | if loc< 0 or loc >=len(observatory['rpfpath']): |
---|
[703] | 18 | self.error = "Invalid Path" |
---|
| 19 | return |
---|
| 20 | else: |
---|
[712] | 21 | loc = observatory['rpfpath'][loc] |
---|
[703] | 22 | if os.path.exists(loc) and os.path.isdir(loc): |
---|
[1295] | 23 | rx = sre.compile("\d{4}-\d{2}-d{2}_\d{4}-M\d{3}.rpf.*") |
---|
| 24 | self.files = [ f for f in os.listdir(loc) if sre.match(rx, f) ] |
---|
| 25 | if projectcode is not None: |
---|
| 26 | self.files = [ f for f in self.files if sre.match(projectcode, |
---|
| 27 | f) ] |
---|
[703] | 28 | if len(self.files) == 0: |
---|
| 29 | self.error = "No rpfits files found" |
---|
| 30 | else: |
---|
| 31 | self.error = "Invalid Path" |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | def __str__(self): |
---|
| 35 | if self.error: |
---|
| 36 | self.message += "<Error>\n" |
---|
| 37 | self.message += self.error |
---|
| 38 | self.message += "</Error>\n" |
---|
| 39 | else: |
---|
| 40 | self.message += "<Listing>\n" |
---|
| 41 | for s in self.files: |
---|
| 42 | self.message += "<File>" + s + "</File>\n" |
---|
| 43 | self.message += "</Listing>\n" |
---|
| 44 | return self.message |
---|
| 45 | if __name__ == "__main__": |
---|
| 46 | import cgi |
---|
| 47 | form = cgi.FieldStorage() |
---|
[1295] | 48 | pfilter = None |
---|
| 49 | if form.has_key('project'): |
---|
| 50 | pfilter = form.getfirst("project", None) |
---|
[703] | 51 | if form.has_key('path'): |
---|
| 52 | pathindex = form.getfirst("path",0) |
---|
[1295] | 53 | print FileList(pathindex, pfilter) |
---|
[703] | 54 | else: |
---|
| 55 | print FileList() |
---|