Skip to content

Commit

Permalink
fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaesivsm committed Dec 1, 2016
1 parent d5549be commit 92f2f00
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

from lib import conf_handling

LOGIN_OPTION_NAME = "CRAWLER_LOGIN"
PASSWORD_OPTION_NAME = "CRAWLER_PASSWD"


def parse_args():
parser = ArgumentParser("""This script is aimed to bootstrap JARR.
Expand Down Expand Up @@ -56,12 +59,13 @@ def _print_line_informations(choices, default):
default = new_default
break
print('(default: %r)' % default, end=' ')
return default


def ask(text, choices=[], default=None, cast=None):
while True:
print(text, end=' ')
_print_line_informations(choices, default)
default = _print_line_informations(choices, default)
print(':', end=' ')

result = input()
Expand All @@ -86,9 +90,8 @@ def ask(text, choices=[], default=None, cast=None):
return result


def build_conf(test=False, creds={}):
def _get_conf(test, creds):
conf, could_import_conf = None, False
logins, passwords = {'CRAWLER_LOGIN'}, {'CRAWLER_PASSWD'}
if not test:
try:
conf = conf_handling.ConfObject()
Expand All @@ -102,6 +105,36 @@ def build_conf(test=False, creds={}):
else:
creds['login'] = conf.CRAWLER_LOGIN
creds['password'] = conf.CRAWLER_PASSWD
return conf, could_import_conf


def _get_default(option, name, test, conf, could_import_conf):
if test and 'test' in option:
return option['test']
elif could_import_conf and hasattr(conf, name):
return getattr(conf, name)
elif name == 'BUNDLE_JS' and can_npm():
return 'local'
return option.get('default')


def _get_value(option, name, default, edit, test, creds):
if (edit and not test) or (name == 'BUNDLE_JS' and not can_npm()):
value = ask(option['ask'], choices=option.get('choices', []),
default=default)
else:
value = default
if 'type' in option:
value = option['type'](value)
if name == LOGIN_OPTION_NAME and creds.get('login'):
value = creds['login']
if name == PASSWORD_OPTION_NAME and creds.get('password'):
value = creds['password']
return value


def build_conf(test=False, creds={}):
conf, could_import_conf = _get_conf(test, creds)

for section in conf_handling.SECTIONS:
section_edit = section.get('edit', True)
Expand All @@ -114,30 +147,9 @@ def build_conf(test=False, creds={}):
title(prefix)
for option in section['options']:
edit = section_edit and option.get('edit', True)

name = conf_handling.get_key(section, option)

if test and 'test' in option:
default = option['test']
elif could_import_conf and hasattr(conf, name):
default = getattr(conf, name)
elif name == 'BUNDLE_JS' and can_npm():
default = 'local'
else:
default = option.get('default')

if (edit and not test) \
or (name == 'BUNDLE_JS' and not can_npm()):
value = ask(option['ask'], choices=option.get('choices', []),
default=default)
else:
value = default
if 'type' in option:
value = option['type'](value)
if name in logins and creds.get('login'):
value = creds['login']
if name in passwords and creds.get('password'):
value = creds['password']
default = _get_default(option, name, test, conf, could_import_conf)
value = _get_value(option, name, default, edit, test, creds)
yield prefix, name, value
print()

Expand Down

0 comments on commit 92f2f00

Please sign in to comment.