Skip to content

Commit

Permalink
enhance plugin_utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Aug 27, 2024
1 parent 04fec57 commit bc7e78b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/besapi/plugin_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import ntpath
import os
import sys
import getpass

import besapi


def get_invoke_folder(verbose=0):
Expand Down Expand Up @@ -120,13 +123,44 @@ def setup_plugin_logging(log_file_name="", verbose=0, console=True):
)


# if __name__ == "__main__":
# print(get_invoke_folder())
# print(get_invoke_file_name())
# setup_plugin_logging(console=True)
# logging.error("test logging")
# parser = setup_plugin_argparse()
# # allow unknown args to be parsed instead of throwing an error:
# args, _unknown = parser.parse_known_args()
def get_besapi_connection(args):
"""get connection to besapi using either args or config file if args not provided"""

password = args.password

# if user was provided as arg but password was not:
if args.user and not password:
logging.warning("Password was not provided, provide REST API password.")
print("Password was not provided, provide REST API password:")
password = getpass.getpass()

# process args, setup connection:
rest_url = args.rest_url

# normalize url to https://HostOrIP:52311
if rest_url and rest_url.endswith("/api"):
rest_url = rest_url.replace("/api", "")

# attempt bigfix connection with provided args:
if args.user and password:
try:
bes_conn = besapi.besapi.BESConnection(args.user, password, rest_url)
# bes_conn.login()
except (
AttributeError,
ConnectionRefusedError,
besapi.besapi.requests.exceptions.ConnectionError,
):
try:
# print(args.besserver)
bes_conn = besapi.besapi.BESConnection(
args.user, password, args.besserver
)
# handle case where args.besserver is None
# AttributeError: 'NoneType' object has no attribute 'startswith'
except AttributeError:
bes_conn = besapi.besapi.get_bes_conn_using_config_file()
else:
bes_conn = besapi.besapi.get_bes_conn_using_config_file()

# logging.error(args)
return bes_conn
9 changes: 9 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
sys.path.reverse()

import besapi
import besapi.plugin_utilities

print("besapi version: " + str(besapi.besapi.__version__))

Expand Down Expand Up @@ -151,4 +152,12 @@ class RequestResult(object):
bes_conn = besapi.besapi.get_bes_conn_using_config_file()
print("login succeeded:", bes_conn.login())

# test plugin_utilities:
print(besapi.plugin_utilities.get_invoke_folder())
print(besapi.plugin_utilities.get_invoke_file_name())
besapi.plugin_utilities.setup_plugin_logging(console=True)
parser = besapi.plugin_utilities.setup_plugin_argparse(plugin_args_required=False)
# allow unknown args to be parsed instead of throwing an error:
args, _unknown = parser.parse_known_args()

sys.exit(0)

0 comments on commit bc7e78b

Please sign in to comment.