docs: regenerate pdoc HTML documentation for v6.5.0b1

This commit is contained in:
fluzzi32 committed 2026-11-17 19:16:11 -03:00
1 parent b3925fee07
commit 693531ed57
15 files changed
+4856 -393

No files matched your search

+124 -2
View File
@@ -86,6 +86,78 @@ el.replaceWith(d);
</details> </details>
<div class="desc"><p>Clear local variables on all frames in an exception's traceback chain.</p></div> <div class="desc"><p>Clear local variables on all frames in an exception's traceback chain.</p></div>
</dd> </dd>
<dt id="connpy.auth_utils.compile_active_context_patterns"><code class="name flex">
<span>def <span class="ident">compile_active_context_patterns</span></span>(<span>cfg_dict: Any, *, strict: bool = False) ‑> list[re.Pattern[str]]</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def compile_active_context_patterns(
cfg_dict: Any,
*,
strict: bool = False,
) -&gt; list[re.Pattern[str]]:
&#34;&#34;&#34;Compile active context regexes purely respecting cfg_dict[&#39;case&#39;].&#34;&#34;&#34;
patterns, _ = compile_active_context_patterns_with_status(cfg_dict, strict=strict)
return patterns</code></pre>
</details>
<div class="desc"><p>Compile active context regexes purely respecting cfg_dict['case'].</p></div>
</dd>
<dt id="connpy.auth_utils.compile_active_context_patterns_with_status"><code class="name flex">
<span>def <span class="ident">compile_active_context_patterns_with_status</span></span>(<span>cfg_dict: Any, *, strict: bool = False) ‑> tuple[list[re.Pattern[str]], bool]</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def compile_active_context_patterns_with_status(
cfg_dict: Any,
*,
strict: bool = False,
) -&gt; tuple[list[re.Pattern[str]], bool]:
&#34;&#34;&#34;Compile active context regexes purely, returning (patterns, had_error).&#34;&#34;&#34;
if is_default_all_context_cfg(cfg_dict):
return ([re.compile(&#34;.*&#34;)], False)
current_ctx = cfg_dict.get(&#34;current_context&#34;, &#34;all&#34;)
contexts = cfg_dict.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})
raw_patterns = contexts.get(current_ctx, [&#34;.*&#34;]) if isinstance(contexts, dict) else [&#34;.*&#34;]
flags = 0 if cfg_dict.get(&#34;case&#34;, False) else re.IGNORECASE
if not isinstance(raw_patterns, list):
if strict:
raise ValueError(&#34;Context regexes must be a list&#34;)
return ([], True)
had_error = False
if len(raw_patterns) &gt; MAX_CONTEXT_REGEXES:
if strict:
raise ValueError(f&#34;Context exceeds maximum of {MAX_CONTEXT_REGEXES} regexes&#34;)
had_error = True
raw_patterns = raw_patterns[:MAX_CONTEXT_REGEXES]
patterns: list[re.Pattern[str]] = []
for pat in raw_patterns:
if not isinstance(pat, str) or not pat or len(pat) &gt; MAX_CONTEXT_PATTERN_LEN:
if strict:
raise ValueError(
f&#34;Context pattern must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} chars&#34;
)
had_error = True
continue
try:
patterns.append(re.compile(pat, flags))
except re.error as exc:
if strict:
raise ValueError(f&#34;Invalid context regex {pat!r}: {exc}&#34;) from None
had_error = True
return (patterns, had_error)</code></pre>
</details>
<div class="desc"><p>Compile active context regexes purely, returning (patterns, had_error).</p></div>
</dd>
<dt id="connpy.auth_utils.extract_compose_profile_refs"><code class="name flex"> <dt id="connpy.auth_utils.extract_compose_profile_refs"><code class="name flex">
<span>def <span class="ident">extract_compose_profile_refs</span></span>(<span>expr: str) ‑> set[str]</span> <span>def <span class="ident">extract_compose_profile_refs</span></span>(<span>expr: str) ‑> set[str]</span>
</code></dt> </code></dt>
@@ -194,6 +266,28 @@ el.replaceWith(d);
</details> </details>
<div class="desc"><p>Return True if value is a string with the explicit compose: prefix.</p></div> <div class="desc"><p>Return True if value is a string with the explicit compose: prefix.</p></div>
</dd> </dd>
<dt id="connpy.auth_utils.is_default_all_context_cfg"><code class="name flex">
<span>def <span class="ident">is_default_all_context_cfg</span></span>(<span>cfg_dict: Any) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def is_default_all_context_cfg(cfg_dict: Any) -&gt; bool:
&#34;&#34;&#34;Return True when config uses the default unrestricted &#39;all&#39; context.&#34;&#34;&#34;
if not isinstance(cfg_dict, dict):
return True
current_ctx = cfg_dict.get(&#34;current_context&#34;, &#34;all&#34;)
if current_ctx != &#34;all&#34;:
return False
contexts = cfg_dict.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})
if not isinstance(contexts, dict):
return True
return contexts.get(&#34;all&#34;, [&#34;.*&#34;]) == [&#34;.*&#34;]</code></pre>
</details>
<div class="desc"><p>Return True when config uses the default unrestricted 'all' context.</p></div>
</dd>
<dt id="connpy.auth_utils.is_rsa_blob"><code class="name flex"> <dt id="connpy.auth_utils.is_rsa_blob"><code class="name flex">
<span>def <span class="ident">is_rsa_blob</span></span>(<span>value: Any) ‑> bool</span> <span>def <span class="ident">is_rsa_blob</span></span>(<span>value: Any) ‑> bool</span>
</code></dt> </code></dt>
@@ -281,6 +375,22 @@ el.replaceWith(d);
</details> </details>
<div class="desc"><p>Recursively mask sensitive node/profile credentials with '***' using a closed key list.</p></div> <div class="desc"><p>Recursively mask sensitive node/profile credentials with '***' using a closed key list.</p></div>
</dd> </dd>
<dt id="connpy.auth_utils.match_active_context_patterns"><code class="name flex">
<span>def <span class="ident">match_active_context_patterns</span></span>(<span>name: Any, patterns: Iterable[re.Pattern[str]]) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def match_active_context_patterns(name: Any, patterns: Iterable[re.Pattern[str]]) -&gt; bool:
&#34;&#34;&#34;Return True if non-empty string name matches at least one compiled context regex.&#34;&#34;&#34;
if not isinstance(name, str) or not name:
return False
return any(p.match(name) for p in patterns)</code></pre>
</details>
<div class="desc"><p>Return True if non-empty string name matches at least one compiled context regex.</p></div>
</dd>
<dt id="connpy.auth_utils.normalize_prompt_line"><code class="name flex"> <dt id="connpy.auth_utils.normalize_prompt_line"><code class="name flex">
<span>def <span class="ident">normalize_prompt_line</span></span>(<span>before_bytes: bytes, after_bytes: bytes) ‑> str</span> <span>def <span class="ident">normalize_prompt_line</span></span>(<span>before_bytes: bytes, after_bytes: bytes) ‑> str</span>
</code></dt> </code></dt>
@@ -628,10 +738,18 @@ el.replaceWith(d);
return [] return []
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)): if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
parsed = validate_json_depth_and_size(stripped) parsed = validate_json_depth_and_size(stripped)
else: elif stripped.startswith(&#34;compose:&#34;):
if stripped.startswith(&#34;compose:&#34;):
validate_compose_syntax(stripped) validate_compose_syntax(stripped)
return [stripped] return [stripped]
elif stripped.startswith(&#34;@&#34;) and not stripped.startswith(&#34;@@&#34;) and &#34;,&#34; in stripped:
parts = [p.strip() for p in stripped.split(&#34;,&#34;)]
if not parts or any(not _is_profile_ref(p) for p in parts):
raise ValueError(
&#34;Comma-separated fallback entries must be valid @profile references&#34;
)
parsed = parts
else:
return [stripped]
if isinstance(parsed, dict): if isinstance(parsed, dict):
items: list[Any] = [parsed] items: list[Any] = [parsed]
@@ -1439,13 +1557,17 @@ class RuntimeAuthCandidate:
<li><h3><a href="#header-functions">Functions</a></h3> <li><h3><a href="#header-functions">Functions</a></h3>
<ul class=""> <ul class="">
<li><code><a title="connpy.auth_utils.clear_exception_frames" href="#connpy.auth_utils.clear_exception_frames">clear_exception_frames</a></code></li> <li><code><a title="connpy.auth_utils.clear_exception_frames" href="#connpy.auth_utils.clear_exception_frames">clear_exception_frames</a></code></li>
<li><code><a title="connpy.auth_utils.compile_active_context_patterns" href="#connpy.auth_utils.compile_active_context_patterns">compile_active_context_patterns</a></code></li>
<li><code><a title="connpy.auth_utils.compile_active_context_patterns_with_status" href="#connpy.auth_utils.compile_active_context_patterns_with_status">compile_active_context_patterns_with_status</a></code></li>
<li><code><a title="connpy.auth_utils.extract_compose_profile_refs" href="#connpy.auth_utils.extract_compose_profile_refs">extract_compose_profile_refs</a></code></li> <li><code><a title="connpy.auth_utils.extract_compose_profile_refs" href="#connpy.auth_utils.extract_compose_profile_refs">extract_compose_profile_refs</a></code></li>
<li><code><a title="connpy.auth_utils.extract_profile_refs_from_value" href="#connpy.auth_utils.extract_profile_refs_from_value">extract_profile_refs_from_value</a></code></li> <li><code><a title="connpy.auth_utils.extract_profile_refs_from_value" href="#connpy.auth_utils.extract_profile_refs_from_value">extract_profile_refs_from_value</a></code></li>
<li><code><a title="connpy.auth_utils.has_insecure_hostkey_checking" href="#connpy.auth_utils.has_insecure_hostkey_checking">has_insecure_hostkey_checking</a></code></li> <li><code><a title="connpy.auth_utils.has_insecure_hostkey_checking" href="#connpy.auth_utils.has_insecure_hostkey_checking">has_insecure_hostkey_checking</a></code></li>
<li><code><a title="connpy.auth_utils.host_exact_re" href="#connpy.auth_utils.host_exact_re">host_exact_re</a></code></li> <li><code><a title="connpy.auth_utils.host_exact_re" href="#connpy.auth_utils.host_exact_re">host_exact_re</a></code></li>
<li><code><a title="connpy.auth_utils.is_compose_expression" href="#connpy.auth_utils.is_compose_expression">is_compose_expression</a></code></li> <li><code><a title="connpy.auth_utils.is_compose_expression" href="#connpy.auth_utils.is_compose_expression">is_compose_expression</a></code></li>
<li><code><a title="connpy.auth_utils.is_default_all_context_cfg" href="#connpy.auth_utils.is_default_all_context_cfg">is_default_all_context_cfg</a></code></li>
<li><code><a title="connpy.auth_utils.is_rsa_blob" href="#connpy.auth_utils.is_rsa_blob">is_rsa_blob</a></code></li> <li><code><a title="connpy.auth_utils.is_rsa_blob" href="#connpy.auth_utils.is_rsa_blob">is_rsa_blob</a></code></li>
<li><code><a title="connpy.auth_utils.mask_node_secrets" href="#connpy.auth_utils.mask_node_secrets">mask_node_secrets</a></code></li> <li><code><a title="connpy.auth_utils.mask_node_secrets" href="#connpy.auth_utils.mask_node_secrets">mask_node_secrets</a></code></li>
<li><code><a title="connpy.auth_utils.match_active_context_patterns" href="#connpy.auth_utils.match_active_context_patterns">match_active_context_patterns</a></code></li>
<li><code><a title="connpy.auth_utils.normalize_prompt_line" href="#connpy.auth_utils.normalize_prompt_line">normalize_prompt_line</a></code></li> <li><code><a title="connpy.auth_utils.normalize_prompt_line" href="#connpy.auth_utils.normalize_prompt_line">normalize_prompt_line</a></code></li>
<li><code><a title="connpy.auth_utils.parse_compose_expression" href="#connpy.auth_utils.parse_compose_expression">parse_compose_expression</a></code></li> <li><code><a title="connpy.auth_utils.parse_compose_expression" href="#connpy.auth_utils.parse_compose_expression">parse_compose_expression</a></code></li>
<li><code><a title="connpy.auth_utils.raise_sanitized_auth_error" href="#connpy.auth_utils.raise_sanitized_auth_error">raise_sanitized_auth_error</a></code></li> <li><code><a title="connpy.auth_utils.raise_sanitized_auth_error" href="#connpy.auth_utils.raise_sanitized_auth_error">raise_sanitized_auth_error</a></code></li>
+6 -6
View File
@@ -156,7 +156,7 @@ el.replaceWith(d);
fbq = [ fbq = [
inquirer.Text( inquirer.Text(
&#34;fallback&#34;, &#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation, validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;), default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
) )
@@ -228,7 +228,7 @@ el.replaceWith(d);
if edit.get(&#34;fallback&#34;): if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;) raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;) def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;))) questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else: else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;) profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
@@ -286,7 +286,7 @@ el.replaceWith(d);
fbq = [ fbq = [
inquirer.Text( inquirer.Text(
&#34;fallback&#34;, &#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation, validate=self.validators.fallback_validation,
) )
] ]
@@ -524,7 +524,7 @@ el.replaceWith(d);
fbq = [ fbq = [
inquirer.Text( inquirer.Text(
&#34;fallback&#34;, &#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation, validate=self.validators.fallback_validation,
) )
] ]
@@ -655,7 +655,7 @@ el.replaceWith(d);
fbq = [ fbq = [
inquirer.Text( inquirer.Text(
&#34;fallback&#34;, &#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation, validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;), default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
) )
@@ -737,7 +737,7 @@ el.replaceWith(d);
if edit.get(&#34;fallback&#34;): if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;) raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;) def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;))) questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else: else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;) profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
+2
View File
@@ -193,6 +193,7 @@ el.replaceWith(d);
uniques = self.app.services.nodes.explode_unique(args.data) uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;) raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;)
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
self.app.services.nodes.add_node(args.data, {}, is_folder=True) self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f&#34;{args.data} added successfully&#34;) printer.success(f&#34;{args.data} added successfully&#34;)
else: else:
@@ -364,6 +365,7 @@ def forms(self):
uniques = self.app.services.nodes.explode_unique(args.data) uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;) raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;)
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
self.app.services.nodes.add_node(args.data, {}, is_folder=True) self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f&#34;{args.data} added successfully&#34;) printer.success(f&#34;{args.data} added successfully&#34;)
else: else:
+128 -28
View File
@@ -163,9 +163,19 @@ el.replaceWith(d);
return True return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES: if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;) _raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and &#34;+&#34; not in stripped: if stripped.startswith(&#34;@&#34;) and not stripped.startswith(&#34;@@&#34;) and &#34;+&#34; not in stripped:
if stripped[1:] not in self.app.profiles: parts = [p.strip() for p in stripped.split(&#34;,&#34;)]
_raise_val_err(f&#34;Profile {stripped} don&#39;t exist&#34;) if any(not p for p in parts):
_raise_val_err(&#34;Fallback profile entry cannot be empty&#34;)
if len(parts) &gt; MAX_FALLBACK_ENTRIES:
_raise_val_err(f&#34;Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries&#34;)
for p in parts:
if not p.startswith(&#34;@&#34;) or not re.fullmatch(r&#34;[A-Za-z0-9_.$:/-]+&#34;, p[1:]):
_raise_val_err(
&#34;Comma-separated fallback entries must be valid @profile references&#34;
)
if p[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {p} don&#39;t exist&#34;)
return True return True
try: try:
if is_compose_expression(stripped): if is_compose_expression(stripped):
@@ -182,19 +192,33 @@ el.replaceWith(d);
_raise_val_err(str(exc)) _raise_val_err(str(exc))
return True return True
def _get_all_valid_nodes(self) -&gt; list[str]:
valid_nodes = list(getattr(self.app, &#34;nodes_list&#34;, []) or [])
if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_nodes&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
return valid_nodes
def _node_exists(self, target: str) -&gt; bool:
valid_nodes = self._get_all_valid_nodes()
if getattr(self.app, &#34;case&#34;, True):
return target in valid_nodes
return target.lower() in {n.lower() for n in valid_nodes}
def jumphost_validation(self, answers, current): def jumphost_validation(self, answers, current):
if current.startswith(&#34;@&#34;): if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(f&#34;Profile {current} don&#39;t exist&#34;)
elif current != &#34;&#34;: elif current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True return True
def profile_jumphost_validation(self, answers, current): def profile_jumphost_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True return True
def default_validation(self, answers, current): def default_validation(self, answers, current):
@@ -212,12 +236,46 @@ el.replaceWith(d);
return True return True
def bulk_folder_validation(self, answers, current): def bulk_folder_validation(self, answers, current):
if not self.app.case: case = getattr(self.app, &#34;case&#34;, False)
if not case:
current = current.lower() current = current.lower()
candidate = current candidate = current.split(&#34;/&#34;)[0] if &#34;/&#34; in current else current
if &#34;/&#34; in current:
candidate = current.split(&#34;/&#34;)[0] if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;context&#34;):
raw_folders = list(getattr(self.app, &#34;folders&#34;, []) or [])
if hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_folders&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
raw_folders = list(raw_res)
def _folder_exists_norm(folder_name: str) -&gt; bool:
if case:
return folder_name in raw_folders
return folder_name.lower() in {f.lower() for f in raw_folders}
if current != &#34;&#34; and not _folder_exists_norm(candidate):
_raise_val_err(f&#34;Location {current} don&#39;t exist&#34;)
ctx_svc = self.app.services.context
if ctx_svc is not None and not ctx_svc.is_default_all_context():
raw_ids = answers.get(&#34;ids&#34;, &#34;&#34;) if isinstance(answers, dict) else &#34;&#34;
for nid in str(raw_ids).split(&#34;,&#34;):
nid_stripped = nid.strip()
if not nid_stripped:
continue
nid_norm = nid_stripped if case else nid_stripped.lower()
full_id = f&#34;{nid_norm}{candidate}&#34; if candidate else nid_norm
if not ctx_svc.matches_active_context(full_id):
_raise_val_err(
f&#34;Node &#39;{full_id}&#39; does not match active context &#39;{ctx_svc.current_context}&#39;.&#34;
)
parent = full_id.partition(&#34;@&#34;)[2]
if parent and not _folder_exists_norm(f&#34;@{parent}&#34;):
_raise_val_err(f&#34;Location @{parent} don&#39;t exist&#34;)
return True
matches = list(filter(lambda k: k == candidate, self.app.folders)) matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != &#34;&#34; and len(matches) == 0: if current != &#34;&#34; and len(matches) == 0:
@@ -248,12 +306,46 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def bulk_folder_validation(self, answers, current): <pre><code class="python">def bulk_folder_validation(self, answers, current):
if not self.app.case: case = getattr(self.app, &#34;case&#34;, False)
if not case:
current = current.lower() current = current.lower()
candidate = current candidate = current.split(&#34;/&#34;)[0] if &#34;/&#34; in current else current
if &#34;/&#34; in current:
candidate = current.split(&#34;/&#34;)[0] if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;context&#34;):
raw_folders = list(getattr(self.app, &#34;folders&#34;, []) or [])
if hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_folders&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
raw_folders = list(raw_res)
def _folder_exists_norm(folder_name: str) -&gt; bool:
if case:
return folder_name in raw_folders
return folder_name.lower() in {f.lower() for f in raw_folders}
if current != &#34;&#34; and not _folder_exists_norm(candidate):
_raise_val_err(f&#34;Location {current} don&#39;t exist&#34;)
ctx_svc = self.app.services.context
if ctx_svc is not None and not ctx_svc.is_default_all_context():
raw_ids = answers.get(&#34;ids&#34;, &#34;&#34;) if isinstance(answers, dict) else &#34;&#34;
for nid in str(raw_ids).split(&#34;,&#34;):
nid_stripped = nid.strip()
if not nid_stripped:
continue
nid_norm = nid_stripped if case else nid_stripped.lower()
full_id = f&#34;{nid_norm}{candidate}&#34; if candidate else nid_norm
if not ctx_svc.matches_active_context(full_id):
_raise_val_err(
f&#34;Node &#39;{full_id}&#39; does not match active context &#39;{ctx_svc.current_context}&#39;.&#34;
)
parent = full_id.partition(&#34;@&#34;)[2]
if parent and not _folder_exists_norm(f&#34;@{parent}&#34;):
_raise_val_err(f&#34;Location @{parent} don&#39;t exist&#34;)
return True
matches = list(filter(lambda k: k == candidate, self.app.folders)) matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != &#34;&#34; and len(matches) == 0: if current != &#34;&#34; and len(matches) == 0:
@@ -334,9 +426,19 @@ el.replaceWith(d);
return True return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES: if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;) _raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and &#34;+&#34; not in stripped: if stripped.startswith(&#34;@&#34;) and not stripped.startswith(&#34;@@&#34;) and &#34;+&#34; not in stripped:
if stripped[1:] not in self.app.profiles: parts = [p.strip() for p in stripped.split(&#34;,&#34;)]
_raise_val_err(f&#34;Profile {stripped} don&#39;t exist&#34;) if any(not p for p in parts):
_raise_val_err(&#34;Fallback profile entry cannot be empty&#34;)
if len(parts) &gt; MAX_FALLBACK_ENTRIES:
_raise_val_err(f&#34;Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries&#34;)
for p in parts:
if not p.startswith(&#34;@&#34;) or not re.fullmatch(r&#34;[A-Za-z0-9_.$:/-]+&#34;, p[1:]):
_raise_val_err(
&#34;Comma-separated fallback entries must be valid @profile references&#34;
)
if p[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {p} don&#39;t exist&#34;)
return True return True
try: try:
if is_compose_expression(stripped): if is_compose_expression(stripped):
@@ -384,10 +486,9 @@ el.replaceWith(d);
<pre><code class="python">def jumphost_validation(self, answers, current): <pre><code class="python">def jumphost_validation(self, answers, current):
if current.startswith(&#34;@&#34;): if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(f&#34;Profile {current} don&#39;t exist&#34;)
elif current != &#34;&#34;: elif current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -448,9 +549,8 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def profile_jumphost_validation(self, answers, current): <pre><code class="python">def profile_jumphost_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
+9 -25
View File
@@ -110,7 +110,7 @@ el.replaceWith(d);
from connpy.grpc_layer.user_registry import UserRegistry from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider from connpy.services.provider import ServiceProvider
fallback_provider = ServiceProvider(config, mode=&#34;local&#34;) fallback_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
registry = UserRegistry(config.defaultdir) registry = UserRegistry(config.defaultdir)
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env # Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
@@ -163,9 +163,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer): <pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer):
def __init__(self, provider, registry=None, debug=False): def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -927,9 +925,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
</summary> </summary>
<pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer): <pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1015,9 +1011,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer): <pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1186,9 +1180,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer): <pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1374,9 +1366,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
</summary> </summary>
<pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): <pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
def __init__(self, provider, registry=None, debug=False): def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -2041,9 +2031,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer): <pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -2148,9 +2136,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer): <pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -2451,9 +2437,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer): <pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -113,7 +113,7 @@ el.replaceWith(d);
try: try:
# Attempt a fresh configuration load # Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config) config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode=&#34;local&#34;) new_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
# Successfully loaded, clean up the old provider # Successfully loaded, clean up the old provider
if old_provider: if old_provider:
@@ -221,7 +221,7 @@ el.replaceWith(d);
try: try:
# Attempt a fresh configuration load # Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config) config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode=&#34;local&#34;) new_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
# Successfully loaded, clean up the old provider # Successfully loaded, clean up the old provider
if old_provider: if old_provider:
+22 -5
View File
@@ -752,6 +752,7 @@ class configfile:
def __init__(self, conf = None, key = None, shared_config = None): def __init__(self, conf = None, key = None, shared_config = None):
self._mutation_lock = threading.RLock() self._mutation_lock = threading.RLock()
self._cache_dirty = False self._cache_dirty = False
self._enforce_context = True
self._shared_config = shared_config self._shared_config = shared_config
&#39;&#39;&#39; &#39;&#39;&#39;
@@ -1098,8 +1099,9 @@ class configfile:
self._fsync_dir_nonfatal(conf) self._fsync_dir_nonfatal(conf)
try: try:
self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(newconfig, f)) self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(newconfig, f))
self._generate_nodes_cache_unlocked(_raise_on_error=True)
self._cache_dirty = False self._cache_dirty = False
if self._is_context_filtering_enabled_unlocked():
self._generate_nodes_cache_unlocked(_raise_on_error=True)
except (OSError, TypeError, ValueError): except (OSError, TypeError, ValueError):
self._cache_dirty = True self._cache_dirty = True
return 0, None return 0, None
@@ -1183,18 +1185,33 @@ class configfile:
self._cache_dirty = True self._cache_dirty = True
return False return False
def _is_context_filtering_enabled_unlocked(self) -&gt; bool:
if not getattr(self, &#34;_enforce_context&#34;, True):
return False
return not (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
def _filter_by_active_context_unlocked(self, items: list[str]) -&gt; tuple[list[str], bool]:
if not self._is_context_filtering_enabled_unlocked() or is_default_all_context_cfg(
self.config
):
return (list(items), False)
patterns, had_err = compile_active_context_patterns_with_status(self.config)
return ([i for i in items if match_active_context_patterns(i, patterns)], had_err)
def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False): def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False):
try: try:
if nodes is None: raw_nodes = self._getallnodes_unlocked() if nodes is None else list(nodes)
nodes = self._getallnodes_unlocked() raw_folders = self._getallfolders_unlocked() if folders is None else list(folders)
if folders is None: nodes, err_n = self._filter_by_active_context_unlocked(raw_nodes)
folders = self._getallfolders_unlocked() folders, err_f = self._filter_by_active_context_unlocked(raw_folders)
if profiles is None: if profiles is None:
profiles = list(self.profiles.keys()) profiles = list(self.profiles.keys())
self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write(&#34;\n&#34;.join(nodes))) self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write(&#34;\n&#34;.join(nodes)))
self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write(&#34;\n&#34;.join(folders))) self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write(&#34;\n&#34;.join(folders)))
self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write(&#34;\n&#34;.join(profiles))) self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write(&#34;\n&#34;.join(profiles)))
if err_n or err_f:
self._cache_dirty = True
except (OSError, TypeError, ValueError): except (OSError, TypeError, ValueError):
if _raise_on_error: if _raise_on_error:
raise raise
+144 -27
View File
@@ -58,15 +58,31 @@ el.replaceWith(d);
<pre><code class="python">class ContextService(BaseService): <pre><code class="python">class ContextService(BaseService):
&#34;&#34;&#34;Business logic for managing and applying regex-based contexts locally.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for managing and applying regex-based contexts locally.&#34;&#34;&#34;
@staticmethod
def _validate_regexes(regexes: Any) -&gt; None:
if not isinstance(regexes, list) or not (1 &lt;= len(regexes) &lt;= MAX_CONTEXT_REGEXES):
raise ValueError(
f&#34;Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns&#34;
)
for pat in regexes:
if not isinstance(pat, str) or not (1 &lt;= len(pat) &lt;= MAX_CONTEXT_PATTERN_LEN):
raise ValueError(
f&#34;Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters&#34;
)
try:
re.compile(pat)
except re.error as exc:
raise ValueError(f&#34;Invalid context regex {pat!r}: {exc}&#34;) from None
@property @property
def contexts(self) -&gt; Dict[str, List[str]]: def contexts(self) -&gt; dict[str, list[str]]:
return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]}) return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})
@property @property
def current_context(self) -&gt; str: def current_context(self) -&gt; str:
return self.config.config.get(&#34;current_context&#34;, &#34;all&#34;) return self.config.config.get(&#34;current_context&#34;, &#34;all&#34;)
def list_contexts(self) -&gt; List[Dict[str, Any]]: def list_contexts(self) -&gt; list[dict[str, Any]]:
result = [] result = []
for name in self.contexts.keys(): for name in self.contexts.keys():
result.append({ result.append({
@@ -76,27 +92,29 @@ el.replaceWith(d);
}) })
return result return result
def add_context(self, name: str, regexes: List[str]): def add_context(self, name: str, regexes: list[str]):
if not name.isalnum(): if not name.isalnum():
raise ValueError(&#34;Context name must be alphanumeric&#34;) raise ValueError(&#34;Context name must be alphanumeric&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name in ctxs: if name in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def update_context(self, name: str, regexes: List[str]): def update_context(self, name: str, regexes: list[str]):
if name == &#34;all&#34;: if name == &#34;all&#34;:
raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;) raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name not in ctxs: if name not in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
@@ -118,15 +136,47 @@ el.replaceWith(d);
if name not in self.contexts: if name not in self.contexts:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
prev_context = self.current_context
self.config.config[&#34;current_context&#34;] = name self.config.config[&#34;current_context&#34;] = name
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
can_refresh = (
self.config._is_context_filtering_enabled_unlocked()
if (
not hasattr(self.config, &#34;assert_called&#34;)
and callable(getattr(self.config, &#34;_is_context_filtering_enabled_unlocked&#34;, None))
)
else getattr(self.config, &#34;_enforce_context&#34;, True)
)
if (
name == &#34;all&#34;
and prev_context != &#34;all&#34;
and can_refresh
and hasattr(self.config, &#34;_generate_nodes_cache&#34;)
):
self.config._generate_nodes_cache()
def get_active_regexes(self) -&gt; List[re.Pattern]: def get_active_regexes(self) -&gt; list[re.Pattern]:
patterns = self.contexts.get(self.current_context, [&#34;.*&#34;]) return compile_active_context_patterns(getattr(self.config, &#34;config&#34;, None))
return [re.compile(p) for p in patterns]
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -&gt; bool: def _match_any(self, node_name: str, patterns: list[re.Pattern]) -&gt; bool:
return any(p.match(node_name) for p in patterns) return match_active_context_patterns(node_name, patterns)
def is_default_all_context(self) -&gt; bool:
return is_default_all_context_cfg(getattr(self.config, &#34;config&#34;, None))
def matches_active_context(self, name: str) -&gt; bool:
if self.is_default_all_context():
return isinstance(name, str) and bool(name)
if not isinstance(name, str) or not name:
return False
return match_active_context_patterns(name, self.get_active_regexes())
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -&gt; None:
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise InvalidConfigurationError(
f&#34;{label} &#39;{unique_id}&#39; does not match active context &#39;{self.current_context}&#39;.&#34;
)
# Hook handlers for filtering # Hook handlers for filtering
def filter_node_list(self, *args, **kwargs): def filter_node_list(self, *args, **kwargs):
@@ -151,14 +201,14 @@ el.replaceWith(d);
</ul> </ul>
<h3>Instance variables</h3> <h3>Instance variables</h3>
<dl> <dl>
<dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : Dict[str, List[str]]</code></dt> <dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : dict[str, list[str]]</code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">@property <pre><code class="python">@property
def contexts(self) -&gt; Dict[str, List[str]]: def contexts(self) -&gt; dict[str, list[str]]:
return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})</code></pre> return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -179,22 +229,23 @@ def current_context(self) -&gt; str:
<h3>Methods</h3> <h3>Methods</h3>
<dl> <dl>
<dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex">
<span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: List[str])</span> <span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: list[str])</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def add_context(self, name: str, regexes: List[str]): <pre><code class="python">def add_context(self, name: str, regexes: list[str]):
if not name.isalnum(): if not name.isalnum():
raise ValueError(&#34;Context name must be alphanumeric&#34;) raise ValueError(&#34;Context name must be alphanumeric&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name in ctxs: if name in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -253,28 +304,40 @@ def current_context(self) -&gt; str:
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex">
<span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> List[re.Pattern]</span> <span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> list[re.Pattern]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def get_active_regexes(self) -&gt; List[re.Pattern]: <pre><code class="python">def get_active_regexes(self) -&gt; list[re.Pattern]:
patterns = self.contexts.get(self.current_context, [&#34;.*&#34;]) return compile_active_context_patterns(getattr(self.config, &#34;config&#34;, None))</code></pre>
return [re.compile(p) for p in patterns]</code></pre> </details>
<div class="desc"></div>
</dd>
<dt id="connpy.services.context_service.ContextService.is_default_all_context"><code class="name flex">
<span>def <span class="ident">is_default_all_context</span></span>(<span>self) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def is_default_all_context(self) -&gt; bool:
return is_default_all_context_cfg(getattr(self.config, &#34;config&#34;, None))</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex">
<span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> List[Dict[str, Any]]</span> <span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> list[dict[str, typing.Any]]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def list_contexts(self) -&gt; List[Dict[str, Any]]: <pre><code class="python">def list_contexts(self) -&gt; list[dict[str, Any]]:
result = [] result = []
for name in self.contexts.keys(): for name in self.contexts.keys():
result.append({ result.append({
@@ -286,6 +349,23 @@ def current_context(self) -&gt; str:
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.matches_active_context"><code class="name flex">
<span>def <span class="ident">matches_active_context</span></span>(<span>self, name: str) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def matches_active_context(self, name: str) -&gt; bool:
if self.is_default_all_context():
return isinstance(name, str) and bool(name)
if not isinstance(name, str) or not name:
return False
return match_active_context_patterns(name, self.get_active_regexes())</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex">
<span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span> <span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span>
</code></dt> </code></dt>
@@ -298,33 +378,67 @@ def current_context(self) -&gt; str:
if name not in self.contexts: if name not in self.contexts:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
prev_context = self.current_context
self.config.config[&#34;current_context&#34;] = name self.config.config[&#34;current_context&#34;] = name
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)
can_refresh = (
self.config._is_context_filtering_enabled_unlocked()
if (
not hasattr(self.config, &#34;assert_called&#34;)
and callable(getattr(self.config, &#34;_is_context_filtering_enabled_unlocked&#34;, None))
)
else getattr(self.config, &#34;_enforce_context&#34;, True)
)
if (
name == &#34;all&#34;
and prev_context != &#34;all&#34;
and can_refresh
and hasattr(self.config, &#34;_generate_nodes_cache&#34;)
):
self.config._generate_nodes_cache()</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex">
<span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: List[str])</span> <span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: list[str])</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def update_context(self, name: str, regexes: List[str]): <pre><code class="python">def update_context(self, name: str, regexes: list[str]):
if name == &#34;all&#34;: if name == &#34;all&#34;:
raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;) raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name not in ctxs: if name not in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.validate_in_active_context"><code class="name flex">
<span>def <span class="ident">validate_in_active_context</span></span>(<span>self, unique_id: str, is_folder: bool = False) ‑> None</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -&gt; None:
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise InvalidConfigurationError(
f&#34;{label} &#39;{unique_id}&#39; does not match active context &#39;{self.current_context}&#39;.&#34;
)</code></pre>
</details>
<div class="desc"></div>
</dd>
</dl> </dl>
<h3>Inherited members</h3> <h3>Inherited members</h3>
<ul class="hlist"> <ul class="hlist">
@@ -352,7 +466,7 @@ def current_context(self) -&gt; str:
<ul> <ul>
<li> <li>
<h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4> <h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4>
<ul class="two-column"> <ul class="">
<li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li>
@@ -360,9 +474,12 @@ def current_context(self) -&gt; str:
<li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.is_default_all_context" href="#connpy.services.context_service.ContextService.is_default_all_context">is_default_all_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.matches_active_context" href="#connpy.services.context_service.ContextService.matches_active_context">matches_active_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.validate_in_active_context" href="#connpy.services.context_service.ContextService.validate_in_active_context">validate_in_active_context</a></code></li>
</ul> </ul>
</li> </li>
</ul> </ul>
+136 -15
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class"> <dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
<span>class <span class="ident">ExecutionService</span></span> <span>class <span class="ident">ExecutionService</span></span>
<span>(</span><span>config=None, remote_stub=None, attempt_tracker=None)</span> <span>(</span><span>config=None, remote_stub=None, attempt_tracker=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -58,14 +58,33 @@ el.replaceWith(d);
<pre><code class="python">class ExecutionService(BaseService): <pre><code class="python">class ExecutionService(BaseService):
&#34;&#34;&#34;Business logic for executing commands on nodes and running automation scripts.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for executing commands on nodes and running automation scripts.&#34;&#34;&#34;
def __init__(self, config=None, remote_stub=None, attempt_tracker=None): def __init__(
self,
config=None,
remote_stub=None,
attempt_tracker=None,
enforce_context: bool = True,
):
super().__init__(config) super().__init__(config)
self.remote_stub = remote_stub self.remote_stub = remote_stub
self.attempt_tracker = ( self.attempt_tracker = (
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker() attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
) )
self.enforce_context = bool(enforce_context)
self.last_fallback_used: dict[str, bool] = {} self.last_fallback_used: dict[str, bool] = {}
def _get_context_service(self):
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(self.config, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(self.config, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(self.config)
def _is_remote_backend(self) -&gt; bool: def _is_remote_backend(self) -&gt; bool:
&#34;&#34;&#34;Return True when execution is configured to run against a remote gRPC backend.&#34;&#34;&#34; &#34;&#34;&#34;Return True when execution is configured to run against a remote gRPC backend.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None: if getattr(self, &#34;remote_stub&#34;, None) is not None:
@@ -84,10 +103,7 @@ el.replaceWith(d);
&#34;connect_timeout and auth_timeout overrides are not supported in remote gRPC mode&#34; &#34;connect_timeout and auth_timeout overrides are not supported in remote gRPC mode&#34;
) )
def _get_remote_stub(self): def _get_remote_channel(self):
&#34;&#34;&#34;Return the configured or lazily constructed ExecutionStub for remote gRPC mode.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return self.remote_stub
cfg = getattr(self.config, &#34;config&#34;, None) cfg = getattr(self.config, &#34;config&#34;, None)
remote_host = cfg.get(&#34;remote_host&#34;) if isinstance(cfg, dict) else None remote_host = cfg.get(&#34;remote_host&#34;) if isinstance(cfg, dict) else None
if not remote_host: if not remote_host:
@@ -95,7 +111,7 @@ el.replaceWith(d);
import grpc import grpc
from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub from connpy.grpc_layer.stubs import AuthClientInterceptor
def get_token(): def get_token():
env_token = os.environ.get(&#34;CONNPY_TOKEN&#34;) env_token = os.environ.get(&#34;CONNPY_TOKEN&#34;)
@@ -111,8 +127,81 @@ el.replaceWith(d);
channel = grpc.insecure_channel(remote_host) channel = grpc.insecure_channel(remote_host)
channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token)) channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
return channel, remote_host
def _get_remote_stub(self):
&#34;&#34;&#34;Return the configured or lazily constructed ExecutionStub for remote gRPC mode.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return self.remote_stub
from connpy.grpc_layer.stubs import ExecutionStub
channel, remote_host = self._get_remote_channel()
return ExecutionStub(channel, remote_host=remote_host) return ExecutionStub(channel, remote_host=remote_host)
def _filter_remote_nodes_filter(self, nodes_filter):
ctx_svc = self._get_context_service()
if ctx_svc is None or ctx_svc.is_default_all_context():
return nodes_filter
if isinstance(nodes_filter, (list, tuple)):
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
else:
if self.remote_stub is not None and callable(
getattr(self.remote_stub, &#34;list_nodes&#34;, None)
):
candidates = self.remote_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is not None and callable(
getattr(getattr(self.remote_stub, &#34;node_stub&#34;, None), &#34;list_nodes&#34;, None)
):
candidates = self.remote_stub.node_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is None:
from connpy.grpc_layer.stubs import NodeStub
channel, remote_host = self._get_remote_channel()
node_stub = NodeStub(channel, remote_host=remote_host, config=self.config)
candidates = node_stub.list_nodes(filter_str=nodes_filter)
else:
candidates = (
self.config._getallnodes(nodes_filter)
if hasattr(self.config, &#34;_getallnodes&#34;)
else []
)
if nodes_filter:
cfg_dict = getattr(self.config, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
flags = 0 if case else re.IGNORECASE
candidates = [c for c in candidates if re.search(nodes_filter, c, flags)]
permitted = ctx_svc.filter_node_list(result=candidates)
if not permitted:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
return [f&#34;^{re.escape(n)}$&#34; for n in permitted]
def _build_remote_filter_batches(self, nodes_filter):
filtered = self._filter_remote_nodes_filter(nodes_filter)
if isinstance(filtered, list):
return [
filtered[i : i + MAX_REMOTE_FILTER_NODES]
for i in range(0, len(filtered), MAX_REMOTE_FILTER_NODES)
]
return [filtered]
@staticmethod
def _aggregate_remote_results(batch_results):
if len(batch_results) == 1:
return batch_results[0]
if all(isinstance(r, dict) for r in batch_results):
merged: dict[str, Any] = {}
for r in batch_results:
merged.update(r)
return merged
if all(isinstance(r, list) for r in batch_results):
merged_list: list[Any] = []
for r in batch_results:
merged_list.extend(r)
return merged_list
return batch_results[-1]
def run_commands( def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
@@ -136,9 +225,11 @@ el.replaceWith(d);
) )
if self._is_remote_backend(): if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub() stub = self._get_remote_stub()
return stub.run_commands( batch_results = [
nodes_filter=nodes_filter, stub.run_commands(
nodes_filter=batch,
commands=commands, commands=commands,
variables=variables, variables=variables,
parallel=parallel, parallel=parallel,
@@ -149,8 +240,14 @@ el.replaceWith(d);
logger=logger, logger=logger,
name=name or &#34;&#34;, name=name or &#34;&#34;,
) )
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -224,9 +321,11 @@ el.replaceWith(d);
) )
if self._is_remote_backend(): if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub() stub = self._get_remote_stub()
return stub.test_commands( batch_results = [
nodes_filter=nodes_filter, stub.test_commands(
nodes_filter=batch,
commands=commands, commands=commands,
expected=expected, expected=expected,
variables=variables, variables=variables,
@@ -238,8 +337,14 @@ el.replaceWith(d);
logger=logger, logger=logger,
name=name or &#34;&#34;, name=name or &#34;&#34;,
) )
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -361,9 +466,11 @@ el.replaceWith(d);
) )
if self._is_remote_backend(): if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub() stub = self._get_remote_stub()
return stub.run_commands( batch_results = [
nodes_filter=nodes_filter, stub.run_commands(
nodes_filter=batch,
commands=commands, commands=commands,
variables=variables, variables=variables,
parallel=parallel, parallel=parallel,
@@ -374,8 +481,14 @@ el.replaceWith(d);
logger=logger, logger=logger,
name=name or &#34;&#34;, name=name or &#34;&#34;,
) )
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -459,9 +572,11 @@ el.replaceWith(d);
) )
if self._is_remote_backend(): if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub() stub = self._get_remote_stub()
return stub.test_commands( batch_results = [
nodes_filter=nodes_filter, stub.test_commands(
nodes_filter=batch,
commands=commands, commands=commands,
expected=expected, expected=expected,
variables=variables, variables=variables,
@@ -473,8 +588,14 @@ el.replaceWith(d);
logger=logger, logger=logger,
name=name or &#34;&#34;, name=name or &#34;&#34;,
) )
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
+244 -50
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class"> <dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class">
<span>class <span class="ident">ImportExportService</span></span> <span>class <span class="ident">ImportExportService</span></span>
<span>(</span><span>config=None)</span> <span>(</span><span>config=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -58,6 +58,44 @@ el.replaceWith(d);
<pre><code class="python">class ImportExportService(BaseService): <pre><code class="python">class ImportExportService(BaseService):
&#34;&#34;&#34;Business logic for YAML/JSON inventory import and export.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for YAML/JSON inventory import and export.&#34;&#34;&#34;
def __init__(self, config=None, enforce_context: bool = True):
super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(target_cfg, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(target_cfg, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallfolders_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -&gt; bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
if case:
return folder_id in raw_folders
return folder_id.lower() in {f.lower() for f in raw_folders}
def export_to_file(self, file_path, folders=None): def export_to_file(self, file_path, folders=None):
&#34;&#34;&#34;Export nodes/folders to a YAML file.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a YAML file.&#34;&#34;&#34;
if os.path.exists(file_path): if os.path.exists(file_path):
@@ -72,16 +110,42 @@ el.replaceWith(d);
def export_to_dict(self, folders=None): def export_to_dict(self, folders=None):
&#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34;
if not folders: ctx_svc = self._get_context_service()
return deepcopy(self.config.connections) ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
else:
# Validate folders exist
for f in folders:
if f != &#34;@&#34; and f not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
if not folders and not ctx_active:
return deepcopy(self.config.connections)
raw_folders = self._get_raw_folders()
if folders:
for f in folders:
if f != &#34;@&#34; and (
not self._folder_exists(f, raw_folders)
or (ctx_active and not ctx_svc.matches_active_context(f))
):
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
flat = self.config._getallnodesfull(folders, extract=False) flat = self.config._getallnodesfull(folders, extract=False)
allowed_folders = []
else:
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
flat = self.config._getallnodesfull(extract=False)
if ctx_active:
flat = ctx_svc.filter_node_dict(result=flat)
nested = {} nested = {}
for folder_id in allowed_folders:
uniques = self.config._explode_unique(folder_id)
if not uniques or &#34;folder&#34; not in uniques:
continue
f_name = uniques[&#34;folder&#34;]
if f_name not in nested:
nested[f_name] = {&#34;type&#34;: &#34;folder&#34;}
if &#34;subfolder&#34; in uniques:
s_name = uniques[&#34;subfolder&#34;]
if s_name not in nested[f_name]:
nested[f_name][s_name] = {&#34;type&#34;: &#34;subfolder&#34;}
for k, v in flat.items(): for k, v in flat.items():
uniques = self.config._explode_unique(k) uniques = self.config._explode_unique(k)
if not uniques: if not uniques:
@@ -126,22 +190,52 @@ el.replaceWith(d);
raise InvalidConfigurationError(f&#34;Failed to read/parse import file: {e}&#34;) raise InvalidConfigurationError(f&#34;Failed to read/parse import file: {e}&#34;)
def import_from_dict(self, data): def import_from_dict(self, data):
&#34;&#34;&#34;Import nodes/folders from a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Import nodes/folders from a dictionary atomically with context pre-validation.&#34;&#34;&#34;
if not isinstance(data, dict): if not isinstance(data, dict):
raise InvalidConfigurationError(&#34;Invalid import data format: expected a dictionary of nodes.&#34;) raise InvalidConfigurationError(
&#34;Invalid import data format: expected a dictionary of nodes.&#34;
)
def _traverse_import(node_data, current_folder=&#39;&#39;, current_subfolder=&#39;&#39;): ctx_svc = self._get_context_service()
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
raw_folders = self._get_raw_folders() if ctx_active else []
planned_ops = []
def _plan_import(node_data, current_folder=&#34;&#34;, current_subfolder=&#34;&#34;):
for k, v in node_data.items(): for k, v in node_data.items():
if k == &#34;type&#34;: if k == &#34;type&#34; or not isinstance(v, dict):
continue continue
if isinstance(v, dict):
node_type = v.get(&#34;type&#34;, &#34;connection&#34;) node_type = v.get(&#34;type&#34;, &#34;connection&#34;)
if node_type == &#34;folder&#34;: if node_type == &#34;folder&#34;:
self.config._folder_add(folder=k) folder_id = f&#34;@{k}&#34;
_traverse_import(v, current_folder=k, current_subfolder=&#39;&#39;) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, k, &#34;&#34;))
_plan_import(v, current_folder=k, current_subfolder=&#34;&#34;)
elif node_type == &#34;subfolder&#34;: elif node_type == &#34;subfolder&#34;:
self.config._folder_add(folder=current_folder, subfolder=k) folder_id = f&#34;@{k}@{current_folder}&#34;
_traverse_import(v, current_folder=current_folder, current_subfolder=k) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, current_folder, k))
_plan_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == &#34;connection&#34;: elif node_type == &#34;connection&#34;:
unique_id = k unique_id = k
if current_subfolder: if current_subfolder:
@@ -149,19 +243,41 @@ el.replaceWith(d);
elif current_folder: elif current_folder:
unique_id = f&#34;{k}@{current_folder}&#34; unique_id = f&#34;{k}@{current_folder}&#34;
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
NodeService._validate_node_payload(v)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
kwargs = deepcopy(v) kwargs = deepcopy(v)
kwargs[&#39;id&#39;] = k kwargs[&#34;id&#34;] = k
kwargs[&#39;folder&#39;] = current_folder kwargs[&#34;folder&#34;] = current_folder
kwargs[&#39;subfolder&#39;] = current_subfolder kwargs[&#34;subfolder&#34;] = current_subfolder
planned_ops.append((&#34;connection&#34;, kwargs))
self.config._connections_add(**kwargs) _plan_import(data)
if _supports_transactional_update(self.config):
def _mut(cfg):
for op in planned_ops:
if op[0] == &#34;folder&#34;:
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
else: else:
# Invalid format skip cfg._connections_add_unlocked(**op[1])
pass
_traverse_import(data) self.config.transactional_update(_mut)
self.config._saveconfig(self.config.file)</code></pre> return
snap_connections = deepcopy(self.config.connections)
try:
for op in planned_ops:
if op[0] == &#34;folder&#34;:
self.config._folder_add(folder=op[1], subfolder=op[2])
else:
self.config._connections_add(**op[1])
self.config._saveconfig(self.config.file)
except BaseException:
self.config.connections.clear()
self.config.connections.update(snap_connections)
raise</code></pre>
</details> </details>
<div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p> <div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p>
<p>Initialize the service.</p> <p>Initialize the service.</p>
@@ -187,16 +303,42 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def export_to_dict(self, folders=None): <pre><code class="python">def export_to_dict(self, folders=None):
&#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34;
if not folders: ctx_svc = self._get_context_service()
return deepcopy(self.config.connections) ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
else:
# Validate folders exist
for f in folders:
if f != &#34;@&#34; and f not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
if not folders and not ctx_active:
return deepcopy(self.config.connections)
raw_folders = self._get_raw_folders()
if folders:
for f in folders:
if f != &#34;@&#34; and (
not self._folder_exists(f, raw_folders)
or (ctx_active and not ctx_svc.matches_active_context(f))
):
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
flat = self.config._getallnodesfull(folders, extract=False) flat = self.config._getallnodesfull(folders, extract=False)
allowed_folders = []
else:
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
flat = self.config._getallnodesfull(extract=False)
if ctx_active:
flat = ctx_svc.filter_node_dict(result=flat)
nested = {} nested = {}
for folder_id in allowed_folders:
uniques = self.config._explode_unique(folder_id)
if not uniques or &#34;folder&#34; not in uniques:
continue
f_name = uniques[&#34;folder&#34;]
if f_name not in nested:
nested[f_name] = {&#34;type&#34;: &#34;folder&#34;}
if &#34;subfolder&#34; in uniques:
s_name = uniques[&#34;subfolder&#34;]
if s_name not in nested[f_name]:
nested[f_name][s_name] = {&#34;type&#34;: &#34;subfolder&#34;}
for k, v in flat.items(): for k, v in flat.items():
uniques = self.config._explode_unique(k) uniques = self.config._explode_unique(k)
if not uniques: if not uniques:
@@ -261,22 +403,52 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def import_from_dict(self, data): <pre><code class="python">def import_from_dict(self, data):
&#34;&#34;&#34;Import nodes/folders from a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Import nodes/folders from a dictionary atomically with context pre-validation.&#34;&#34;&#34;
if not isinstance(data, dict): if not isinstance(data, dict):
raise InvalidConfigurationError(&#34;Invalid import data format: expected a dictionary of nodes.&#34;) raise InvalidConfigurationError(
&#34;Invalid import data format: expected a dictionary of nodes.&#34;
)
def _traverse_import(node_data, current_folder=&#39;&#39;, current_subfolder=&#39;&#39;): ctx_svc = self._get_context_service()
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
raw_folders = self._get_raw_folders() if ctx_active else []
planned_ops = []
def _plan_import(node_data, current_folder=&#34;&#34;, current_subfolder=&#34;&#34;):
for k, v in node_data.items(): for k, v in node_data.items():
if k == &#34;type&#34;: if k == &#34;type&#34; or not isinstance(v, dict):
continue continue
if isinstance(v, dict):
node_type = v.get(&#34;type&#34;, &#34;connection&#34;) node_type = v.get(&#34;type&#34;, &#34;connection&#34;)
if node_type == &#34;folder&#34;: if node_type == &#34;folder&#34;:
self.config._folder_add(folder=k) folder_id = f&#34;@{k}&#34;
_traverse_import(v, current_folder=k, current_subfolder=&#39;&#39;) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, k, &#34;&#34;))
_plan_import(v, current_folder=k, current_subfolder=&#34;&#34;)
elif node_type == &#34;subfolder&#34;: elif node_type == &#34;subfolder&#34;:
self.config._folder_add(folder=current_folder, subfolder=k) folder_id = f&#34;@{k}@{current_folder}&#34;
_traverse_import(v, current_folder=current_folder, current_subfolder=k) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, current_folder, k))
_plan_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == &#34;connection&#34;: elif node_type == &#34;connection&#34;:
unique_id = k unique_id = k
if current_subfolder: if current_subfolder:
@@ -284,21 +456,43 @@ el.replaceWith(d);
elif current_folder: elif current_folder:
unique_id = f&#34;{k}@{current_folder}&#34; unique_id = f&#34;{k}@{current_folder}&#34;
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
NodeService._validate_node_payload(v)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
kwargs = deepcopy(v) kwargs = deepcopy(v)
kwargs[&#39;id&#39;] = k kwargs[&#34;id&#34;] = k
kwargs[&#39;folder&#39;] = current_folder kwargs[&#34;folder&#34;] = current_folder
kwargs[&#39;subfolder&#39;] = current_subfolder kwargs[&#34;subfolder&#34;] = current_subfolder
planned_ops.append((&#34;connection&#34;, kwargs))
self.config._connections_add(**kwargs) _plan_import(data)
if _supports_transactional_update(self.config):
def _mut(cfg):
for op in planned_ops:
if op[0] == &#34;folder&#34;:
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
else: else:
# Invalid format skip cfg._connections_add_unlocked(**op[1])
pass
_traverse_import(data) self.config.transactional_update(_mut)
self.config._saveconfig(self.config.file)</code></pre> return
snap_connections = deepcopy(self.config.connections)
try:
for op in planned_ops:
if op[0] == &#34;folder&#34;:
self.config._folder_add(folder=op[1], subfolder=op[2])
else:
self.config._connections_add(**op[1])
self.config._saveconfig(self.config.file)
except BaseException:
self.config.connections.clear()
self.config.connections.update(snap_connections)
raise</code></pre>
</details> </details>
<div class="desc"><p>Import nodes/folders from a dictionary.</p></div> <div class="desc"><p>Import nodes/folders from a dictionary atomically with context pre-validation.</p></div>
</dd> </dd>
<dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex"> <dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex">
<span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span> <span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span>
File diff suppressed because it is too large. Load diff
+213 -46
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.node_service.NodeService"><code class="flex name class"> <dt id="connpy.services.node_service.NodeService"><code class="flex name class">
<span>class <span class="ident">NodeService</span></span> <span>class <span class="ident">NodeService</span></span>
<span>(</span><span>config=None)</span> <span>(</span><span>config=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -73,8 +73,104 @@ el.replaceWith(d);
&#34;type&#34;, &#34;type&#34;,
}) })
def __init__(self, config=None): def __init__(self, config=None, enforce_context: bool = True):
super().__init__(config) super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(target_cfg, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(target_cfg, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallfolders_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _get_raw_nodes(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallnodes_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallnodes_unlocked()
return target_cfg._getallnodes()
def _derive_parent_folder(self, cfg, unique_id: str, is_folder: bool) -&gt; str | None:
explode_fn = (
cfg._explode_unique_unlocked
if (
not hasattr(cfg, &#34;assert_called&#34;)
and callable(getattr(cfg, &#34;_explode_unique_unlocked&#34;, None))
)
else cfg._explode_unique
)
uniques = explode_fn(unique_id)
if is_folder:
if (
not isinstance(unique_id, str)
or not unique_id.startswith(&#34;@&#34;)
or not isinstance(uniques, dict)
or &#34;folder&#34; not in uniques
):
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
return f&#34;@{uniques[&#39;folder&#39;]}&#34;
return None
if (
not isinstance(unique_id, str)
or unique_id.startswith(&#34;@&#34;)
or not isinstance(uniques, dict)
or not uniques.get(&#34;id&#34;)
):
raise InvalidConfigurationError(f&#34;Invalid node name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques:
return f&#34;@{uniques[&#39;subfolder&#39;]}@{uniques[&#39;folder&#39;]}&#34;
if &#34;folder&#34; in uniques:
return f&#34;@{uniques[&#39;folder&#39;]}&#34;
return None
def _folder_exists(self, parent_folder: str, raw_folders: list[str], cfg=None) -&gt; bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
if case:
return parent_folder in raw_folders
return parent_folder.lower() in {f.lower() for f in raw_folders}
def _ensure_in_active_context(
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
) -&gt; None:
ctx_svc = self._get_context_service()
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(unique_id)
):
if not_found_msg is not None:
raise NodeNotFoundError(not_found_msg)
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found.&#34;)
@staticmethod @staticmethod
def _validate_node_payload(data): def _validate_node_payload(data):
@@ -113,6 +209,10 @@ el.replaceWith(d);
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)] nodes = [n for n in nodes if re.search(filter_str, n, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
if not format_str: if not format_str:
return nodes return nodes
@@ -169,10 +269,14 @@ el.replaceWith(d);
else: else:
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)] folders = [f for f in folders if re.search(filter_str, f, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders return folders
def get_node_details(self, unique_id): def get_node_details(self, unique_id):
&#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34; &#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
try: try:
details = self.config.getitem(unique_id) details = self.config.getitem(unique_id)
if not details: if not details:
@@ -190,50 +294,45 @@ el.replaceWith(d);
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles) self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
def validate_parent_folder(self, unique_id, is_folder=False): def validate_parent_folder(self, unique_id, is_folder=False):
&#34;&#34;&#34;Check if parent folder exists for a given node unique ID.&#34;&#34;&#34; &#34;&#34;&#34;Check if unique_id is in active context and its parent folder exists.&#34;&#34;&#34;
if is_folder: parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
uniques = self.config._explode_unique(unique_id) ctx_svc = self._get_context_service()
if uniques and &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques: if ctx_svc is not None:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34; ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder not in self.config._getallfolders(): if parent_folder is not None and not self._folder_exists(
parent_folder, self._get_raw_folders(), self.config
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
else:
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
def add_node(self, unique_id, data, is_folder=False): def add_node(self, unique_id, data, is_folder=False):
&#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34; &#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34;
if not is_folder: if not is_folder:
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
self._validate_node_payload(data) self._validate_node_payload(data)
self.validate_parent_folder(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _mut(cfg): def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked() all_folders = cfg._getallfolders_unlocked()
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
ctx_svc = self._get_context_service(cfg)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if is_folder: if is_folder:
if unique_id in all_folders: if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;) raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
cfg._folder_add_unlocked(**uniques) cfg._folder_add_unlocked(**uniques)
else: else:
if unique_id in all_nodes: if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
payload = deepcopy(data) payload = deepcopy(data)
if &#34;id&#34; not in payload: if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
@@ -290,6 +389,7 @@ el.replaceWith(d);
def update_node(self, unique_id, data, save=True): def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34; &#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
if _supports_transactional_update(self.config) and save: if _supports_transactional_update(self.config) and save:
def _mut(cfg): def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
@@ -316,6 +416,7 @@ el.replaceWith(d);
def delete_node(self, unique_id, is_folder=False, save=True): def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34; &#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config) and save: if _supports_transactional_update(self.config) and save:
def _mut(cfg): def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
@@ -346,6 +447,7 @@ el.replaceWith(d);
def connect_node(self, unique_id, sftp=False, debug=False, logger=None): def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34; &#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
from connpy.core import node from connpy.core import node
from ..auth_utils import resolve_fallback_entries from ..auth_utils import resolve_fallback_entries
@@ -382,10 +484,28 @@ el.replaceWith(d);
def move_node(self, src_id, dst_id, copy=False): def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34; &#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f&#34;Source node &#39;{src_id}&#39; not found.&#34;
)
self._validate_node_name(dst_id) self._validate_node_name(dst_id)
self.validate_parent_folder(dst_id, is_folder=False)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _mut(cfg): def _mut(cfg):
ctx_svc = self._get_context_service(cfg)
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(src_id)
):
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, cfg._getallfolders_unlocked(), cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
try: try:
node_data = cfg.getitem_unlocked(src_id) node_data = cfg.getitem_unlocked(src_id)
except (KeyError, TypeError): except (KeyError, TypeError):
@@ -432,12 +552,23 @@ el.replaceWith(d);
def bulk_add(self, ids, hosts, common_data): def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34; &#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
self._validate_node_payload(common_data) self._validate_node_payload(common_data)
for uid in ids:
self.validate_parent_folder(uid, is_folder=False)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _build_entries(cfg): def _build_entries(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
ctx_svc = self._get_context_service(cfg)
planned = [] planned = []
for i, uid in enumerate(ids): for i, uid in enumerate(ids):
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(uid, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if uid in all_nodes: if uid in all_nodes:
continue continue
try: try:
@@ -555,30 +686,30 @@ el.replaceWith(d);
if not is_folder: if not is_folder:
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
self._validate_node_payload(data) self._validate_node_payload(data)
self.validate_parent_folder(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _mut(cfg): def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked() all_folders = cfg._getallfolders_unlocked()
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
ctx_svc = self._get_context_service(cfg)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if is_folder: if is_folder:
if unique_id in all_folders: if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;) raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
cfg._folder_add_unlocked(**uniques) cfg._folder_add_unlocked(**uniques)
else: else:
if unique_id in all_nodes: if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
payload = deepcopy(data) payload = deepcopy(data)
if &#34;id&#34; not in payload: if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
@@ -632,12 +763,23 @@ el.replaceWith(d);
<pre><code class="python">def bulk_add(self, ids, hosts, common_data): <pre><code class="python">def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34; &#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
self._validate_node_payload(common_data) self._validate_node_payload(common_data)
for uid in ids:
self.validate_parent_folder(uid, is_folder=False)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _build_entries(cfg): def _build_entries(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
ctx_svc = self._get_context_service(cfg)
planned = [] planned = []
for i, uid in enumerate(ids): for i, uid in enumerate(ids):
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(uid, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if uid in all_nodes: if uid in all_nodes:
continue continue
try: try:
@@ -712,6 +854,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None): <pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34; &#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
from connpy.core import node from connpy.core import node
from ..auth_utils import resolve_fallback_entries from ..auth_utils import resolve_fallback_entries
@@ -758,6 +901,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True): <pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34; &#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config) and save: if _supports_transactional_update(self.config) and save:
def _mut(cfg): def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id) uniques = cfg._explode_unique_unlocked(unique_id)
@@ -874,6 +1018,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def get_node_details(self, unique_id): <pre><code class="python">def get_node_details(self, unique_id):
&#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34; &#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
try: try:
details = self.config.getitem(unique_id) details = self.config.getitem(unique_id)
if not details: if not details:
@@ -906,6 +1051,9 @@ el.replaceWith(d);
else: else:
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)] folders = [f for f in folders if re.search(filter_str, f, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders</code></pre> return folders</code></pre>
</details> </details>
<div class="desc"><p>Return all unique folders, optionally filtered by regex.</p></div> <div class="desc"><p>Return all unique folders, optionally filtered by regex.</p></div>
@@ -927,6 +1075,10 @@ el.replaceWith(d);
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)] nodes = [n for n in nodes if re.search(filter_str, n, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
if not format_str: if not format_str:
return nodes return nodes
@@ -981,10 +1133,28 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def move_node(self, src_id, dst_id, copy=False): <pre><code class="python">def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34; &#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f&#34;Source node &#39;{src_id}&#39; not found.&#34;
)
self._validate_node_name(dst_id) self._validate_node_name(dst_id)
self.validate_parent_folder(dst_id, is_folder=False)
if _supports_transactional_update(self.config): if _supports_transactional_update(self.config):
def _mut(cfg): def _mut(cfg):
ctx_svc = self._get_context_service(cfg)
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(src_id)
):
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, cfg._getallfolders_unlocked(), cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
try: try:
node_data = cfg.getitem_unlocked(src_id) node_data = cfg.getitem_unlocked(src_id)
except (KeyError, TypeError): except (KeyError, TypeError):
@@ -1040,6 +1210,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def update_node(self, unique_id, data, save=True): <pre><code class="python">def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34; &#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
if _supports_transactional_update(self.config) and save: if _supports_transactional_update(self.config) and save:
def _mut(cfg): def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked() all_nodes = cfg._getallnodes_unlocked()
@@ -1075,21 +1246,17 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def validate_parent_folder(self, unique_id, is_folder=False): <pre><code class="python">def validate_parent_folder(self, unique_id, is_folder=False):
&#34;&#34;&#34;Check if parent folder exists for a given node unique ID.&#34;&#34;&#34; &#34;&#34;&#34;Check if unique_id is in active context and its parent folder exists.&#34;&#34;&#34;
if is_folder: parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
uniques = self.config._explode_unique(unique_id) ctx_svc = self._get_context_service()
if uniques and &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques: if ctx_svc is not None:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34; ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder not in self.config._getallfolders(): if parent_folder is not None and not self._folder_exists(
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;) parent_folder, self._get_raw_folders(), self.config
else: ):
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)</code></pre> raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)</code></pre>
</details> </details>
<div class="desc"><p>Check if parent folder exists for a given node unique ID.</p></div> <div class="desc"><p>Check if unique_id is in active context and its parent folder exists.</p></div>
</dd> </dd>
</dl> </dl>
<h3>Inherited members</h3> <h3>Inherited members</h3>
+24 -32
View File
@@ -176,18 +176,13 @@ el.replaceWith(d);
resolved = deepcopy(node_data) resolved = deepcopy(node_data)
auth_referenced_profiles = [] auth_referenced_profiles = []
other_referenced_profiles = [] for key in (&#34;user&#34;, &#34;password&#34;):
for key, value in resolved.items(): value = resolved.get(key)
if key == &#34;fallback&#34;:
continue
target_refs = (
auth_referenced_profiles if key in (&#34;user&#34;, &#34;password&#34;) else other_referenced_profiles
)
if isinstance(value, str): if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;): if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:] ref = value[1:]
if key != &#34;password&#34; or ref in profiles: if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref) auth_referenced_profiles.append(ref)
elif isinstance(value, (list, tuple)): elif isinstance(value, (list, tuple)):
for item in value: for item in value:
if ( if (
@@ -197,7 +192,7 @@ el.replaceWith(d);
): ):
ref = item[1:] ref = item[1:]
if key != &#34;password&#34; or ref in profiles: if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref) auth_referenced_profiles.append(ref)
def _get_resolved_ref(prof_name): def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH: if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
@@ -262,15 +257,16 @@ el.replaceWith(d);
resolved_list.append(item) resolved_list.append(item)
resolved[key] = resolved_list resolved[key] = resolved_list
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles)) dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
for base_profile_name in ordered_profiles: for base_profile_name in dedup_auth_profiles:
with suppress(ProfileNotFoundError): with suppress(ProfileNotFoundError):
base_profile = _get_resolved_ref(base_profile_name) base_profile = _get_resolved_ref(base_profile_name)
for key, value in base_profile.items(): for key in (&#34;user&#34;, &#34;password&#34;, &#34;fallback&#34;):
if (key not in resolved or resolved[key] in (&#34;&#34;, [], None)) and ( val = base_profile.get(key)
value not in (&#34;&#34;, [], None) or key not in resolved if (
): key not in resolved or resolved[key] in (&#34;&#34;, [], None)
resolved[key] = deepcopy(value) ) and val not in (&#34;&#34;, [], None):
resolved[key] = deepcopy(val)
if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None): if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
try: try:
@@ -503,18 +499,13 @@ el.replaceWith(d);
resolved = deepcopy(node_data) resolved = deepcopy(node_data)
auth_referenced_profiles = [] auth_referenced_profiles = []
other_referenced_profiles = [] for key in (&#34;user&#34;, &#34;password&#34;):
for key, value in resolved.items(): value = resolved.get(key)
if key == &#34;fallback&#34;:
continue
target_refs = (
auth_referenced_profiles if key in (&#34;user&#34;, &#34;password&#34;) else other_referenced_profiles
)
if isinstance(value, str): if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;): if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:] ref = value[1:]
if key != &#34;password&#34; or ref in profiles: if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref) auth_referenced_profiles.append(ref)
elif isinstance(value, (list, tuple)): elif isinstance(value, (list, tuple)):
for item in value: for item in value:
if ( if (
@@ -524,7 +515,7 @@ el.replaceWith(d);
): ):
ref = item[1:] ref = item[1:]
if key != &#34;password&#34; or ref in profiles: if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref) auth_referenced_profiles.append(ref)
def _get_resolved_ref(prof_name): def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH: if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
@@ -589,15 +580,16 @@ el.replaceWith(d);
resolved_list.append(item) resolved_list.append(item)
resolved[key] = resolved_list resolved[key] = resolved_list
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles)) dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
for base_profile_name in ordered_profiles: for base_profile_name in dedup_auth_profiles:
with suppress(ProfileNotFoundError): with suppress(ProfileNotFoundError):
base_profile = _get_resolved_ref(base_profile_name) base_profile = _get_resolved_ref(base_profile_name)
for key, value in base_profile.items(): for key in (&#34;user&#34;, &#34;password&#34;, &#34;fallback&#34;):
if (key not in resolved or resolved[key] in (&#34;&#34;, [], None)) and ( val = base_profile.get(key)
value not in (&#34;&#34;, [], None) or key not in resolved if (
): key not in resolved or resolved[key] in (&#34;&#34;, [], None)
resolved[key] = deepcopy(value) ) and val not in (&#34;&#34;, [], None):
resolved[key] = deepcopy(val)
if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None): if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
try: try:
+76 -12
View File
@@ -65,7 +65,7 @@ el.replaceWith(d);
</dd> </dd>
<dt id="connpy.services.provider.ServiceProvider"><code class="flex name class"> <dt id="connpy.services.provider.ServiceProvider"><code class="flex name class">
<span>class <span class="ident">ServiceProvider</span></span> <span>class <span class="ident">ServiceProvider</span></span>
<span>(</span><span>config, mode='local', remote_host=None)</span> <span>(</span><span>config, mode='local', remote_host=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -75,19 +75,22 @@ el.replaceWith(d);
<pre><code class="python">class ServiceProvider: <pre><code class="python">class ServiceProvider:
&#34;&#34;&#34;Dynamic service backend. Transparently provides local or remote services.&#34;&#34;&#34; &#34;&#34;&#34;Dynamic service backend. Transparently provides local or remote services.&#34;&#34;&#34;
def __init__(self, config, mode=&#34;local&#34;, remote_host=None): def __init__(self, config, mode=&#34;local&#34;, remote_host=None, enforce_context: bool = True):
from ..auth_utils import AuthAttemptTracker from ..auth_utils import AuthAttemptTracker
self.mode = mode self.mode = mode
self.config = config self.config = config
self.remote_host = remote_host self.remote_host = remote_host
self.enforce_context = bool(enforce_context)
self.auth_tracker = AuthAttemptTracker() self.auth_tracker = AuthAttemptTracker()
self.nodes = None
self._system = None self._system = None
self._execution = None self._execution = None
self._import_export = None self._import_export = None
self._sync = None self._sync = None
self._users = None self._users = None
self._ai = None self._ai = None
self.set_enforce_context(self.enforce_context)
if mode == &#34;local&#34;: if mode == &#34;local&#34;:
self._init_local() self._init_local()
@@ -96,6 +99,18 @@ el.replaceWith(d);
else: else:
raise ValueError(f&#34;Unknown service mode: {mode}&#34;) raise ValueError(f&#34;Unknown service mode: {mode}&#34;)
def set_enforce_context(self, enforce_context: bool) -&gt; None:
self.enforce_context = bool(enforce_context)
if self.config is not None and not hasattr(self.config, &#34;assert_called&#34;):
self.config._enforce_context = self.enforce_context
for svc in (
getattr(self, &#34;nodes&#34;, None),
self._execution,
self._import_export,
):
if svc is not None and hasattr(svc, &#34;enforce_context&#34;):
svc.enforce_context = self.enforce_context
def _init_local(self): def _init_local(self):
from .config_service import ConfigService from .config_service import ConfigService
from .context_service import ContextService from .context_service import ContextService
@@ -103,7 +118,7 @@ el.replaceWith(d);
from .plugin_service import PluginService from .plugin_service import PluginService
from .profile_service import ProfileService from .profile_service import ProfileService
self.nodes = NodeService(self.config) self.nodes = NodeService(self.config, enforce_context=self.enforce_context)
self.profiles = ProfileService(self.config) self.profiles = ProfileService(self.config)
self.config_svc = ConfigService(self.config) self.config_svc = ConfigService(self.config)
self.plugins = PluginService(self.config) self.plugins = PluginService(self.config)
@@ -161,7 +176,12 @@ el.replaceWith(d);
self.plugins = PluginStub(channel, remote_host=self.remote_host) self.plugins = PluginStub(channel, remote_host=self.remote_host)
self.ai = AIStub(channel, remote_host=self.remote_host) self.ai = AIStub(channel, remote_host=self.remote_host)
self.system = SystemStub(channel, remote_host=self.remote_host) self.system = SystemStub(channel, remote_host=self.remote_host)
self.execution = ExecutionStub(channel, remote_host=self.remote_host) self.execution = ExecutionStub(
channel,
remote_host=self.remote_host,
config=self.config,
node_stub=self.nodes,
)
self.import_export = ImportExportStub(channel, remote_host=self.remote_host) self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
self.auth = AuthStub(channel, remote_host=self.remote_host) self.auth = AuthStub(channel, remote_host=self.remote_host)
@@ -180,10 +200,13 @@ el.replaceWith(d);
def execution(self): def execution(self):
if self._execution is None and self.mode == &#34;local&#34;: if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService from .execution_service import ExecutionService
if &#34;attempt_tracker&#34; in inspect.signature(ExecutionService.__init__).parameters: params = inspect.signature(ExecutionService.__init__).parameters
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker) kwargs = {}
else: if &#34;attempt_tracker&#34; in params:
self._execution = ExecutionService(self.config) kwargs[&#34;attempt_tracker&#34;] = self.auth_tracker
if &#34;enforce_context&#34; in params:
kwargs[&#34;enforce_context&#34;] = self.enforce_context
self._execution = ExecutionService(self.config, **kwargs)
return self._execution return self._execution
@execution.setter @execution.setter
@@ -194,6 +217,12 @@ el.replaceWith(d);
def import_export(self): def import_export(self):
if self._import_export is None and self.mode == &#34;local&#34;: if self._import_export is None and self.mode == &#34;local&#34;:
from .import_export_service import ImportExportService from .import_export_service import ImportExportService
params = inspect.signature(ImportExportService.__init__).parameters
if &#34;enforce_context&#34; in params:
self._import_export = ImportExportService(
self.config, enforce_context=self.enforce_context
)
else:
self._import_export = ImportExportService(self.config) self._import_export = ImportExportService(self.config)
return self._import_export return self._import_export
@@ -262,10 +291,13 @@ def ai(self):
def execution(self): def execution(self):
if self._execution is None and self.mode == &#34;local&#34;: if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService from .execution_service import ExecutionService
if &#34;attempt_tracker&#34; in inspect.signature(ExecutionService.__init__).parameters: params = inspect.signature(ExecutionService.__init__).parameters
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker) kwargs = {}
else: if &#34;attempt_tracker&#34; in params:
self._execution = ExecutionService(self.config) kwargs[&#34;attempt_tracker&#34;] = self.auth_tracker
if &#34;enforce_context&#34; in params:
kwargs[&#34;enforce_context&#34;] = self.enforce_context
self._execution = ExecutionService(self.config, **kwargs)
return self._execution</code></pre> return self._execution</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -280,6 +312,12 @@ def execution(self):
def import_export(self): def import_export(self):
if self._import_export is None and self.mode == &#34;local&#34;: if self._import_export is None and self.mode == &#34;local&#34;:
from .import_export_service import ImportExportService from .import_export_service import ImportExportService
params = inspect.signature(ImportExportService.__init__).parameters
if &#34;enforce_context&#34; in params:
self._import_export = ImportExportService(
self.config, enforce_context=self.enforce_context
)
else:
self._import_export = ImportExportService(self.config) self._import_export = ImportExportService(self.config)
return self._import_export</code></pre> return self._import_export</code></pre>
</details> </details>
@@ -331,6 +369,31 @@ def users(self):
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
</dl> </dl>
<h3>Methods</h3>
<dl>
<dt id="connpy.services.provider.ServiceProvider.set_enforce_context"><code class="name flex">
<span>def <span class="ident">set_enforce_context</span></span>(<span>self, enforce_context: bool) ‑> None</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_enforce_context(self, enforce_context: bool) -&gt; None:
self.enforce_context = bool(enforce_context)
if self.config is not None and not hasattr(self.config, &#34;assert_called&#34;):
self.config._enforce_context = self.enforce_context
for svc in (
getattr(self, &#34;nodes&#34;, None),
self._execution,
self._import_export,
):
if svc is not None and hasattr(svc, &#34;enforce_context&#34;):
svc.enforce_context = self.enforce_context</code></pre>
</details>
<div class="desc"></div>
</dd>
</dl>
</dd> </dd>
</dl> </dl>
</section> </section>
@@ -356,6 +419,7 @@ def users(self):
<li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.set_enforce_context" href="#connpy.services.provider.ServiceProvider.set_enforce_context">set_enforce_context</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li>