Compare commits
43 Commits
beta_0.2.1
...
691b68cb4c
| Author | SHA1 | Date | |
|---|---|---|---|
| 691b68cb4c | |||
| 461533b717 | |||
| caff94ab41 | |||
| efcec653cb | |||
| a0f2f83a8a | |||
| af80345ff6 | |||
| f96d9b3257 | |||
| b0e3594b49 | |||
| cf828c7f97 | |||
| 49e16a522c | |||
| 927d5358f5 | |||
| 0bbfb9b9bf | |||
| a012f8f1e2 | |||
| 394b940841 | |||
| b615f52313 | |||
| 799e0239a1 | |||
| 5270fab084 | |||
| 5469a01c09 | |||
| 9b86007e34 | |||
| 40bf210067 | |||
| ae19816da1 | |||
| d2041c5355 | |||
| c8bf85cb6a | |||
| 9dcaacf64a | |||
| b82a04e3bb | |||
| 29924ffb36 | |||
| b09ea6a8c5 | |||
| 209fae5575 | |||
| 37fe1f5203 | |||
| c48e16fdb9 | |||
| 67b61a3d5f | |||
| 2425e0a215 | |||
| 6738f73db7 | |||
| e99ee7925f | |||
| d910027bc0 | |||
| 2dc233eef0 | |||
| 09563a997c | |||
| a26367c0aa | |||
| 8bc7bf5584 | |||
| b3834f4798 | |||
| f0a7ea1874 | |||
| c41ff7e0f5 | |||
| 78f8c991bd |
179
.gitignore
vendored
179
.gitignore
vendored
@@ -1,2 +1,179 @@
|
||||
__pycache__
|
||||
releases
|
||||
version.py
|
||||
tester
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# UV
|
||||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
#uv.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
||||
.pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
# Ruff stuff:
|
||||
.ruff_cache/
|
||||
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
|
||||
31
refit/README.md
Normal file
31
refit/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# refit
|
||||
|
||||
`refit` is a file, directory manipulation and creation tool.
|
||||
|
||||
## ToDos
|
||||
|
||||
1. folder and file creation
|
||||
1.1 simple file and folder creation
|
||||
1.2 recursive file and folder creation
|
||||
1.3 file in folder creation
|
||||
2. file movement
|
||||
2.1 apply a pattern what to move to where
|
||||
3. file removal
|
||||
3.1 remove all files like '*.tar'
|
||||
|
||||
- ~~make input default the current directory and the second argument after `refit create`~~
|
||||
- ~~maybe get rid of valid input ??~~
|
||||
- rework rf_create_decider() so it does not execute if no inputs are given -> valid input check back for no arguments passed?
|
||||
- check done in rf_create_decider()
|
||||
- get rid of input argument, default to current directory and and make it positional
|
||||
- implement back in the valid input check
|
||||
|
||||
# Changelog
|
||||
|
||||
<2025-09-30> V0.3.2 - Refactoring librefit and added proper docstrings; begun to remove the check for the valid input and put it in the decider
|
||||
<2025-09-29> V0.3.1 - Removed the requirement for an input
|
||||
<2025-09-29> V0.3.0 - Added file creation in the pattern like directories
|
||||
<2025-09-29> V0.2.4 - Improved logging and log readability
|
||||
<2025-09-28> V0.2.3 - Added logging for version file and --filemode path to the decider
|
||||
<2025-09-28> V0.2.0 - Added librefit for standard functions
|
||||
<2025-09-28> V0.1.0 - Added the creation of multiple numbered directories in a given directory with the pattern default directory_n
|
||||
10
refit/pyproject.toml
Normal file
10
refit/pyproject.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "refit"
|
||||
version = "0.0.1"
|
||||
|
||||
[tool.setuptools.packages]
|
||||
find = { where = ["src"] }
|
||||
0
refit/src/__init__.py
Normal file
0
refit/src/__init__.py
Normal file
141
refit/src/modules/librefit.py
Normal file
141
refit/src/modules/librefit.py
Normal file
@@ -0,0 +1,141 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .refit_logger import logger
|
||||
|
||||
|
||||
def get_standard_name_number(current_number: str, number_str_length: int) -> str:
|
||||
"""Returns a number string filled to the length of the input number
|
||||
|
||||
This function returns the number in a standartized way as a string.
|
||||
As input it takes the current number of the string to build and a
|
||||
number which determines the length of the string.
|
||||
|
||||
Args:
|
||||
current_number (str): The current number of the item.
|
||||
number_str_length (int): The length of the string which gets returned.
|
||||
|
||||
Examples:
|
||||
>>> get_standard_name_number(1, 2)
|
||||
'01'
|
||||
>>> get_standard_name_number(23, 4)
|
||||
'0023'
|
||||
"""
|
||||
# logger.debug(
|
||||
# f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
|
||||
# )
|
||||
current_number = str(current_number)
|
||||
standard_name_number = str.zfill(current_number, number_str_length)
|
||||
# logger.debug(
|
||||
# f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
|
||||
# )
|
||||
return standard_name_number
|
||||
|
||||
|
||||
def get_standard_folder_name(name: str) -> str:
|
||||
"""Returnes a standard name either from a list or the default value.
|
||||
|
||||
This function sanitizes the input, which gets passed as a list or None from
|
||||
argparse. The function either chooses the first entry of the list, given to
|
||||
the --name argument or returns the default value 'directory'
|
||||
|
||||
Args:
|
||||
name (list[str] | None): A list of names if passed to the --name argument
|
||||
or None if no name is passed.
|
||||
|
||||
Returns:
|
||||
str: The file name. Returns 'file' as default value if name argument is 'None'
|
||||
otherwise the first element of the list.
|
||||
|
||||
Examples:
|
||||
>>> get_standard_file_name(None)
|
||||
'file'
|
||||
>>> get_standard_file_name(["example"])
|
||||
'example'
|
||||
>>> get_standard_file_name(["directory_name", "example"])
|
||||
'directory_name'
|
||||
"""
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: get_standard_folder_name() MSG: entered function with value: '{name}'"
|
||||
)
|
||||
standard_folder_name = name[0] if name is not None else "directory"
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: get_standard_folder_name() MSG: exiting function with folder name: '{standard_folder_name}'"
|
||||
)
|
||||
|
||||
return standard_folder_name
|
||||
|
||||
|
||||
def get_standard_file_name(name) -> str:
|
||||
"""Returnes a name either from a list or the default value.
|
||||
|
||||
This function sanitizes the input, which gets passed as a list or None from
|
||||
argparse. The function either chooses the first entry of the list, given to
|
||||
the --name argument or returns the default value 'file'
|
||||
|
||||
Args:
|
||||
name (list[str] | None): A list of names if passed to the --name argument
|
||||
or None if no name is passed.
|
||||
|
||||
Returns:
|
||||
str: The file name. Returns 'file' as default value if name argument is 'None'
|
||||
otherwise the first element of the list.
|
||||
|
||||
Examples:
|
||||
>>> get_standard_file_name(None)
|
||||
'file'
|
||||
>>> get_standard_file_name(["example"])
|
||||
'example'
|
||||
>>> get_standard_file_name(["file_name", "example"])
|
||||
'file_name'
|
||||
"""
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: get_standard_file_name() MSG: entered function with name='{name}'"
|
||||
)
|
||||
standard_file_name = name[0] if name is not None else "file"
|
||||
logger.debug(
|
||||
f"FUNC: get_standard_file_name MSG: Continuing with expected input: '{standard_file_name}'"
|
||||
)
|
||||
|
||||
logger.debug("FUNC get_standard_file_name() MSG: Exit")
|
||||
return standard_file_name
|
||||
|
||||
|
||||
def get_current_path(path) -> str:
|
||||
"""Checks if the path argument is emty and applies the current directory as working path.
|
||||
|
||||
This function takes an list with strings as an input. If the input is `None` the current
|
||||
directory is taken as the working directory.
|
||||
If the path is passed, a check for its existence takes place.
|
||||
|
||||
Args:
|
||||
path (str): _The current working directory._
|
||||
|
||||
Returns:
|
||||
str: _Returns the path of the current directory after check for existence_
|
||||
"""
|
||||
logger.debug(f"FUNC: get_current_path() MSG: entered function with path = '{path}'")
|
||||
if path is None:
|
||||
path = "."
|
||||
logger.debug(
|
||||
f"FUNC: {get_current_path.__name__} MSG: Path now has the value: '{path}'"
|
||||
)
|
||||
return path
|
||||
else:
|
||||
# Here the check for the path to exist takes place
|
||||
|
||||
if os.path.exists(path) is True:
|
||||
logger.debug(
|
||||
f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
|
||||
)
|
||||
return path
|
||||
else:
|
||||
ERROR_MESSAGE = (
|
||||
f"FUNC: {get_current_path.__name__} MSG: '{path}' does not exist"
|
||||
)
|
||||
logger.warning(ERROR_MESSAGE)
|
||||
print(ERROR_MESSAGE)
|
||||
sys.exit(1)
|
||||
154
refit/src/modules/refit_create.py
Normal file
154
refit/src/modules/refit_create.py
Normal file
@@ -0,0 +1,154 @@
|
||||
from logging import log
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from .refit_logger import logger
|
||||
from . import librefit
|
||||
|
||||
# logger.debug("Initiated refit_create.py")
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
|
||||
class Refit_Create:
|
||||
"""A class to create folders and files.
|
||||
|
||||
It first calls the decider which lets the create_input_valid() function
|
||||
check if the input argument exists. If create_input_valid() returns
|
||||
'True' it continues to execute the command as per the given arguments.
|
||||
|
||||
default folder name: directory"""
|
||||
|
||||
def __init__(self, args):
|
||||
"""Initiating variables for creation"""
|
||||
self.args = args
|
||||
|
||||
self.name = args.name
|
||||
self.input = args.input
|
||||
self.n = args.n
|
||||
self.filemode = args.filemode
|
||||
self.recursive = args.recursive
|
||||
|
||||
def create_n_folders(self, n, input, name):
|
||||
"""Creates an set ammount of folders. Using the default directory
|
||||
name if no other is provided."""
|
||||
|
||||
logger.debug(f"FUNC: create_n_folders() ARGS: n={n} input={input} name={name}")
|
||||
|
||||
# Creating the length of the suffix number_string.
|
||||
length_n = len(str(n))
|
||||
logger.debug(
|
||||
f"FUNC: create_n_folders() MSG: Length of numbering string added to the name:{length_n}"
|
||||
)
|
||||
|
||||
# Get either the default folder name or the input name as string.
|
||||
logger.debug(
|
||||
f"FUNC: create_n_folders() MSG: Type of name value: {type(name)} name={name}"
|
||||
)
|
||||
folder_name = librefit.get_standard_folder_name(name)
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: create_n_folders() MSG: Folder name: {folder_name} post get_standard_folder_name() call"
|
||||
)
|
||||
|
||||
while n > 0:
|
||||
# iterating down for the files number.
|
||||
folder_number = str(n - 1)
|
||||
|
||||
# Passing the number and the length of the string to get the string back.
|
||||
number_string = librefit.get_standard_name_number(folder_number, length_n)
|
||||
|
||||
# Creating path for the folder
|
||||
temp_name = f"{folder_name}_{number_string}"
|
||||
folder_creation_path = os.path.join(input, temp_name)
|
||||
|
||||
# Creating folder and subtracting n by one for the number_string
|
||||
os.mkdir(folder_creation_path)
|
||||
n -= 1
|
||||
|
||||
def create_n_files(self, n, input, name):
|
||||
"""Creates an set ammount of files, using the default file name
|
||||
if none is provided."""
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: create_n_files() MSG: Entered function VALUES: n={self.n} name={self.name} input={self.input}"
|
||||
)
|
||||
|
||||
# Get the name from the input argument.
|
||||
file_name = librefit.get_standard_file_name(name)
|
||||
|
||||
# Get the length of the entered number to fill it with 0
|
||||
length_n = len(str(n))
|
||||
logger.debug(
|
||||
f"FUNC: create_n_files() MSG: file name='{file_name}' length_n={length_n}"
|
||||
)
|
||||
|
||||
while n > 0:
|
||||
# Get number of the file(s) to create
|
||||
file_number = str(n - 1)
|
||||
number_string = librefit.get_standard_name_number(file_number, length_n)
|
||||
|
||||
# Get the name of the file, either applying default or using first list item.
|
||||
temp_name = f"{file_name}_{number_string}"
|
||||
|
||||
file_path = Path(os.path.join(input, temp_name)) # Build file path
|
||||
file_path.touch(exist_ok=True) # creating file
|
||||
|
||||
logger.debug(
|
||||
f"FUNC: create_n_files MSG: created file at {os.path.join(input, temp_name)}"
|
||||
)
|
||||
# Counting down n for the next ieration of the while-loop
|
||||
n -= 1
|
||||
|
||||
def input_validator(self):
|
||||
logger.debug("FUNC: input_validator()")
|
||||
return True
|
||||
|
||||
def rf_create_decider(self):
|
||||
"""Coordination of the 'create' sub command"""
|
||||
logger.debug("FUNC: rf_create_decider() MSG: Entered decider function")
|
||||
# !!!ToDo: implement input_validator()
|
||||
if self.input_validator():
|
||||
logger.debug("yippie")
|
||||
|
||||
# if no input is provided, current directory is set to input
|
||||
logger.debug(f"DEBUG HERE!: type of path is: {type(self.input)}")
|
||||
self.input = librefit.get_current_path(self.input)
|
||||
logger.debug(
|
||||
f"FUNC: rf_create_decider() MSG: successfully retrievd input value: '{self.input}'"
|
||||
)
|
||||
|
||||
# Exit the program if the -n argument is not passed
|
||||
if self.n is None:
|
||||
logger.error(
|
||||
f"FUNC rf_create_decider() MSG: the number value cannot be '{self.n}'"
|
||||
)
|
||||
print("Use the '-n' flag for the create command.")
|
||||
sys.exit(1)
|
||||
|
||||
# Exits the program if recursive and filemode flags are set at the same time
|
||||
if self.filemode:
|
||||
logger.debug("DECISION filemode set, creating files instead of folders")
|
||||
if self.recursive is None:
|
||||
self.create_n_files(self.n, self.input, self.name)
|
||||
else:
|
||||
logger.error("Recursive and filemode don´t work together.")
|
||||
print("Recursive and filemode don´t work together.")
|
||||
|
||||
if self.recursive:
|
||||
print("help")
|
||||
|
||||
if self.recursive is None:
|
||||
self.create_n_folders(self.n, self.input, self.name)
|
||||
else:
|
||||
logger.debug(
|
||||
f"FUNC: rf_create_decider(end) MSG: given arguments: {self.args}"
|
||||
)
|
||||
print(
|
||||
"Use '-n' argument to create directories.\nPlease use 'refit create -h' for help"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
def __call__(self):
|
||||
"""Gets called when the 'create' subcommand is used."""
|
||||
self.rf_create_decider()
|
||||
32
refit/src/modules/refit_logger.py
Normal file
32
refit/src/modules/refit_logger.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
log_dir = Path.home() / ".local" / "share" / "refit"
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
log_file = log_dir / "refit.log"
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
filename=log_file,
|
||||
)
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
def handle_exception(exec_type, exec_value, exec_traceback):
|
||||
if issubclass(exec_type, KeyboardInterrupt):
|
||||
sys.__excepthook__(exec_type, exec_value, exec_traceback)
|
||||
return
|
||||
logger.error(
|
||||
"External error accured",
|
||||
exc_info=(exec_type, exec_value, exec_traceback),
|
||||
)
|
||||
|
||||
|
||||
sys.excepthook = handle_exception
|
||||
|
||||
# logger.debug(f"Log path:\t{log_dir}")
|
||||
# logger.debug(f"Log file:\t{log_file}")
|
||||
96
refit/src/refit.py
Normal file
96
refit/src/refit.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
import configparser
|
||||
|
||||
from modules.refit_logger import logger
|
||||
from modules.refit_create import Refit_Create
|
||||
|
||||
|
||||
CONFIG_FILE = "version.cfg"
|
||||
|
||||
|
||||
def read_version_config():
|
||||
logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}")
|
||||
config = configparser.ConfigParser()
|
||||
config.read(CONFIG_FILE)
|
||||
|
||||
if not os.path.exists(CONFIG_FILE):
|
||||
logger.error(
|
||||
f"Func: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'"
|
||||
)
|
||||
return "x.x.x"
|
||||
|
||||
if "VERSION" not in config:
|
||||
logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'")
|
||||
return "x.x.x"
|
||||
|
||||
try:
|
||||
v = config["VERSION"]
|
||||
major = v.get("major", "0")
|
||||
minor = v.get("minor", "0")
|
||||
patch = v.get("patch", "0")
|
||||
logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}")
|
||||
return f"{major}.{minor}.{patch}"
|
||||
except Exception:
|
||||
logger.warning("Couldn not read version from config file")
|
||||
return "x.x.x"
|
||||
|
||||
|
||||
REFIT_VERSION = f"Refit Beta {read_version_config()}"
|
||||
|
||||
# ---------------------------ARGPARSE START---------------------------
|
||||
# Main Parser
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="Refit",
|
||||
description="This is a file and directory manipulation tool.\
|
||||
it can create, move and delete files and directories as well as \
|
||||
renaming them",
|
||||
epilog=REFIT_VERSION,
|
||||
)
|
||||
|
||||
# Main Parser Arguments
|
||||
|
||||
# Create Parser
|
||||
subparser = parser.add_subparsers(
|
||||
title="Commands",
|
||||
dest="create",
|
||||
required=False,
|
||||
)
|
||||
|
||||
# Create Parser Arguments
|
||||
create_parser = subparser.add_parser("create", help="creates a new file/folder")
|
||||
create_parser.add_argument("-n", type=int, help="number of items")
|
||||
create_parser.add_argument("-i", "--input", help="input file")
|
||||
create_parser.add_argument(
|
||||
"--name",
|
||||
nargs="*",
|
||||
help="the name of the folder you want to create\n Default: directory",
|
||||
)
|
||||
create_parser.add_argument(
|
||||
"--filemode", action="store_true", help="creates files instead of directories"
|
||||
)
|
||||
create_parser.add_argument(
|
||||
"-r",
|
||||
"--recursive",
|
||||
action="store_true",
|
||||
# nargs="*",
|
||||
help="Sets the recursive mode for folders to true. First argumet\n is for the depth and the second for the width.",
|
||||
)
|
||||
create_parser.set_defaults(command_class=Refit_Create)
|
||||
|
||||
args = parser.parse_args()
|
||||
# ---------------------------ARGPARSE END-----------------------------
|
||||
|
||||
|
||||
# Dispatcher
|
||||
# determines what code gets addressed based of the users chosen flags.
|
||||
if hasattr(args, "command_class"):
|
||||
logger.debug(f"In dispatcher with args: {args}")
|
||||
Refit_Create = args.command_class
|
||||
create_command_instance = Refit_Create(args)
|
||||
create_command_instance()
|
||||
else:
|
||||
parser.print_help()
|
||||
logger.info("No input, exiting with exit code: 1")
|
||||
sys.exit(1)
|
||||
5
refit/src/version.cfg
Normal file
5
refit/src/version.cfg
Normal file
@@ -0,0 +1,5 @@
|
||||
[VERSION]
|
||||
major = 0
|
||||
minor = 3
|
||||
patch = 2
|
||||
|
||||
44
refit/test_librefit.py
Normal file
44
refit/test_librefit.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from src.modules.librefit import (
|
||||
get_standard_folder_name,
|
||||
get_standard_name_number,
|
||||
get_standard_file_name,
|
||||
)
|
||||
|
||||
|
||||
def test_get_default_file_name():
|
||||
"""Tests if the function returns the correct value"""
|
||||
default_name = get_standard_file_name(None)
|
||||
assert "file" in default_name
|
||||
|
||||
|
||||
def test_get_default_folder_name():
|
||||
"""Test if the default directory name gets returned"""
|
||||
default_name = get_standard_folder_name(None)
|
||||
assert "directory" in default_name
|
||||
|
||||
|
||||
def test_get_standard_name_number():
|
||||
"""Tests if the number function returns the correctly formatted string."""
|
||||
name_number = get_standard_name_number(20, 3)
|
||||
assert "020" in name_number
|
||||
|
||||
|
||||
def test_get_filename():
|
||||
"""Tests if a passed filename is returned properly"""
|
||||
filename = get_standard_file_name(["testname"])
|
||||
assert "testname" in filename
|
||||
|
||||
|
||||
def test_get_folder_name():
|
||||
"""Tests if the function returns the passed folder name correctly"""
|
||||
folder_name = ["folder"]
|
||||
return_folder_name = get_standard_folder_name(folder_name)
|
||||
assert "folder" in return_folder_name
|
||||
|
||||
|
||||
def test_folder_name_list():
|
||||
"""The function is supposed to only return the first name of the
|
||||
passed list"""
|
||||
folder_names = ["folder1", "folder2", "folder3"]
|
||||
return_folder_name = get_standard_folder_name(folder_names)
|
||||
assert "folder1" in return_folder_name
|
||||
@@ -29,12 +29,15 @@ def execute_as_subprocess(command, base_path, verbosity=False):
|
||||
"""executes the string given with the '-c, --command' flag."""
|
||||
|
||||
logger.debug("Entered execute_as_subprocess()")
|
||||
logger.debug(f"Path:\t{base_path}\nCommand:\t{command}")
|
||||
logger.debug(f"Path:\t{base_path}\nCommand:\t\t\t{command}")
|
||||
|
||||
# Decicion if the terminal output is verbose or not
|
||||
if verbosity:
|
||||
# Verbose output
|
||||
logger.info("Running subprocess with terminal output.")
|
||||
subprocess.run(command, cwd=base_path, shell=True)
|
||||
else:
|
||||
# Suppressed output
|
||||
logger.info("Running with suppressed stdout and stderr")
|
||||
subprocess.run(
|
||||
command,
|
||||
@@ -43,5 +46,4 @@ def execute_as_subprocess(command, base_path, verbosity=False):
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.STDOUT,
|
||||
)
|
||||
|
||||
logger.debug("Exited execute_as_subprocess()")
|
||||
|
||||
@@ -13,5 +13,5 @@ logging.basicConfig(
|
||||
)
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.debug(f"Log path:\t{log_dir}")
|
||||
logger.debug(f"Log file:\t{log_file}")
|
||||
# logger.debug(f"Log path:\t{log_dir}")
|
||||
# logger.debug(f"Log file:\t{log_file}")
|
||||
|
||||
@@ -5,12 +5,13 @@ from modules.tempbox_functions import execute_as_subprocess
|
||||
from modules.tempbox_logger import logger
|
||||
|
||||
tempbox_version = "Tempbox Beta b0.2.1"
|
||||
|
||||
# Argument parsing
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="Tempbox",
|
||||
prog="tempbox",
|
||||
description="This program accepts an\
|
||||
command whicht it executes in an temporary directory in /temp.",
|
||||
# epilog="helloooooooo",
|
||||
epilog=tempbox_version,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@@ -19,20 +20,19 @@ parser.add_argument(
|
||||
action="store_true",
|
||||
help="Activates or deactivates verbose output. (default=%(default)s)",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--command",
|
||||
help="Takes the string right after the flag to execute it.",
|
||||
)
|
||||
|
||||
parser.add_argument("-V", "--version", action="version", version=tempbox_version)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
# Begin of script logic
|
||||
if args.command is not None:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
logger.debug(f"'{temp_dir}' was created")
|
||||
logger.info(f"'{temp_dir}' was created")
|
||||
if args.command is not None:
|
||||
execute_as_subprocess(
|
||||
args.command,
|
||||
@@ -41,7 +41,4 @@ if args.command is not None:
|
||||
)
|
||||
else:
|
||||
parser.print_help()
|
||||
logger.info("Printed Version")
|
||||
|
||||
|
||||
# Creates a temporary directory and executes the command in it.
|
||||
logger.debug("Printed Version")
|
||||
|
||||
Reference in New Issue
Block a user