Skip to content

reprexlite.config

Attributes

Classes

ParsingMethod

Bases: str, Enum

Methods for parsing input strings.

Parameters:

Name Type Description Default
AUTO str

Automatically identify reprex-style or doctest-style input.

required
DECLARED str

Use configured values for parsing.

required
Source code in reprexlite/config.py
13
14
15
16
17
18
19
20
21
22
class ParsingMethod(str, Enum):
    """Methods for parsing input strings.

    Args:
        AUTO (str): Automatically identify reprex-style or doctest-style input.
        DECLARED (str): Use configured values for parsing.
    """

    AUTO = "auto"
    DECLARED = "declared"

ReprexConfig dataclass

Configuration dataclass for reprexlite. Used to configure input parsing and output formatting.

Source code in reprexlite/config.py
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@dataclass
class ReprexConfig:
    """Configuration dataclass for reprexlite. Used to configure input parsing and output
    formatting.
    """

    # Formatting
    venue: str = field(
        default="gh",
        metadata={
            "help": (
                "Key to identify the output venue that the reprex will be shared in. Used to "
                'select an appropriate formatter. See "Venues Formatting" documentation for '
                "formats included with reprexlite."
            )
        },
    )
    advertise: Optional[bool] = field(
        default=None,
        metadata={
            "help": (
                "Whether to include a footer that credits reprexlite. If unspecified, will depend "
                "on specified venue formatter's default."
            )
        },
    )
    session_info: bool = field(
        default=False,
        metadata={
            "help": (
                "Include details about session and environment that the reprex was generated with."
            )
        },
    )
    style: bool = field(
        default=False,
        metadata={
            "help": "Whether to autoformat code with black. Requires black to be installed."
        },
    )
    prompt: str = field(
        default="",
        metadata={"help": "Prefix to use as primary prompt for code lines."},
    )
    continuation: str = field(
        default="",
        metadata={"help": "Prefix to use as secondary prompt for continued code lines."},
    )
    comment: str = field(
        default="#>",
        metadata={"help": "Prefix to use for results returned by expressions."},
    )
    keep_old_results: bool = field(
        default=False,
        metadata={
            "help": (
                "Whether to additionally include results of expressions detected in the original "
                "input when formatting the reprex output."
            )
        },
    )
    # Parsing
    parsing_method: str = field(
        default="auto",
        metadata={
            "help": (
                "Method for parsing input. 'auto' will automatically detect either default "
                "reprex-style input or standard doctest-style input. 'declared' will allow you to "
                "specify custom line prefixes. Values for 'prompt', 'continuation', and 'comment' "
                "will be used for both output formatting and input parsing, unless the associated "
                "'input_*' override settings are supplied."
            )
        },
    )
    input_prompt: Optional[str] = field(
        default=None,
        metadata={
            "help": (
                "Prefix to use as primary prompt for code lines when parsing input. Only used if "
                "'parsing_method' is 'declared'. If not set, 'prompt' is used for both input "
                "parsing and output formatting."
            )
        },
    )
    input_continuation: Optional[str] = field(
        default=None,
        metadata={
            "help": (
                "Prefix to use as secondary prompt for continued code lines when parsing input. "
                "Only used if 'parsing_method' is 'declared'. If not set, 'prompt' is used for "
                "both input parsing and output formatting."
            )
        },
    )
    input_comment: Optional[str] = field(
        default=None,
        metadata={
            "help": (
                "Prefix to use for results returned by expressions when parsing input. Only used "
                "if 'parsing_method' is 'declared'. If not set, 'prompt' is used for both input "
                "parsing and output formatting."
            )
        },
    )

    def __post_init__(self):
        # Validate venue
        if self.venue not in formatter_registry:
            raise InvalidVenueError(
                f"{self.venue} is not a valid value for parsing method."
                f"Valid values are: {list(formatter_registry.keys())}"
            )
        # Validate prompt and continuation prefixes
        if len(self.prompt) != len(self.continuation):
            raise PromptLengthMismatchError(
                f"Primary prompt ('{self.prompt}') and continuation prompt "
                f"('{self.continuation}') must be equal lengths."
            )
        # Validate parsing method
        try:
            ParsingMethod(self.parsing_method)
        except ValueError:
            raise InvalidParsingMethodError(
                f"{self.parsing_method} is not a valid value for parsing method."
                f"Valid values are: {[pm.value for pm in ParsingMethod]}"
            )

    @property
    def resolved_input_prompt(self):
        if self.input_prompt is not None:
            return self.input_prompt
        return self.prompt

    @property
    def resolved_input_continuation(self):
        if self.input_continuation is not None:
            return self.input_continuation
        return self.continuation

    @property
    def resolved_input_comment(self):
        if self.input_comment is not None:
            return self.input_comment
        return self.comment

    @classmethod
    def get_help(cls, field_name: str):
        return cls.__dataclass_fields__[field_name].metadata["help"]