diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b99913d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +trim_trailing_space = true +max_line_length = 120 +indent_style = space + +[*.py] +indent_size = 4 + +[*.html,*.css,*.js,*.ts] +indent_size = 2 diff --git a/.gitignore b/.gitignore index 02ed0c7..0185f33 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__ *.pyo *.pyd *.sqlite +venv diff --git a/scribeengine/config.py b/scribeengine/config.py index c711760..bc30766 100644 --- a/scribeengine/config.py +++ b/scribeengine/config.py @@ -67,3 +67,68 @@ def read_config_from_file(filename): flask_config[key] = value _fix_special_cases(flask_config) return flask_config + + +def write_config_file(config, filename): + """ + Write the generated config to a file + """ + parser = ConfigParser() + for section, options in config.items(): + if not parser.has_section(section): + parser.add_section(section) + for option, value in options.items(): + parser.set(section, option, value) + with open(filename, "w") as config_file: + parser.write(config_file) + + +def prompt(question, options=None, default=None, type_=str): + """ + Prompt the user for an answer, returning the default value if none is given. + """ + if options: + question = '{question} ({options})'.format(question=question, options='/'.join(options)) + if default: + question = '{question} [{default}]'.format(question=question, default=default) + answer = input('{question}: '.format(question=question)).strip() + if not answer: + return default + elif not isinstance(answer, type_): + answer = type_(answer) + return answer + + +def ask_db_questions(): + db_type = prompt('Database type', ['sqlite', 'mysql', 'postgres'], 'sqlite').lower() + if db_type == 'sqlite': + db_name = prompt('Database filename', default='scribeengine.sqlite') + return '{db_type}:///{db_name}'.format(db_type=db_type, db_name=db_name) + db_name = prompt('Database name', default='scribeengine') + db_user = prompt('Database user', default='scribeengine') + db_password = prompt('Database password', default='mysupersecretpassword') + db_host = prompt('Database host', default='localhost') + if db_type in ['mysql', 'mariadb']: + db_port = prompt('Database port', default=3306, type_=int) + elif db_type == 'postgres': + db_port = prompt('Database port', default=5432, type_=int) + else: + db_port = prompt('Database port', default=0, type_=int) + return '{db_type}://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'.format(db_type=db_type, + db_user=db_user, + db_password=db_password, + db_host=db_host, + db_port=db_port, + db_name=db_name) + + +def main(): + """ + Ask the user a set of questions, and then write them to a config file. + """ + config = { + 'sqlalchemy': { + 'database_uri': ask_db_questions() + } + } + write_config_file(config, 'config.ini')