-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcec0a4
commit 59aff7d
Showing
12 changed files
with
164 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +0,0 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class ChartDataError(Exception): | ||
""" | ||
Custom exception for errors when getting chart data. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class CustomChart(ABC): | ||
""" | ||
Represents a custom chart. | ||
""" | ||
|
||
def __init__(self, chart_id: str) -> None: | ||
""" | ||
Class constructor. | ||
Args: | ||
chart_id (str): The id of the chart. | ||
Raises: | ||
ValueError: If chart_id is None or empty. | ||
""" | ||
if not chart_id: | ||
raise ValueError("chart_id cannot be None or empty!") | ||
|
||
self.chart_id = chart_id | ||
|
||
def _get_request_json_object(self) -> dict | None: | ||
""" | ||
Gets the JSON object for the chart request. | ||
Returns: | ||
dict: A dictionary representing the chart, or None if the data is invalid or an exception occurs. | ||
Raises: | ||
ChartDataError: If there is an error getting the chart data. | ||
""" | ||
chart = {"chartId": self.chart_id} | ||
try: | ||
data = self.get_chart_data() | ||
if not data: # If the data is empty we don't send the chart. | ||
return None | ||
|
||
chart["data"] = data | ||
|
||
except Exception as e: | ||
raise ChartDataError( | ||
f"Failed to get data for custom chart with id {self.chart_id}" | ||
) from e | ||
|
||
return chart | ||
|
||
@abstractmethod | ||
def get_chart_data(self) -> dict | None: | ||
""" | ||
Gets the data for the chart. | ||
Returns: | ||
dict: A dictionary with the chart data. | ||
Raises: | ||
Exception: If there is an error getting the chart data. | ||
""" | ||
31 changes: 1 addition & 30 deletions
31
src/endstone_bstats/_charts/pie.py → src/endstone_bstats/_charts/advanced_pie.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
from endstone_bstats._errors import ChartDataError | ||
|
||
|
||
class CustomChart(ABC): | ||
""" | ||
Represents a custom chart. | ||
""" | ||
|
||
def __init__(self, chart_id: str) -> None: | ||
""" | ||
Class constructor. | ||
Args: | ||
chart_id (str): The id of the chart. | ||
Raises: | ||
ValueError: If chart_id is None or empty. | ||
""" | ||
if not chart_id: | ||
raise ValueError("chart_id cannot be None or empty!") | ||
|
||
self.chart_id = chart_id | ||
|
||
def _get_request_json_object(self) -> dict | None: | ||
""" | ||
Gets the JSON object for the chart request. | ||
Returns: | ||
dict: A dictionary representing the chart, or None if the data is invalid or an exception occurs. | ||
Raises: | ||
ChartDataError: If there is an error getting the chart data. | ||
""" | ||
chart = {"chartId": self.chart_id} | ||
try: | ||
data = self.get_chart_data() | ||
if not data: # If the data is empty we don't send the chart. | ||
return None | ||
|
||
chart["data"] = data | ||
|
||
except Exception as e: | ||
raise ChartDataError( | ||
f"Failed to get data for custom chart with id {self.chart_id}" | ||
) from e | ||
|
||
return chart | ||
|
||
@abstractmethod | ||
def get_chart_data(self) -> dict | None: | ||
""" | ||
Gets the data for the chart. | ||
Returns: | ||
dict: A dictionary with the chart data. | ||
Raises: | ||
Exception: If there is an error getting the chart data. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
from endstone_bstats._charts.custom_chart import CustomChart | ||
|
||
|
||
class SimplePie(CustomChart): | ||
def __init__(self, chart_id: str, get_value: Callable[[], str | None]) -> None: | ||
""" | ||
Class constructor. | ||
Args: | ||
chart_id (str): The id of the chart. | ||
get_value (Callable[[], str | None]): The callable which is used to request the chart data. | ||
""" | ||
|
||
super().__init__(chart_id) | ||
self.get_value = get_value | ||
|
||
def get_chart_data(self) -> dict | None: | ||
""" | ||
Gets the data for the simple pie chart. | ||
Returns: | ||
dict | None: A dictionary with the chart data. | ||
""" | ||
|
||
data = {} | ||
value = self.get_value() | ||
if not value: | ||
return None # skip the chart | ||
|
||
data["value"] = value | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from pathlib import Path | ||
import uuid | ||
from pathlib import Path | ||
|
||
import tomlkit | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class ChartDataError(Exception): | ||
""" | ||
Custom exception for errors when getting chart data. | ||
""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
|
||
from endstone_bstats import AdvancedPie | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import pytest | ||
|
||
from endstone_bstats import SimplePie | ||
|
||
|
||
|