def _add_details(self, info): """ Takes the dict returned by the API call and sets the corresponding attributes on the object. """ for (key, val) in info.iteritems(): if isinstance(key, six.text_type): key = key.encode(pyrax.get_encoding()) setattr(self, key, val)
def get_object(self, name): """ Return the StorageObject in this container with the specified name. """ if isinstance(name, str): name = name.decode(pyrax.get_encoding()) ret = self._object_cache.get(name) if not ret: ret = self.client.get_object(self, name) self._object_cache[name] = ret return ret
def _add_details(self, info): """ Takes the dict returned by the API call and sets the corresponding attributes on the object. """ for (key, val) in six.iteritems(info): if isinstance(key, six.text_type) and six.PY2: key = key.encode(pyrax.get_encoding()) elif isinstance(key, bytes): key = key.decode("utf-8") else: key = key setattr(self, key, val)
def _add_details(self, info): """ Takes the dict returned by the API call and sets the corresponding attributes on the object. """ for (key, val) in six.iteritems(info): if isinstance(key, six.text_type): key = key.encode(pyrax.get_encoding()) # six binary needs to be dropped back into str key = key.decode("utf-8") elif isinstance(key, bytes): key = key.decode("utf-8") setattr(self, key, val)
def get_object(self, name, cached=True): """ Return the StorageObject in this container with the specified name. By default, if a reference to that object has already been retrieved, a cached reference will be returned. If you need to get an updated version of the object, pass `cached=False` to the method call. """ if isinstance(name, str): name = name.decode(pyrax.get_encoding()) ret = None if cached: ret = self._object_cache.get(name) if not ret: ret = self.client.get_object(self, name) self._object_cache[name] = ret return ret
def get_object(self, name): """ Return the StorageObject in this container with the specified name. """ if isinstance(name, str): name = name.decode(pyrax.get_encoding()) ret = self._object_cache.get(name) if not ret: cont_objs = self.client.get_container_objects(self.name, full_listing=True) objs = [obj for obj in cont_objs if obj.name == name] try: ret = objs[0] except IndexError: raise exc.NoSuchObject("No object with the name '%s' exists" % name) self._object_cache[name] = ret return ret
def get_temp_url(self, container, obj, seconds, method="GET"): """ Given a storage object in a container, returns a URL that can be used to access that object. The URL will expire after `seconds` seconds. The only methods supported are GET and PUT. Anything else will raise an InvalidTemporaryURLMethod exception. """ cname = self._resolve_name(container) oname = self._resolve_name(obj) mod_method = method.upper().strip() if mod_method not in ("GET", "PUT"): raise exc.InvalidTemporaryURLMethod("Method must be either 'GET' " "or 'PUT'; received '%s'." % method) key = self.get_temp_url_key() if not key: raise exc.MissingTemporaryURLKey("You must set the key for " "Temporary URLs before you can generate them. This is " "done via the `set_temp_url_key()` method.") conn_url = self.connection.url v1pos = conn_url.index("/v1/") base_url = conn_url[:v1pos] path_parts = (conn_url[v1pos:], cname, oname) cleaned = (part.strip("/\\") for part in path_parts) pth = "/%s" % "/".join(cleaned) if isinstance(pth, unicode): pth = pth.encode(pyrax.get_encoding()) expires = int(time.time() + int(seconds)) hmac_body = "%s\n%s\n%s" % (mod_method, expires, pth) try: sig = hmac.new(key, hmac_body, hashlib.sha1).hexdigest() except TypeError as e: raise exc.UnicodePathError("Due to a bug in Python, the TempURL " "function only works with ASCII object paths.") temp_url = "%s%s?temp_url_sig=%s&temp_url_expires=%s" % (base_url, pth, sig, expires) return temp_url
def test_get_encoding(self): sav = pyrax.get_setting pyrax.get_setting = Mock(return_value=None) enc = pyrax.get_encoding() self.assertEqual(enc, pyrax.default_encoding) pyrax.get_setting = sav
def _safe_quote(val): # urllib.parse.quote expects bytes SAFE_QUOTE_CHARS = b"/.?&=," if isinstance(val, six.text_type): val = val.encode(pyrax.get_encoding()) return urllib.parse.quote(val, safe=SAFE_QUOTE_CHARS)