• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cisagov / skeleton-python-library / 4166051491

pending completion
4166051491

push

github

GitHub
Merge pull request #117 from cisagov/improvement/add_github_actions_attributions

2 of 2 branches covered (100.0%)

Branch coverage included in aggregate %.

40 of 40 relevant lines covered (100.0%)

6.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/example/example.py
1
"""example is an example Python library and tool.
2

3
Divide one integer by another and log the result. Also log some information
4
from an environment variable and a package resource.
5

6
EXIT STATUS
7
    This utility exits with one of the following values:
8
    0   Calculation completed successfully.
9
    >0  An error occurred.
10

11
Usage:
12
  example [--log-level=LEVEL] <dividend> <divisor>
13
  example (-h | --help)
14

15
Options:
16
  -h --help              Show this message.
17
  --log-level=LEVEL      If specified, then the log level will be set to
18
                         the specified value.  Valid values are "debug", "info",
19
                         "warning", "error", and "critical". [default: info]
20
"""
21

22
# Standard Python Libraries
23
import logging
6✔
24
import os
6✔
25
import sys
6✔
26
from typing import Any, Dict
6✔
27

28
# Third-Party Libraries
29
import docopt
6✔
30
import pkg_resources
6✔
31
from schema import And, Schema, SchemaError, Use
6✔
32

33
from ._version import __version__
6✔
34

35
DEFAULT_ECHO_MESSAGE: str = "Hello World from the example default!"
6✔
36

37

38
def example_div(dividend: int, divisor: int) -> float:
6✔
39
    """Print some logging messages."""
40
    logging.debug("This is a debug message")
6✔
41
    logging.info("This is an info message")
6✔
42
    logging.warning("This is a warning message")
6✔
43
    logging.error("This is an error message")
6✔
44
    logging.critical("This is a critical message")
6✔
45
    return dividend / divisor
6✔
46

47

48
def main() -> None:
6✔
49
    """Set up logging and call the example function."""
50
    args: Dict[str, str] = docopt.docopt(__doc__, version=__version__)
6✔
51
    # Validate and convert arguments as needed
52
    schema: Schema = Schema(
6✔
53
        {
54
            "--log-level": And(
55
                str,
56
                Use(str.lower),
57
                lambda n: n in ("debug", "info", "warning", "error", "critical"),
58
                error="Possible values for --log-level are "
59
                + "debug, info, warning, error, and critical.",
60
            ),
61
            "<dividend>": Use(int, error="<dividend> must be an integer."),
62
            "<divisor>": And(
63
                Use(int),
64
                lambda n: n != 0,
65
                error="<divisor> must be an integer that is not 0.",
66
            ),
67
            str: object,  # Don't care about other keys, if any
68
        }
69
    )
70

71
    try:
6✔
72
        validated_args: Dict[str, Any] = schema.validate(args)
6✔
73
    except SchemaError as err:
6✔
74
        # Exit because one or more of the arguments were invalid
75
        print(err, file=sys.stderr)
6✔
76
        sys.exit(1)
6✔
77

78
    # Assign validated arguments to variables
79
    dividend: int = validated_args["<dividend>"]
6✔
80
    divisor: int = validated_args["<divisor>"]
6✔
81
    log_level: str = validated_args["--log-level"]
6✔
82

83
    # Set up logging
84
    logging.basicConfig(
6✔
85
        format="%(asctime)-15s %(levelname)s %(message)s", level=log_level.upper()
86
    )
87

88
    logging.info("%d / %d == %f", dividend, divisor, example_div(dividend, divisor))
6✔
89

90
    # Access some data from an environment variable
91
    message: str = os.getenv("ECHO_MESSAGE", DEFAULT_ECHO_MESSAGE)
6✔
92
    logging.info('ECHO_MESSAGE="%s"', message)
6✔
93

94
    # Access some data from our package data (see the setup.py)
95
    secret_message: str = (
6✔
96
        pkg_resources.resource_string("example", "data/secret.txt")
97
        .decode("utf-8")
98
        .strip()
99
    )
100
    logging.info('Secret="%s"', secret_message)
6✔
101

102
    # Stop logging and clean up
103
    logging.shutdown()
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc