Source code for imagepypelines.core.io_tools
# @Email: jmaggio14@gmail.com
# @Website: https://www.imagepypelines.org/
# @License: https://github.com/jmaggio14/imagepypelines/blob/master/LICENSE
# @github: https://github.com/jmaggio14/imagepypelines
#
# Copyright (c) 2018 - 2020 Jeff Maggio, Jai Mehra, Ryan Hartzell
#
import os
import glob
import sys
from types import FunctionType, SimpleNamespace
import numpy as np
from functools import partial
import base64
from cryptography import fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
################################################################################
# Constants
################################################################################
################################################################################
# Functions
################################################################################
[docs]def passgen(passwd, salt=''):
"""generate a hashed key from a password
Args:
passwd (None,str): password to hash
salt (str): optional, salt for your password
Returns:
bytes: hashed passkey safe string
"""
# generate a proper key using Fernet library
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt.encode(),
iterations=100000,
backend=default_backend()
)
return base64.urlsafe_b64encode( kdf.derive( passwd.encode() ) )
# -------------------------------- Input/Output --------------------------------
[docs]def prevent_overwrite(filename,create_file=False):
"""
checks to see if a file or directory already exists and creates a
new filename if it does. It can also create the file if specificed
when creating a file, this function assumes that if there is no file
extension then it should create a directory
NOTE:
This function creates unique filenames by creating them and
checking their current existence. THIS CAN BE A SLOW
PROCESS -- it is much more efficient to keep track of filenames
internally in your application
Args:
filename (str): the full file or directory path to be
overwrite protected
create_file (bool): Default is False
boolean indicating whether or not to create the file
before returning
Returns:
str: the assuredly unique output filename
"""
base_filename,extension = os.path.splitext(filename)
out_filename = filename
file_exists = os.path.exists(out_filename)
num = 0
while file_exists:
num += 1
out_filename = "{base}({num}){ext}".format(base=base_filename,
num=make_numbered_prefix(num,0),
ext=extension)
file_exists = os.path.exists(out_filename)
if create_file:
if extension == "":
input_is_directory = True
else:
input_is_directory = False
if input_is_directory:
os.makedirs(out_filename)
else:
base_path = os.path.split(out_filename)[0]
if not os.path.exists(base_path) and base_path != "":
os.makedirs(base_path)
with open(out_filename,'w') as out:
out.write('')
return out_filename
[docs]def make_numbered_prefix(file_number,number_digits=5):
"""
returns a number string designed to be used in the prefix of
systematic outputs.
example use case
"00001example_output_file.txt"
"00002example_output_file.txt"
Args:
file_number (int): the number you want to prefix
number_digits (int): minimum number of digits in the output
string. Default is 5
Returns:
str: string of numbers with standard at least
'number_digits' of digits
"""
file_number = int( file_number )
if file_number < 0:
file_number_is_negative = True
else:
file_number_is_negative = False
file_number_string = str( abs(file_number) )
len_num_string = len(file_number_string)
if len_num_string < number_digits:
prefix = '0' * int(number_digits - len_num_string)
else:
prefix = ''
if file_number_is_negative:
prefix = '-' + prefix
numbered_string = prefix + file_number_string
return numbered_string