FreeCAD

Форк
0
/
AddonStats.py 
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 """
25
from __future__ import annotations
26

27
from dataclasses import dataclass
28
from datetime import datetime
29
from typing import Optional
30

31
import addonmanager_freecad_interface as fci
32

33

34
def to_int_or_zero(inp: [str | int | None]):
35
    try:
36
        return int(inp)
37
    except TypeError:
38
        return 0
39

40

41
def time_string_to_datetime(inp: str) -> Optional[datetime]:
42
    try:
43
        return datetime.fromisoformat(inp)
44
    except ValueError:
45
        try:
46
            # Support for the trailing "Z" was added in Python 3.11 -- strip it and see if it works now
47
            return datetime.fromisoformat(inp[:-1])
48
        except ValueError:
49
            fci.Console.PrintWarning(f"Unable to parse '{str}' as a Python datetime")
50
            return None
51

52

53
@dataclass
54
class AddonStats:
55
    """Statistics about an addon: not all stats apply to all addon types"""
56

57
    last_update_time: datetime | None = None
58
    date_created: datetime | None = None
59
    stars: int = 0
60
    open_issues: int = 0
61
    forks: int = 0
62
    license: str = ""
63
    page_views_last_month: int = 0
64

65
    @classmethod
66
    def from_json(cls, json_dict: dict):
67
        new_stats = AddonStats()
68
        if "pushed_at" in json_dict:
69
            new_stats.last_update_time = time_string_to_datetime(json_dict["pushed_at"])
70
        if "created_at" in json_dict:
71
            new_stats.date_created = time_string_to_datetime(json_dict["created_at"])
72
        if "stargazers_count" in json_dict:
73
            new_stats.stars = to_int_or_zero(json_dict["stargazers_count"])
74
        if "forks_count" in json_dict:
75
            new_stats.forks = to_int_or_zero(json_dict["forks_count"])
76
        if "open_issues_count" in json_dict:
77
            new_stats.open_issues = to_int_or_zero(json_dict["open_issues_count"])
78
        if "license" in json_dict:
79
            if json_dict["license"] != "NOASSERTION" and json_dict["license"] != "None":
80
                new_stats.license = json_dict["license"]  # Might be None or "NOASSERTION"
81
        return new_stats
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.