167
167
def parse_ts(ts):
168
168
return datetime.datetime.strptime(ts, ISO8601)
170
def find_class(module_name, class_name):
170
def find_class(module_name, class_name=None):
172
module_name = "%s.%s" % (module_name, class_name)
171
173
modules = module_name.split('.')
173
for module_name in modules:
174
fp, pathname, description = imp.find_module(module_name, path)
175
module = imp.load_module(module_name, fp, pathname, description)
176
if hasattr(module, '__path__'):
177
path = module.__path__
178
return getattr(module, class_name)
177
for m in modules[1:]:
181
c = getattr(__import__(".".join(modules[0:-1])), m)
180
184
def update_dme(username, password, dme_id, ip_address):
186
190
s = urllib2.urlopen(dme_url % (username, password, dme_id, ip_address))
189
def fetch_file(uri, file=None):
193
def fetch_file(uri, file=None, username=None, password=None):
191
195
Fetch a file based on the URI provided. If you do not pass in a file pointer
192
196
a tempfile.NamedTemporaryFile, or None if the file could not be
193
197
retrieved is returned.
194
The URI can be either an HTTP url, or "s3:bucket_name/key_name"
198
The URI can be either an HTTP url, or "s3://bucket_name/key_name"
196
200
boto.log.info('Fetching %s' % uri)
198
202
file = tempfile.NamedTemporaryFile()
200
204
working_dir = boto.config.get("General", "working_dir")
201
if uri.startswith('s3:'):
202
bucket_name, key_name = uri[len('s3:'):].split('/')
205
if uri.startswith('s3://'):
206
bucket_name, key_name = uri[len('s3://'):].split('/')
203
207
c = boto.connect_s3()
204
208
bucket = c.get_bucket(bucket_name)
205
209
key = bucket.get_key(key_name)
206
210
key.get_contents_to_file(file)
207
elif uri.startswith('http'):
212
if username and password:
213
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
214
passman.add_password(None, uri, username, password)
215
authhandler = urllib2.HTTPBasicAuthHandler(passman)
216
opener = urllib2.build_opener(authhandler)
217
urllib2.install_opener(opener)
208
218
s = urllib2.urlopen(uri)
209
219
file.write(s.read())
212
223
boto.log.exception('Problem Retrieving file: %s' % uri)