pathgen.py 2.82 KB
Newer Older
1
2
# -*- coding: utf-8 -*-

3
4
# py_tools_ds - A collection of geospatial data analysis tools that simplify standard
# operations when handling geospatial raster and vector data as well as projections.
5
#
Daniel Scheffler's avatar
Daniel Scheffler committed
6
# Copyright (C) 2016-2021  Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
7
8
9
10
11
#
# This software was developed within the context of the GeoMultiSens project funded
# by the German Federal Ministry of Education and Research
# (project grant code: 01 IS 14 010 A-C).
#
12
13
14
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
15
#
16
#   http://www.apache.org/licenses/LICENSE-2.0
17
#
18
19
20
21
22
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
23

24
25
26
import tempfile
import os

27
28
29
30
__author__ = "Daniel Scheffler"


def get_tempfile(ext=None, prefix=None, tgt_dir=None):
Daniel Scheffler's avatar
Daniel Scheffler committed
31
32
33
34
    """Return the path to a tempfile.mkstemp() file that can be passed to any function that expects a physical path.

    NOTE: The tempfile has to be deleted manually.

35
36
37
    :param ext:     file extension (None if None)
    :param prefix:  optional file prefix
    :param tgt_dir: target directory (automatically set if None)
Daniel Scheffler's avatar
Daniel Scheffler committed
38
    """
39
40
    prefix = 'py_tools_ds__' if prefix is None else prefix
    fd, path = tempfile.mkstemp(prefix=prefix, suffix=ext, dir=tgt_dir)
41
42
43
44
45
46
    os.close(fd)
    return path


def get_generic_outpath(dir_out='', fName_out='', prefix='', ext='', create_outDir=True,
                        prevent_overwriting=False):
Daniel Scheffler's avatar
Daniel Scheffler committed
47
    """Generate an output path accourding to the given parameters.
48
49
50
51
52
53
54
55
56
57

    :param dir_out:             output directory
    :param fName_out:           output filename
    :param prefix:              a prefix for the output filename. ignored if fName_out is given
    :param ext:                 the file extension to use
    :param create_outDir:       whether to automatically create the output directory or not
    :param prevent_overwriting: whether to prevent that a output filename is chosen that already exist in the filesystem
    :return:
    """
    dir_out = dir_out if dir_out else os.path.abspath(os.path.curdir)
58
59
    if create_outDir and not os.path.isdir(dir_out):
        os.makedirs(dir_out)
60
61

    if not fName_out:
62
        fName_out = '%soutput' % prefix + ('.%s' % ext if ext else '')
63
        if prevent_overwriting:
64
            count = 1
65
            while os.path.exists(os.path.join(dir_out, fName_out)):
66
                if count == 1:
67
68
69
70
                    fName_out += str(count)
                else:
                    fName_out[-1] = str(count)

71
    return os.path.join(dir_out, fName_out)