networka 0.1.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. network_toolkit/__about__.py +4 -0
  2. network_toolkit/__init__.py +8 -0
  3. network_toolkit/__main__.py +10 -0
  4. network_toolkit/builtin_sequences/arista_eos/common.yml +53 -0
  5. network_toolkit/builtin_sequences/mikrotik_routeros/common.yml +52 -0
  6. network_toolkit/cli.py +265 -0
  7. network_toolkit/commands/__init__.py +8 -0
  8. network_toolkit/commands/backup.py +441 -0
  9. network_toolkit/commands/complete.py +172 -0
  10. network_toolkit/commands/config.py +1068 -0
  11. network_toolkit/commands/diff.py +485 -0
  12. network_toolkit/commands/download.py +197 -0
  13. network_toolkit/commands/firmware.py +780 -0
  14. network_toolkit/commands/info.py +377 -0
  15. network_toolkit/commands/list.py +345 -0
  16. network_toolkit/commands/routerboard_upgrade.py +204 -0
  17. network_toolkit/commands/run.py +839 -0
  18. network_toolkit/commands/schema_unified.py +91 -0
  19. network_toolkit/commands/sequence.py +17 -0
  20. network_toolkit/commands/ssh.py +540 -0
  21. network_toolkit/commands/ssh_engines.py +0 -0
  22. network_toolkit/commands/ssh_fallback.py +97 -0
  23. network_toolkit/commands/ssh_platform.py +120 -0
  24. network_toolkit/commands/upload.py +237 -0
  25. network_toolkit/commands/vendor_config_backup.py +378 -0
  26. network_toolkit/common/color_magic.py +177 -0
  27. network_toolkit/common/command.py +135 -0
  28. network_toolkit/common/command_base.py +135 -0
  29. network_toolkit/common/command_helpers.py +214 -0
  30. network_toolkit/common/credentials.py +86 -0
  31. network_toolkit/common/defaults.py +17 -0
  32. network_toolkit/common/errors.py +130 -0
  33. network_toolkit/common/interactive_confirmation.py +144 -0
  34. network_toolkit/common/logging.py +47 -0
  35. network_toolkit/common/output.py +594 -0
  36. network_toolkit/common/output_clean.py +244 -0
  37. network_toolkit/common/paths.py +86 -0
  38. network_toolkit/common/resolver.py +207 -0
  39. network_toolkit/common/styles.py +199 -0
  40. network_toolkit/common/table_generator.py +169 -0
  41. network_toolkit/common/table_providers.py +716 -0
  42. network_toolkit/config.py +1506 -0
  43. network_toolkit/credentials.py +288 -0
  44. network_toolkit/device.py +1658 -0
  45. network_toolkit/device_transfers.py +170 -0
  46. network_toolkit/exceptions.py +78 -0
  47. network_toolkit/ip_device.py +241 -0
  48. network_toolkit/platforms/__init__.py +21 -0
  49. network_toolkit/platforms/base.py +267 -0
  50. network_toolkit/platforms/cisco_ios/__init__.py +6 -0
  51. network_toolkit/platforms/cisco_ios/constants.py +15 -0
  52. network_toolkit/platforms/cisco_ios/operations.py +426 -0
  53. network_toolkit/platforms/cisco_iosxe/__init__.py +6 -0
  54. network_toolkit/platforms/cisco_iosxe/constants.py +15 -0
  55. network_toolkit/platforms/cisco_iosxe/operations.py +580 -0
  56. network_toolkit/platforms/cisco_shared/__init__.py +0 -0
  57. network_toolkit/platforms/cisco_shared/operations.py +0 -0
  58. network_toolkit/platforms/factory.py +190 -0
  59. network_toolkit/platforms/mikrotik_routeros/__init__.py +8 -0
  60. network_toolkit/platforms/mikrotik_routeros/confirmation_patterns.py +24 -0
  61. network_toolkit/platforms/mikrotik_routeros/constants.py +31 -0
  62. network_toolkit/platforms/mikrotik_routeros/operations.py +686 -0
  63. network_toolkit/results_enhanced.py +364 -0
  64. network_toolkit/schemas/__init__.py +8 -0
  65. network_toolkit/sequence_manager.py +239 -0
  66. network_toolkit/shell_completion/bash_completion_nw.sh +339 -0
  67. network_toolkit/shell_completion/zsh_completion_nw.zsh +136 -0
  68. network_toolkit/transport/__init__.py +15 -0
  69. network_toolkit/transport/factory.py +113 -0
  70. network_toolkit/transport/interfaces.py +46 -0
  71. network_toolkit/transport/nornir_inventory.py +173 -0
  72. network_toolkit/transport/nornir_netmiko.py +277 -0
  73. network_toolkit/transport/scrapli_sync.py +36 -0
  74. network_toolkit/tui/__init__.py +18 -0
  75. network_toolkit/tui/app.py +1275 -0
  76. network_toolkit/tui/clipboard.py +46 -0
  77. network_toolkit/tui/compat.py +111 -0
  78. network_toolkit/tui/constants.py +111 -0
  79. network_toolkit/tui/controller.py +550 -0
  80. network_toolkit/tui/data.py +149 -0
  81. network_toolkit/tui/dialogs.py +196 -0
  82. network_toolkit/tui/helpers.py +55 -0
  83. network_toolkit/tui/keymap.py +31 -0
  84. network_toolkit/tui/layout.py +111 -0
  85. network_toolkit/tui/models.py +93 -0
  86. network_toolkit/tui/output_manager.py +372 -0
  87. network_toolkit/tui/prompt.py +172 -0
  88. network_toolkit/tui/services.py +285 -0
  89. networka-0.1.9.dist-info/METADATA +316 -0
  90. networka-0.1.9.dist-info/RECORD +93 -0
  91. networka-0.1.9.dist-info/WHEEL +4 -0
  92. networka-0.1.9.dist-info/entry_points.txt +2 -0
  93. networka-0.1.9.dist-info/licenses/LICENSE +73 -0
@@ -0,0 +1,339 @@
1
+ #!/usr/bin/env bash
2
+ # Dynamic Bash completion for nw powered by `nw __complete`.
3
+
4
+ # Guard for old bash without completion helpers
5
+ if ! declare -F _init_completion >/dev/null 2>&1; then
6
+ _init_completion() { return 0; }
7
+ fi
8
+
9
+ _nw() {
10
+ local cur prev words cword
11
+ _init_completion || {
12
+ cur=${COMP_WORDS[COMP_CWORD]}
13
+ prev=${COMP_WORDS[COMP_CWORD-1]}
14
+ }
15
+
16
+ cur=${cur:-${COMP_WORDS[COMP_CWORD]}}
17
+ prev=${prev:-${COMP_WORDS[COMP_CWORD-1]}}
18
+
19
+ # Helper: find value after an option (e.g. --config)
20
+ _after_opt() {
21
+ local opt="$1"; local i
22
+ for (( i=1; i<${#COMP_WORDS[@]}-1; i++ )); do
23
+ if [[ "${COMP_WORDS[i]}" == "$opt" ]]; then
24
+ echo "${COMP_WORDS[i+1]}"; return 0
25
+ fi
26
+ done
27
+ return 1
28
+ }
29
+
30
+ # Resolve config option if provided
31
+ local cfg
32
+ cfg=$(_after_opt --config)
33
+ [[ -z "$cfg" ]] && cfg=$(_after_opt -c)
34
+ if [[ -z "$cfg" ]]; then
35
+ if [[ -d config || -f config/config.yml ]]; then
36
+ cfg="config"
37
+ else
38
+ cfg="devices.yml"
39
+ fi
40
+ fi
41
+
42
+ # Try to find nw command - check if available, otherwise try python module
43
+ local nw_cmd=""
44
+ if command -v nw >/dev/null 2>&1; then
45
+ nw_cmd="nw"
46
+ elif command -v python >/dev/null 2>&1; then
47
+ nw_cmd="python -m network_toolkit.cli"
48
+ else
49
+ return 0 # No way to run completion
50
+ fi
51
+
52
+ # Dynamic lists via hidden command with fallback
53
+ _nw_list() {
54
+ local what="$1"; shift
55
+ case "$what" in
56
+ commands)
57
+ # Try the completion command first, then fallback to static list
58
+ local result
59
+ result=$($nw_cmd __complete --for commands 2>/dev/null)
60
+ if [[ -n "$result" ]]; then
61
+ echo "$result"
62
+ else
63
+ echo "info run upload download backup firmware ssh diff list config schema complete"
64
+ fi
65
+ return ;;
66
+ devices)
67
+ # Try the completion command first, then fallback to parsing config
68
+ local result
69
+ result=$($nw_cmd __complete --for devices --config "$cfg" 2>/dev/null)
70
+ if [[ -n "$result" ]]; then
71
+ echo "$result"
72
+ else
73
+ # Fallback: parse devices.yml directly
74
+ if [[ -f "$cfg/devices.yml" ]]; then
75
+ grep -E "^ [a-zA-Z0-9_-]+:" "$cfg/devices.yml" 2>/dev/null | sed 's/^ \([^:]*\):.*/\1/' | tr '\n' ' '
76
+ elif [[ -f "$cfg" && "$cfg" =~ \.ya?ml$ ]]; then
77
+ grep -E "^ [a-zA-Z0-9_-]+:" "$cfg" 2>/dev/null | sed 's/^ \([^:]*\):.*/\1/' | tr '\n' ' '
78
+ fi
79
+ fi
80
+ return ;;
81
+ groups)
82
+ # Try the completion command first, then fallback to parsing config
83
+ local result
84
+ result=$($nw_cmd __complete --for groups --config "$cfg" 2>/dev/null)
85
+ if [[ -n "$result" ]]; then
86
+ echo "$result"
87
+ else
88
+ # Fallback: parse groups.yml directly
89
+ if [[ -f "$cfg/groups.yml" ]]; then
90
+ grep -E "^ [a-zA-Z0-9_-]+:" "$cfg/groups.yml" 2>/dev/null | sed 's/^ \([^:]*\):.*/\1/' | tr '\n' ' '
91
+ fi
92
+ fi
93
+ return ;;
94
+ sequences)
95
+ local dev="$1"; shift || true
96
+ local result
97
+ if [[ -n "$dev" ]]; then
98
+ result=$($nw_cmd __complete --for sequences --device "$dev" --config "$cfg" 2>/dev/null)
99
+ else
100
+ result=$($nw_cmd __complete --for sequences --config "$cfg" 2>/dev/null)
101
+ fi
102
+ if [[ -n "$result" ]]; then
103
+ echo "$result"
104
+ else
105
+ # Fallback: basic sequences
106
+ echo "system_info interface_status backup_config"
107
+ fi
108
+ return ;;
109
+ sequence-groups)
110
+ $nw_cmd __complete --for sequence-groups --config "$cfg" 2>/dev/null; return ;;
111
+ tags)
112
+ $nw_cmd __complete --for tags --config "$cfg" 2>/dev/null; return ;;
113
+ esac
114
+ }
115
+
116
+ local cmd
117
+ cmd=${COMP_WORDS[1]}
118
+
119
+ # First arg: suggest commands
120
+ if [[ ${COMP_CWORD} -eq 1 ]]; then
121
+ COMPREPLY=( $(compgen -W "$(_nw_list commands)" -- "$cur") )
122
+ return 0
123
+ fi
124
+
125
+ # File/directory/value options
126
+ case "$prev" in
127
+ --config|-c)
128
+ # complete YAML files and directories
129
+ local files dirs
130
+ files=$(compgen -f -X '!*.yml !*.yaml' -- "$cur")
131
+ dirs=$(compgen -d -- "$cur")
132
+ COMPREPLY=( $files $dirs )
133
+ return 0 ;;
134
+ --output-mode|-o)
135
+ COMPREPLY=( $(compgen -W "default light dark no-color raw" -- "$cur") ); return 0 ;;
136
+ --layout)
137
+ COMPREPLY=( $(compgen -W "tiled even-horizontal even-vertical main-horizontal main-vertical" -- "$cur") ); return 0 ;;
138
+ --results-dir)
139
+ COMPREPLY=( $(compgen -d -- "$cur") ); return 0 ;;
140
+ --raw)
141
+ COMPREPLY=( $(compgen -W "txt json" -- "$cur") ); return 0 ;;
142
+ --platform|-p)
143
+ COMPREPLY=( $(compgen -W "mikrotik_routeros" -- "$cur") ); return 0 ;;
144
+ --port)
145
+ COMPREPLY=( $(compgen -W "22 2222 8022" -- "$cur") ); return 0 ;;
146
+ --auth)
147
+ COMPREPLY=( $(compgen -W "key-first key password interactive" -- "$cur") ); return 0 ;;
148
+ esac
149
+
150
+ # Common options per command
151
+ local common_opts="--config -c --verbose -v --help -h"
152
+ local output_opts="--output-mode -o"
153
+ local run_opts="--store-results -s --results-dir --raw --interactive-auth -i --platform -p --port $output_opts $common_opts"
154
+ local upload_opts="--remote-name -r --verify --no-verify --checksum-verify --no-checksum-verify --max-concurrent -j $common_opts"
155
+ local download_opts="--delete-remote --keep-remote --verify --no-verify $common_opts"
156
+ local config_backup_opts="$common_opts"
157
+ local firmware_upgrade_opts="$common_opts"
158
+ local firmware_downgrade_opts="$common_opts"
159
+ local bios_upgrade_opts="$common_opts"
160
+ local list_devices_opts="$output_opts $common_opts"
161
+ local list_groups_opts="$output_opts $common_opts"
162
+ local list_sequences_opts="--vendor --category $output_opts $common_opts"
163
+ local ssh_opts="--config -c --auth --user --password --layout --session-name --window-name --reuse --sync --no-sync --use-sshpass --attach --no-attach --platform -p --port $common_opts"
164
+ local info_opts="--interactive-auth -i $output_opts $common_opts"
165
+ local config_validate_opts="$common_opts"
166
+ local config_init_opts="$common_opts --force"
167
+ local diff_opts="$common_opts"
168
+
169
+ # Helper: complete from a list or options
170
+ _opts() { COMPREPLY=( $(compgen -W "$1" -- "$cur") ); }
171
+
172
+ case "$cmd" in
173
+ info)
174
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
175
+ # Offer devices, groups, and sequences for info command
176
+ local devices groups sequences
177
+ devices=$(_nw_list devices)
178
+ groups=$(_nw_list groups)
179
+ sequences=$(_nw_list sequences)
180
+ _opts "$devices $groups $sequences"
181
+ else
182
+ _opts "$info_opts"
183
+ fi
184
+ ;;
185
+ run)
186
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
187
+ # Show groups first, then devices for friendlier targeting
188
+ local groups devices
189
+ groups=$(_nw_list groups)
190
+ devices=$(_nw_list devices)
191
+ _opts "$groups $devices"
192
+ elif [[ ${COMP_CWORD} -eq 3 ]]; then
193
+ local target="${COMP_WORDS[2]}"
194
+ # If target is a device, include vendor/device sequences
195
+ _opts "$(_nw_list sequences "$target")"
196
+ else
197
+ _opts "$run_opts"
198
+ fi
199
+ ;;
200
+ ssh)
201
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
202
+ local groups devices
203
+ groups=$(_nw_list groups)
204
+ devices=$(_nw_list devices)
205
+ _opts "$groups $devices"
206
+ else
207
+ if [[ $cur == -* ]]; then _opts "$ssh_opts"; fi
208
+ fi
209
+ ;;
210
+ upload)
211
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
212
+ local groups devices
213
+ groups=$(_nw_list groups)
214
+ devices=$(_nw_list devices)
215
+ _opts "$devices $groups"
216
+ elif [[ ${COMP_CWORD} -eq 3 ]]; then
217
+ compopt -o filenames 2>/dev/null || true
218
+ COMPREPLY=( $(compgen -f -- "$cur") )
219
+ else
220
+ _opts "$upload_opts"
221
+ fi
222
+ ;;
223
+ download)
224
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
225
+ local groups devices
226
+ groups=$(_nw_list groups)
227
+ devices=$(_nw_list devices)
228
+ _opts "$devices $groups"
229
+ elif [[ ${COMP_CWORD} -eq 4 ]]; then
230
+ # local path argument
231
+ compopt -o filenames 2>/dev/null || true
232
+ COMPREPLY=( $(compgen -d -- "$cur") )
233
+ else
234
+ _opts "$download_opts"
235
+ fi
236
+ ;;
237
+ backup)
238
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
239
+ _opts "config comprehensive vendors"
240
+ elif [[ ${COMP_CWORD} -eq 3 && ("${COMP_WORDS[2]}" == "config" || "${COMP_WORDS[2]}" == "comprehensive") ]]; then
241
+ local groups devices
242
+ groups=$(_nw_list groups)
243
+ devices=$(_nw_list devices)
244
+ _opts "$devices $groups"
245
+ else
246
+ _opts "$config_backup_opts"
247
+ fi
248
+ ;;
249
+ firmware)
250
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
251
+ _opts "upgrade downgrade bios vendors"
252
+ elif [[ ${COMP_CWORD} -eq 3 && ("${COMP_WORDS[2]}" == "upgrade" || "${COMP_WORDS[2]}" == "downgrade" || "${COMP_WORDS[2]}" == "bios") ]]; then
253
+ local groups devices
254
+ groups=$(_nw_list groups)
255
+ devices=$(_nw_list devices)
256
+ _opts "$devices $groups"
257
+ else
258
+ _opts "$firmware_upgrade_opts"
259
+ fi
260
+ ;;
261
+ diff)
262
+ if [[ ${COMP_CWORD} -eq 2 ]]; then
263
+ local groups devices
264
+ groups=$(_nw_list groups)
265
+ devices=$(_nw_list devices)
266
+ _opts "$devices $groups"
267
+ else
268
+ _opts "$diff_opts"
269
+ fi
270
+ ;;
271
+ list)
272
+ # Handle list subcommands
273
+ if [[ ${#COMP_WORDS[@]} -eq 3 ]]; then
274
+ # If we're at position 2 (after "nw list"), suggest subcommands
275
+ COMPREPLY=( $(compgen -W "devices groups sequences supported-types" -- "$cur") )
276
+ elif [[ ${#COMP_WORDS[@]} -gt 3 ]]; then
277
+ # Handle options for specific subcommands
278
+ case "${COMP_WORDS[2]}" in
279
+ devices)
280
+ _opts "$list_devices_opts" ;;
281
+ groups)
282
+ _opts "$list_groups_opts" ;;
283
+ sequences)
284
+ _opts "$list_sequences_opts" ;;
285
+ supported-types)
286
+ _opts "$common_opts" ;;
287
+ *)
288
+ _opts "$common_opts" ;;
289
+ esac
290
+ fi
291
+ ;;
292
+ config)
293
+ # Handle config subcommands
294
+ if [[ $COMP_CWORD -gt 2 ]]; then
295
+ local config_subcommand="${COMP_WORDS[2]}"
296
+ case "$config_subcommand" in
297
+ init)
298
+ _opts "$config_init_opts" ;;
299
+ validate)
300
+ _opts "$config_validate_opts" ;;
301
+ *)
302
+ _opts "$common_opts" ;;
303
+ esac
304
+ else
305
+ # Complete config subcommands
306
+ COMPREPLY=( $(compgen -W "init validate" -- "$cur") )
307
+ fi
308
+ ;;
309
+ complete)
310
+ # Handle completion command options
311
+ _opts "$common_opts --for --device"
312
+ ;;
313
+ schema)
314
+ # Handle schema subcommands
315
+ if [[ $COMP_CWORD -gt 2 ]]; then
316
+ local schema_subcommand="${COMP_WORDS[2]}"
317
+ case "$schema_subcommand" in
318
+ update|info)
319
+ _opts "$common_opts" ;;
320
+ *)
321
+ _opts "$common_opts" ;;
322
+ esac
323
+ else
324
+ # Complete schema subcommands
325
+ COMPREPLY=( $(compgen -W "update info" -- "$cur") )
326
+ fi
327
+ ;;
328
+ *)
329
+ # Fallback: suggest common opts
330
+ if [[ $cur == -* ]]; then _opts "$common_opts"; fi ;;
331
+ esac
332
+
333
+ return 0
334
+ }
335
+
336
+ # Register the completion function for nw and common aliases
337
+ complete -F _nw nw 2>/dev/null || true
338
+ complete -F _nw networka 2>/dev/null || true
339
+ complete -F _nw network-toolkit 2>/dev/null || true
@@ -0,0 +1,136 @@
1
+ #compdef nw networka network-toolkit
2
+
3
+ _nw_complete() {
4
+ local -a cmds opts values
5
+ local curcontext="$curcontext" state line
6
+ typeset -A opt_args
7
+
8
+ _arguments -C \
9
+ '(- : )1:command:->cmds' \
10
+ '(-c --config)'{-c,--config}'[Configuration file or directory]:path:_files -g "*.yml *.yaml" -/' \
11
+ '(-v --verbose)'{-v,--verbose}'[Enable verbose output]' \
12
+ '(-h --help)'{-h,--help}'[Show help]' \
13
+ '(-s --store-results)'{-s,--store-results}'[Store results]' \
14
+ '(-)-results-dir[Results directory]:directory:_files -/' \
15
+ '(-)--raw[Raw output format]:format:(txt json)' \
16
+ '(-o --output-mode)'{-o,--output-mode}'[Output mode]:mode:(default light dark no-color raw)' \
17
+ '(-i --interactive-auth)'{-i,--interactive-auth}'[Interactive authentication]' \
18
+ '(-p --platform)'{-p,--platform}'[Platform type]:platform:(mikrotik_routeros)' \
19
+ '(-)--port[SSH port]:port:(22 2222 8022)' \
20
+ '(-)--layout[tmux layout]:layout:(tiled even-horizontal even-vertical main-horizontal main-vertical)' \
21
+ '(-)--auth[Authentication mode]:auth:(key-first key password interactive)' \
22
+ '*::args:->args'
23
+
24
+ local cfg
25
+ if [[ -n ${opt_args[--config]} ]]; then
26
+ cfg=${opt_args[--config]}
27
+ elif [[ -d config || -f config/config.yml ]]; then
28
+ cfg="config"
29
+ else
30
+ cfg="devices.yml"
31
+ fi
32
+
33
+ case $state in
34
+ cmds)
35
+ values=(${(f)"$(nw __complete --for commands 2>/dev/null)"})
36
+ _describe -t commands 'nw commands' values && return
37
+ ;;
38
+ args)
39
+ case $words[2] in
40
+ info)
41
+ # Offer devices, groups, and sequences for info command
42
+ local -a devices groups sequences annotated
43
+ devices=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"})
44
+ groups=(${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
45
+ sequences=(${(f)"$(nw __complete --for sequences --config \"$cfg\" 2>/dev/null)"})
46
+ annotated=()
47
+ local d g s
48
+ # Annotate device entries
49
+ for d in $devices; do
50
+ annotated+=("$d:device")
51
+ done
52
+ # Annotate group entries
53
+ for g in $groups; do
54
+ annotated+=("$g:group")
55
+ done
56
+ # Annotate sequence entries
57
+ for s in $sequences; do
58
+ annotated+=("$s:sequence")
59
+ done
60
+ _describe -t targets 'info targets' annotated && return ;;
61
+ run)
62
+ if (( CURRENT == 3 )); then
63
+ # Groups first, then devices for better UX, annotate entries
64
+ local -a groups devices annotated
65
+ groups=(${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
66
+ devices=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"})
67
+ annotated=()
68
+ local g d
69
+ for g in $groups; do annotated+="$g:group"; done
70
+ for d in $devices; do annotated+="$d:device"; done
71
+ values=($annotated)
72
+ _describe -t targets 'targets (groups first)' values && return
73
+ elif (( CURRENT == 4 )); then
74
+ local target=$words[3]
75
+ values=(${(f)"$(nw __complete --for sequences --device \"$target\" --config \"$cfg\" 2>/dev/null)"})
76
+ _describe -t sequences 'sequences' values && return
77
+ fi ;;
78
+ ssh)
79
+ if (( CURRENT == 3 )); then
80
+ local -a groups devices annotated
81
+ groups=(${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
82
+ devices=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"})
83
+ annotated=()
84
+ local g d
85
+ for g in $groups; do annotated+="$g:group"; done
86
+ for d in $devices; do annotated+="$d:device"; done
87
+ values=($annotated)
88
+ _describe -t targets 'targets (groups first)' values && return
89
+ fi ;;
90
+ config)
91
+ if (( CURRENT == 3 )); then
92
+ values=("init:Initialize a network toolkit configuration" "validate:Validate the configuration file")
93
+ _describe -t subcommands 'config subcommands' values && return
94
+ fi ;;
95
+ schema)
96
+ if (( CURRENT == 3 )); then
97
+ values=("update:Update JSON schemas for YAML editor validation" "info:Display information about JSON schema files")
98
+ _describe -t subcommands 'schema subcommands' values && return
99
+ fi ;;
100
+ backup)
101
+ if (( CURRENT == 3 )); then
102
+ values=("config:Backup device configuration" "comprehensive:Perform comprehensive backup" "vendors:Show vendor support")
103
+ _describe -t subcommands 'backup subcommands' values && return
104
+ elif (( CURRENT == 4 && ($words[3] == "config" || $words[3] == "comprehensive") )); then
105
+ values=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"} ${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
106
+ _describe -t targets 'targets' values && return
107
+ fi ;;
108
+ firmware)
109
+ if (( CURRENT == 3 )); then
110
+ values=("upgrade:Upgrade firmware" "downgrade:Downgrade firmware" "bios:Upgrade BIOS" "vendors:Show vendor support")
111
+ _describe -t subcommands 'firmware subcommands' values && return
112
+ elif (( CURRENT == 4 && ($words[3] == "upgrade" || $words[3] == "downgrade" || $words[3] == "bios") )); then
113
+ values=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"} ${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
114
+ _describe -t targets 'targets' values && return
115
+ fi ;;
116
+ list)
117
+ if (( CURRENT == 3 )); then
118
+ values=("devices:List all configured network devices" "groups:List all configured device groups" "sequences:List available sequences" "supported-types:List supported device types")
119
+ _describe -t subcommands 'list subcommands' values && return
120
+ fi ;;
121
+ complete)
122
+ # Handle completion command - mostly internal use
123
+ ;;
124
+ upload|download|diff)
125
+ if (( CURRENT == 3 )); then
126
+ values=(${(f)"$(nw __complete --for devices --config \"$cfg\" 2>/dev/null)"} ${(f)"$(nw __complete --for groups --config \"$cfg\" 2>/dev/null)"})
127
+ _describe -t targets 'targets' values && return
128
+ fi ;;
129
+ esac
130
+ ;;
131
+ esac
132
+
133
+ return 0
134
+ }
135
+
136
+ compdef _nw_complete nw networka network-toolkit
@@ -0,0 +1,15 @@
1
+ """Transport layer abstractions for nw.
2
+
3
+ Option B: sync transport interface with Scrapli adapter.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from network_toolkit.transport.interfaces import CommandResult, Transport
9
+ from network_toolkit.transport.scrapli_sync import ScrapliSyncTransport
10
+
11
+ __all__ = [
12
+ "CommandResult",
13
+ "ScrapliSyncTransport",
14
+ "Transport",
15
+ ]
@@ -0,0 +1,113 @@
1
+ """Transport factory for creating different connection types."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Any, Protocol
6
+
7
+ if TYPE_CHECKING:
8
+ from nornir import Nornir # pragma: no cover
9
+
10
+ from network_toolkit.config import NetworkConfig
11
+ from network_toolkit.transport.interfaces import Transport
12
+
13
+
14
+ class TransportFactory(Protocol):
15
+ """Factory for creating transport instances."""
16
+
17
+ def create_transport(
18
+ self,
19
+ device_name: str,
20
+ config: NetworkConfig,
21
+ connection_params: dict[str, Any],
22
+ ) -> Transport:
23
+ """Create a transport instance for the given device."""
24
+ ...
25
+
26
+
27
+ class ScrapliTransportFactory:
28
+ """Factory for creating Scrapli-based transports."""
29
+
30
+ def create_transport(
31
+ self,
32
+ device_name: str,
33
+ config: NetworkConfig,
34
+ connection_params: dict[str, Any],
35
+ ) -> Transport:
36
+ """Create a Scrapli transport instance."""
37
+ # Import Scrapli symbol from device module so unit tests that patch
38
+ # `network_toolkit.device.Scrapli` continue to intercept driver creation.
39
+ # Also import the adapter symbol from transport module.
40
+ from network_toolkit.device import Scrapli as DeviceScrapli
41
+ from network_toolkit.transport import (
42
+ ScrapliSyncTransport as DeviceScrapliSyncTransport,
43
+ )
44
+
45
+ params = dict(connection_params)
46
+ params["transport"] = (params.get("transport") or "system").lower()
47
+
48
+ # Create Scrapli driver and wrap it in our adapter
49
+ driver = DeviceScrapli(**params)
50
+ adapter = DeviceScrapliSyncTransport(driver)
51
+ # Expose raw driver for callers/tests that inspect underlying open/close
52
+ try:
53
+ adapter._raw_driver = driver # type: ignore[attr-defined]
54
+ except Exception:
55
+ pass
56
+ return adapter
57
+
58
+
59
+ class NornirNetmikoTransportFactory:
60
+ """Factory for creating Nornir+Netmiko based transports."""
61
+
62
+ def __init__(self) -> None:
63
+ """Initialize the factory."""
64
+ self._nornir_runner: Nornir | None = None
65
+
66
+ def create_transport(
67
+ self,
68
+ device_name: str,
69
+ config: NetworkConfig,
70
+ connection_params: dict[str, Any],
71
+ ) -> Transport:
72
+ """Create a Nornir+Netmiko transport instance."""
73
+ # nornir_netmiko transport coming soon - use scrapli for now
74
+ msg = (
75
+ "nornir_netmiko transport is not yet fully implemented. "
76
+ "This feature is coming soon. Please use 'scrapli' transport for now."
77
+ )
78
+ raise NotImplementedError(msg)
79
+
80
+ def _setup_nornir(self, config: NetworkConfig) -> Nornir:
81
+ """Setup Nornir runner with inventory from config."""
82
+ try:
83
+ # Import nornir-netmiko to register its plugins
84
+ import importlib.util
85
+
86
+ if importlib.util.find_spec("nornir_netmiko"):
87
+ import nornir_netmiko # type: ignore # noqa: F401
88
+ from nornir.core import Nornir # type: ignore
89
+ except ImportError as e:
90
+ error_msg = "Nornir package required. Install with: pip install nornir"
91
+ raise ImportError(error_msg) from e
92
+
93
+ from network_toolkit.transport.nornir_inventory import build_nornir_inventory
94
+
95
+ # Build inventory directly
96
+ inventory = build_nornir_inventory(config)
97
+
98
+ # Create Nornir instance directly with inventory - simplest approach
99
+ nr = Nornir(inventory=inventory)
100
+ return nr
101
+
102
+
103
+ def get_transport_factory(transport_type: str = "scrapli") -> TransportFactory:
104
+ """Get the appropriate transport factory."""
105
+ if transport_type == "scrapli":
106
+ return ScrapliTransportFactory()
107
+ else:
108
+ supported_transports = ["scrapli"]
109
+ error_msg = (
110
+ f"Unknown transport type: '{transport_type}'. "
111
+ f"Supported transports: {', '.join(supported_transports)}"
112
+ )
113
+ raise ValueError(error_msg)
@@ -0,0 +1,46 @@
1
+ """Transport interfaces for device command execution.
2
+
3
+ Keep it minimal: sync-only, small contract.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from dataclasses import dataclass
9
+ from typing import Protocol, runtime_checkable
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class CommandResult:
14
+ """Result of a command execution."""
15
+
16
+ result: str
17
+ failed: bool = False
18
+
19
+
20
+ @dataclass(frozen=True)
21
+ class ConnectionState:
22
+ """Connection state information."""
23
+
24
+ connected: bool
25
+ transport_type: str
26
+ device_name: str
27
+ host: str
28
+ port: int
29
+ last_activity: float | None = None
30
+
31
+
32
+ @runtime_checkable
33
+ class Transport(Protocol):
34
+ """Minimal sync transport contract."""
35
+
36
+ def open(self) -> None: # pragma: no cover - thin adapter
37
+ ...
38
+
39
+ def close(self) -> None: # pragma: no cover - thin adapter
40
+ ...
41
+
42
+ def send_command(self, command: str) -> CommandResult: ...
43
+
44
+ def send_interactive(
45
+ self, interact_events: list[tuple[str, str, bool]], timeout_ops: float
46
+ ) -> str: ...