tyrannosaurus.parser.LiteralParser.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nop 10
dl 0
loc 23
rs 9.376
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""
2
Parsing of various files.
3
4
Original source: https://github.com/dmyersturnbull/tyrannosaurus
5
Copyright 2020–2021 Douglas Myers-Turnbull
6
Licensed under the Apache License, Version 2.0 (the "License");
7
you may not use this file except in compliance with the License.
8
You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0
9
"""
10
from typing import Any, Optional, Sequence, Union
11
12
from tyrannosaurus import TyrannoInfo
13
from tyrannosaurus.enums import DevStatus, License
14
15
16
class LiteralParser:
17
    def __init__(
18
        self,
19
        project: str,
20
        user: Optional[str],
21
        authors: Optional[Sequence[str]],
22
        description: str,
23
        keywords: Sequence[str],
24
        version: str,
25
        status: DevStatus,
26
        license_name: Union[str, License],
27
        tyranno_vr: str,
28
    ):
29
        self.project = project.lower()
30
        # TODO doing this in two places
31
        self.pkg = project.replace("_", "").replace("-", "").replace(".", "").lower()
32
        self.user = user
33
        self.authors = authors
34
        self.description = description
35
        self.keywords = keywords
36
        self.version = version
37
        self.status = status
38
        self.license = License.of(license_name)
39
        self.tyranno_vr = tyranno_vr
40
41
    def parse(self, s: str) -> str:
42
        today, now, now_utc = TyrannoInfo.today, TyrannoInfo.now, TyrannoInfo.now_utc
43
        timestamp = TyrannoInfo.pretty_timestamp_with_offset
44
        utc_stamp = TyrannoInfo.pretty_timestamp_utc
45
        reps = {
46
            "today": str(today),
47
            "today.str": today.strftime("%Y-%m-%d"),
48
            "today.year": str(today.year),
49
            "today.month": str(today.month),
50
            "today.Month": today.strftime("%B"),
51
            "today.day": str(today.day),
52
            "now": timestamp,
53
            "now.utc": utc_stamp,
54
            "now.iso": now.replace(microsecond=0).isoformat(),
55
            "now.utciso": now_utc.replace(microsecond=0).isoformat(),
56
            "now.hour": str(now.hour),
57
            "now.minute": str(now.minute),
58
            "now.second": str(now.second),
59
            "project": self.project.lower(),
60
            "Project": self.project.capitalize(),
61
            "PROJECT": self.project.upper(),
62
            "pkg": self.pkg,
63
            "Pkg": self.pkg.title(),
64
            "user": "<<TODO:user>>" if self.user is None else self.user,
65
            "authors": self._pretty(self.authors),
66
            "authors.list": self._list(self.authors),
67
            "version": self.version,
68
            "status.Name": self.status.name.capitalize(),
69
            "status.name": self.status.name,
70
            "status.pypi": self.status.pypi,
71
            "status.dunder": self.status.dunder,
72
            "status.Description": self.status.description.capitalize(),
73
            "status.Description.": self._sentence(self.status.description),
74
            "status.description": self.status.description,
75
            "Description": self.description.capitalize(),
76
            "description": self.description,
77
            "Description.": self._sentence(self.description),
78
            "keywords": self._pretty(self.keywords),
79
            "keywords.list": self._list(self.keywords),
80
            "license": self.license.name,
81
            "license.name": self.license.full_name,
82
            "license.spdx": self.license.spdx,
83
            "license.official": self.license.spdx,
84
            "license.family": self.license.family,
85
            "license.header": self.download_license_template(header=True),
86
            "license.full": self.download_license_template(header=False),
87
            "license.url": self.license.url,
88
            "tyranno.version": self.tyranno_vr,
89
        }
90
        for k, v in reps.items():
91
            s = s.replace("$${" + k + "}", v)
92
        return s
93
94
    def download_license_template(self, header: bool) -> str:
95
        text = self.license.download_header() if header else self.license.download_license()
96
        return (
97
            text.replace("{{ organization }}", self.project + " authors")
98
            .replace("{{ year }}", str(TyrannoInfo.today.year))
99
            .replace("{{ project }}", self.project)
100
        )
101
102
    def _sentence(self, v: str) -> str:
103
        return v.capitalize().strip(".") + "."
104
105
    def _pretty(self, v: Optional[Sequence[Any]]) -> str:
106
        if v is None:
107
            v = []
108
        return ", ".join(['"' + str(k) + '"' for k in v])
109
110
    def _list(self, v: Optional[Sequence[Any]]) -> str:
111
        if v is None:
112
            v = []
113
        return "[" + ", ".join(['"' + str(k) + '"' for k in v]) + "]"
114
115
116
__all__ = ["LiteralParser"]
117