besapi 3.2.6__tar.gz → 3.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: besapi
3
- Version: 3.2.6
3
+ Version: 3.3.3
4
4
  Summary: Library for working with the BigFix REST API
5
5
  Home-page: https://github.com/jgstew/besapi
6
6
  Author: Matt Hansen, James Stewart
@@ -29,7 +29,7 @@ import requests
29
29
  from lxml import etree, objectify
30
30
  from pkg_resources import resource_filename
31
31
 
32
- __version__ = "3.2.6"
32
+ __version__ = "3.3.3"
33
33
 
34
34
  besapi_logger = logging.getLogger("besapi")
35
35
 
@@ -353,7 +353,7 @@ class BESConnection:
353
353
  def session_relevance_string(self, relevance, **kwargs):
354
354
  """Get Session Relevance Results string"""
355
355
  rel_result_array = self.session_relevance_array(
356
- "(it as string) of " + relevance, **kwargs
356
+ "(it as string) of ( " + relevance + " )", **kwargs
357
357
  )
358
358
  return "\n".join(rel_result_array)
359
359
 
@@ -0,0 +1,166 @@
1
+ """This is a set of utility functions for use in multiple plugins"""
2
+
3
+ import argparse
4
+ import getpass
5
+ import logging
6
+ import logging.handlers
7
+ import ntpath
8
+ import os
9
+ import sys
10
+
11
+ import besapi
12
+
13
+
14
+ def get_invoke_folder(verbose=0):
15
+ """Get the folder the script was invoked from"""
16
+ # using logging here won't actually log it to the file:
17
+
18
+ if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
19
+ if verbose:
20
+ print("running in a PyInstaller bundle")
21
+ invoke_folder = os.path.abspath(os.path.dirname(sys.executable))
22
+ else:
23
+ if verbose:
24
+ print("running in a normal Python process")
25
+ invoke_folder = os.path.abspath(os.path.dirname(__file__))
26
+
27
+ if verbose:
28
+ print(f"invoke_folder = {invoke_folder}")
29
+
30
+ return invoke_folder
31
+
32
+
33
+ def get_invoke_file_name(verbose=0):
34
+ """Get the filename the script was invoked from"""
35
+ # using logging here won't actually log it to the file:
36
+
37
+ if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
38
+ if verbose:
39
+ print("running in a PyInstaller bundle")
40
+ invoke_file_path = sys.executable
41
+ else:
42
+ if verbose:
43
+ print("running in a normal Python process")
44
+ invoke_file_path = __file__
45
+
46
+ if verbose:
47
+ print(f"invoke_file_path = {invoke_file_path}")
48
+
49
+ # get just the file name, return without file extension:
50
+ return os.path.splitext(ntpath.basename(invoke_file_path))[0]
51
+
52
+
53
+ def setup_plugin_argparse(plugin_args_required=False):
54
+ """setup argparse for plugin use"""
55
+ arg_parser = argparse.ArgumentParser(
56
+ description="Provde command line arguments for REST URL, username, and password"
57
+ )
58
+ arg_parser.add_argument(
59
+ "-v",
60
+ "--verbose",
61
+ help="Set verbose output",
62
+ required=False,
63
+ action="count",
64
+ default=0,
65
+ )
66
+ arg_parser.add_argument(
67
+ "-c",
68
+ "--console",
69
+ help="log output to console",
70
+ required=False,
71
+ action="store_true",
72
+ )
73
+ arg_parser.add_argument(
74
+ "-besserver", "--besserver", help="Specify the BES URL", required=False
75
+ )
76
+ arg_parser.add_argument(
77
+ "-r", "--rest-url", help="Specify the REST URL", required=plugin_args_required
78
+ )
79
+ arg_parser.add_argument(
80
+ "-u", "--user", help="Specify the username", required=plugin_args_required
81
+ )
82
+ arg_parser.add_argument(
83
+ "-p", "--password", help="Specify the password", required=False
84
+ )
85
+
86
+ return arg_parser
87
+
88
+
89
+ def setup_plugin_logging(log_file_name="", verbose=0, console=True):
90
+ """setup logging for plugin use"""
91
+ # get folder the script was invoked from:
92
+ invoke_folder = get_invoke_folder(verbose)
93
+
94
+ if not log_file_name or log_file_name == "":
95
+ log_file_name = get_invoke_file_name() + ".log"
96
+
97
+ # set different log levels:
98
+ log_level = logging.WARNING
99
+ if verbose:
100
+ log_level = logging.INFO
101
+ if verbose > 1:
102
+ log_level = logging.DEBUG
103
+
104
+ # get path to put log file in:
105
+ log_filename = os.path.join(invoke_folder, log_file_name)
106
+
107
+ handlers = [
108
+ logging.handlers.RotatingFileHandler(
109
+ log_filename, maxBytes=5 * 1024 * 1024, backupCount=1
110
+ )
111
+ ]
112
+
113
+ # log output to console if arg provided:
114
+ if console:
115
+ handlers.append(logging.StreamHandler())
116
+
117
+ # setup logging:
118
+ logging.basicConfig(
119
+ encoding="utf-8",
120
+ level=log_level,
121
+ format="%(asctime)s %(levelname)s:%(message)s",
122
+ handlers=handlers,
123
+ )
124
+
125
+
126
+ def get_besapi_connection(args):
127
+ """get connection to besapi using either args or config file if args not provided"""
128
+
129
+ password = args.password
130
+
131
+ # if user was provided as arg but password was not:
132
+ if args.user and not password:
133
+ logging.warning("Password was not provided, provide REST API password.")
134
+ print("Password was not provided, provide REST API password:")
135
+ password = getpass.getpass()
136
+
137
+ # process args, setup connection:
138
+ rest_url = args.rest_url
139
+
140
+ # normalize url to https://HostOrIP:52311
141
+ if rest_url and rest_url.endswith("/api"):
142
+ rest_url = rest_url.replace("/api", "")
143
+
144
+ # attempt bigfix connection with provided args:
145
+ if args.user and password:
146
+ try:
147
+ bes_conn = besapi.besapi.BESConnection(args.user, password, rest_url)
148
+ # bes_conn.login()
149
+ except (
150
+ AttributeError,
151
+ ConnectionRefusedError,
152
+ besapi.besapi.requests.exceptions.ConnectionError,
153
+ ):
154
+ try:
155
+ # print(args.besserver)
156
+ bes_conn = besapi.besapi.BESConnection(
157
+ args.user, password, args.besserver
158
+ )
159
+ # handle case where args.besserver is None
160
+ # AttributeError: 'NoneType' object has no attribute 'startswith'
161
+ except AttributeError:
162
+ bes_conn = besapi.besapi.get_bes_conn_using_config_file()
163
+ else:
164
+ bes_conn = besapi.besapi.get_bes_conn_using_config_file()
165
+
166
+ return bes_conn
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: besapi
3
- Version: 3.2.6
3
+ Version: 3.3.3
4
4
  Summary: Library for working with the BigFix REST API
5
5
  Home-page: https://github.com/jgstew/besapi
6
6
  Author: Matt Hansen, James Stewart
@@ -7,6 +7,7 @@ setup.py
7
7
  src/besapi/__init__.py
8
8
  src/besapi/__main__.py
9
9
  src/besapi/besapi.py
10
+ src/besapi/plugin_utilities.py
10
11
  src/besapi.egg-info/PKG-INFO
11
12
  src/besapi.egg-info/SOURCES.txt
12
13
  src/besapi.egg-info/dependency_links.txt
@@ -25,6 +25,7 @@ if not args.test_pip:
25
25
  sys.path.reverse()
26
26
 
27
27
  import besapi
28
+ import besapi.plugin_utilities
28
29
 
29
30
  print("besapi version: " + str(besapi.besapi.__version__))
30
31
 
@@ -151,4 +152,13 @@ if bigfix_cli.bes_conn:
151
152
  bes_conn = besapi.besapi.get_bes_conn_using_config_file()
152
153
  print("login succeeded:", bes_conn.login())
153
154
 
155
+ # test plugin_utilities:
156
+ print(besapi.plugin_utilities.get_invoke_folder())
157
+ print(besapi.plugin_utilities.get_invoke_file_name())
158
+ # the following doesn't seem to work in python 3.7:
159
+ # besapi.plugin_utilities.setup_plugin_logging(console=True)
160
+ parser = besapi.plugin_utilities.setup_plugin_argparse(plugin_args_required=False)
161
+ # allow unknown args to be parsed instead of throwing an error:
162
+ args, _unknown = parser.parse_known_args()
163
+
154
164
  sys.exit(0)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes