Coverage for pyngrok/conf.py : 96.55%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import os
3from pyngrok.installer import get_ngrok_bin
5__author__ = "Alex Laird"
6__copyright__ = "Copyright 2021, Alex Laird"
7__version__ = "5.0.5"
9BIN_DIR = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), "bin"))
10DEFAULT_NGROK_PATH = os.path.join(BIN_DIR, get_ngrok_bin())
11DEFAULT_CONFIG_PATH = None
13DEFAULT_NGROK_CONFIG_PATH = os.path.join(os.path.expanduser("~"), ".ngrok2", "ngrok.yml")
15_default_pyngrok_config = None
18class PyngrokConfig:
19 """
20 An object containing ``pyngrok``'s configuration for interacting with the ``ngrok`` binary. All values are
21 optional when it is instantiated, and default values will be used for parameters not passed.
23 Use :func:`~pyngrok.conf.get_default` and :func:`~pyngrok.conf.set_default` to interact with the default
24 ``pyngrok_config``, or pass another instance of this object as the ``pyngrok_config`` keyword arg to most
25 methods in the :mod:`~pyngrok.ngrok` module to override the default.
27 .. code-block:: python
29 from pyngrok import conf, ngrok
31 # Here we update the entire default config
32 pyngrok_config = conf.PyngrokConfig(ngrok_path="/usr/local/bin/ngrok")
33 conf.set_default(pyngrok_config)
35 # Here we update just one variable in the default config
36 conf.get_default().ngrok_path = "/usr/local/bin/ngrok"
38 # Here we leave the default config as-is and pass an override
39 pyngrok_config = conf.PyngrokConfig(ngrok_path="/usr/local/bin/ngrok")
40 ngrok.connect(pyngrok_config=pyngrok_config)
42 :var ngrok_path: The path to the ``ngrok`` binary, defaults to the value in
43 `conf.DEFAULT_NGROK_PATH <index.html#config-file>`_
44 :vartype ngrok_path: str
45 :var config_path: The path to the ``ngrok`` config, defaults to ``None`` and ``ngrok`` manages it.
46 :vartype config_path: str
47 :var auth_token: An authtoken to pass to commands (overrides what is in the config).
48 :vartype auth_token: str
49 :var region: The region in which ``ngrok`` should start.
50 :vartype region: str
51 :var monitor_thread: Whether ``ngrok`` should continue to be monitored (for logs, etc.) after it startup
52 is complete.
53 :vartype monitor_thread: bool
54 :var log_event_callback: A callback that will be invoked each time ``ngrok`` emits a log. ``monitor_thread``
55 must be set to ``True`` or the function will stop being called after ``ngrok`` finishes starting.
56 :vartype log_event_callback: types.FunctionType
57 :var startup_timeout: The max number of seconds to wait for ``ngrok`` to start before timing out.
58 :vartype startup_timeout: int
59 :var max_logs: The max number of logs to store in :class:`~pyngrok.process.NgrokProcess`'s ``logs`` variable.
60 :vartype max_logs: int
61 :var request_timeout: The max timeout when making requests to ``ngrok``'s API.
62 :vartype request_timeout: float
63 :var start_new_session: Passed to :py:class:`subprocess.Popen` when launching ``ngrok``. (Python 3 and POSIX only)
64 :vartype start_new_session: bool
65 :var reconnect_session_retries: The max number of times to retry establishing a new session with ``ngrok`` if the
66 connection fails on startup.
67 :vartype reconnect_session_retries: int
68 """
70 def __init__(self,
71 ngrok_path=None,
72 config_path=None,
73 auth_token=None,
74 region=None,
75 monitor_thread=True,
76 log_event_callback=None,
77 startup_timeout=15,
78 max_logs=100,
79 request_timeout=4,
80 start_new_session=False,
81 reconnect_session_retries=0):
82 self.ngrok_path = DEFAULT_NGROK_PATH if ngrok_path is None else ngrok_path
83 self.config_path = DEFAULT_CONFIG_PATH if config_path is None else config_path
84 self.auth_token = auth_token
85 self.region = region
86 self.monitor_thread = monitor_thread
87 self.log_event_callback = log_event_callback
88 self.startup_timeout = startup_timeout
89 self.max_logs = max_logs
90 self.request_timeout = request_timeout
91 self.start_new_session = start_new_session
92 self.reconnect_session_retries = reconnect_session_retries
95def get_default():
96 """
97 Get the default config to be used with methods in the :mod:`~pyngrok.ngrok` module. To override the
98 default individually, the ``pyngrok_config`` keyword arg can also be passed to most of these methods,
99 or set a new default config with :func:`~pyngrok.conf.set_default`.
101 :return: The default ``pyngrok_config``.
102 :rtype: PyngrokConfig
103 """
104 if _default_pyngrok_config is None:
105 set_default(PyngrokConfig())
107 return _default_pyngrok_config
110def set_default(pyngrok_config):
111 """
112 Set a new default config to be used with methods in the :mod:`~pyngrok.ngrok` module. To override the
113 default individually, the ``pyngrok_config`` keyword arg can also be passed to most of these methods.
115 :param pyngrok_config: The new ``pyngrok_config`` to be used by default.
116 :type pyngrok_config: PyngrokConfig
117 """
118 global _default_pyngrok_config
120 _default_pyngrok_config = pyngrok_config