Skip to content

Commit

Permalink
Add methods.
Browse files Browse the repository at this point in the history
Add new methods for often used commands.
  • Loading branch information
a0x77n committed Oct 3, 2016
1 parent 1faa380 commit b25559f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import code
import json
import os
import readline
import sys
import json

from octopus.server.DBInterface import ResultTransformer
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):
self.lines = []
def pushText(self,text):

def pushText(self, text):
self.pushLines(text.splitlines())
def pushLines(self,lines):

def pushLines(self, lines):
self.lines = lines + self.lines

def next(self):
return self.lines.pop(0)

def empty(self):
return self.lines == []

def iterate(self):
while not self.empty():
yield self.next()
Expand All @@ -29,28 +35,29 @@ class OctopusInteractiveConsole(code.InteractiveConsole):
def __init__(self, octopus_shell, locals=None):
def reload(path=config["steps"]["dir"]):
_reload(octopus_shell, path)

def include(path):
f = open(path,'r')
f = open(path, 'r')
self.input_lines.pushText(f.read())
f.close()

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

def _preprocess(self,source, filename, symbol):
def _preprocess(self, source, filename, symbol):
self.input_lines = ScriptInputProvider()
self.input_lines.pushText(source)
lines = source.splitlines()
scriptlines = []
codeblock = []
line_no = 0
for l in self.input_lines.iterate():
if len(l)>0 and l[0] == '!':
if len(l) > 0 and l[0] == '!':
codeblock.append(l[1:])
else:
if len(codeblock)>0:
c = code.compile_command("\n".join(codeblock),filename,'exec')
if len(codeblock) > 0:
c = code.compile_command("\n".join(codeblock), filename, 'exec')
codeblock = []
if c == None:
raise Exception("Incomplete command in block ending at line {}".format(line_no))
Expand All @@ -64,23 +71,27 @@ def toggleJSON(self):
# 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.octopus_shell.toggle_json()
self.json_enabled = True

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

source = self._preprocess(source,filename,symbol)
source = self._preprocess(source, filename, symbol)

if not source:
return False

if source[0] == '!':
return super().runsource(source[1:], filename, symbol)
try:
response = self.octopus_shell.run_command(source)
if source == "quit":
self._save_history()
# terminate shell at server side
self.octopus_shell.quit()
# terminate shell at client side
return super().runsource("exit()", filename, symbol)
else:
response = self.octopus_shell.run_command(source)
except Exception as e:
if "[MultipleCompilationErrorsException]" in str(e):
# incomplete input
Expand All @@ -92,7 +103,7 @@ def runsource(self, source, filename="<input>", symbol="single"):

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

for line in response:
self.write(line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def run_command(self, command):
raise RuntimeError(response)
return response.split('\n')

def quit(self):
self.run_command("quit")

def toggle_json(self):
self.run_command("toggle_json")

def close(self):
self._socket.close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ if args.subcommand in ['connect', 'co']:

if len(shells) == 0 and not args.dbname:
print("No matching shells found.", file=sys.stderr)
elif args.dbname and ( len(shells) == 0 or args.newshell ):
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, args.raw_output)
Expand Down

0 comments on commit b25559f

Please sign in to comment.