long forgotten changes ;-)

This commit is contained in:
Doncho N. Gunchev 2023-01-11 00:08:38 +02:00
parent 7da9878c06
commit fc2d92dc03
Signed by: dgunchev
GPG key ID: D30FD19F37E002A9
5 changed files with 47 additions and 45 deletions

View file

@ -1,13 +1,11 @@
# Common ls aliases (RHEL, Fedora)
#alias l='ls -Fax'
#alias ll='ls -Alg'
# show hidden files and directories
#alias l.='ls -d .[^.]* ..?* --color=tty 2>/dev/null'
#alias ls='ls -F -X -B -T 0 --color=tty'
alias l.='ls -d .[^.]* ..?* --color=tty 2>/dev/null'
# long format with ISO dates
alias lll='ls -Al "--time-style=+%Y-%m-%d %H:%M:%S %4Z"'
# color less (restricted)
alias less='less -R'
# color ip route
alias ip='ip --color=auto'

8
bash/z-env.sh Executable file
View file

@ -0,0 +1,8 @@
EDITOR=mcedit
export EDITOR
HISTSIZE=20480
export HISTSIZE
FZF_DEFAULT_OPTS="--history-size=$HISTSIZE"
export FZF_DEFAULT_OPTS

2
bash/z-time_style.sh Executable file
View file

@ -0,0 +1,2 @@
TIME_STYLE=long-iso
export TIME_STYLE

View file

@ -5,17 +5,17 @@
This program check if the given EGN (Universal Citizen Number)
is valid in Bulgaria. All EGNs are accepted as arguments.
You can also import egn_check from this file.
You can also import and use the egn_check function.
"""
__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = "Doncho Gunchev <gunchev@gmail.com>"
__depends__ = ["Python-3"]
__copyright__ = "GPLv2+/BSD"
def egn_check(egn):
'''Check if the given EGN (Bulgarian Universal Citizen Number) is valid.'''
def egn_check(egn) -> bool:
"""Check if the given EGN (Bulgarian Universal Citizen Number) is valid."""
if len(egn) != 10:
return False
multipliers = (2, 4, 8, 5, 10, 9, 7, 3, 6)
@ -30,12 +30,12 @@ def egn_check(egn):
if __name__ == '__main__':
import sys
print("EGN check version " + __version__ + ", by Mr.700")
print(f"EGN check version {__version__}, by {__author__}")
if len(sys.argv) < 2:
print("\n" + __doc__.strip())
print(f"\n{__doc__.strip()}")
else:
for arg in sys.argv[1:]:
if egn_check(arg):
print(arg + " - OK")
print(f'{arg} - OK')
else:
print(arg + " - BAD")
print(f'{arg} - BAD')

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""\
"""
==============================================
mksparse.py - sparse file / disk image creator
==============================================
@ -35,19 +35,21 @@ import sys
__version__ = "0.3"
__author__ = "Doncho Gunchev <gunchev@gmail.com>, Brad Watson"
__depends__ = ['Python-3']
# __copyright__ = """Ask Brad Watson, I want nothing."""
# __copyright__ = 'Ask Brad Watson, I want nothing.'
class MkSparseError(Exception):
"""MkSpace errors"""
def mk_sparse(file_name, file_size):
def mk_sparse(file_name: str, file_size: int):
"""Create a sparse file by truncating it at given position"""
try:
sparse_file = open(file_name, 'wb+')
except EnvironmentError as exc:
raise MkSparseError("Error: Can't create file {!r}:\n\t{}".format(file_name, exc))
raise MkSparseError("Error: Can't create file {file_name!r}:\n\t{exc}")
try:
# Note that I don't want (you too) to write() anything in the file
@ -62,34 +64,27 @@ def mk_sparse(file_name, file_size):
sparse_file.close()
except EnvironmentError:
pass
raise MkSparseError("Error: Can't truncate {!r}:\n\t{}".format(file_name, exc))
raise MkSparseError("Error: Can't truncate {file_name!r}:\n\t{exc}")
try:
sparse_file.close()
except EnvironmentError as exc:
raise MkSparseError("Error: Can't close {!r}:\n\t{}".format(file_name, exc))
raise MkSparseError(f"Error: Can't close {file_name!r}:\n\t{exc}")
def parse_file_size(file_size):
'''file size validation and parsing'''
def parse_file_size(file_size: str) -> int:
"""file size validation and parsing"""
xlt: dict[str, int] = dict(k=2 ** 10, m=2 ** 20, g=2 ** 30, t=2 ** 40, p=2 ** 50, e=2 ** 60, z=2 ** 70, y=2 ** 80)
try:
size_str, dim = re.match('^(\\d+)([kmgt])?$', file_size).groups()
size_str, dim = re.match('^(\\d+)([' + ''.join(xlt.keys()) + '])?$', file_size).groups()
except AttributeError: # if it did not match we get None, which has no .groups...
raise ValueError('Bad image size given: {!r}'.format(file_size))
size = int(size_str) # can not raise...
size: int = int(size_str) # can not raise...
if dim is None:
return size
if dim == 'k':
return size * 1024
if dim == 'm':
return size * 1024 * 1024
if dim == 'g':
return size * 1024 * 1024 * 1024
if dim == 't':
return size * 1024 * 1024 * 1024 * 1024
raise NotImplementedError('Size modifier {!r} not handled.'.format(dim))
return size * xlt[dim]
def main():
@ -103,25 +98,24 @@ def main():
sys.exit(1)
# 'Process' command line parameters
file_name = sys.argv[1]
file_size = sys.argv[2]
file_name: str = sys.argv[1]
file_size: str = sys.argv[2]
def err_exit(reason: str, code: int):
"""Print error (exception) and exit with error code"""
print(f'{my_name}: {reason}', file=sys.stderr)
sys.exit(code)
# Check if the file exists, -f (force) would be a good parameter to add
if os.path.exists(file_name):
print("{}: Error: file (directory) {!r} already exists!".format(my_name, file_name), file=sys.stderr)
sys.exit(1)
err_exit(f'Error: file/directory {file_name!r} already exists!', 17)
try:
mk_sparse(file_name, parse_file_size(file_size))
except MkSparseError as exc:
print('{}: {}'.format(my_name, exc), file=sys.stderr)
sys.exit(2)
except ValueError as exc:
print('{}: {}'.format(my_name, exc), file=sys.stderr)
sys.exit(3)
except NotImplementedError as exc:
print('{}: {}'.format(my_name, exc), file=sys.stderr)
sys.exit(4)
err_exit(str(exc), 1)
except (ValueError, KeyError) as exc:
err_exit(str(exc), 64) # EX_USAGE
if __name__ == "__main__":