~ubuntu-branches/ubuntu/karmic/python-boto/karmic

« back to all changes in this revision

Viewing changes to boto/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-08-18 17:08:12 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080818170812-b19z47gj515lc5b4
Tags: 1.3a-0ubuntu1
New upstream release. (LP: #246824)

Show diffs side-by-side

added added

removed removed

Lines of Context:
167
167
def parse_ts(ts):
168
168
    return datetime.datetime.strptime(ts, ISO8601)
169
169
 
170
 
def find_class(module_name, class_name):
 
170
def find_class(module_name, class_name=None):
 
171
    if class_name:
 
172
        module_name = "%s.%s" % (module_name, class_name)
171
173
    modules = module_name.split('.')
172
174
    path = None
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)
 
175
    c = None
 
176
 
 
177
    for m in modules[1:]:
 
178
        if c:
 
179
            c = getattr(c, m)
 
180
        else:
 
181
            c = getattr(__import__(".".join(modules[0:-1])), m)
 
182
    return c
179
183
    
180
184
def update_dme(username, password, dme_id, ip_address):
181
185
    """
186
190
    s = urllib2.urlopen(dme_url % (username, password, dme_id, ip_address))
187
191
    return s.read()
188
192
 
189
 
def fetch_file(uri, file=None):
 
193
def fetch_file(uri, file=None, username=None, password=None):
190
194
    """
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"
195
199
    """
196
200
    boto.log.info('Fetching %s' % uri)
197
201
    if file == None:
198
202
        file = tempfile.NamedTemporaryFile()
199
203
    try:
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'):
 
211
        else:
 
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())
210
220
        file.seek(0)
211
221
    except:
 
222
        raise
212
223
        boto.log.exception('Problem Retrieving file: %s' % uri)
213
224
        file = None
214
225
    return file
440
451
        return str(self.str)
441
452
   
442
453
    def __eq__(self, other):
443
 
        return str(_hashfn(value).hexdigest()) == str(self.str)
 
454
        return str(_hashfn(other).hexdigest()) == str(self.str)
444
455
 
445
456
    def __len__(self):
446
457
        return len(self.str)