-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR2: Add new preference and AnnouncementChecker
class
#1509
base: shrivaths/changelog-announcement-1
Are you sure you want to change the base?
Changes from 34 commits
de22a57
69ff1ed
3fc0e75
0db2904
7ab6d8b
7639142
babb86d
6eddcf0
3f99b8b
f8fb1f1
d9947c3
4cb0c6a
3323115
fc856bf
39ed0b6
dce4dab
9e1ebf7
d35f029
21ea789
3cc4188
59f07ae
acdf130
fbb1039
63e329a
cbb661d
9c726af
68cb64b
2c92712
303a2d2
b708717
a679e61
02757e6
090e36f
aca6c7a
c218b10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
import os | ||
|
||
# Set the file paths | ||
input_md_file = os.path.join(os.path.dirname(__file__), "bulletin.md") | ||
output_json_file = os.path.join(os.path.dirname(__file__), "bulletin.json") | ||
|
||
|
||
def generate_json_file(): | ||
with open(input_md_file, "r", encoding="utf-8") as md_file: | ||
markdown_content = md_file.read() | ||
bulletin_json = [] | ||
content = "" | ||
|
||
# Initialize title and date with default values | ||
title = "DEFAULT_TITLE" | ||
date = "DEFAULT_DATE" | ||
|
||
for line in markdown_content.split("\n"): | ||
# Skip if the line begins with # | ||
if line.startswith("# "): | ||
continue | ||
elif line.startswith("---"): | ||
bulletin_json.append({"title": title, "date": date, "content": content}) | ||
content = "" | ||
# Reset title and date to their default values after each section | ||
title = "DEFAULT_TITLE" | ||
date = "DEFAULT_DATE" | ||
elif line.startswith("## "): | ||
title = line[3:].strip() | ||
elif line.startswith("_"): | ||
date = line[1 : len(line) - 1].strip() | ||
else: | ||
content += line + "\n" | ||
# Append last section | ||
bulletin_json.append({"title": title, "date": date, "content": content}) | ||
|
||
with open("_static/bulletin.json", "w") as json_file: | ||
json.dump(bulletin_json, json_file, indent=4) | ||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The Given this information, the hardcoded path in the Analysis chainThe JSON file is written to a hardcoded path. Ensure this path is correct and the file has the proper permissions to be written to. Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check if the _static directory exists and has write permissions.
if [ -w "_static/" ]; then
echo "_static directory is writable."
else
echo "_static directory is not writable or does not exist."
fi
Length of output: 136 Script: #!/bin/bash
# Search for the _static directory within the repository
fd _static
# Check for build or deployment scripts that might create the _static directory
fd --extension sh --exec grep -H "_static" {}
Length of output: 74 |
||
|
||
|
||
if __name__ == "__main__": | ||
generate_json_file() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,16 @@ | |
import attr | ||
import pandas as pd | ||
import requests | ||
from typing import List, Dict, Any | ||
from typing import List, Dict, Any, Optional, Tuple | ||
import json | ||
import os | ||
from datetime import datetime | ||
|
||
|
||
REPO_ID = "talmolab/sleap" | ||
ANALYTICS_ENDPOINT = "https://analytics.sleap.ai/ping" | ||
# TODO: Change the Bulletin URL to the main website before deploying | ||
BULLETIN_JSON_ENDPOINT = "https://sleap.ai/develop/docs/_static/bulletin.json" | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
|
@@ -146,6 +151,71 @@ def get_release(self, version: str) -> Release: | |
) | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class AnnouncementChecker: | ||
"""Checker for new announcements on the bulletin page of sleap.""" | ||
|
||
state: "GuiState" | ||
_previous_announcement_date: str = None | ||
bulletin_json_data: Optional[List[Dict[str, str]]] = None | ||
json_data_url: str = BULLETIN_JSON_ENDPOINT | ||
checked: bool = attr.ib(default=False, init=False) | ||
|
||
@property | ||
def previous_announcement_date(self): | ||
_previous_announcement_date = self.state["announcement last seen date"] | ||
return _previous_announcement_date | ||
Comment on lines
+165
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling the case where the key "announcement last seen date" might not exist in |
||
|
||
def check_for_bulletin_data(self) -> Optional[List[Dict]]: | ||
"""Reads the bulletin data from the JSON file endpoint.""" | ||
try: | ||
self.checked = True | ||
self.bulletin_json_data = requests.get(self.json_data_url).json() | ||
except (requests.ConnectionError, requests.Timeout): | ||
self.bulletin_json_data = None | ||
|
||
def new_announcement_available(self) -> bool: | ||
"""Check if latest announcement is available.""" | ||
if not self.checked: | ||
self.check_for_bulletin_data() | ||
if self.bulletin_json_data: | ||
if self.previous_announcement_date: | ||
latest_date = datetime.strptime( | ||
self.bulletin_json_data[0]["date"], "%m/%d/%Y" | ||
) | ||
previous_date = datetime.strptime( | ||
self.previous_announcement_date, "%m/%d/%Y" | ||
) | ||
if latest_date > previous_date: | ||
return True | ||
else: | ||
return False | ||
else: | ||
return True | ||
else: | ||
return False | ||
|
||
def update_latest_announcement(self) -> Optional[Tuple[str, str, str]]: | ||
"""Return latest announcements on the releases page not seen by user.""" | ||
if self.new_announcement_available(): | ||
announcement_markdown = "" | ||
for announcement in self.bulletin_json_data: | ||
announcement_content = "\n".join(announcement["content"].split("\n")) | ||
announcement_markdown += ( | ||
"## " | ||
+ announcement["title"] | ||
+ "\n" | ||
+ announcement["date"] | ||
+ "\n" | ||
+ announcement_content | ||
+ "\n" | ||
) | ||
self.state["announcement"] = announcement_markdown | ||
self.state["announcement last seen date"] = self.bulletin_json_data[0][ | ||
"date" | ||
] | ||
|
||
|
||
def get_analytics_data() -> Dict[str, Any]: | ||
"""Gather data to be transmitted to analytics backend.""" | ||
import os | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
""" | ||
|
||
from sleap import util | ||
from datetime import date | ||
|
||
|
||
class Preferences(object): | ||
|
@@ -28,6 +29,8 @@ class Preferences(object): | |
"node label size": 12, | ||
"show non-visible nodes": True, | ||
"share usage data": True, | ||
"announcement last seen date": False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value for "announcement last seen date" is set to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @talmo I've updated the default value for "announcement last seen date" to _defaults = {
# ... other defaults ...
"announcement last seen date": None,
# ... other defaults ...
} Please ensure to handle this |
||
"announcement": False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value for "announcement" is set to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @talmo I've noted the change for the default value of "announcement" to be an empty string or _defaults = {
# ... other defaults ...
"announcement last seen date": None,
"announcement": "",
} You can apply this change to the |
||
} | ||
_filename = "preferences.yaml" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"title": "title1", "date": "10/09/2023", "content": "New announcement"}, {"title": "title2", "date": "10/07/2023", "content": "Old Announcment"}] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pytest | ||
|
||
TEST_BULLETIN_JSON = "tests/data/announcement_checker_bulletin/test_bulletin.json" | ||
|
||
|
||
@pytest.fixture | ||
def bulletin_json_path(): | ||
return TEST_BULLETIN_JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably needs a stronger check, like a date format regexp or another more explicit indicator. There are lots of cases where we might start a line with
"_"
in the main content.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a regex that checks if the entire line has only the date content and it is in a certain format.