Michael Pasternak has uploaded a new change for review.

Change subject: cli: implement CLI invocation capabilities #895559
......................................................................

cli: implement CLI invocation capabilities #895559

Change-Id: Ie1d31b3321441b7efef6a27f6223ef96c803f1e5
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=895559
Signed-off-by: Michael pasternak <[email protected]>
---
M src/cli/context.py
M src/ovirtcli/infrastructure/options.py
M src/ovirtcli/infrastructure/settings.py
M src/ovirtcli/shell/engineshell.py
4 files changed, 64 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/18/20618/1

diff --git a/src/cli/context.py b/src/cli/context.py
index 25e667e..66ea6de 100644
--- a/src/cli/context.py
+++ b/src/cli/context.py
@@ -86,7 +86,7 @@
         logger.setLevel(logging.INFO)
         self._logger = logger
 
-    def __collect_connection_data(self):
+    def _collect_connection_data(self):
         try:
             if self.settings['ovirt-shell:url'] == '' and \
             not self.__is_option_specified_in_cli_args('--url')  and \
@@ -127,9 +127,6 @@
             ('-c' in self.args or '--connect' in self.args)) or \
         self.settings.get('cli:autoconnect')
 
-    def __is_help(self):
-        return '-h' in self.args or '--help' in self.args
-
     def _load_settings(self):
         """Load settings."""
         found, old_format = self.settings.load_config_file()
@@ -138,8 +135,6 @@
         elif old_format:
             self.settings.write_config_file()
         self.__exclude_app_options()
-        if not self.__is_help() and self.__is_auto_connect():
-            self.__collect_connection_data()
         self.settings.add_callback('cli:debug', self._set_debug)
         self._set_debug('cli:debug', self.settings['cli:debug'])
 
@@ -302,13 +297,30 @@
               )
         )
 
-    def _pint_text(self, text):
+    def _pint_text(self, text, file=sys.stdout):  # @ReservedAssignment
         """
-        prints text to stdout
+        prints text to output device
 
-        @param text: text
+        @param text: text to print
+        @param file: the output device to fetch encoding from
         """
-        sys.stdout.write("\n" + text + "\n")
+        encoding = self.__get_encoding(file)
+        file.write(
+               "\n" +
+               text.encode(encoding, "replace") +
+               "\n"
+        )
+
+    def __get_encoding(self, file):  # @ReservedAssignment
+        """
+        fetches encoding from the file
+        
+        @param file: the output device to fetch encoding from
+        """
+        encoding = getattr(file, "encoding", None)
+        if not encoding:
+            encoding = sys.getdefaultencoding()
+        return encoding
 
     def _read_command(self):
         """Parse input until we can parse at least one full command, and
diff --git a/src/ovirtcli/infrastructure/options.py 
b/src/ovirtcli/infrastructure/options.py
index 4474088..6315764 100644
--- a/src/ovirtcli/infrastructure/options.py
+++ b/src/ovirtcli/infrastructure/options.py
@@ -55,6 +55,8 @@
                         help='read commands from FILE instead of stdin')
         self.add_option('-e', '--extended-prompt', action='store_true',
                         help='display extra information in the prompt')
+        self.add_option('-E', '--execute-command',
+                        help='execute command and print the output to STDIO')
         self.disable_interspersed_args()
 
         # list of hidden app. options (long format)
diff --git a/src/ovirtcli/infrastructure/settings.py 
b/src/ovirtcli/infrastructure/settings.py
index 49e66bb..27a076c 100644
--- a/src/ovirtcli/infrastructure/settings.py
+++ b/src/ovirtcli/infrastructure/settings.py
@@ -74,7 +74,8 @@
         ('ovirt-shell:misc_commands', str, '%s shell commands:' % PRODUCT),
         ('ovirt-shell:version', str, ''),
         ('ovirt-shell:prompt', str, ''),
-        ('ovirt-shell:extended_prompt', boolean, False)
+        ('ovirt-shell:extended_prompt', boolean, False),
+        ('ovirt-shell:execute_command', str, None)
     ]
 
     # config file white list
diff --git a/src/ovirtcli/shell/engineshell.py 
b/src/ovirtcli/shell/engineshell.py
index c55ea9f..75d62ff 100644
--- a/src/ovirtcli/shell/engineshell.py
+++ b/src/ovirtcli/shell/engineshell.py
@@ -240,10 +240,11 @@
         triggered when StateMachine.CONNECTED state is acquired
         """
         self.context.history.enable()
-        self._print(
-           OvirtCliSettings.CONNECTED_TEMPLATE % \
-           self.context.settings.get('ovirt-shell:version')
-        )
+        if not self.context.settings.get('ovirt-shell:execute_command'):
+            self._print(
+               OvirtCliSettings.CONNECTED_TEMPLATE % \
+               self.context.settings.get('ovirt-shell:version')
+            )
 
         self.__set_prompt(
             mode=PromptMode.Connected
@@ -254,7 +255,10 @@
         triggered when StateMachine.DISCONNECTED state is acquired
         """
         self.context.history.disable()
-        self._print(OvirtCliSettings.DISCONNECTED_TEMPLATE)
+        if not self.context.settings.get('ovirt-shell:execute_command'):
+            self._print(
+                OvirtCliSettings.DISCONNECTED_TEMPLATE
+            )
         self.__set_prompt(
             mode=PromptMode.Disconnected
         )
@@ -296,12 +300,17 @@
         del args
         self.__persist_cmd_options(opts)
         if opts.connect or self.context.settings.get('cli:autoconnect'):
-            self.do_clear('')
+            self.context._collect_connection_data()
+            if not self.context.settings.get('ovirt-shell:execute_command'):
+                self.do_clear('')
             self.do_connect(s)
             if opts.file:
-                cmd.Cmd.intro = None
                 self.do_file(opts.file)
-            self.cmdloop(clear=False)
+            elif opts.execute_command:
+                self.__execute_command(opts.execute_command)
+                self.do_exit('')
+            else:
+                self.cmdloop(clear=False)
         else:
             self.cmdloop()
 
@@ -406,8 +415,27 @@
         self.onError.fire()
         self.context._handle_exception(CommandError(msg))
 
-    def _print(self, msg):
-        self.context._pint_text(msg)
+    def _print(self, msg, file=sys.stdout):  # @ReservedAssignment
+        """
+        prints message to the output device (default stdout).
+        
+        @param msg: text to print
+        @param file: output device
+        """
+        self.context._pint_text(msg, file)
+
+    def __execute_command(self, execute):
+        """
+        executes command from the command line
+        via -E/--execute-command
+        
+        @param execute: the command to execute
+        """
+        self.context.settings['cli:autopage'] = False
+        for command in execute.split(';'):
+            line = self.precmd(command)
+            self.print_line(line)
+            self.onecmd(line)
 
     def __handler(self, signum, frame):
         self.onSigInt.fire()


-- 
To view, visit http://gerrit.ovirt.org/20618
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie1d31b3321441b7efef6a27f6223ef96c803f1e5
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Michael Pasternak <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to