def load_tokc(self):
try:
with open(self.base_fname + '.tokc', mode='rb') as f:
self.tokc = pickle.load(f)
if not self.tokc:
raise IndexLoadError
except (IOError, pickle.UnpicklingError):
raise IndexLoadError
python类UnpicklingError()的实例源码
def _restore_drover(workdir):
"""Restores a saved drover state contained within a workdir.
Args:
workdir: A string containing the path to the workdir used by drover.
"""
try:
with open(os.path.join(workdir, '.git', 'drover'), 'rb') as f:
drover = cPickle.load(f)
drover._process_options()
return drover
except (IOError, cPickle.UnpicklingError):
raise Error('%r is not git drover workdir' % workdir)
def findGlobal(self, module, klass):
"""Find class name."""
if (module, klass) not in self.allowedGlobals():
raise UnpicklingError("For security reasons, you can\'t unpickle"
" objects from module %s with type %s." % (module, klass))
g = {}
exec 'from %s import %s as theClass' % (module, klass) in g
return g['theClass']
def decode(self, data):
try:
return cPickle.loads(data)
except cPickle.UnpicklingError:
raise CodingError()
def loads(self, s):
up = Unpickler(BytesIO(s))
up.persistent_load = self._get_object
try:
return up.load()
except KeyError, e:
raise UnpicklingError("Could not find Node class for %s" % e)
def loads(self, s):
up = Unpickler(BytesIO(s))
up.persistent_load = self._get_object
try:
return up.load()
except KeyError, e:
raise UnpicklingError("Could not find Node class for %s" % e)
def load(pickle_file):
"""output: is_exist, value"""
try:
pickle_fd = open(pickle_file, "r")
except IOError as err:
if errno.ENOENT == err.errno:
debug("cache file does not exist: %s" % pickle_file)
return False, None
assert False
try:
value = cPickle.load(pickle_fd)
return True, value
except (ValueError, UnpicklingError, EOFError):
error("cannot read pickle file: %s, suggest re-fetch the pickle file" % pickle_file)
assert False
def main():
opts = parseOptions()
if (not opts):
exit(1)
#pushUrl = opts.push_url
obsList = opts.obs_list.split(',')
host = opts.push_host
port = int(opts.port)
client = ngamsPClient.ngamsPClient(host, port, timeOut = NGAMS_SOCK_TIMEOUT_DEF)
toUrl = getPushURL("%s:%d" % (host, port), gateway = proxy_archive)
stageUrl = 'http://%s/ASYNCLISTRETRIEVE' % opts.data_mover
for obsNum in obsList:
print "Checking observation: %s" % obsNum
files = getFileIdsByObsNum(obsNum)
deliverFileIds = []
for fileId in files:
# first check if MIT has it or not
if (not hasMITGotIt(client, fileId)):
deliverFileIds.append(fileId)
"""
fileName = getFileFullPath(fileId)
if (not os.path.exists(fileName)):
print "\tFile %s does not exist" % fileName
continue
onTape = ngamsMWACortexTapeApi.isFileOnTape(fileName)
if (1 == onTape):
stageFile(fileName)
print "\tPushing file %s to MIT" % fileId
archiveFile(fileName, client)
"""
else:
print "\tFile %s is already at MIT. Skip it." % fileId
myReq = AsyncListRetrieveRequest(deliverFileIds, toUrl)
strReq = pickle.dumps(myReq)
try:
print "Sending async retrieve request to the data mover %s" % opts.data_mover
request = urllib2.Request(stageUrl)
base64string = base64.encodestring('ngasmgr:ngas$dba').replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
strRes = urllib2.urlopen(request, data = strReq, timeout = NGAMS_SOCK_TIMEOUT_DEF).read()
myRes = pickle.loads(strRes)
#strRes = urllib2.urlopen(stageUrl, data = strReq, timeout = NGAMS_SOCK_TIMEOUT_DEF).read()
#myRes = pickle.loads(strRes)
if (myRes):
print myRes.errorcode
else:
print 'Response is None when async staging files for obsNum %s' % obsNum
except (UnpicklingError, socket.timeout) as uerr:
print "Something wrong while sending async retrieve request for obsNum %s, %s" % (obsNum, str(uerr))
def loadpickle(fln):
"""
load a pickle and return content as dictionary
Parameters
==========
fln : string
filename
Returns
=======
out : dict
dictionary with content from file
See Also
========
savepickle
Examples
========
**note**: If fln is not found, but the same filename with '.gz'
is found, will attempt to open the .gz as a gzipped file.
>>> d = loadpickle('test.pbin')
"""
if not os.path.exists(fln) and os.path.exists(fln + '.gz'):
gzip = True
fln += '.gz'
else:
try:
with open(fln, 'rb') as fh:
try: #Py3k
return pickle.load(fh, encoding='latin1')
except TypeError:
return pickle.load(fh)
except pickle.UnpicklingError: #maybe it's a gzip?
gzip = True
else:
gzip = False
if gzip:
try:
import zlib
with open(fln, 'rb') as fh:
stream = zlib.decompress(fh.read(), 16 + zlib.MAX_WBITS)
try: #Py3k
return pickle.loads(stream, encoding='latin1')
except TypeError:
return pickle.loads(stream)
except MemoryError:
import gzip
with open(fln) as fh:
gzh = gzip.GzipFile(fileobj=fh)
try: #Py3k
contents = pickle.load(gzh, encoding='latin1')
except TypeError:
contents = pickle.load(gzh)
gzh.close()
return contents
# -----------------------------------------------