Browse Source

Closes #5424: Allow passing Python code to nbshell using --command

Jeremy Stretch 5 years ago
parent
commit
27e27788cd
2 changed files with 18 additions and 3 deletions
  1. 4 0
      docs/release-notes/version-2.9.md
  2. 14 3
      netbox/extras/management/commands/nbshell.py

+ 4 - 0
docs/release-notes/version-2.9.md

@@ -2,6 +2,10 @@
 
 
 ## v2.9.11 (FUTURE)
 ## v2.9.11 (FUTURE)
 
 
+### Enhancements
+
+* [#5424](https://github.com/netbox-community/netbox/issues/5424) - Allow passing Python code to `nbshell` using `--command`
+
 ### Bug Fixes
 ### Bug Fixes
 
 
 * [#5383](https://github.com/netbox-community/netbox/issues/5383) - Fix setting user password via REST API
 * [#5383](https://github.com/netbox-community/netbox/issues/5383) - Fix setting user password via REST API

+ 14 - 3
netbox/extras/management/commands/nbshell.py

@@ -25,12 +25,18 @@ class Command(BaseCommand):
     help = "Start the Django shell with all NetBox models already imported"
     help = "Start the Django shell with all NetBox models already imported"
     django_models = {}
     django_models = {}
 
 
+    def add_arguments(self, parser):
+        parser.add_argument(
+            '-c', '--command',
+            help='Python code to execute (instead of starting an interactive shell)',
+        )
+
     def _lsmodels(self):
     def _lsmodels(self):
         for app, models in self.django_models.items():
         for app, models in self.django_models.items():
             app_name = apps.get_app_config(app).verbose_name
             app_name = apps.get_app_config(app).verbose_name
-            print('{}:'.format(app_name))
+            print(f'{app_name}:')
             for m in models:
             for m in models:
-                print('  {}'.format(m))
+                print(f'  {m}')
 
 
     def get_namespace(self):
     def get_namespace(self):
         namespace = {}
         namespace = {}
@@ -46,7 +52,7 @@ class Command(BaseCommand):
 
 
             # Constants
             # Constants
             try:
             try:
-                app_constants = sys.modules['{}.constants'.format(app)]
+                app_constants = sys.modules[f'{app}.constants']
                 for name in dir(app_constants):
                 for name in dir(app_constants):
                     namespace[name] = getattr(app_constants, name)
                     namespace[name] = getattr(app_constants, name)
             except KeyError:
             except KeyError:
@@ -64,5 +70,10 @@ class Command(BaseCommand):
         return namespace
         return namespace
 
 
     def handle(self, **options):
     def handle(self, **options):
+        # If Python code has been passed, execute it and exit.
+        if options['command']:
+            exec(options['command'], self.get_namespace())
+            return
+
         shell = code.interact(banner=BANNER_TEXT, local=self.get_namespace())
         shell = code.interact(banner=BANNER_TEXT, local=self.get_namespace())
         return shell
         return shell