Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mod = sys.modules[mod_name]
except KeyError:
lgr.warning("Failed to access module %s among sys.modules" % mod_name)
return
# go through the known entries and register them within the collector, and
# decorate corresponding methods
# There could be multiple records per module
for obj_path, obj_entry_records in iteritems(self._entry_records[mod_name]):
parent, obj_name = None, None
if obj_path:
# so we point to an object within the mod
try:
parent, obj_name, obj = find_object(mod, obj_path)
except (KeyError, AttributeError) as e:
lgr.warning("Could not find %s in module %s: %s" % (obj_path, mod, e))
continue
# there could be multiple per func
lgr.log(4, "Considering %d records for decoration of %s:%s", len(obj_entry_records), parent, obj_name)
for obj_entry_record in obj_entry_records:
entry = obj_entry_record['entry']
# Add entry explicitly
self._collector.add(entry)
if obj_path: # if not entire module -- decorate!
decorator = self._collector.dcite(entry.get_key(), **obj_entry_record['kwargs'])
lgr.debug("Decorating %s:%s with %s", parent, obj_name, decorator)
obj_decorated = decorator(obj)
setattr(parent, obj_name, obj_decorated)
# override previous obj with the decorated one if there are multiple decorators
obj = obj_decorated
else:
if PY2:
return doi.decode('utf-8')
return doi
# else -- fetch it
headers = {'Accept': 'application/x-bibtex; charset=utf-8'}
url = 'https://doi.org/' + doi
while retries > 0:
lgr.debug("Submitting GET to %s with headers %s", url, headers)
r = requests.get(url, headers=headers)
r.encoding = 'UTF-8'
bibtex = r.text.strip()
if bibtex.startswith('@'):
# no more retries necessary
break
lgr.warning("Failed to obtain bibtex from doi.org, retrying...")
time.sleep(sleep) # give some time to the server
retries -= 1
status_code = r.status_code
if not bibtex.startswith('@'):
raise ValueError('Query %(url)s for BibTeX for a DOI %(doi)s (wrong doi?) has failed. '
'Response code %(status_code)d. '
#'BibTeX response was: %(bibtex)s'
% locals())
if not exists(cached):
cache_dir = dirname(cached)
if not exists(cache_dir):
os.makedirs(cache_dir)
with open(cached, 'w') as f:
if PY2:
f.write(bibtex.encode('utf-8'))
else:
def deactivate(self):
if not self._orig_import:
lgr.warning("_orig_import is not yet known, so we haven't decorated default importer yet."
" Nothing TODO")
return
if not self._active: # pragma: no cover
lgr.error("Must have not happened, but we will survive!")
lgr.debug("Assigning original importer")
__builtin__.__import__ = self._orig_import
self._orig_import = None
self._active = False