Skip to content

Commit

Permalink
Add some of the suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
shrivaths16 committed Oct 3, 2023
1 parent babb86d commit 6eddcf0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
12 changes: 10 additions & 2 deletions docs/make_bulletin_json.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import json
import os

# Set the file paths
input_md_file = 'D:\TalmoLab\sleap\docs\bulletin.md'
output_json_file = 'D:\TalmoLab\sleap\docs\\bulletin.json'
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('_'):
Expand Down
4 changes: 3 additions & 1 deletion sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def __init__(
self.state.connect("show non-visible nodes", self.plotFrame)

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

if self.state["share usage data"]:
ping_analytics()
Expand Down
42 changes: 17 additions & 25 deletions sleap/gui/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import pandas as pd
import requests
from typing import List, Dict, Any, Optional, Tuple
from sleap import prefs
import json
import os


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


@attr.s(auto_attribs=True)
Expand Down Expand Up @@ -153,43 +154,34 @@ def get_release(self, version: str) -> Release:
class AnnouncementChecker:
"""Checker for new announcements on the bulletin page of sleap."""

app: "MainWindow"
bulletin_json_path: str = BULLETIN_JSON
previous_announcement_date: str = prefs.prefs["announcement last seen date"]
previous_announcement_date: str = app.state["announcement last seen date"]
_latest_data: Optional[Dict[str, str]] = None

def check_for_announcement(self) -> bool:
"""Returns if new announcements are available."""
def _read_bulletin_data(self) -> Dict[str, str]:
"""Reads the bulletin data from the JSON file."""
try:
# Attempt to open the file in read mode
with open(self.bulletin_json_path, 'r', encoding='utf-8') as jsf:
# Load the JSON data into a Python data structure
data = json.load(jsf)
latest_data = data[0]

if latest_data['date'] != self.previous_announcement_date:
return True
return data[0]
except FileNotFoundError:
return False
return {}

def get_latest_announcement(self) -> Optional[Tuple[str, str]]:
"""Return latest announcements on the releases page not seen by user."""
success = self.check_for_announcement()
if success:
# Attempt to open the file in read mode
with open(self.bulletin_json_path, 'r', encoding='utf-8') as jsf:
# Load the JSON data into a Python data structure
data = json.load(jsf)
latest_data = data[0]

if latest_data['date'] != self.previous_announcement_date:
return (latest_data['date'], latest_data['content'])
self._latest_datalatest_data = self._read_bulletin_data()
if self._latest_data and self._latest_data['date'] != self.previous_announcement_date:
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 not None:
prefs.prefs["announcement last seen date"] = announcement[0]
prefs.prefs["announcement"] = announcement[1]
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]:
Expand Down

0 comments on commit 6eddcf0

Please sign in to comment.