Beispiel #1
0
    def fetch_or_create(cls, data, file_type=None, im=None, attributes=()):
        sha256 = hashlib.sha256(data).hexdigest()
        obj = (cls.query.filter(cls.sha256 == sha256).first())
        if obj is None:
            attributes = dict(attributes)
            if file_type is None and im is not None:
                file_type = images.image_file_type(im)
            if file_type is None:
                raise ValueError('a file type is required')
            if im is not None:
                attributes.update({
                    'width': im.size.width,
                    'height': im.size.height
                })
            elif file_type == 'swf':
                attributes.update(flash.parse_flash_header(BytesIO(data)))
            obj = cls(sha256=sha256,
                      file_type=file_type,
                      attributes=attributes)

            # Write our file to disk
            real_path = obj.full_file_path
            makedirs_exist_ok(os.path.dirname(real_path))
            with open(real_path, 'wb') as outfile:
                outfile.write(data)

            cls.dbsession.add(obj)
        return obj
Beispiel #2
0
def test_makedirs_with_extant_directories(tmpdir):
    """
    ``makedirs_exist_ok`` doesn't care if the directories already exist.
    """
    d = tmpdir.join('a', 'b', 'c')
    d.ensure(dir=True)
    files.makedirs_exist_ok(d.strpath)
    assert d.exists()
Beispiel #3
0
def test_makedirs_default_behavior(tmpdir):
    """
    ``makedirs_exist_ok`` creates multiple levels of directories.
    """
    d = tmpdir.join('a', 'b', 'c')
    assert not d.exists()
    files.makedirs_exist_ok(d.strpath)
    assert d.exists()
Beispiel #4
0
 def init_from_data(self, data):
     path = ['static', 'media'] + fanout(self.sha256, (2, 2, 2)) + ['%s.%s' % (self.sha256, self.file_type)]
     self.file_path = os.path.join(*path)
     self.file_url = '/' + self.file_path
     real_path = self.full_file_path
     makedirs_exist_ok(os.path.dirname(real_path))
     with open(real_path, 'wb') as outfile:
         outfile.write(data)
Beispiel #5
0
def test_makedirs_with_extant_directories(tmpdir):
    """
    ``makedirs_exist_ok`` doesn't care if the directories already exist.
    """
    d = tmpdir.join('a', 'b', 'c')
    d.ensure(dir=True)
    files.makedirs_exist_ok(d.strpath)
    assert d.exists()
Beispiel #6
0
def test_makedirs_default_behavior(tmpdir):
    """
    ``makedirs_exist_ok`` creates multiple levels of directories.
    """
    d = tmpdir.join('a', 'b', 'c')
    assert not d.exists()
    files.makedirs_exist_ok(d.strpath)
    assert d.exists()
Beispiel #7
0
def test_makedirs_reraises_other_errors(tmpdir):
    """
    ``makedirs_exist_ok`` reraises errors it can't handle, such as EACCES.
    """
    tmpdir.chmod(0)
    d = tmpdir.join('a', 'b', 'c')
    with pytest.raises(OSError) as e:
        files.makedirs_exist_ok(d.strpath)
    assert e.value.errno == errno.EACCES
Beispiel #8
0
def test_makedirs_reraises_other_errors(tmpdir):
    """
    ``makedirs_exist_ok`` reraises errors it can't handle, such as EACCES.
    """
    tmpdir.chmod(0)
    d = tmpdir.join('a', 'b', 'c')
    with pytest.raises(OSError) as e:
        files.makedirs_exist_ok(d.strpath)
    assert e.value.errno == errno.EACCES
Beispiel #9
0
 def init_from_data(self, data):
     path = ['static', 'media'] + fanout(
         self.sha256,
         (2, 2, 2)) + ['%s.%s' % (self.sha256, self.file_type)]
     self.file_path = os.path.join(*path)
     self.file_url = '/' + self.file_path
     real_path = self.full_file_path
     makedirs_exist_ok(os.path.dirname(real_path))
     with open(real_path, 'wb') as outfile:
         outfile.write(data)
Beispiel #10
0
def make_character_directory(target):
    path = d.get_character_directory(target)
    makedirs_exist_ok(path)
Beispiel #11
0
def ensure_file_directory(filename):
    dirname = os.path.dirname(filename)
    makedirs_exist_ok(dirname)