Skip to content

Commit

Permalink
Merge branch 'postgres' of https://github.com/rzerres/opensips-cli in…
Browse files Browse the repository at this point in the history
…to rzerres-postgres
  • Loading branch information
liviuchircu committed Jun 14, 2019
2 parents 07f5f94 + 575d829 commit 233aea7
Show file tree
Hide file tree
Showing 6 changed files with 1,125 additions and 249 deletions.
58 changes: 50 additions & 8 deletions opensipscli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@
from opensipscli.modules import *

class OpenSIPSCLIShell(cmd.Cmd, object):

"""
OpenSIPS-Cli shell
"""
modules = {}
registered_atexit = False

def __init__(self, options):
"""
contructor for OpenSIPS-Cli
"""

self.debug = options.debug
self.execute = options.execute
Expand Down Expand Up @@ -79,6 +84,9 @@ def __init__(self, options):
self.update_instance(cfg.current_instance)

def update_logger(self):
"""
alter logging level
"""

# first of all, let's handle logging
if self.debug:
Expand All @@ -88,10 +96,16 @@ def update_logger(self):
logger.setLevel(level)

def clear_instance(self):
"""
update history
"""
# make sure we dump everything before swapping files
self.history_write()

def update_instance(self, instance):
"""
constructor of an OpenSIPS-Cli instance
"""

# first of all, let's handle logging
self.current_instance = instance
Expand Down Expand Up @@ -145,12 +159,18 @@ def update_instance(self, instance):
self.modules[name] = (imod, mod.__get_methods__(imod))

def history_write(self):
"""
save history file
"""
history_file = cfg.get('history_file')
logger.debug("saving history in {}".format(history_file))
os.makedirs(os.path.expanduser(os.path.dirname(history_file)), exist_ok=True)
readline.write_history_file(os.path.expanduser(history_file))

def preloop(self):
"""
preload a history file
"""
history_file = cfg.get('history_file')
logger.debug("using history file {}".format(history_file))
try:
Expand All @@ -162,7 +182,9 @@ def preloop(self):
atexit.register(self.history_write)

def postcmd(self, stop, line):

"""
post command after switching instance
"""
if self.current_instance != cfg.current_instance:
self.clear_instance()
self.update_instance(cfg.current_instance)
Expand All @@ -171,8 +193,10 @@ def postcmd(self, stop, line):

return stop

# Overwritten funtion in order not to print misc commands
def print_topics(self, header, cmds, cmdlen, maxcol):
"""
print topics, omit misc commands
"""
if header is not None:
if cmds:
self.stdout.write('%s\n' % str(header))
Expand All @@ -181,8 +205,10 @@ def print_topics(self, header, cmds, cmdlen, maxcol):
self.columnize(cmds, maxcol-1)
self.stdout.write('\n')

# Overwritten function in order to catch SIGINT
def cmdloop(self, intro=None):
"""
command loop, catching SIGINT
"""
if self.execute:
if len(self.command) < 1:
logger.error("no modules to run specified!")
Expand Down Expand Up @@ -215,12 +241,18 @@ def emptyline(self):
super().emptyline()

def complete_modules(self, text):
"""
complete modules selection based on given text
"""
l = [a for a in self.modules.keys() if a.startswith(text)]
if len(l) == 1:
l[0] = l[0] + " "
return l

def complete_functions(self, module, text, line, begidx, endidx):
"""
complete function selection based on given text
"""

# builtin commands
params = line.split()
Expand All @@ -243,6 +275,9 @@ def complete_functions(self, module, text, line, begidx, endidx):

# Overwritten function for our customized auto-complete
def complete(self, text, state):
"""
auto-complete selection based on given text and state parameters
"""
if state == 0:
origline = readline.get_line_buffer()
line = origline.lstrip()
Expand All @@ -268,12 +303,15 @@ def complete(self, text, state):

# Execute commands from Modules
def run_command(self, module, cmd, params):
"""
run a module command with given parameters
"""
try:
mod = self.modules[module]
except (AttributeError, KeyError):
logger.error("no module '{}' loaded".format(module))
return -1
# if the module dones not return any methods (returned None)
# if the module does not return any methods (returned None)
# we simply call the module's name method
if not mod[1]:
if params is not None:
Expand All @@ -289,7 +327,7 @@ def run_command(self, module, cmd, params):
logger.error("no command '{}' in module '{}'".
format(cmd, module))
return -1
logger.debug("running command '{}' '{}'".format(cmd, params))
logger.debug("running command '%s' with %i arguments: %s'", cmd, len(params), params)
return mod[0].__invoke__(cmd, params)

def default(self, line):
Expand All @@ -303,8 +341,10 @@ def default(self, line):
params = aux[2:]
self.run_command(module, cmd, params)

# Print history
def do_history(self, line):
"""
print entries in history file
"""
if not line:
try:
with open(os.path.expanduser(cfg.get('history_file'))) as hf:
Expand All @@ -313,8 +353,10 @@ def do_history(self, line):
except FileNotFoundError:
pass

# Used to set a dynamic setting
def do_set(self, line):
"""
handle dynamic settings (key-value pairs)
"""
parsed = line.split('=', 1)
if len(parsed) < 2:
logger.error("setting value format is 'key=value'!")
Expand Down
Loading

0 comments on commit 233aea7

Please sign in to comment.