Skip to content

Commit

Permalink
Iss66 - Improvements to export handling (#72)
Browse files Browse the repository at this point in the history
* save log file to hidden file

Keeps users HOME dir a little tidier

* Add logs to .cache

As per https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html

* Remove capitalize, fix logging output for export

* Update export docstring

* Allow -n to set export name

* Pass new arguements to export function

* Allow -d to set export directory

* Remove export path home dir default

* Updated readme
  • Loading branch information
gadgieOps authored Jan 30, 2023
1 parent 7817982 commit 73845d3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Install from PyPI
You may need to log out and log in to see all the changes.
### Export a profile as a ".knsv" file to share it with your friends!
`konsave -e <profile name>` or `konsave --export-profile <profile name>`
### Export a profile, setting the output dir and archive name
`konsave -e <profile name> -d <archive directory> -n <archive name>`
or
`konsave --export-profile <profile name> --archive-directory <archive directory> --export-name <export name>`
### Export a profile, overwrite files if they already exist
`konsave -e <profile name> -f` or `konsave --export-profile <profile name> --force`
*note: without --force, the export will be appended with the date and time to ensure unique naming and no data is overwritten
### Import a ".knsv" file
`konsave -i <path to the file>` or `konsave --import-profile <path to the file>`
### Show current version
Expand Down
17 changes: 16 additions & 1 deletion konsave/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def _get_parser() -> argparse.ArgumentParser:
action="store_true",
help="Overwrite already saved profiles",
)
parser.add_argument(
"-d",
"--export-directory",
required=False,
help="Specify the export directory when exporting a profile",
metavar="<directory>"
)
parser.add_argument(
"-n",
"--export-name",
required=False,
help="Specify the export name when exporting a profile",
metavar="<archive-name>"
)
parser.add_argument(
"-v", "--version", required=False, action="store_true", help="Show version"
)
Expand Down Expand Up @@ -119,7 +133,8 @@ def main():
elif args.apply:
apply_profile(args.apply, list_of_profiles, length_of_lop)
elif args.export_profile:
export(args.export_profile, list_of_profiles, length_of_lop)
export(args.export_profile, list_of_profiles, length_of_lop,
args.export_directory, args.export_name, args.force)
elif args.import_profile:
import_profile(args.import_profile)
elif args.version:
Expand Down
33 changes: 25 additions & 8 deletions konsave/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def log(msg, *args, **kwargs):
*args: any arguments for the function print()
**kwargs: any keyword arguments for the function print()
"""
print(f"Konsave: {msg.capitalize()}", *args, **kwargs)
print(f"Konsave: {msg}", *args, **kwargs)


@exception_handler
Expand Down Expand Up @@ -249,13 +249,17 @@ def remove_profile(profile_name, profile_list, profile_count):


@exception_handler
def export(profile_name, profile_list, profile_count):
"""It will export the specified profile as a ".knsv" file in the home directory.
def export(profile_name, profile_list, profile_count, archive_dir, archive_name, force):
"""It will export the specified profile as a ".knsv" to the specified directory.
If there is no specified directory, the directory is set to the current working directory.
Args:
profile_name: name of the profile to be exported
profile_list: the list of all created profiles
profile_count: number of profiles created
directory: output directory for the export
force: force the overwrite of existing export file
name: the name of the resulting archive
"""

# assert
Expand All @@ -264,12 +268,25 @@ def export(profile_name, profile_list, profile_count):

# run
profile_dir = os.path.join(PROFILES_DIR, profile_name)
export_path = os.path.join(HOME, profile_name)

if os.path.exists(export_path):
rand_str = list("abcdefg12345")
shuffle(rand_str)
export_path = export_path + "".join(rand_str)
if archive_name:
profile_name = archive_name

if archive_dir:
export_path = os.path.join(archive_dir, profile_name)
else:
export_path = os.path.join(os.getcwd(), profile_name)

# Only continue if export_path, export_path.ksnv and export_path.zip don't exist
# Appends date and time to create a unique file name
if not force:
while True:
paths = [f"{export_path}", f"{export_path}.knsv", f"{export_path}.zip"]
if any([os.path.exists(path) for path in paths]):
time = "f{:%d-%m-%Y:%H-%M-%S}".format(datetime.now())
export_path = f"{export_path}_{time}"
else:
break

# compressing the files as zip
log("Exporting profile. It might take a minute or two...")
Expand Down

0 comments on commit 73845d3

Please sign in to comment.