Coverage for pyngrok/conf.py : 96.43%

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.1.0"
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 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 """
67 def __init__(self,
68 ngrok_path=None,
69 config_path=None,
70 auth_token=None,
71 region=None,
72 monitor_thread=True,
73 log_event_callback=None,
74 startup_timeout=15,
75 max_logs=100,
76 request_timeout=4,
77 start_new_session=False):
78 self.ngrok_path = DEFAULT_NGROK_PATH if ngrok_path is None else ngrok_path
79 self.config_path = DEFAULT_CONFIG_PATH if config_path is None else config_path
80 self.auth_token = auth_token
81 self.region = region
82 self.monitor_thread = monitor_thread
83 self.log_event_callback = log_event_callback
84 self.startup_timeout = startup_timeout
85 self.max_logs = max_logs
86 self.request_timeout = request_timeout
87 self.start_new_session = start_new_session
90def get_default():
91 """
92 Get the default config to be used with methods in the :mod:`~pyngrok.ngrok` module. To override the
93 default individually, the ``pyngrok_config`` keyword arg can also be passed to most of these methods,
94 or set a new default config with :func:`~pyngrok.conf.set_default`.
96 :return: The default ``pyngrok_config``.
97 :rtype: PyngrokConfig
98 """
99 if _default_pyngrok_config is None:
100 set_default(PyngrokConfig())
102 return _default_pyngrok_config
105def set_default(pyngrok_config):
106 """
107 Set a new default config to be used with methods in the :mod:`~pyngrok.ngrok` module. To override the
108 default individually, the ``pyngrok_config`` keyword arg can also be passed to most of these methods.
110 :param pyngrok_config: The new ``pyngrok_config`` to be used by default.
111 :type pyngrok_config: PyngrokConfig
112 """
113 global _default_pyngrok_config
115 _default_pyngrok_config = pyngrok_config