This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
129 lines (103 loc) · 5.08 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from argparse import ArgumentParser
from dotenv import load_dotenv
import os
import pkgutil
import logging
import sys, inspect
from src.importer import all_importer
from src.model import *
from src.settings import Settings
from src.constants import Constants
from src.invoicing.Invoicer import Invoicer
from src.invoicing.Mailer import Mailer
from src.helper.DatabaseCompressor import DatabaseCompressor
from src.app import app
def create_app():
Settings().logger = logging.getLogger("gunicorn.error")
print("create app")
try:
init_db()
flask_app = app.create_app()
gunicorn_logger = logging.getLogger("gunicorn.error")
flask_app.logger.handlers = gunicorn_logger.handlers
flask_app.logger.setLevel(gunicorn_logger.level)
return flask_app
except Exception as e:
print("exception {0}".format(repr(e)))
Settings().logger.error(e)
def init_db():
Settings().init_db()
Settings().db.create_tables([Resource, ServiceType, Config, Account, Invoice, Service, ServiceComponent, ServiceCustomer, DataImport, ServiceComponent2Service, Job, AdditionalInvoicePosition, ServiceComponentPart])
Settings().db.close()
def load_env(args):
if args and args.env:
from pathlib import Path # python3 only
env_path = Path(args.env)
load_dotenv(dotenv_path=env_path)
print('loaded env from argument')
elif os.environ.get('APPLICATION_SETTINGS'):
load_dotenv(dotenv_path=os.environ['APPLICATION_SETTINGS'])
print('loaded env from APPLICATION_SETTINGS environment file')
else:
print("No application environment provided")
if __name__ == "__main__":
# Arg Parse
parser = ArgumentParser(add_help = False)
parser.add_argument("-e", "--env", required=False)
subparsers = parser.add_subparsers(title="cmd", dest="cmd")
import_parser = subparsers.add_parser("import")
import_parser.add_argument("importer", choices=all_importer, help="the importer to run")
import_parser.add_argument("-n", "--name", action="store", required=True, help="the job name")
import_parser.add_argument("-f", "--import-file", action="store", help="optional import file")
bill_parser = subparsers.add_parser("bill")
bill_parser.add_argument("provider", action="store", choices=Constants.Providers, help="The invoices to send")
bill_parser.add_argument("-n", "--name", action="store", required=True, help="the job name")
run_parser = subparsers.add_parser("run", help="Run the admin app")
compress_parser = subparsers.add_parser("compress", help="Compresses the db by joining resource rows with same content and summing the cost")
compress_parser.add_argument("-n", "--name", action="store", required=True, help="the job name")
mail_parser = subparsers.add_parser("mailer", help="Send mails with last billing info to all customers")
mail_parser.add_argument("-n", "--name", action="store", required=True, help="the job name")
migrate_parser = subparsers.add_parser("migrate", help="Run the admin app")
migrate_parser.add_argument("migrate_module", action="store", help="Run a mysql migrate script from src/migrations")
upload_parser =subparsers.add_parser("upload", help="Uploads the file through the comadapter")
upload_parser.add_argument("--file", action="store", required=True, help="path to the csv file")
upload_parser.add_argument("--provider", action="store", required=True, choices=Constants.Providers)
args = parser.parse_args()
load_env(args)
if args.cmd == "run":
# init app & db
flask_app = create_app()
flask_app.run(debug=os.getenv('DEBUG') == 'True')
else:
# only init db
init_db()
if args.cmd == "import":
import importlib
klazz = getattr(importlib.import_module("src.importer.{importer}".format(importer=args.importer)), args.importer)
instance = klazz(args.name)
print("Running import with {importer}".format(importer=args.importer))
if args.import_file:
instance.run(args.import_file)
else:
instance.run()
elif args.cmd == "bill":
print("Running invoicer with {importer}".format(importer=args.provider))
csv = Invoicer.invoice_resources(args.provider)
elif args.cmd == "upload":
print("Upload file with {provider}".format(provider=args.provider))
with open(args.file, 'r') as file:
csv_string = file.read()
Invoicer.upload_file(csv_string, args.provider)
print("file has been uploaded")
elif args.cmd == "compress":
print("Compressing database")
DatabaseCompressor.compress()
elif args.cmd == "mailer":
print("Sending mail info to all service customers about last billing cycle")
Mailer.send_mails_with_last_billing_info()
elif args.cmd == "migrate":
import importlib
print("Migrating schema")
klazz = getattr(importlib.import_module("src.migrations.{migrate}".format(migrate=args.migrate_module)), "MigrateDb")
instance = klazz()
instance.run()