FreeCAD
81 строка · 3.7 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2024 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24""" Classes and structures related to Addon sidecar information """
25from __future__ import annotations26
27from dataclasses import dataclass28from datetime import datetime29from typing import Optional30
31import addonmanager_freecad_interface as fci32
33
34def to_int_or_zero(inp: [str | int | None]):35try:36return int(inp)37except TypeError:38return 039
40
41def time_string_to_datetime(inp: str) -> Optional[datetime]:42try:43return datetime.fromisoformat(inp)44except ValueError:45try:46# Support for the trailing "Z" was added in Python 3.11 -- strip it and see if it works now47return datetime.fromisoformat(inp[:-1])48except ValueError:49fci.Console.PrintWarning(f"Unable to parse '{str}' as a Python datetime")50return None51
52
53@dataclass
54class AddonStats:55"""Statistics about an addon: not all stats apply to all addon types"""56
57last_update_time: datetime | None = None58date_created: datetime | None = None59stars: int = 060open_issues: int = 061forks: int = 062license: str = ""63page_views_last_month: int = 064
65@classmethod66def from_json(cls, json_dict: dict):67new_stats = AddonStats()68if "pushed_at" in json_dict:69new_stats.last_update_time = time_string_to_datetime(json_dict["pushed_at"])70if "created_at" in json_dict:71new_stats.date_created = time_string_to_datetime(json_dict["created_at"])72if "stargazers_count" in json_dict:73new_stats.stars = to_int_or_zero(json_dict["stargazers_count"])74if "forks_count" in json_dict:75new_stats.forks = to_int_or_zero(json_dict["forks_count"])76if "open_issues_count" in json_dict:77new_stats.open_issues = to_int_or_zero(json_dict["open_issues_count"])78if "license" in json_dict:79if json_dict["license"] != "NOASSERTION" and json_dict["license"] != "None":80new_stats.license = json_dict["license"] # Might be None or "NOASSERTION"81return new_stats82