Skip to content
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

Open
wants to merge 35 commits into
base: shrivaths/changelog-announcement-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
de22a57
Add new preference and annoucementchecker class
shrivaths16 Sep 19, 2023
69ff1ed
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Sep 20, 2023
3fc0e75
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Sep 21, 2023
0db2904
Add new preferences and bulletin json converter
shrivaths16 Sep 28, 2023
7ab6d8b
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Sep 29, 2023
7639142
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Oct 2, 2023
babb86d
Merge branch 'shrivaths/changelog-announcement-1' of https://github.c…
shrivaths16 Oct 3, 2023
6eddcf0
Add some of the suggestions
shrivaths16 Oct 3, 2023
3f99b8b
Typo
shrivaths16 Oct 3, 2023
f8fb1f1
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Oct 12, 2023
d9947c3
Add test function for the announcement checker class
shrivaths16 Oct 16, 2023
4cb0c6a
Fix path bug
shrivaths16 Oct 16, 2023
3323115
Add boolean for new announcement
shrivaths16 Oct 17, 2023
fc856bf
Add fixtures, pass mainwindow states
shrivaths16 Oct 18, 2023
39ed0b6
Lint files
shrivaths16 Oct 19, 2023
dce4dab
Fix bulletin json creation
shrivaths16 Oct 30, 2023
9e1ebf7
Set latest data with title
shrivaths16 Oct 30, 2023
d35f029
Correct json directory
shrivaths16 Nov 2, 2023
21ea789
additional condition to announcementchecker class
shrivaths16 Nov 30, 2023
3cc4188
Better date comparison, more tests
shrivaths16 Dec 2, 2023
59f07ae
Add more conditions to announcementchecker class
shrivaths16 Dec 5, 2023
acdf130
Correct default value in prefs
shrivaths16 Dec 5, 2023
fbb1039
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Dec 14, 2023
63e329a
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Dec 20, 2023
cbb661d
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Dec 21, 2023
9c726af
Merge branch 'shrivaths/changelog-announcement-1' into shrivaths/chan…
shrivaths16 Jan 3, 2024
68cb64b
add branch to website workflow
shrivaths16 Jan 6, 2024
2c92712
add json file to the website
shrivaths16 Jan 8, 2024
303a2d2
generate json in the correct path
shrivaths16 Jan 9, 2024
b708717
store json as text file
shrivaths16 Jan 9, 2024
a679e61
keep_files is set true
shrivaths16 Jan 9, 2024
02757e6
add json to _static directory
shrivaths16 Jan 9, 2024
090e36f
Modify AnnouncementChecker Class with bulletin url
shrivaths16 Jan 11, 2024
aca6c7a
add date to announcement
shrivaths16 Jan 11, 2024
c218b10
add regex for date check
shrivaths16 Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/make_bulletin_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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"):
if 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()
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

else:
content += line + "\n"
# Append last section
bulletin_json.append({"title": title, "date": date, "content": content})

with open(output_json_file, "w", encoding="utf-8") as json_file:
json.dump(bulletin_json, json_file, ensure_ascii=False, indent=4)


if __name__ == "__main__":
generate_json_file()
7 changes: 6 additions & 1 deletion sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from sleap.gui.overlays.tracks import TrackListOverlay, TrackTrailOverlay
from sleap.gui.shortcuts import Shortcuts
from sleap.gui.state import GuiState
from sleap.gui.web import ReleaseChecker, ping_analytics
from sleap.gui.web import ReleaseChecker, AnnouncementChecker, ping_analytics
from sleap.gui.widgets.docks import (
InstancesDock,
SkeletonDock,
Expand Down Expand Up @@ -158,6 +158,8 @@ def __init__(
self.state["share usage data"] = prefs["share usage data"]
self.state["skeleton_preview_image"] = None
self.state["skeleton_description"] = "No skeleton loaded yet"
self.state["announcement last seen date"] = prefs["announcement last seen date"]
self.state["announcement"] = prefs["announcement"]
if no_usage_data:
self.state["share usage data"] = False
self.state["clipboard_track"] = None
Expand All @@ -168,6 +170,7 @@ def __init__(
self.state.connect("show non-visible nodes", self.plotFrame)

self.release_checker = ReleaseChecker()
self.announcement_checker = AnnouncementChecker(app=self)

if self.state["share usage data"]:
ping_analytics()
Expand Down Expand Up @@ -223,6 +226,8 @@ def closeEvent(self, event):
prefs["color predicted"] = self.state["color predicted"]
prefs["trail shade"] = self.state["trail_shade"]
prefs["share usage data"] = self.state["share usage data"]
prefs["announcement last seen date"] = self.state["announcement last seen date"]
prefs["announcement"] = self.state["announcement"]

# Save preferences.
prefs.save()
Expand Down
56 changes: 55 additions & 1 deletion sleap/gui/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
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


REPO_ID = "talmolab/sleap"
ANALYTICS_ENDPOINT = "https://analytics.sleap.ai/ping"
BASE_DIR = os.path.dirname(
os.path.abspath(os.path.join(__file__, os.path.pardir, os.pardir))
)
BULLETIN_JSON = os.path.join(BASE_DIR, "..", "docs", "bulletin.json")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BASE_DIR and BULLETIN_JSON global variables are correctly defined. However, consider using os.path.dirname(os.path.realpath(__file__)) instead of os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir)) for BASE_DIR to get the directory of the current script. This is a more reliable way to get the directory of the current script, especially when the script is called from a different directory.

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
BULLETIN_JSON = os.path.join(BASE_DIR, "..", "docs", "bulletin.json")



@attr.s(auto_attribs=True)
Expand Down Expand Up @@ -146,6 +152,54 @@ def get_release(self, version: str) -> Release:
)


@attr.s(auto_attribs=True)
class AnnouncementChecker:
"""Checker for new announcements on the bulletin page of sleap."""

app: "MainWindow"
shrivaths16 marked this conversation as resolved.
Show resolved Hide resolved
bulletin_json_path: str = BULLETIN_JSON
_previous_announcement_date: str = None
_latest_data: Optional[Dict[str, str]] = None

@property
def previous_announcement_date(self):
_previous_announcement_date = self.app.state["announcement last seen date"]
return _previous_announcement_date

def _read_bulletin_data(self):
"""Reads the bulletin data from the JSON file."""
try:
with open(self.bulletin_json_path, "r") as jsf:
data = json.load(jsf)
self._latest_data = data[0]
except FileNotFoundError:
self._latest_data = {}

@property
def new_announcement(self) -> bool:
self._read_bulletin_data()
if (
self._latest_data
and self._latest_data["date"] != self.previous_announcement_date
):
return True
return False

def get_latest_announcement(self) -> Optional[Tuple[str, str]]:
"""Return latest announcements on the releases page not seen by user."""
if self.new_announcement:
return (self._latest_data["date"], self._latest_data["content"])
return None

def update_announcement(self):
"""Update the last seen date of announcement in preferences."""
announcement = self.get_latest_announcement()
if announcement is None:
return
self.app.state["announcement last seen date"] = announcement[0]
self.app.state["announcement"] = announcement[1]


def get_analytics_data() -> Dict[str, Any]:
"""Gather data to be transmitted to analytics backend."""
import os
Expand Down
3 changes: 3 additions & 0 deletions sleap/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from sleap import util
from datetime import date


class Preferences(object):
Expand All @@ -28,6 +29,8 @@ class Preferences(object):
"node label size": 12,
"show non-visible nodes": True,
"share usage data": True,
"announcement last seen date": None,
"announcement": None,
}
_filename = "preferences.yaml"

Expand Down
42 changes: 41 additions & 1 deletion tests/gui/test_web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import pandas as pd
from sleap.gui.web import ReleaseChecker, Release, get_analytics_data, ping_analytics
from sleap.gui.web import (
ReleaseChecker,
Release,
AnnouncementChecker,
get_analytics_data,
ping_analytics,
)
import pytest
from sleap.gui.app import create_app
import json
import os


def test_release_from_json():
Expand Down Expand Up @@ -72,6 +81,37 @@ def test_release_checker():
assert checker.releases[1] != rls_test


def test_announcementchecker():

BULLETIN_JSON_PATH = "D:\\TalmoLab\\sleap\\tests\\data\\announcement_checker_bulletin\\test_bulletin.json"
shrivaths16 marked this conversation as resolved.
Show resolved Hide resolved
app = create_app()
app.state = {}
app.state["announcement last seen date"] = "10/10/2023"
shrivaths16 marked this conversation as resolved.
Show resolved Hide resolved
checker = AnnouncementChecker(app=app, bulletin_json_path=BULLETIN_JSON_PATH)

# Check if the announcement checker gets the correct date from the app
assert checker.previous_announcement_date == "10/10/2023"

# Create dummy JSON file to check
bulletin_data = [
{"title": "title1", "date": "10/12/2023", "content": "New announcement"},
{"title": "title2", "date": "10/07/2023", "content": "Old Announcment"},
]
with open(BULLETIN_JSON_PATH, "w") as test_file:
json.dump(bulletin_data, test_file)

# Check if latest announcement is fetched
announcement = checker.get_latest_announcement()
assert announcement == ("10/12/2023", "New announcement")

checker.update_announcement()
assert app.state["announcement last seen date"] == "10/12/2023"
assert app.state["announcement"] == "New announcement"

# Delete the JSON file
os.remove(BULLETIN_JSON_PATH)


This comment was marked as off-topic.

def test_get_analytics_data():
analytics_data = get_analytics_data()
assert "platform" in analytics_data
Loading