RFCartography/rfcartography/__init__.py
2023-01-03 14:42:54 +01:00

49 lines
1.6 KiB
Python

from flask import Flask
from rfcartography.index_parser import IndexParser
from rfcartography.rfcartographer import RFCartographer
from rfcartography.routing import register_routers
from rfcartography.errors import register_errorhandlers
META = {'NAME': "RFCartography",
'VERSION': "1.0.0",
'SOURCE': "https://git.undefinedbehavior.de/undef/RFCartography"}
def create_app(test_config: dict = None) -> Flask:
"""set up the flask application"""
# create app
app = Flask(import_name=__name__,
instance_relative_config=True)
# load configuration
if test_config is None:
app.config.from_pyfile(filename='config.py')
else:
app.config.from_mapping(mapping=test_config)
# apply default config values where missing
if app.config['SERVER_NAME'] is None:
app.config['SERVER_NAME'] = 'localhost'
if not 'NAMESPACE' in app.config:
app.config['NAMESPACE'] = 'http://www.rfc-editor.org/rfc-index'
if not 'DEPTH_DEFAULT' in app.config:
app.config['DEPTH_DEFAULT'] = 8
app.config['META'] = META
# import rfc index data
try:
with app.open_instance_resource(app.config['INDEX_FILE']) as rfc_index:
xml: str = rfc_index.read()
except:
print('Error: INDEX_FILE could no be opened.\nExiting...')
exit(1)
# set up the RFCartographer
parser: IndexParser = IndexParser(xml, app.config['NAMESPACE'])
app.cartographer: RFCartographer = RFCartographer(parser.get_index())
# register request handlers
register_errorhandlers(app)
register_routers(app)
return app