diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 272f339..2a63908 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ # https://github.com/pre-commit/pre-commit-hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.1.0 + rev: v4.3.0 hooks: - id: check-yaml - id: check-json @@ -27,7 +27,7 @@ repos: # - id: no-commit-to-branch # args: [--branch, main] - repo: https://github.com/adrienverge/yamllint.git - rev: v1.26.3 + rev: v1.28.0 hooks: - id: yamllint args: [-c=.yamllint.yaml] @@ -35,11 +35,7 @@ repos: rev: v5.10.1 hooks: - id: isort - - repo: https://gitlab.com/pycqa/flake8 - rev: 3.9.2 - hooks: - - id: flake8 - repo: https://github.com/psf/black - rev: 22.1.0 + rev: 22.10.0 hooks: - id: black diff --git a/src/besapi/besapi.py b/src/besapi/besapi.py index 15511cc..7585fc4 100644 --- a/src/besapi/besapi.py +++ b/src/besapi/besapi.py @@ -9,6 +9,7 @@ Library for communicating with the BES (BigFix) REST API. """ +import configparser import datetime import json import logging @@ -17,8 +18,6 @@ import site import string -# import urllib3.poolmanager - try: from urllib import parse except ImportError: @@ -113,6 +112,7 @@ def parse_bes_modtime(string_datetime): return datetime.datetime.strptime(string_datetime, "%a, %d %b %Y %H:%M:%S %z") +# import urllib3.poolmanager # # https://docs.python-requests.org/en/latest/user/advanced/#transport-adapters # class HTTPAdapterBiggerBlocksize(requests.adapters.HTTPAdapter): # """custom HTTPAdapter for requests to override blocksize @@ -150,6 +150,48 @@ def parse_bes_modtime(string_datetime): # ) +def get_bes_conn_using_config_file(conf_file=None): + """ + read connection values from config file + return besapi connection + """ + config_paths = [ + "/etc/besapi.conf", + os.path.expanduser("~/besapi.conf"), + os.path.expanduser("~/.besapi.conf"), + "besapi.conf", + ] + # if conf_file specified, then only use that: + if conf_file: + config_paths = [conf_file] + + configparser_instance = configparser.ConfigParser() + + found_config_files = configparser_instance.read(config_paths) + + if found_config_files and configparser_instance: + print("Attempting BESAPI Connection using config file:", found_config_files) + try: + BES_ROOT_SERVER = configparser_instance.get("besapi", "BES_ROOT_SERVER") + except BaseException: + BES_ROOT_SERVER = None + + try: + BES_USER_NAME = configparser_instance.get("besapi", "BES_USER_NAME") + except BaseException: + BES_USER_NAME = None + + try: + BES_PASSWORD = configparser_instance.get("besapi", "BES_PASSWORD") + except BaseException: + BES_PASSWORD = None + + if BES_ROOT_SERVER and BES_USER_NAME and BES_PASSWORD: + return BESConnection(BES_USER_NAME, BES_PASSWORD, BES_ROOT_SERVER) + + return None + + class BESConnection: """BigFix RESTAPI connection abstraction class""" diff --git a/tests/tests.py b/tests/tests.py index b4d3f21..a5e3def 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -132,3 +132,5 @@ class RequestResult(object): 'CMD /C python -m besapi ls clear ls conf "query number of bes computers" version error_count exit', check=True, ) + bes_conn = besapi.besapi.get_bes_conn_using_config_file() + print("login succeeded:", bes_conn.login())