Skip to content

Commit 568e23b

Browse files
committed
Console improvements
1 parent 42e68d3 commit 568e23b

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

mygeotab/cli.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def logout(self):
117117
with open(self._get_config_file(), "w") as configfile:
118118
config.write(configfile)
119119
self.credentials = None
120-
sys.exit(0)
121120

122121

123122
def login(session, user, password, database=None, server=None):
@@ -146,13 +145,7 @@ def login(session, user, password, database=None, server=None):
146145
sys.exit(0)
147146

148147

149-
@click.group(invoke_without_command=True, help="Lists active sessions")
150-
@click.pass_obj
151-
def sessions(session):
152-
"""Shows the current logged in sessions.
153-
154-
:param session: The current Session object.
155-
"""
148+
def list_active_sessions(session):
156149
active_sessions = session.get_sessions()
157150
if not active_sessions:
158151
click.echo("(No active sessions)")
@@ -161,6 +154,23 @@ def sessions(session):
161154
click.echo(active_session)
162155

163156

157+
@click.group(invoke_without_command=True, help="Lists and manages active session credentials")
158+
@click.option("--list", "-l", is_flag=True, help="Display active sessions")
159+
@click.pass_obj
160+
def sessions(session, list):
161+
"""Shows the current logged in sessions.
162+
163+
:param session: The current Session object.
164+
"""
165+
if list:
166+
list_active_sessions(session)
167+
return
168+
ctx = click.get_current_context()
169+
if ctx.invoked_subcommand is None:
170+
click.echo(ctx.get_help())
171+
ctx.exit()
172+
173+
164174
@click.command(help="Log out from a MyGeotab server")
165175
@click.argument("database", nargs=1, required=True)
166176
@click.pass_obj
@@ -172,6 +182,7 @@ def remove(session, database):
172182
"""
173183
session.load(database)
174184
session.logout()
185+
list_active_sessions(session)
175186

176187

177188
@click.command(help="Launch an interactive MyGeotab console")
@@ -183,9 +194,9 @@ def remove(session, database):
183194
def console(session, database=None, user=None, password=None, server=None):
184195
"""An interactive Python API console for MyGeotab
185196
186-
If IPython is installed, it will launch an interactive IPython console instead of the built-in Python console. The
187-
IPython console has numerous advantages over the stock Python console, including: colors, pretty printing,
188-
command auto-completion, and more.
197+
If either IPython or ptpython are installed, it will launch an interactive console using those libraries instead of
198+
the built-in Python console. Using IPython or ptpython has numerous advantages over the stock Python console,
199+
including: colors, pretty printing, command auto-completion, and more.
189200
190201
By default, all library objects are available as locals in the script, with 'myg' being the active API object.
191202
@@ -196,17 +207,27 @@ def console(session, database=None, user=None, password=None, server=None):
196207
:param server: The server ie. my23.geotab.com. Optional as this usually gets resolved upon authentication.
197208
"""
198209
local_vars = _populate_locals(database, password, server, session, user)
199-
version = "MyGeotab Console {0} [Python {1}]".format(mygeotab.__version__, sys.version.replace("\n", ""))
210+
myg_console_version = "MyGeotab Console {0}".format(mygeotab.__version__)
211+
version = "{0} [Python {1}]".format(myg_console_version, sys.version.replace("\n", ""))
200212
auth_line = ("Logged in as: %s" % session.credentials) if session.credentials else "Not logged in"
201213
banner = "\n".join([version, auth_line])
202214
try:
203-
from IPython import embed
215+
from ptpython.repl import embed
216+
217+
def configure(repl):
218+
repl.prompt_style = "ipython"
204219

205-
embed(banner1=banner, user_ns=local_vars)
220+
embed(globals=globals(), locals=local_vars, title="{0}. {1}".format(myg_console_version, auth_line),
221+
configure=configure)
206222
except ImportError:
207-
import code
223+
try:
224+
from IPython import embed
225+
226+
embed(banner1=banner, user_ns=local_vars, colors="neutral")
227+
except ImportError:
228+
import code
208229

209-
code.interact(banner, local=local_vars)
230+
code.interact(banner, local=local_vars)
210231

211232

212233
@click.group()

0 commit comments

Comments
 (0)