Skip to content

Commit

Permalink
rename files, implement methods, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed May 31, 2016
1 parent 035379d commit aef3685
Show file tree
Hide file tree
Showing 52 changed files with 1,127 additions and 625 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ secrets.py
*.lock
src/forklift.log*
src/config.json
config.json
31 changes: 16 additions & 15 deletions src/forklift/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
forklift config --add <folder-path>
forklift config --remove <folder-path>
forklift config --list
forklift list [<folder-path>]
forklift list-pallets [<folder-path>]
forklift lift [<file-path>]
Arguments:
Expand All @@ -20,13 +20,13 @@
config --add path/to/folder adds a path to the config. Checks for duplicates.
config --remove path/to/folder removes a path from the config.
config --list outputs the list of pallet folder paths in your config file.
list outputs the list of pallets from the config.
list path/to/folder outputs the list of pallets for the passed in path.
list-pallets outputs the list of pallets from the config.
list-pallets path/to/folder outputs the list of pallets for the passed in path.
lift the main entry for running all of pallets found in the config paths.
lift path/to/file run a specific pallet.
'''

import lift
import cli
import logging.config
import sys
from docopt import docopt
Expand All @@ -38,35 +38,36 @@ def main():

if args['config']:
if args['--init']:
message = lift.init()
message = cli.init()
print('config file created: {}'.format(message))

if args['--add'] and args['<folder-path>']:
message = lift.add_config_folder(args['<folder-path>'])
message = cli.add_config_folder(args['<folder-path>'])
print(message)

if args['--remove'] and args['<folder-path>']:
message = lift.remove_pallet_folder(args['<folder-path>'])
print('{} removed from config file'.format(message))
message = cli.remove_config_folder(args['<folder-path>'])
print(message)

if args['--list']:
lift.list_config_folders()
elif args['list']:
for folder in cli.list_config_folders():
print(folder)
elif args['list-pallets']:
if args['<folder-path>']:
pallets = lift.list_pallets(args['<path>'])
pallets = cli.list_pallets(args['<path>'])
else:
pallets = lift.list_pallets()
pallets = cli.list_pallets()

if len(pallets) == 0:
print('No pallets found!')
else:
for plug in pallets:
print(': '.join(plug))
elif args['update']:
elif args['lift']:
if args['<file-path>']:
lift.lift(args['<file-path>'])
cli.start_lift(args['<file-path>'])
else:
lift.lift()
cli.start_lift()


def _setup_logging():
Expand Down
162 changes: 162 additions & 0 deletions src/forklift/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env python
# * coding: utf8 *
'''
lift.py
A module that contains the implementation of the cli commands
'''

import logging
import settings
import sys
from glob import glob
from json import dumps, loads
from os.path import abspath, exists, join, splitext, basename, dirname
from models import Pallet
import lift
import core

log = logging.getLogger(settings.LOGGER)


def init():
if exists('config.json'):
return 'config file already created.'

default_pallet_locations = ['c:\\scheduled']

log.debug('creating config.json file.')

return _set_config_folders(default_pallet_locations)


def add_config_folder(folder):
folders = get_config_folders()

if folder in folders:
return '{} is already in the config folders list!'.format(folder)

try:
_validate_config_folder(folder, raises=True)
except Exception as e:
return e.message

folders.append(folder)

_set_config_folders(folders)

return 'added {}'.format(folder)


def remove_config_folder(folder):
folders = get_config_folders()

try:
folders.remove(folder)
except ValueError:
return '{} is not in the config folders list!'.format(folder)

_set_config_folders(folders)

return 'removed {}'.format(folder)


def list_pallets(folders=None):
if folders is None:
folders = get_config_folders()

return _get_pallets_in_folders(folders)


def list_config_folders():
folders = get_config_folders()

validate_results = []
for folder in folders:
validate_results.append(_validate_config_folder(folder))

return validate_results


def _set_config_folders(folders):
if type(folders) != list:
raise Exception('config file data must be a list.')

with open('config.json', 'w') as json_data_file:
data = dumps(folders)

log.debug('writing %s to %s', data, abspath(json_data_file.name))
json_data_file.write(data)

return abspath(json_data_file.name)


def get_config_folders():
if not exists('config.json'):
raise Exception('config file not found.')

with open('config.json', 'r') as json_data_file:
config = loads(json_data_file.read())

return config


def _validate_config_folder(folder, raises=False):
if exists(folder):
valid = 'valid'
else:
valid = 'invalid!'
if raises:
raise Exception('{}: {}'.format(folder, valid))

return('{}: {}'.format(folder, valid))


def _get_pallets_in_folders(folders):
pallets = []

for folder in folders:
for py_file in glob(join(folder, '*.py')):
pallets.extend(_get_pallets_in_file(py_file))

return pallets


def _get_pallets_in_file(file_path):
pallets = []
name = splitext(basename(file_path))[0]
folder = dirname(file_path)

if folder not in sys.path:
sys.path.append(folder)

mod = __import__(name)

for member in dir(mod):
try:
potential_class = getattr(mod, member)
if issubclass(potential_class, Pallet) and potential_class != Pallet:
pallets.append((file_path, member))
except:
#: member was likely not a class
pass

return pallets


def start_lift(file_path=None):
if file_path is not None:
pallet_infos = _get_pallets_in_file(file_path)
else:
pallet_infos = list_pallets()

pallets = []
for info in pallet_infos:
module_name = splitext(basename(info[0]))[0]
class_name = info[1]
PalletClass = getattr(__import__(module_name), class_name)
pallets.append(PalletClass())

lift.process_crates_for(pallets, core.update)

print(lift.process_pallets(pallets))

0 comments on commit aef3685

Please sign in to comment.