Skip to content

Commit

Permalink
v1.0.9
Browse files Browse the repository at this point in the history
v1.0.9
  • Loading branch information
WhaleJ84 authored Jan 12, 2021
2 parents 8b7382d + 8ebb1cf commit 34a7564
Show file tree
Hide file tree
Showing 19 changed files with 295 additions and 157 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.coverage
hb_organiser/config.py
hb_organiser.log
queue.txt
args.conf
5 changes: 1 addition & 4 deletions build/lib/hb_organiser/check.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""
Contains methods used to check given input before operating upon it.
"""
import logging
from pathlib import Path
from os import listdir
from os.path import isdir, abspath, join

from hb_organiser.bundle_objects import Library, Bundle, Item, File

logger = logging.getLogger(__name__)


def source_exists(source):
"""
Expand All @@ -27,7 +24,7 @@ def source_exists(source):
print("ERROR: no path given")
return False
return True
print(f"ERROR: specified source does not exist: {source}")
print(f"ERROR: specified source does not exist: {abspath(source)}")
return False


Expand Down
119 changes: 83 additions & 36 deletions build/lib/hb_organiser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
Contains the code pertaining to the CLI. All back-end logic is located elsewhere.
"""
import argparse
from os.path import abspath
from sys import argv

from hb_organiser.check import source_exists
from hb_organiser.config import Config
from hb_organiser.organiser import HBOrganiser
from hb_organiser.logger import logger


def cli(args=None):
Expand All @@ -21,54 +20,102 @@ def cli(args=None):
parser.add_argument(
"platform", nargs="+", type=str,
help="specifies what platform of bundles to be organised.",
choices=['all', 'android', 'audio', 'ebook', 'humble play', 'key', 'linux',
'mac', 'origin', 'steam', 'unique links', 'video', 'windows']
# choices=['all', 'android', 'audio', 'ebook', 'humble play', 'key', 'linux',
# 'mac', 'origin', 'steam', 'unique links', 'video', 'windows']
choices=['all', 'android', 'audio', 'ebook', 'linux', 'mac', 'video', 'windows']
)
parser.add_argument(
"-c", "--conf", type=str,
metavar="PATH",
help="points to configuration file containing arguments such as source and destination. "
"one argument is expected per line in KEY=VALUE format (e.g. SRC=/path/to/file). "
"manually specified flags will overwrite any values specified in the file."
)
parser.add_argument(
"-s", "--source", type=str,
metavar='SRC',
help="source directory of bundles. overrides location specified in config file."
metavar="SRC",
help="source directory of bundles. overrides SRC specified in config file if present."
)
parser.add_argument(
"-d", "--dest", type=str,
metavar="DEST",
help="destination directory of sorted bundles. overrides DEST specified in config file if present. "
"if no destination is given, a dry-run will occur - displaying what would have happened."
)
parser.add_argument(
"-d", "--destination", type=str,
metavar='DEST',
help="destination directory of sorted bundles. overrides location specified in config file. "
"if no destination is given, a dry run will occur - displaying what would have happened."
"-D", "--dry-run",
action='store_true',
help="performs a dry-run, not actually performing any copy operations. "
"requires a source be specified via flag or config file to run. "
"overwrites destination specified via flag and config file if present."
)

if args is not None:
args = parser.parse_args(args)
else:
args = parser.parse_args()
return parser.parse_args(args)
return parser.parse_args()


def read_config(path):
"""
Reads pre-defined arguments from the specified path.
:param path: Path to the config file.
:type path: str
:return:
"""
arguments = {}
with open(abspath(path)) as conf:
for line in conf:
key, value = line.partition("=")[::2]
arguments[key.strip()] = str(value).strip('\n')
conf.close()
return arguments


def verify_args(args):
"""
Determines if the arguments given suffice or need to be overwritten.
:param args:
:return:
:rtype: list
"""
source = None
destination = None

if args.conf:
conf_args = read_config(args.conf)
source = conf_args["SRC"]
destination = conf_args["DEST"]

# def verify_args(args):
if args.source:
source = args.source
else:
try:
source = Config.SOURCE
logger.debug('No source given via CLI. Reading "%s" from config', Config.SOURCE)
except KeyError:
source = None
logger.debug('No source given via CLI or config. Defaulting to None')

if args.destination:
destination = args.destination
else:
try:
destination = Config.DESTINATION
logger.debug('No destination given via CLI. Reading "%s" from config', Config.DESTINATION)
except KeyError:
destination = None
logger.debug('No destination given via CLI or config. Defaulting to None')

if args.dest:
destination = args.dest

if args.dry_run:
destination = None

if not source_exists(source):
return False
organiser = HBOrganiser(source, destination, args.platform)
organiser.loop_through_bundles()
return True
return [None, destination, args.platform]
return [source, destination, args.platform]


def main():
"""
Performs the list of tasks to be completed when called as a pip module.
:return:
"""
pip_arg = verify_args(cli())
if pip_arg[0] is not None:
pip_organiser = HBOrganiser(pip_arg[0], pip_arg[1], pip_arg[2])
pip_organiser.loop_through_bundles()


if __name__ == '__main__':
cli(argv[1:])
arg = verify_args(cli(argv[1:]))
if arg[0] is not None:
organiser = HBOrganiser(arg[0], arg[1], arg[2])
organiser.loop_through_bundles()
8 changes: 6 additions & 2 deletions build/lib/hb_organiser/organiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,20 @@ def copy_file(self, source, destination, task=None):
"""
complete = 'DONE'
try:
if source not in open('queue.txt') and not isfile(destination):
log = open('queue.txt')
if source not in log and not isfile(destination):
log.close()
print(f"{task} COPYING: {source} {destination}\r", end="", flush=True)
complete = 'COPIED'
log = open('queue.txt', 'w')
log.write(source)
log.close()
elif source in open('queue.txt'):
elif source in log:
log.close()
print(f"{task} RE-COPING: {source} {destination}\r", end="", flush=True)
complete = 'RE-COPIED'
else:
log.close()
print(f"{task} SKIPPED: {destination}")
return True
except FileNotFoundError:
Expand Down
74 changes: 54 additions & 20 deletions build/lib/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os.path import abspath
from unittest import TestCase

from hb_organiser.cli import *
from hb_organiser.cli import cli, verify_args


class HBOrganiserCliTesCase(TestCase):
Expand All @@ -12,37 +12,71 @@ def tearDown(self) -> None:
pass

def test_verify_args_returns_true_on_good_relative_source_path_with_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library/', 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library/', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[0])

def test_verify_args_returns_true_on_good_absolute_source_path_with_trailing_forward_slash(self):
result = cli(['-s', abspath('tests/test_libraries/library/'), 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', abspath('tests/test_libraries/library/'), 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[0])

def test_verify_args_returns_true_on_good_relative_source_path_without_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library', 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[0])

def test_verify_args_returns_true_on_good_absolute_source_path_without_trailing_forward_slash(self):
result = cli(['-s', abspath('tests/test_libraries/library'), 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', abspath('tests/test_libraries/library'), 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[0])

def test_verify_args_returns_false_on_source_bad_path(self):
result = cli(['-s', 'tests/test_libraries/fake_library', 'all'])
HBOrganiserCliTesCase.assertFalse(self, result)
def test_verify_args_returns_none_on_source_bad_path(self):
arguments = cli(['-s', 'tests/test_libraries/fake_library', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertIsNone(self, result[0])

def test_verify_args_returns_true_on_good_relative_destination_path_with_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library/', '-d', 'tests/test_destination/', 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library/', '-d', 'tests/test_destination/', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[1])

def test_verify_args_returns_true_on_good_absolute_destination_path_with_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library/', '-d', abspath('tests/test_destination/'), 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library/', '-d', abspath('tests/test_destination/'), 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[1])

def test_verify_args_returns_true_on_good_relative_destination_path_without_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library/', '-d', 'tests/test_destination', 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library/', '-d', 'tests/test_destination', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[1])

def test_verify_args_returns_true_on_good_absolute_destination_path_without_trailing_forward_slash(self):
result = cli(['-s', 'tests/test_libraries/library/', '-d', abspath('tests/test_destination'), 'all'])
HBOrganiserCliTesCase.assertTrue(self, result)
arguments = cli(['-s', 'tests/test_libraries/library/', '-d', abspath('tests/test_destination'), 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertTrue(self, result[1])

def test_dry_run_flag_overwrites_destination_flag_to_none(self):
arguments = cli(['-s', 'tests/test_libraries/library', '-d', 'tests/test_destination', '-D', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertIsNone(self, result[1])

def test_verify_args_returns_source_on_good_config_path(self):
arguments = cli(['-c' 'tests/test.conf', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertEqual(self, result[0], 'tests/test_libraries/library')

def test_verify_args_return_destination_on_good_config_path(self):
arguments = cli(['-c', 'tests/test.conf', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertEqual(self, result[1], 'tests/test_destination')

def test_verify_args_overwrites_config_source_on_set_source_flag(self):
arguments = cli(['-c', 'tests/test.conf', '-s', 'tests/test_destination', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertEqual(self, result[0], 'tests/test_destination')

def test_verify_args_overwrites_config_destination_on_set_destination_flag(self):
arguments = cli(['-c', 'tests/test.conf', '-d', 'tests/test_libraries/library', 'all'])
result = verify_args(arguments)
HBOrganiserCliTesCase.assertEqual(self, result[1], 'tests/test_libraries/library')
Binary file added dist/hb_organiser-1.0.9-py3-none-any.whl
Binary file not shown.
Binary file added dist/hb_organiser-1.0.9.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion hb_organiser.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: hb-organiser
Version: 1.0.8
Version: 1.0.9
Summary: Organises Humble Bundle bundles based on their platform.
Home-page: https://github.com/WhaleJ84/hb_organiser
Author: James Whale
Expand Down
2 changes: 0 additions & 2 deletions hb_organiser.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ hb_organiser/__init__.py
hb_organiser/bundle_objects.py
hb_organiser/check.py
hb_organiser/cli.py
hb_organiser/config.py
hb_organiser/logger.py
hb_organiser/organiser.py
hb_organiser.egg-info/PKG-INFO
hb_organiser.egg-info/SOURCES.txt
Expand Down
2 changes: 1 addition & 1 deletion hb_organiser.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[console_scripts]
hb_organiser = hb_organiser.cli:cli
hb_organiser = hb_organiser.cli:main

5 changes: 1 addition & 4 deletions hb_organiser/check.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""
Contains methods used to check given input before operating upon it.
"""
import logging
from pathlib import Path
from os import listdir
from os.path import isdir, abspath, join

from hb_organiser.bundle_objects import Library, Bundle, Item, File

logger = logging.getLogger(__name__)


def source_exists(source):
"""
Expand All @@ -27,7 +24,7 @@ def source_exists(source):
print("ERROR: no path given")
return False
return True
print(f"ERROR: specified source does not exist: {source}")
print(f"ERROR: specified source does not exist: {abspath(source)}")
return False


Expand Down
Loading

0 comments on commit 34a7564

Please sign in to comment.