コード例 #1
0
    def _download_file(self, resp):
        assert "Content-Disposition" in resp.headers
        download_dirpath = projects.request_directory("regional")

        filepath = os.path.abspath(
            os.path.join(
                download_dirpath,
                resp.headers["Content-Disposition"].replace(
                    "attachment; filename=", ""),
            ))

        if config._windows and len(str(filepath)) > 250:
            # Windows has an absolute limit of 255 characters in a filepath
            if len(str(os.path.abspath(download_dirpath))) > 200:
                ERROR = """Cannot safely save files in this project directory.
                The project name is too long: {} characters for complete directory path, should fewer than 200.
                The directory used for downloads is: {}
                Please start a new project with a shorter project name."""
                raise WindowsPathCharacterLimit(
                    ERROR.format(len(download_dirpath), download_dirpath))
            filepath = os.path.abspath(
                os.path.join(
                    download_dirpath,
                    uuid.uuid4().hex + filepath.split(".")[-1],
                ))

        chunk = 128 * 1024
        with open(filepath, "wb") as f:
            while True:
                segment = resp.raw.read(chunk)
                if not segment:
                    break
                f.write(segment)
        return filepath
コード例 #2
0
def get_presample_directory(id_, overwrite=False, dirpath=None):
    if dirpath is None:
        if projects:
            dirpath = Path(projects.request_directory('presamples')) / id_
        else:
            dirpath = Path(os.getcwd()) / id_
    else:
        dirpath = Path(dirpath) / id_
    if os.path.isdir(dirpath):
        if not overwrite:
            raise ValueError(
                "The presampled directory {} already exists".format(dirpath))
        else:
            shutil.rmtree(dirpath)
    os.mkdir(dirpath)
    return dirpath
コード例 #3
0
    def export_objs(cls,
                    objs,
                    filename,
                    folder="export",
                    backwards_compatible=False):
        """Export a list of objects. Can have heterogeneous types.

        Args:
            * *objs* (list): List of objects to export.
            * *filename* (str): Name of file to create.
            * *folder* (str, optional): Folder to create file in. Default is ``export``.
            * *backwards_compatible* (bool, optional): Create package compatible with bw2data version 1.

        Returns:
            Filepath of created file.

        """
        filepath = os.path.join(projects.request_directory(folder),
                                safe_filename(filename) + u".bw2package")
        cls._write_file(
            filepath,
            [cls._prepare_obj(o, backwards_compatible) for o in objs])
        return filepath
コード例 #4
0
from bw2data.data_store import DataStore
from bw2data.serialization import SerializedDict
from bw2data import projects

projects.request_directory("unlinked")


class _UnlinkedData(SerializedDict):
    filename = "unlinked_data.json"


unlinked_data = _UnlinkedData()


class UnlinkedData(DataStore):
    _metadata = unlinked_data
    _intermediate_dir = "unlinked"

    def validate(self, *args, **kwargs):
        return
コード例 #5
0
ファイル: migrations.py プロジェクト: yangqiu91/brightway2-io
 def __init__(self, *args, **kwargs):
     super(Migration, self).__init__(*args, **kwargs)
     self._intermediate_dir = projects.request_directory("migrations")
コード例 #6
0
 def write(self):
     """Write report data to file"""
     dirpath = projects.request_directory("reports")
     filepath = os.path.join(dirpath, "report.%s.json" % self.uuid)
     JsonWrapper.dump(self.report, filepath)
コード例 #7
0
def test_request_directory():
    projects.request_directory("foo")
    assert "foo" in os.listdir(projects.dir)
コード例 #8
0
 def presamples_dir():
     """Needs to be function for tests"""
     return Path(projects.request_directory("presamples"))