cengal.file_system.file_manager.versions.v_0.file_manager

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19__all__ = [
 20    'get_file_hash', 
 21    'get_file_modification_date', 
 22    'current_src_file_dir', 
 23    'path_relative_to_current_src', 
 24    'path_relative_to_current_dir', 
 25    'file_exists', 
 26    'full_ext', 
 27    'full_ext_parts', 
 28    'last_ext', 
 29    'has_ext', 
 30    'file_name', 
 31]
 32
 33
 34import hashlib
 35# import os
 36import datetime
 37import inspect
 38from typing import Optional
 39from os import getcwd, extsep
 40from os.path import exists, isfile, normpath, dirname, getmtime, realpath, join, basename
 41
 42"""
 43Module Docstring
 44Docstrings: http://www.python.org/dev/peps/pep-0257/
 45"""
 46
 47__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 48__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 49__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 50__license__ = "Apache License, Version 2.0"
 51__version__ = "4.4.1"
 52__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 53__email__ = "gtalk@butenkoms.space"
 54# __status__ = "Prototype"
 55__status__ = "Development"
 56# __status__ = "Production"
 57
 58
 59def get_file_hash(full_file_name, hash_format_string=None):
 60    hash_format_string = hash_format_string or '{} {}'
 61    file_content = None
 62    with open(full_file_name, 'rb') as file:
 63        file_content = file.read()
 64    file_hash = hash_format_string.format(hashlib.sha512(file_content).hexdigest(), hex(len(file_content))[2:])
 65    return file_hash
 66
 67
 68def get_file_modification_date(full_file_name):
 69    time_stamp = getmtime(full_file_name)
 70    return datetime.datetime.fromtimestamp(time_stamp)
 71
 72
 73def current_src_file_dir() -> str:
 74    return dirname(realpath(inspect.currentframe().f_back.f_code.co_filename))
 75
 76
 77def path_relative_to_current_src(relative_path: Optional[str]=None) -> str:
 78    relative_path = relative_path or str()
 79    return normpath(join(dirname(realpath(inspect.currentframe().f_back.f_code.co_filename)), normpath(relative_path)))
 80
 81
 82def path_relative_to_current_dir(relative_path: Optional[str]=None) -> str:
 83    relative_path = relative_path or str()
 84    return normpath(join(getcwd(), normpath(relative_path)))
 85
 86
 87def file_exists(file_path: str) -> bool:
 88    return exists(file_path) and isfile(file_path)
 89
 90
 91def full_ext(file_path: str) -> str:
 92    return extsep.join(basename(normpath(file_path)).split(extsep)[1:])
 93
 94
 95def full_ext_parts(file_path: str) -> str:
 96    return basename(normpath(file_path)).split(extsep)[1:]
 97
 98
 99def last_ext(file_path: str) -> str:
100    return basename(normpath(file_path)).split(extsep)[-1]
101
102
103def has_ext(file_path: str) -> str:
104    return extsep in basename(normpath(file_path))
105
106
107def file_name(file_path: str) -> str:
108    return basename(normpath(file_path)).split(extsep)[0]
def get_file_hash(full_file_name, hash_format_string=None):
60def get_file_hash(full_file_name, hash_format_string=None):
61    hash_format_string = hash_format_string or '{} {}'
62    file_content = None
63    with open(full_file_name, 'rb') as file:
64        file_content = file.read()
65    file_hash = hash_format_string.format(hashlib.sha512(file_content).hexdigest(), hex(len(file_content))[2:])
66    return file_hash
def get_file_modification_date(full_file_name):
69def get_file_modification_date(full_file_name):
70    time_stamp = getmtime(full_file_name)
71    return datetime.datetime.fromtimestamp(time_stamp)
def current_src_file_dir() -> str:
74def current_src_file_dir() -> str:
75    return dirname(realpath(inspect.currentframe().f_back.f_code.co_filename))
def path_relative_to_current_src(relative_path: Union[str, NoneType] = None) -> str:
78def path_relative_to_current_src(relative_path: Optional[str]=None) -> str:
79    relative_path = relative_path or str()
80    return normpath(join(dirname(realpath(inspect.currentframe().f_back.f_code.co_filename)), normpath(relative_path)))
def path_relative_to_current_dir(relative_path: Union[str, NoneType] = None) -> str:
83def path_relative_to_current_dir(relative_path: Optional[str]=None) -> str:
84    relative_path = relative_path or str()
85    return normpath(join(getcwd(), normpath(relative_path)))
def file_exists(file_path: str) -> bool:
88def file_exists(file_path: str) -> bool:
89    return exists(file_path) and isfile(file_path)
def full_ext(file_path: str) -> str:
92def full_ext(file_path: str) -> str:
93    return extsep.join(basename(normpath(file_path)).split(extsep)[1:])
def full_ext_parts(file_path: str) -> str:
96def full_ext_parts(file_path: str) -> str:
97    return basename(normpath(file_path)).split(extsep)[1:]
def last_ext(file_path: str) -> str:
100def last_ext(file_path: str) -> str:
101    return basename(normpath(file_path)).split(extsep)[-1]
def has_ext(file_path: str) -> str:
104def has_ext(file_path: str) -> str:
105    return extsep in basename(normpath(file_path))
def file_name(file_path: str) -> str:
108def file_name(file_path: str) -> str:
109    return basename(normpath(file_path)).split(extsep)[0]