2024-11-13 15:41:33 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
* Delete cookie if session does not match
|
|
|
|
|
|
|
|
* Delete entry/tag/jid combination row upon removal of a tag.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2024-11-20 17:42:52 +01:00
|
|
|
from blasta.config import Cache, Settings, Share
|
2024-11-13 15:41:33 +01:00
|
|
|
from blasta.http.instance import HttpInstance
|
2024-11-17 13:00:31 +01:00
|
|
|
from blasta.database.sqlite import DatabaseSQLite
|
2024-11-13 15:41:33 +01:00
|
|
|
import json
|
|
|
|
import logging
|
2024-11-17 13:00:31 +01:00
|
|
|
import os
|
2024-11-13 15:41:33 +01:00
|
|
|
from os.path import getsize, exists
|
2024-11-20 17:42:52 +01:00
|
|
|
import shutil
|
2024-11-13 15:41:33 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
from typing import Optional
|
|
|
|
import uvicorn
|
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except:
|
|
|
|
import tomli as tomllib
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-11-17 15:24:16 +01:00
|
|
|
directory_config = Settings.get_directory()
|
|
|
|
sql_filename = os.path.join(directory_config, 'blasta.sql')
|
2024-11-17 13:00:31 +01:00
|
|
|
directory_data = Share.get_directory()
|
2024-11-17 15:24:16 +01:00
|
|
|
dbs_filename = os.path.join(directory_data, 'main.sqlite')
|
|
|
|
if not exists(dbs_filename) or not getsize(dbs_filename):
|
|
|
|
DatabaseSQLite.create_tables(sql_filename, dbs_filename)
|
2024-11-13 15:41:33 +01:00
|
|
|
accounts = {}
|
|
|
|
sessions = {}
|
|
|
|
http_instance = HttpInstance(accounts, sessions)
|
|
|
|
return http_instance.app
|
|
|
|
|
|
|
|
if __name__ == 'blasta.__main__':
|
2024-11-20 17:42:52 +01:00
|
|
|
|
|
|
|
directory = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
# Copy data files
|
|
|
|
directory_data = Share.get_directory()
|
|
|
|
if not os.path.exists(directory_data):
|
|
|
|
directory_assets = os.path.join(directory, 'assets')
|
|
|
|
directory_assets_new = shutil.copytree(directory_assets, directory_data)
|
|
|
|
print(f'Data directory {directory_assets_new} has been created and populated.')
|
|
|
|
|
|
|
|
# Copy settings files
|
|
|
|
directory_settings = Settings.get_directory()
|
|
|
|
if not os.path.exists(directory_settings):
|
|
|
|
directory_configs = os.path.join(directory, 'configs')
|
|
|
|
directory_settings_new = shutil.copytree(directory_configs, directory_settings)
|
|
|
|
print(f'Settings directory {directory_settings_new} has been created and populated.')
|
|
|
|
|
|
|
|
# Create cache directories
|
|
|
|
directory_cache = Cache.get_directory()
|
|
|
|
if not os.path.exists(directory_cache):
|
|
|
|
print(f'Creating a cache directory at {directory_cache}.')
|
|
|
|
os.mkdir(directory_cache)
|
|
|
|
for subdirectory in ('data', 'export', 'items'):
|
|
|
|
subdirectory_cache = os.path.join(directory_cache, subdirectory)
|
|
|
|
if not os.path.exists(subdirectory_cache):
|
|
|
|
print(f'Creating a cache subdirectory at {subdirectory_cache}.')
|
|
|
|
os.mkdir(subdirectory_cache)
|
|
|
|
|
2024-11-13 15:41:33 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog='blasta',
|
|
|
|
description='Blasta - A collaborative annotation system.',
|
|
|
|
usage='%(prog)s [OPTION]...')
|
|
|
|
parser.add_argument('-v', '--version', help='print version',
|
|
|
|
action='version', version='0.1')
|
|
|
|
parser.add_argument('-p', '--port', help='port number', dest='port')
|
|
|
|
parser.add_argument('-o', '--open', help='open an html browser', action='store_const', const=True, dest='open')
|
|
|
|
args = parser.parse_args()
|
2024-11-17 17:12:01 +01:00
|
|
|
port = int(args.port or 8000)
|
2024-11-20 17:42:52 +01:00
|
|
|
|
|
|
|
app = main()
|
2024-11-13 15:41:33 +01:00
|
|
|
uvicorn.run(app, host='localhost', port=port)
|
2024-11-20 17:42:52 +01:00
|
|
|
|
2024-11-13 15:41:33 +01:00
|
|
|
if args.open:
|
|
|
|
# TODO Check first time
|
|
|
|
webbrowser.open('http://localhost:{}/help/about'.format(port))
|
|
|
|
webbrowser.open_new_tab('http://localhost:{}'.format(port))
|
|
|
|
|