prefilter_app / src /utils.py
mtyrrell's picture
Cost effectivness refactor
4680ed9
from io import BytesIO
from openpyxl import Workbook
from openpyxl.styles import Font, NamedStyle, PatternFill
# from openpyxl.styles.differential import DifferentialStyle
import logging
from logging.handlers import RotatingFileHandler
import os
import configparser
import logging
logger = logging.getLogger(__name__)
def setup_logging():
# Set up logging
log_dir = 'logs'
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, 'app.log')
# Create a RotatingFileHandler
file_handler = RotatingFileHandler(log_file, maxBytes=1024 * 1024, backupCount=5)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
# Configure the root logger
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[file_handler, logging.StreamHandler()])
# Return a logger instance
return logging.getLogger(__name__)
def getconfig(configfile_path: str):
"""
Read the config file
Params
----------------
configfile_path: file path of .cfg file
"""
config = configparser.ConfigParser()
try:
config.read_file(open(configfile_path))
return config
except:
logging.warning("config file not found")
# Function for creating Upload template file
def create_excel():
wb = Workbook()
sheet = wb.active
sheet.title = "template"
columns = ['id',
'country',
'organization',
'scope',
'technology',
'financial',
'barrier',
'technology_rationale',
'project_rationale',
'project_objectives',
'maf_funding_requested',
'contributions_public_sector',
'contributions_private_sector',
'contributions_other',
'mitigation_potential']
sheet.append(columns) # Appending columns to the first row
# formatting
for c in sheet['A1:O4'][0]:
c.fill = PatternFill('solid', fgColor = 'bad8e1')
c.font = Font(bold=True)
# Save to a BytesIO object
output = BytesIO()
wb.save(output)
return output.getvalue()