Coverage for py_tools_ds/io/pathgen.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
3# py_tools_ds - A collection of geospatial data analysis tools that simplify standard
4# operations when handling geospatial raster and vector data as well as projections.
5#
6# Copyright (C) 2016-2021
7# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
8# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam,
9# Germany (https://www.gfz-potsdam.de/)
10#
11# This software was developed within the context of the GeoMultiSens project funded
12# by the German Federal Ministry of Education and Research
13# (project grant code: 01 IS 14 010 A-C).
14#
15# Licensed under the Apache License, Version 2.0 (the "License");
16# you may not use this file except in compliance with the License.
17# You may obtain a copy of the License at
18#
19# http://www.apache.org/licenses/LICENSE-2.0
20#
21# Unless required by applicable law or agreed to in writing, software
22# distributed under the License is distributed on an "AS IS" BASIS,
23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24# See the License for the specific language governing permissions and
25# limitations under the License.
27import tempfile
28import os
30__author__ = "Daniel Scheffler"
33def get_tempfile(ext=None, prefix=None, tgt_dir=None):
34 """Return the path to a tempfile.mkstemp() file that can be passed to any function that expects a physical path.
36 NOTE: The tempfile has to be deleted manually.
38 :param ext: file extension (None if None)
39 :param prefix: optional file prefix
40 :param tgt_dir: target directory (automatically set if None)
41 """
42 prefix = 'py_tools_ds__' if prefix is None else prefix
43 fd, path = tempfile.mkstemp(prefix=prefix, suffix=ext, dir=tgt_dir)
44 os.close(fd)
45 return path
48def get_generic_outpath(dir_out='', fName_out='', prefix='', ext='', create_outDir=True,
49 prevent_overwriting=False):
50 """Generate an output path accourding to the given parameters.
52 :param dir_out: output directory
53 :param fName_out: output filename
54 :param prefix: a prefix for the output filename. ignored if fName_out is given
55 :param ext: the file extension to use
56 :param create_outDir: whether to automatically create the output directory or not
57 :param prevent_overwriting: whether to prevent that a output filename is chosen that already exist in the filesystem
58 :return:
59 """
60 dir_out = dir_out if dir_out else os.path.abspath(os.path.curdir)
61 if create_outDir and not os.path.isdir(dir_out):
62 os.makedirs(dir_out)
64 if not fName_out:
65 fName_out = '%soutput' % prefix + ('.%s' % ext if ext else '')
66 if prevent_overwriting:
67 count = 1
68 while os.path.exists(os.path.join(dir_out, fName_out)):
69 if count == 1:
70 fName_out += str(count)
71 else:
72 fName_out[-1] = str(count)
74 return os.path.join(dir_out, fName_out)