File: config.py

package info (click to toggle)
python-jira 3.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 884 kB
  • sloc: python: 7,395; sh: 13; makefile: 7; xml: 4
file content (130 lines) | stat: -rw-r--r-- 3,856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
"""
This module allows people to keep their jira server credentials outside their script, in a configuration file that is not saved in the source control.

Also, this simplifies the scripts by not having to write the same initialization code for each script.

"""
import configparser
import logging
import os
import sys
from typing import Optional, Union

from jira.client import JIRA


def get_jira(
    profile: Optional[str] = None,
    url: str = "http://localhost:2990",
    username: str = "admin",
    password: str = "admin",
    appid=None,
    autofix=False,
    verify: Union[bool, str] = True,
):
    """Return a JIRA object by loading the connection details from the `config.ini` file.

    Args:
        profile (Optional[str]): The name of the section from config.ini file that stores server config url/username/password
        url (str): URL of the Jira server
        username (str): username to use for authentication
        password (str): password to use for authentication
        appid: appid
        autofix: autofix
        verify (Union[bool, str]): boolean indicating whether SSL certificates should be
            verified, or path to a CA_BUNDLE file or directory with certificates of
            trusted CAs.

    Returns:
        JIRA: an instance to a JIRA object.

    Raises:
        EnvironmentError

    Usage:

        >>> from jira.config import get_jira
        >>>
        >>> jira = get_jira(profile='jira')

    Also create a `config.ini` like this and put it in current directory, user home directory or PYTHONPATH.

    .. code-block:: none

        [jira]
        url=https://jira.atlassian.com
        # only the `url` is mandatory
        user=...
        pass=...
        appid=...
        verify=...

    """

    def findfile(path):
        """Find the file named path in the sys.path.

        Returns the full path name if found, None if not found
        """
        paths = [".", os.path.expanduser("~")]
        paths.extend(sys.path)
        for dirname in paths:
            possible = os.path.abspath(os.path.join(dirname, path))
            if os.path.isfile(possible):
                return possible
        return None

    if isinstance(verify, bool):
        verify = "yes" if verify else "no"
    else:
        verify = verify

    config = configparser.ConfigParser(
        defaults={
            "user": None,
            "pass": None,
            "appid": appid,
            "autofix": autofix,
            "verify": verify,
        },
        allow_no_value=True,
    )

    config_file = findfile("config.ini")
    if config_file:
        logging.debug(f"Found {config_file} config file")

    if not profile:
        if config_file:
            config.read(config_file)
            try:
                profile = config.get("general", "default-jira-profile")
            except configparser.NoOptionError:
                pass

    if profile:
        if config_file:
            config.read(config_file)
            url = config.get(profile, "url")
            username = config.get(profile, "user")
            password = config.get(profile, "pass")
            appid = config.get(profile, "appid")
            autofix = config.get(profile, "autofix")
            try:
                verify = config.getboolean(profile, "verify")
            except ValueError:
                verify = config.get(profile, "verify")
        else:
            raise OSError(
                "%s was not able to locate the config.ini file in current directory, user home directory or PYTHONPATH."
                % __name__
            )

    options = JIRA.DEFAULT_OPTIONS
    options["server"] = url
    options["autofix"] = autofix
    options["appid"] = appid
    options["verify"] = verify

    return JIRA(options=options, basic_auth=(username, password))
    # self.jira.config.debug = debug