Skip to content

Commit

Permalink
0.60 scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ezio416 committed Mar 15, 2023
1 parent d485237 commit f6fbb34
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 11 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "py416"
version = "0.59"
version = "0.60"
authors = [
{ name="Ezio416", email="[email protected]" },
]
Expand All @@ -26,3 +26,7 @@ dependencies = [
[project.urls]
"Homepage" = "https://github.com/ezio416/py416"
"Bug Tracker" = "https://github.com/ezio416/py416/issues"

[project.scripts]
rmd = "py416.scripts:rmd"
uzd = "py416.scripts:uzd"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
clear_dist.py
py -m build ..
build_pkg
twine upload ../dist/*
pause
2 changes: 2 additions & 0 deletions scripts/clear-build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clear_dist.py
py -m build ..
5 changes: 5 additions & 0 deletions scripts/flake_scripts.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
REM run flake8 on py416.scripts
REM E501 is the "line too long" error
flake8 --extend-ignore=E501 ../src/py416/scripts.py
set /p tmp="Press enter to exit"
6 changes: 3 additions & 3 deletions src/py416/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Name: py416
Author: Ezio416
Created: 2022-08-15
Updated: 2023-02-22
Version: 0.59
Updated: 2023-03-14
Version: 0.60
A collection of various functions
'''
from .general import *
__version__ = 0, 59
__version__ = 0, 60
v = __version__
15 changes: 9 additions & 6 deletions src/py416/files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
| Author: Ezio416
| Created: 2022-08-16
| Updated: 2023-02-22
| Updated: 2023-03-14
- Functions for filesystem and path string manipulation
Expand Down Expand Up @@ -1017,9 +1017,10 @@ def unzip(path: str, remove: bool = False) -> None:

def unzipdir(path: str = '.', ignore_errors: bool = True) -> int:
'''
- unzips all archives in a folder (only 1st level) until it is unable to continue
- deletes all archives as it unzips
- supports archives of type: (.7z, .gz, .rar, .tar, .zip)
- unzips all archive files in a folder until it is unable to continue
- recursive on archive files, but not folders
- deletes all archive files as it unzips
- supports archive files of type: (.7z, .gz, .rar, .tar, .zip)
Parameters
----------
Expand All @@ -1043,13 +1044,15 @@ def unzipdir(path: str = '.', ignore_errors: bool = True) -> int:
while True:
unzipped_this_run = 0
for file in listdir(path, dirs=False):
if not file.endswith(('.7z', '.gz', '.rar', '.tar', '.zip')):
continue
try:
unzip(file, remove=True)
unzipped += 1
unzipped_this_run += 1
except Exception:
except Exception as e:
if not bool(ignore_errors):
raise
raise Exception(f'unzipped {unzipped} archive file(s) before failing: {e}')
if not unzipped_this_run:
break
return unzipped
51 changes: 51 additions & 0 deletions src/py416/scripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''
| Author: Ezio416
| Created: 2023-03-14
| Updated: 2023-03-14
- Functions behind console scripts
- Call these functions directly by name from the command line
'''
import sys

from .files import rmdir, unzipdir


def rmd():
'''
- recursively deletes empty directories
- wraps `os.rmdir() <https://docs.python.org/3/library/os.html#os.rmdir>`_
Arguments
---------
- none: delete empty folders where we are, but leave our parent intact
- one argument: path to folder
- folder in which to delete empty folders
- two arguments: path to folder, anything
- if a second value is present, we'll also try to delete the given folder
- the second value itself is ignored
'''
if len(sys.argv) > 1:
if len(sys.argv) > 2:
print(f'deleted {rmdir(sys.argv[1])} empty folders')
return
print(f'deleted {rmdir(sys.argv[1], delroot=False)} empty folders')
return
print(f'deleted {rmdir(".", delroot=False)} empty folders')


def uzd():
'''
- unzips all archive files in a folder until it is unable to continue
- recursive on archive files, but not folders
- deletes all archive files as it unzips
- supports archive files of type: (.7z, .gz, .rar, .tar, .zip)
Arguments
---------
- if any argument is passed, the script will stop upon error
'''
if len(sys.argv) > 1:
print(f'unzipped {unzipdir(ignore_errors=False)} archive file(s)')
return
print(f'unzipped {unzipdir()} archive file(s)')

0 comments on commit f6fbb34

Please sign in to comment.