Skip to content

Commit

Permalink
Merge pull request #40 from aanil/fix_git
Browse files Browse the repository at this point in the history
Fix for reading dotenv file
  • Loading branch information
aanil authored May 15, 2024
2 parents d19b4e7 + 1178350 commit 002caa9
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 153 deletions.
5 changes: 3 additions & 2 deletions daily_read/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import daily_read.config
""" DailyRead version
"""

config_values = daily_read.config.Config()
__version__ = "0.0.1"
59 changes: 33 additions & 26 deletions daily_read/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,57 @@
import daily_read.utils


config_values = daily_read.config.Config()
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
@click.option("--env_file_path", type=click.Path())
@click.pass_context
def daily_read_cli(ctx, env_file_path):
config_values = daily_read.config.Config(env_file_path=env_file_path)
if ctx.obj is None:
ctx.obj = dict()
ctx.obj["config_values"] = config_values

rich_handler = RichHandler()
rich_handler.setFormatter(logging.Formatter("%(message)s"))
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - Commit: %(commit)s - %(message)s"
rich_handler = RichHandler()
rich_handler.setFormatter(logging.Formatter("%(message)s"))
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - Commit: %(commit)s - %(message)s"

if not os.path.isabs(config_values.LOG_LOCATION):
raise ValueError(f"Log location is not an absolute path: {config_values.LOG_LOCATION}")
if not os.path.isabs(config_values.LOG_LOCATION):
raise ValueError(f"Log location is not an absolute path: {config_values.LOG_LOCATION}")

if os.path.exists(config_values.LOG_LOCATION) and not os.path.isdir(config_values.LOG_LOCATION):
raise ValueError(f"Log Location exists but is not a directory: {config_values.LOG_LOCATION}")
if os.path.exists(config_values.LOG_LOCATION) and not os.path.isdir(config_values.LOG_LOCATION):
raise ValueError(f"Log Location exists but is not a directory: {config_values.LOG_LOCATION}")

log_file = os.path.join(config_values.LOG_LOCATION, "DailyRead.log")
log_file = os.path.join(config_values.LOG_LOCATION, "DailyRead.log")

rotating_file_handler = logging.handlers.RotatingFileHandler(
log_file, maxBytes=1024 * 1024 * 100, backupCount=5
) # 5 files of 100MB
rotating_file_handler.addFilter(daily_read.utils.ContextFilter())
rotating_file_handler = logging.handlers.RotatingFileHandler(
log_file, maxBytes=1024 * 1024 * 100, backupCount=5
) # 5 files of 100MB
rotating_file_handler.addFilter(daily_read.utils.ContextFilter())

logging.basicConfig(
level="INFO",
format=LOG_FORMAT,
handlers=[rich_handler, rotating_file_handler],
)

logging.basicConfig(
level="INFO",
format=LOG_FORMAT,
handlers=[rich_handler, rotating_file_handler],
)

log = logging.getLogger(__name__)


@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
def daily_read_cli():
pass


### GENERATE ###
@daily_read_cli.group()
def generate():
@click.pass_context
def generate(ctx):
"""Generate reports and save in a local git repository"""
pass


@generate.command(name="all")
@click.option("-u", "--upload", is_flag=True, help="Trigger upload of reports.")
@click.option("--develop", is_flag=True, help="Only generate max 5 reports, for dev purposes.")
def generate_all(upload=False, develop=False):
@click.pass_context
def generate_all(ctx, upload=False, develop=False):
# Fetch data from all sources (configurable)
config_values = ctx.obj["config_values"]
projects_data = daily_read.ngi_data.ProjectDataMaster(config_values)

log.info(f"Fetching data for {projects_data.source_names}")
Expand Down Expand Up @@ -137,7 +142,9 @@ def generate_all(upload=False, develop=False):
help="Include projects that are older than 6 months.",
is_flag=True,
)
def generate_single(project, include_older=False):
@click.pass_context
def generate_single(ctx, project, include_older=False):
config_values = ctx.obj["config_values"]
projects_data = daily_read.ngi_data.ProjectDataMaster(config_values)
# Fetch all projects so that the report will look the same
log.info("Fetching data from NGI sources")
Expand Down
4 changes: 2 additions & 2 deletions daily_read/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class Config(object):
def __init__(self):
dotenv.load_dotenv()
def __init__(self, env_file_path):
dotenv.load_dotenv(dotenv_path=env_file_path)
self.ORDER_PORTAL_URL = os.getenv("DAILY_READ_ORDER_PORTAL_URL")
self.ORDER_PORTAL_API_KEY = os.getenv("DAILY_READ_ORDER_PORTAL_API_KEY")
self.REPORTS_LOCATION = os.getenv("DAILY_READ_REPORTS_LOCATION")
Expand Down
134 changes: 72 additions & 62 deletions daily_read/templates/daily_report.html.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<iframe id="ngi_progress_report" srcdoc='
<iframe
id="ngi_progress_report"
srcdoc='
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
Expand Down Expand Up @@ -153,69 +155,73 @@
{% else %}
<div id="projects_list">
{% for date_prio, date_type in priority | dictsort %}
{% if date_type != "None"%}
<div class="row p-3">
<div
class="col-sm border-end border-primary me-3 d-flex align-items-center"
style="--bs-border-width: 15px;"
>
<div class="fs-1">
{{ date_type }}
</div>
<div class="fs-5">
<i class="bi bi-patch-question px-1" data-bs-toggle="tooltip" data-bs-title="{{status_desc[date_type]}}"></i>
{% if date_type != "None" %}
<div class="row p-3">
<div
class="col-sm border-end border-primary me-3 d-flex align-items-center"
style="--bs-border-width: 15px;"
>
<div class="fs-1">
{{ date_type }}
</div>
<div class="fs-5">
<i
class="bi bi-patch-question px-1"
data-bs-toggle="tooltip"
data-bs-title="{{ status_desc[date_type] }}"
></i>
</div>
</div>
</div>
<div class="col-sm projects-accordions">
{% for project in data["projects"][date_type] %}
<div class="accordion mb-1">
<div class="accordion-item">
<h2 class="accordion-header">
<button
class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#panel_project_{{ project.project_id }}"
aria-expanded="false"
aria-controls="panel_project_{{ project.project_id }}"
>
<h4 class="mb-0">{{ project.internal_name }}</h4>
</button>
</h2>
<div id="panel_project_{{ project.project_id }}" class="accordion-collapse collapse">
<div class="accordion-body pb-0">
<p>
Order Portal:
<a target="_parent" href="{{ portal_url }}/order/{{ project.project_id }}">
{{ project.project_id }}
</a>
</p>
{% for date, statuses in project.project_dates | dictsort %}
{% if project.project_dates[date] and project.project_dates[date] != "XXXX-XX-XX" %}
<dl class="row ml-3">
<dt class="list-group-item col-4">{{ date }}</dt>
<dd class="list-group-item col-8">
{% for status in statuses %}
<i class="bi bi-{{ icons[status] }}"></i>
<span style="padding-left: 0.5rem">{{ status }}</span>
<i
class="bi bi-patch-question px-1"
data-bs-toggle="tooltip"
data-bs-title="{{ status_desc[status] }}"
></i
><br />
{% endfor %}
</dd>
</dl>
{% endif %}
{% endfor %}
<div class="col-sm projects-accordions">
{% for project in data["projects"][date_type] %}
<div class="accordion mb-1">
<div class="accordion-item">
<h2 class="accordion-header">
<button
class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#panel_project_{{ project.project_id }}"
aria-expanded="false"
aria-controls="panel_project_{{ project.project_id }}"
>
<h4 class="mb-0">{{ project.internal_name }}</h4>
</button>
</h2>
<div id="panel_project_{{ project.project_id }}" class="accordion-collapse collapse">
<div class="accordion-body pb-0">
<p>
Order Portal:
<a target="_parent" href="{{ portal_url }}/order/{{ project.project_id }}">
{{ project.project_id }}
</a>
</p>
{% for date, statuses in project.project_dates | dictsort %}
{% if project.project_dates[date] and project.project_dates[date] != "XXXX-XX-XX" %}
<dl class="row ml-3">
<dt class="list-group-item col-4">{{ date }}</dt>
<dd class="list-group-item col-8">
{% for status in statuses %}
<i class="bi bi-{{ icons[status] }}"></i>
<span style="padding-left: 0.5rem">{{ status }}</span>
<i
class="bi bi-patch-question px-1"
data-bs-toggle="tooltip"
data-bs-title="{{ status_desc[status] }}"
></i
><br />
{% endfor %}
</dd>
</dl>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
Expand All @@ -235,6 +241,10 @@
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
</script>
</body>
' width="120%" height="2000" frameborder="0" sandbox="allow-scripts allow-top-navigation"></iframe>

</html>
'
width="120%"
height="2000"
frameborder="0"
sandbox="allow-scripts allow-top-navigation"
></iframe>
</html>
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = "0.0.1"
from daily_read import __version__

with open("README.md") as f:
readme = f.read()
Expand All @@ -10,7 +10,7 @@

setup(
name="Daily Read",
version=version,
version=__version__,
description="A utility to generate and upload automatic progress reports for NGI Sweden.",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def data_repo_full(
@pytest.fixture
def mock_project_data_record():
def _method(status):
config_values = config.Config()
config_values = config.Config(env_file_path=".test.env")
if status == "open":
mock_record = ngi_data.ProjectDataRecord(
"NGIS/2023/NGI123456.json",
Expand Down Expand Up @@ -441,3 +441,10 @@ def mocked_statusdb_conn_rows():
},
)
return [row1, row2, row3]


@pytest.fixture(autouse=True)
def get_env_file_path():
"""returns the test env file path"""

return ".test.env"
4 changes: 2 additions & 2 deletions tests/test_daily_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from daily_read import daily_report, config, ngi_data, order_portal


def test_write_report_to_out_dir(data_repo_full, mock_project_data_record, create_report_path):
def test_write_report_to_out_dir(data_repo_full, mock_project_data_record, create_report_path, get_env_file_path):
"""Test existence of html report when provided with out_dir"""
orderer = "[email protected]"
order_id = "NGI123456"
config_values = config.Config()
config_values = config.Config(env_file_path=get_env_file_path)
daily_rep = daily_report.DailyReport()
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
Expand Down
Loading

0 comments on commit 002caa9

Please sign in to comment.