edq-utils 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl

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.

Potentially problematic release.


This version of edq-utils might be problematic. Click here for more details.

edq/__init__.py CHANGED
@@ -2,4 +2,4 @@
2
2
  General Python tools used by several EduLinq projects.
3
3
  """
4
4
 
5
- __version__ = '0.1.3'
5
+ __version__ = '0.1.5'
edq/testing/cli.py CHANGED
@@ -214,6 +214,22 @@ class CLITestInfo:
214
214
 
215
215
  return CLITestInfo(test_name, base_dir, data_dir, temp_dir, **options)
216
216
 
217
+ @typing.runtime_checkable
218
+ class TestMethodWrapperFunction(typing.Protocol):
219
+ """
220
+ A function that can be used to wrap/modify a CLI test method before it is attached to the test class.
221
+ """
222
+
223
+ def __call__(self,
224
+ test_method: typing.Callable,
225
+ test_info_path: str,
226
+ ) -> typing.Callable:
227
+ """
228
+ Wrap and/or modify the CLI test method before it is attached to the test class.
229
+ See _get_test_method() for the input method.
230
+ The returned method will be used in-place of the input one.
231
+ """
232
+
217
233
  def read_test_file(path: str) -> typing.Tuple[typing.Dict[str, typing.Any], str]:
218
234
  """ Read a test case file and split the output into JSON data and text. """
219
235
 
@@ -257,7 +273,9 @@ def replace_path_pattern(text: str, key: str, target_dir: str) -> str:
257
273
  def _get_test_method(test_name: str, path: str, data_dir: str) -> typing.Callable:
258
274
  """ Get a test method that represents the test case at the given path. """
259
275
 
260
- def __method(self: edq.testing.unittest.BaseTest) -> None:
276
+ def __method(self: edq.testing.unittest.BaseTest,
277
+ reraise_exception_types: typing.Union[typing.Tuple[typing.Type], None] = None,
278
+ **kwargs: typing.Any) -> None:
261
279
  test_info = CLITestInfo.load_path(path, test_name, getattr(self, BASE_TEMP_DIR_ATTR), data_dir)
262
280
 
263
281
  # Allow the test class a chance to modify the test info before the test runs.
@@ -281,6 +299,9 @@ def _get_test_method(test_name: str, path: str, data_dir: str) -> typing.Callabl
281
299
  if (test_info.error):
282
300
  self.fail(f"No error was not raised when one was expected ('{str(test_info.expected_stdout)}').")
283
301
  except BaseException as ex:
302
+ if ((reraise_exception_types is not None) and isinstance(ex, reraise_exception_types)):
303
+ raise ex
304
+
284
305
  if (not test_info.error):
285
306
  raise ex
286
307
 
@@ -306,7 +327,8 @@ def _get_test_method(test_name: str, path: str, data_dir: str) -> typing.Callabl
306
327
 
307
328
  return __method
308
329
 
309
- def add_test_paths(target_class: type, data_dir: str, paths: typing.List[str]) -> None:
330
+ def add_test_paths(target_class: type, data_dir: str, paths: typing.List[str],
331
+ test_method_wrapper: typing.Union[TestMethodWrapperFunction, None] = None) -> None:
310
332
  """ Add tests from the given test files. """
311
333
 
312
334
  # Attach a temp directory to the testing class so all tests can share a common base temp dir.
@@ -321,12 +343,18 @@ def add_test_paths(target_class: type, data_dir: str, paths: typing.List[str]) -
321
343
  test_name = 'test_cli__' + basename
322
344
 
323
345
  try:
324
- setattr(target_class, test_name, _get_test_method(test_name, path, data_dir))
346
+ test_method = _get_test_method(test_name, path, data_dir)
325
347
  except Exception as ex:
326
348
  raise ValueError(f"Failed to parse test case '{path}'.") from ex
327
349
 
328
- def discover_test_cases(target_class: type, test_cases_dir: str, data_dir: str) -> None:
350
+ if (test_method_wrapper is not None):
351
+ test_method = test_method_wrapper(test_method, path)
352
+
353
+ setattr(target_class, test_name, test_method)
354
+
355
+ def discover_test_cases(target_class: type, test_cases_dir: str, data_dir: str,
356
+ test_method_wrapper: typing.Union[TestMethodWrapperFunction, None] = None) -> None:
329
357
  """ Look in the text cases directory for any test cases and add them as test methods to the test class. """
330
358
 
331
359
  paths = list(sorted(glob.glob(os.path.join(test_cases_dir, "**", "*.txt"), recursive = True)))
332
- add_test_paths(target_class, data_dir, paths)
360
+ add_test_paths(target_class, data_dir, paths, test_method_wrapper = test_method_wrapper)
edq/util/net.py CHANGED
@@ -626,6 +626,22 @@ class HTTPExchange(edq.util.json.DictConverter):
626
626
 
627
627
  return HTTPExchange(**data)
628
628
 
629
+ @typing.runtime_checkable
630
+ class HTTPExchangeComplete(typing.Protocol):
631
+ """
632
+ A function that can be called after a request has been made (and exchange constructed).
633
+ """
634
+
635
+ def __call__(self,
636
+ exchange: HTTPExchange
637
+ ) -> str:
638
+ """
639
+ Called after an HTTP exchange has been completed.
640
+ """
641
+
642
+ _make_request_exchange_complete_func: typing.Union[HTTPExchangeComplete, None] = None # pylint: disable=invalid-name
643
+ """ If not None, call this func after make_request() has created its HTTPExchange. """
644
+
629
645
  def find_open_port(
630
646
  start_port: int = DEFAULT_START_PORT, end_port: int = DEFAULT_END_PORT,
631
647
  wait_time: float = DEFAULT_PORT_SEARCH_WAIT_SEC) -> int:
@@ -671,6 +687,7 @@ def make_request(method: str, url: str,
671
687
  http_exchange_extension: str = DEFAULT_HTTP_EXCHANGE_EXTENSION,
672
688
  add_http_prefix: bool = True,
673
689
  additional_requests_options: typing.Union[typing.Dict[str, typing.Any], None] = None,
690
+ exchange_complete_func: typing.Union[HTTPExchangeComplete, None] = None,
674
691
  **kwargs: typing.Any) -> typing.Tuple[requests.Response, str]:
675
692
  """
676
693
  Make an HTTP request and return the response object and text body.
@@ -713,7 +730,7 @@ def make_request(method: str, url: str,
713
730
  else:
714
731
  options['data'] = data
715
732
 
716
- logging.debug("Making %s request: '%s'.", method, url)
733
+ logging.debug("Making %s request: '%s' (options = %s).", method, url, options)
717
734
  response = requests.request(method, url, **options)
718
735
 
719
736
  body = response.text
@@ -726,9 +743,11 @@ def make_request(method: str, url: str,
726
743
 
727
744
  response.raise_for_status()
728
745
 
729
- if (output_dir is not None):
746
+ exchange = None
747
+ if ((output_dir is not None) or (exchange_complete_func is not None) or (_make_request_exchange_complete_func is not None)):
730
748
  exchange = HTTPExchange.from_response(response, headers_to_skip = headers_to_skip, params_to_skip = params_to_skip)
731
749
 
750
+ if ((output_dir is not None) and (exchange is not None)):
732
751
  path = os.path.abspath(os.path.join(output_dir, *exchange.get_url().split('/')))
733
752
 
734
753
  query = urllib.parse.urlencode(exchange.parameters)
@@ -744,6 +763,12 @@ def make_request(method: str, url: str,
744
763
  edq.util.dirent.mkdir(os.path.dirname(path))
745
764
  edq.util.json.dump_path(exchange, path, indent = 4, sort_keys = False)
746
765
 
766
+ if ((exchange_complete_func is not None) and (exchange is not None)):
767
+ exchange_complete_func(exchange)
768
+
769
+ if ((_make_request_exchange_complete_func is not None) and (exchange is not None)):
770
+ _make_request_exchange_complete_func(exchange) # pylint: disable=not-callable
771
+
747
772
  return response, body
748
773
 
749
774
  def make_get(url: str, **kwargs: typing.Any) -> typing.Tuple[requests.Response, str]:
edq/util/parse.py ADDED
@@ -0,0 +1,33 @@
1
+ import typing
2
+
3
+ BOOL_TRUE_STRINGS: typing.Set[str] = {
4
+ 'true', 't',
5
+ 'yes', 'y',
6
+ '1',
7
+ }
8
+
9
+ BOOL_FALSE_STRINGS: typing.Set[str] = {
10
+ 'false', 'f',
11
+ 'no', 'n',
12
+ '0',
13
+ }
14
+
15
+ def boolean(raw_text: typing.Union[str, bool]) -> bool:
16
+ """
17
+ Parse a boolean from a string using common string representations for true/false.
18
+ This function assumes the entire string is the boolean (not just a part of it).
19
+ If the string is not true or false, then raise an exception.
20
+ """
21
+
22
+ if (isinstance(raw_text, bool)):
23
+ return raw_text
24
+
25
+ text = str(raw_text).lower().strip()
26
+
27
+ if (text in BOOL_TRUE_STRINGS):
28
+ return True
29
+
30
+ if (text in BOOL_FALSE_STRINGS):
31
+ return False
32
+
33
+ raise ValueError(f"Could not convert text to boolean: '{raw_text}'.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: edq-utils
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: Common utilities used by EduLinq Python projects.
5
5
  Author-email: Eriq Augustine <eriq@edulinq.org>
6
6
  License: MIT License
@@ -1,4 +1,4 @@
1
- edq/__init__.py,sha256=aOQ6CQ9r8ar_5BvwewsqizTA8G2aaTUJFFI3ePI5xqs,86
1
+ edq/__init__.py,sha256=JWuQig2oh9EbAaMaO0ZUA1GK6IWZ6OTe1Zv7BiGki-o,86
2
2
  edq/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  edq/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  edq/cli/version.py,sha256=SxarRVD_AVA-nD4pLVMe6ZjSJpMr7h_r3DgYYs42vjE,591
@@ -21,7 +21,7 @@ edq/procedure/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
21
  edq/procedure/verify_exchanges.py,sha256=Bv-wscTaSx5bbQfD6tZutCtG_pyHuFIBhk_RB5ZMJEQ,2765
22
22
  edq/testing/__init__.py,sha256=IKd3fPU_8d_jP19HxG-zKwxFwn7nqFGGtXOY5slY41c,32
23
23
  edq/testing/asserts.py,sha256=BxWTH9aQFwmzb0tf5hx3V4aeJzmiBOO-QajaMM6zklI,2523
24
- edq/testing/cli.py,sha256=Pzb1-6nrC6kYRBTI4R-pCfwDQ7reQUzH68xLn2ZSdME,12916
24
+ edq/testing/cli.py,sha256=pkms1Gy1VzXjRp3bT53TlnxtQUMuN-OntvRNlcpd960,14088
25
25
  edq/testing/cli_test.py,sha256=IqzdK1fEit_3HagSU-nNI4EjkoQp6-I88JGZ1_x_uQk,525
26
26
  edq/testing/httpserver.py,sha256=r2xSFkHTEzO92vW9XV5g43kC3vOcyQD-RVedXke1IZI,20311
27
27
  edq/testing/httpserver_test.py,sha256=tjBgBbKTHeqE1ugHyao4HpW7FNPTkBGpWK3vuqJgNQg,12123
@@ -68,14 +68,15 @@ edq/util/hash.py,sha256=yjXGBCZNvMm49RaPCi9Ygf5FLUpGUrbVU7v8l8hn5Hc,1369
68
68
  edq/util/hash_test.py,sha256=GzvCwgPdzsOn5o63lE8cUurAnj-4arHnYCUcctTnWMQ,2714
69
69
  edq/util/json.py,sha256=nl_cxrlP97RX1BFtys8IT_3IfO0-XvBDQBe6YdrblB4,5936
70
70
  edq/util/json_test.py,sha256=utUVRbw3z42ke4fpRVI294RrFHcMKms8khVYRkISNk4,8009
71
- edq/util/net.py,sha256=UNaFNEyVFxITATHS4-2IcFPkW29WDiIm22fiN7-wUOs,33403
71
+ edq/util/net.py,sha256=3heVBv2PR8SkiNwFK3sW8qrdXiafpD1_TB2lbfFC0KE,34527
72
+ edq/util/parse.py,sha256=zA-GGbY5WF-rfAcWFlnYjDXQaNkxhoyLJ8X81UceCLw,786
72
73
  edq/util/pyimport.py,sha256=26OIuCXELyqtwlooMqDEs4GJQrkrAgxnXNYTlqqtsBY,2852
73
74
  edq/util/pyimport_test.py,sha256=Xno0MIa3yMTfBfoTgjKCIMpr1ZShU6bvo9rBRdecXQU,4202
74
75
  edq/util/reflection.py,sha256=jPcW6h0fwSDYh04O5rUxlgoF7HK6fVQ2mq7DD9qPrEg,972
75
76
  edq/util/time.py,sha256=anoNM_KniARLombv2BnsoHuCzDqMKiDdIzV7RUe2ZOk,2648
76
77
  edq/util/time_test.py,sha256=iQZwzVTVQQ4TdXrLb9MUMCYlKrIe8qyF-hiC9YLTaMo,4610
77
- edq_utils-0.1.3.dist-info/licenses/LICENSE,sha256=MS4iYEl4rOxMoprZuc86iYVoyk4YgaVoMt7WmGvVF8w,1064
78
- edq_utils-0.1.3.dist-info/METADATA,sha256=FdzYP_tFRJAJdOBMLfwQPBtAh6jeDhxTQBpSoqjlmek,7535
79
- edq_utils-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
80
- edq_utils-0.1.3.dist-info/top_level.txt,sha256=znBHSj6tgXtcMKrUVtovLli5fIEJCb7d-BMxTLRK4zk,4
81
- edq_utils-0.1.3.dist-info/RECORD,,
78
+ edq_utils-0.1.5.dist-info/licenses/LICENSE,sha256=MS4iYEl4rOxMoprZuc86iYVoyk4YgaVoMt7WmGvVF8w,1064
79
+ edq_utils-0.1.5.dist-info/METADATA,sha256=hzjgzjrj2uICOExh_Ca7jHTMIMXmkImmvM-_1GY-69w,7535
80
+ edq_utils-0.1.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
81
+ edq_utils-0.1.5.dist-info/top_level.txt,sha256=znBHSj6tgXtcMKrUVtovLli5fIEJCb7d-BMxTLRK4zk,4
82
+ edq_utils-0.1.5.dist-info/RECORD,,