Skip to content

Commit

Permalink
Merge pull request #130 from timhemel/shellrunner
Browse files Browse the repository at this point in the history
Add option to output raw data structure instead of string representation.
  • Loading branch information
a0x77n committed Oct 3, 2016
2 parents 884458b + 4c0f606 commit 1faa380
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import os
import readline
import sys
import json

from octopus.shell.completer.octopus_rlcompleter import OctopusShellCompleter
from octopus.shell.config.config import config
from octopus.shell.octopus_shell_utils import reload as _reload
from octopus.server.DBInterface import ResultTransformer

class ScriptInputProvider:
def __init__(self):
Expand Down Expand Up @@ -34,6 +36,7 @@ def include(path):

super().__init__(locals={"reload": reload, "include": include }, filename="<console>")
self.octopus_shell = octopus_shell
self.json_enabled = False

def _preprocess(self,source, filename, symbol):
self.input_lines = ScriptInputProvider()
Expand All @@ -56,6 +59,14 @@ def _preprocess(self,source, filename, symbol):
line_no += 1
return "\n".join(scriptlines)

def toggleJSON(self):
"""Show raw data structures in JSON format. Only works in scripted mode."""
# Output from commands like toggle_json on the interactive console return
# response data that cannot be serialized in JSON format, that is why this
# only works in non-interactive mode.
self.octopus_shell.run_command("toggle_json")
self.json_enabled = True

def runsource(self, source, filename="<input>", symbol="single"):

source = self._preprocess(source,filename,symbol)
Expand All @@ -79,9 +90,14 @@ def runsource(self, source, filename="<input>", symbol="single"):
if not response:
return False

if self.json_enabled:
response = ResultTransformer().transform(response)
response = [ json.dumps(response, sort_keys=True) ]

for line in response:
self.write(line)
self.write('\n')

return False

def write(self, data):
Expand Down
14 changes: 11 additions & 3 deletions projects/octopus/python/octopus-tools/scripts/octopus-shell
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ from octopus.shell.octopus_console import OctopusInteractiveConsole
from octopus.shell.octopus_shell import OctopusShellConnection


def connect(host, port, script=None, quit=False):
def connect(host, port, script=None, quit=False, raw_output=False):
try:
octopus_shell = OctopusShellConnection(host, port)
octopus_shell.connect()

try:
if script:
console = OctopusInteractiveConsole(octopus_shell)
if raw_output:
console.toggleJSON()
console.runsource(script.read())
else:
console = OctopusInteractiveConsole(octopus_shell)
Expand Down Expand Up @@ -97,6 +99,12 @@ connect_parser.add_argument(
default=False,
help="destroy the shell at the end of the session.")

connect_parser.add_argument(
"-r", "--raw-output",
action='store_true',
default=False,
help="Display output as raw JSON data structure instead of string representation (script mode only).")

connect_parser.add_argument(
"script",
type=argparse.FileType('r'),
Expand Down Expand Up @@ -147,11 +155,11 @@ if args.subcommand in ['connect', 'co']:
elif args.dbname and ( len(shells) == 0 or args.newshell ):
shellport = shell_manager.create(args.dbname)
print("Connecting to database '{}' on port {}.".format(args.dbname, shellport), file=sys.stderr)
connect(args.server, shellport, args.script, args.exitshell)
connect(args.server, shellport, args.script, args.exitshell, args.raw_output)
elif len(shells) == 1:
shellport, dbname, name, occupied = shells[0]
print("Connecting to database '{}' on port {}.".format(dbname, shellport), file=sys.stderr)
connect(args.server, shellport, args.script, args.exitshell)
connect(args.server, shellport, args.script, args.exitshell, args.raw_output)
else:
print("Found {} shells.".format(len(shells)), file=sys.stderr)
printshells(shells, sys.stderr)
Expand Down

0 comments on commit 1faa380

Please sign in to comment.