Skip to content

Commit

Permalink
Load cube from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdartus committed Dec 16, 2014
1 parent 36008d8 commit 959e96d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
28 changes: 26 additions & 2 deletions libs/client/commandparser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from cmd import Cmd
from connector import Connector

Expand All @@ -18,9 +19,32 @@ def do_list(self, args):

def do_use(self, cube_name):
if not cube_name:
print('A name should be specified')
print('[Error] A name should be specified')
else:
self.connector.use_cube(cube_name)
try:
self.connector.use_cube(cube_name)
except Exception, e:
print('[Error] ' + e)

def do_load(self, args):
args = args.strip().split(" ")
if len(args) != 2:
print('[Error] An input file and a configuration file are needed')
return

paths = []
for f in args:
if not os.path.isfile(f):
print("Impossible to locate: {0}".format(f))
return
paths.append(os.path.abspath(f))

try:
ret = self.connector.load(paths[0], paths[1])
except Exception, e:
print('[Error] ' + e)
else:
print("Loading: {0}".format(ret["name"]))

def do_exit(self, args):
print("Bye!")
Expand Down
62 changes: 30 additions & 32 deletions libs/client/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,54 @@ class Connector(object):
def __init__(self, port):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.LINGER, 0)
socket.connect("tcp://127.0.0.1:{0}".format(port))
self.socket = socket
self.favorite_cube = None

@property
def is_connected(self):
payload = {
"cmd": "ping"
}
try:
self.socket.send_json(payload)
ret = self.socket.recv_json()
except zmq.error.ZMQError:
return False
else:
return ret.get('status') == "OK"

def send_command(self, cmd_name, data):
payload = {
"cmd": cmd_name,
"data": data
}
try:
self.socket.send_json(payload)

self.socket.send_json(payload)
poller = zmq.Poller()
poller.register(self.socket, zmq.POLLIN)

if poller.poll(2*1000):
ret = self.socket.recv_json()
except zmq.error.ZMQError:
return "Impossible to connect to NanocubeDB", None
else:
if ret['status'] == "error":
return None, ret['error']
else:
return ret['data'], None
raise IOError("Request timeout")

def list_cubes(self):
ret, err = self.send_command("list", None)
if err:
print(err)
return None
if ret.get('status') == "error":
raise Exception(ret.get('error'))
else:
return ret
return ret.get('data')

@property
def is_connected(self):
try:
self.send_command("ping", None)
except IOError:
return False
else:
return True

def list_cubes(self):
return self.send_command("list", None)

def use_cube(self, cube_name):
ret, err = self.send_command("info", {
ret = self.send_command("info", {
"cube": cube_name
})
if err:
print(err)
else:
self.favorite_cube = ret
self.favorite_cube = ret

def load_cube(self, input_file, config_file):
return self.send_command("load", {
"input": input_file,
"config": config_file
})

def close_connection(self):
self.socket.close()

0 comments on commit 959e96d

Please sign in to comment.