Changes towards splitting up the library and (maybe?) publishing on PyPi.
[python_utils.git] / config.py
index a98db575661621e3e7baf5c285cb71337f9d0d52..4d885149529901aedfa884ab602d3e1c3c096f17 100644 (file)
--- a/config.py
+++ b/config.py
@@ -90,8 +90,6 @@ import re
 import sys
 from typing import Any, Dict, List, Optional, Tuple
 
-import scott_secrets
-
 # This module is commonly used by others in here and should avoid
 # taking any unnecessary dependencies back on them.
 
@@ -330,6 +328,46 @@ class Config:
             env = env[1:]
         return var, env, chunks
 
+    @staticmethod
+    def _to_bool(in_str: str) -> bool:
+        """
+        Args:
+            in_str: the string to convert to boolean
+
+        Returns:
+            A boolean equivalent of the original string based on its contents.
+            All conversion is case insensitive.  A positive boolean (True) is
+            returned if the string value is any of the following:
+
+            * "true"
+            * "t"
+            * "1"
+            * "yes"
+            * "y"
+            * "on"
+
+            Otherwise False is returned.
+
+        >>> to_bool('True')
+        True
+
+        >>> to_bool('1')
+        True
+
+        >>> to_bool('yes')
+        True
+
+        >>> to_bool('no')
+        False
+
+        >>> to_bool('huh?')
+        False
+
+        >>> to_bool('on')
+        True
+        """
+        return in_str.lower() in ("true", "1", "yes", "y", "t", "on")
+
     def _augment_sys_argv_from_environment_variables(self):
         """Internal.  Look at the system environment for variables that match
         commandline arg names.  This is done via some munging such that:
@@ -366,9 +404,7 @@ class Config:
                                 self.saved_messages.append(
                                     f'Initialized from environment: {var} = {value}'
                                 )
-                                from string_utils import to_bool
-
-                                if len(chunks) == 1 and to_bool(value):
+                                if len(chunks) == 1 and Config._to_bool(value):
                                     sys.argv.append(var)
                                 elif len(chunks) > 1:
                                     sys.argv.append(var)
@@ -421,8 +457,11 @@ class Config:
             if loadfile[:3] == 'zk:':
                 from kazoo.client import KazooClient
 
+                import scott_secrets
+
                 try:
                     if self.zk is None:
+
                         self.zk = KazooClient(
                             hosts=scott_secrets.ZOOKEEPER_NODES,
                             use_ssl=True,
@@ -545,6 +584,8 @@ class Config:
                     if not self.zk:
                         from kazoo.client import KazooClient
 
+                        import scott_secrets
+
                         self.zk = KazooClient(
                             hosts=scott_secrets.ZOOKEEPER_NODES,
                             use_ssl=True,