def add(self, fp, asset_id = None, **kw): """add a new file to the store :param fp: the file pointer to the file to be stored :param asset_id: An optional asset id. If not given, an asset id will be generated. Needs to be a string :param kw: optional keyword arguments simply passed back :return: A dictionary containing ``asset_id``, the filesystem ``path`` and the ``content_length`` """ if asset_id is None: asset_id = unicode(uuid.uuid4()) path = self._get_path(asset_id) dirpath = os.path.split(path)[0] if not os.path.exists(dirpath): os.makedirs(dirpath) dest_fp = open(path, "wb") shutil.copyfileobj(fp, dest_fp) dest_fp.close() content_length = os.path.getsize(path) res = AttributeMapper( asset_id = asset_id, path = path, content_length = content_length ) res.update(kw) return res
def add(self, fp, asset_id=None, **kw): """add a new file to the store :param fp: the file pointer to the file to be stored :param asset_id: An optional asset id. If not given, an asset id will be generated. Needs to be a string :param kw: optional keyword arguments simply passed back :return: A dictionary containing ``asset_id``, the filesystem ``path`` and the ``content_length`` """ if asset_id is None: asset_id = unicode(uuid.uuid4()) path = self._get_path(asset_id) dirpath = os.path.split(path)[0] if not os.path.exists(dirpath): os.makedirs(dirpath) dest_fp = open(path, "wb") shutil.copyfileobj(fp, dest_fp) dest_fp.close() content_length = os.path.getsize(path) res = AttributeMapper(asset_id=asset_id, path=path, content_length=content_length) res.update(kw) return res
def test_nested_update(): am = AttributeMapper({'a': AttributeMapper({ 'b': 9, 'c': 10, })}) am.update({'foo': 'bar', 'a': {'b': 13}}) assert am.a.b == 13 assert am.a.c == 10 assert am.foo == "bar"
def test_dotted_update_last_missing(): am = AttributeMapper({'a': AttributeMapper({ 'b': 9, 'c': 10, })}) am.update({'foo': 'bar', 'a.e': 13}) assert am.a.e == 13 assert am.a.c == 10 assert am.foo == "bar"
def test_dotted_update2(): am = AttributeMapper( {'a': AttributeMapper({ 'b': 9, 'c': AttributeMapper({'d': 19}), })}) am.update({'foo': 'bar', 'a.b': 13, 'a.c.d': 21}) assert am.a.b == 13 assert am.a.c.d == 21 assert am.foo == "bar"
def test_dotted_update_last_missing(): am = AttributeMapper({ 'a' : AttributeMapper({ 'b' : 9, 'c' : 10, }) }) am.update({ 'foo' : 'bar', 'a.e' : 13 }) assert am.a.e == 13 assert am.a.c == 10 assert am.foo == "bar"
def test_nested_update(): am = AttributeMapper({ 'a' : AttributeMapper({ 'b' : 9, 'c' : 10, }) }) am.update({ 'foo' : 'bar', 'a' : { 'b' : 13 } }) assert am.a.b == 13 assert am.a.c == 10 assert am.foo == "bar"
def test_dotted_update2(): am = AttributeMapper({ 'a' : AttributeMapper({ 'b' : 9, 'c' : AttributeMapper({ 'd' : 19 }), }) }) am.update({ 'foo' : 'bar', 'a.b' : 13, 'a.c.d' : 21 }) assert am.a.b == 13 assert am.a.c.d == 21 assert am.foo == "bar"