| 1 | def _pdbFetch(site, url, params, statusName, saveDir='', saveName='',
|
|---|
| 2 | ignore_cache=False, post=True):
|
|---|
| 3 |
|
|---|
| 4 | print "custom _pdbFetch"
|
|---|
| 5 | if saveName and not ignore_cache:
|
|---|
| 6 | print "fetching local file"
|
|---|
| 7 | try:
|
|---|
| 8 | from chimera import fetch
|
|---|
| 9 | except Exception, v:
|
|---|
| 10 | print "importing fetch threw error:", str(v)
|
|---|
| 11 | raise
|
|---|
| 12 | print "fetch imported"
|
|---|
| 13 | path = fetch.fetch_local_file(saveDir, saveName)
|
|---|
| 14 | print "fetch_local_file returned", path
|
|---|
| 15 | if path:
|
|---|
| 16 | print "fetched local file", saveName, "to", path
|
|---|
| 17 | return path
|
|---|
| 18 | print "issuing status"
|
|---|
| 19 | from chimera import replyobj
|
|---|
| 20 | replyobj.status('Fetching %s from web site %s' %
|
|---|
| 21 | (statusName, site), blankAfter=0)
|
|---|
| 22 |
|
|---|
| 23 | from chimera import tasks
|
|---|
| 24 | cancelled = []
|
|---|
| 25 | def cancelCB():
|
|---|
| 26 | cancelled.append(True)
|
|---|
| 27 | print "creating task"
|
|---|
| 28 | task = tasks.Task("Fetch %s" % statusName, cancelCB, modal=True)
|
|---|
| 29 |
|
|---|
| 30 | def reportCB(barrived, bsize, fsize):
|
|---|
| 31 | if cancelled:
|
|---|
| 32 | raise IOError("cancelled at user request")
|
|---|
| 33 | if fsize > 0:
|
|---|
| 34 | percent = (100.0 * barrived * bsize) / fsize
|
|---|
| 35 | prog = '%.0f%% of %d bytes' % (percent, fsize)
|
|---|
| 36 | else:
|
|---|
| 37 | prog = '%.0f Kbytes received' % ((barrived * bsize) / 1024,)
|
|---|
| 38 | task.updateStatus(prog)
|
|---|
| 39 | import urllib
|
|---|
| 40 | print "encoding params"
|
|---|
| 41 | params = urllib.urlencode(params)
|
|---|
| 42 | print "urlretrieve"
|
|---|
| 43 | try:
|
|---|
| 44 | if post:
|
|---|
| 45 | print "post", "http://%s/%s" % (site, url), "with data:", params
|
|---|
| 46 | tf, headers = urllib.urlretrieve("http://%s/%s" % (site, url),
|
|---|
| 47 | data=params, reporthook=reportCB)
|
|---|
| 48 | else:
|
|---|
| 49 | print "non-post", "http://%s/%s?%s" % (site, url, params)
|
|---|
| 50 | tf, headers = urllib.urlretrieve("http://%s/%s?%s"
|
|---|
| 51 | % (site, url, params), reporthook=reportCB)
|
|---|
| 52 | except (IOError, UnicodeError), v:
|
|---|
| 53 | replyobj.status("")
|
|---|
| 54 | raise NonChimeraError("Error during PDB fetch: " + str(v))
|
|---|
| 55 | finally:
|
|---|
| 56 | task.finished()
|
|---|
| 57 | task = None
|
|---|
| 58 |
|
|---|
| 59 | replyobj.status('Done fetching %s; verifying...' % statusName)
|
|---|
| 60 | fetched = open(tf, "r")
|
|---|
| 61 | numLines = 0
|
|---|
| 62 | for l in fetched:
|
|---|
| 63 | numLines += 1
|
|---|
| 64 | if numLines >= 20:
|
|---|
| 65 | break
|
|---|
| 66 | else:
|
|---|
| 67 | # too short; not a PDB file
|
|---|
| 68 | fetched.close()
|
|---|
| 69 | os.unlink(tf)
|
|---|
| 70 | replyobj.status("")
|
|---|
| 71 | raise UserError, "No such ID: %s" % statusName
|
|---|
| 72 | fetched.close()
|
|---|
| 73 | if saveName:
|
|---|
| 74 | from chimera import fetch
|
|---|
| 75 | spath = fetch.save_fetched_file(tf, saveDir, saveName)
|
|---|
| 76 | if spath:
|
|---|
| 77 | tf = spath
|
|---|
| 78 | replyobj.status("Opening %s..." % statusName, blankAfter=0)
|
|---|
| 79 | return tf
|
|---|
| 80 |
|
|---|
| 81 | import chimera
|
|---|
| 82 | oldPdbFetch = chimera._pdbFetch
|
|---|
| 83 | chimera._pdbFetch = _pdbFetch
|
|---|
| 84 | try:
|
|---|
| 85 | chimera.runCommand("open pdb:2val")
|
|---|
| 86 | print "open worked"
|
|---|
| 87 | except:
|
|---|
| 88 | print "open failed"
|
|---|
| 89 | import urllib
|
|---|
| 90 | params = urllib.urlencode({})
|
|---|
| 91 | try:
|
|---|
| 92 | urllib.urlretrieve("http://www.rcsb.org/files/pdb/2VAL.pdb", data=params)
|
|---|
| 93 | print "plain http to RCSB works"
|
|---|
| 94 | except:
|
|---|
| 95 | print "plain http to RCSB fails"
|
|---|
| 96 | try:
|
|---|
| 97 | urllib.urlretrieve("https://www.rcsb.org/files/pdb/2VAL.pdb", data=params)
|
|---|
| 98 | print "https to RCSB works"
|
|---|
| 99 | except:
|
|---|
| 100 | print "https to RCSB fails"
|
|---|
| 101 | try:
|
|---|
| 102 | urllib.urlretrieve("http://www.cgl.ucsf.edu/chimera/docindex.html", data=params)
|
|---|
| 103 | print "plain http to CGL works"
|
|---|
| 104 | except:
|
|---|
| 105 | print "plain http to CGL fails"
|
|---|
| 106 |
|
|---|
| 107 | import os
|
|---|
| 108 | host_file = "C:\\Windows\\System32\\drivers\\etc\\host"
|
|---|
| 109 | if os.path.exists(host_file):
|
|---|
| 110 | print "host file exists; contents:"
|
|---|
| 111 | f = open(host_file, "r")
|
|---|
| 112 | for line in f:
|
|---|
| 113 | print line,
|
|---|
| 114 | f.close()
|
|---|
| 115 | else:
|
|---|
| 116 | print "host file does not exist"
|
|---|
| 117 |
|
|---|
| 118 | chimera._pdbFetch = oldPdbFetch
|
|---|