Skip to content

Commit 10158d5

Browse files
committed
added utility to read and cache remote metadata feeds.
1 parent 95ffb58 commit 10158d5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/utility/__init__.py

Whitespace-only changes.

src/utility/metadata.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os.path, sys, time, urllib
2+
from time import strftime
3+
import logging
4+
5+
__author__ = 'rhoerbe'
6+
7+
logger = logging.getLogger(__name__)
8+
9+
def fetch_metadata(url, path, maxage=600):
10+
"""
11+
:param url: metadata remote location
12+
:param path: metdata file name
13+
:param maxage: if max age of existing metadata file (s) is exceeded,
14+
the file will be fetched from the remote location
15+
"""
16+
fetch = False
17+
if not os.path.isfile(path):
18+
fetch = True
19+
logger.debug("metadata file %s not found" % path)
20+
elif (os.path.getmtime(path) + maxage) < time.time():
21+
fetch = True
22+
logger.debug("metadata file %s from %s is more than %s s old" %
23+
(path,
24+
strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(path))),
25+
maxage))
26+
else:
27+
logger.debug("metadata file %s is less than %s s old" % (path, maxage))
28+
if fetch:
29+
f=urllib.URLopener()
30+
try:
31+
f.retrieve(url, path)
32+
logger.debug("downloaded metadata from %s into %s" % (url, path))
33+
except:
34+
logger.debug("downloaded metadata from %s failed: %s" %
35+
(url, sys.exc_info()[0]))
36+

0 commit comments

Comments
 (0)