Skip to content

Commit 525212e

Browse files
committed
cleaning, docs
1 parent 361c2da commit 525212e

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

tmuxp/cli.py

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ def prompt_choices(name, choices, default=None, resolve=ascii_lowercase,
115115
return rv
116116

117117

118-
class ConfigCompleter(argcomplete.completers.FilesCompleter):
118+
class ConfigFileCompleter(argcomplete.completers.FilesCompleter):
119+
120+
""" argcomplete completer for tmuxp files. """
119121

120122
def __call__(self, prefix, **kwargs):
121123
completion = argcomplete.completers.FilesCompleter.__call__(
@@ -130,6 +132,8 @@ def __call__(self, prefix, **kwargs):
130132

131133
class TmuxinatorCompleter(argcomplete.completers.FilesCompleter):
132134

135+
""" argcomplete completer for Tmuxinator files. """
136+
133137
def __call__(self, prefix, **kwargs):
134138
completion = argcomplete.completers.FilesCompleter.__call__(
135139
self, prefix, **kwargs
@@ -145,6 +149,8 @@ def __call__(self, prefix, **kwargs):
145149

146150
class TeamocilCompleter(argcomplete.completers.FilesCompleter):
147151

152+
""" argcomplete completer for Teamocil files. """
153+
148154
def __call__(self, prefix, **kwargs):
149155
completion = argcomplete.completers.FilesCompleter.__call__(
150156
self, prefix, **kwargs
@@ -158,16 +164,19 @@ def __call__(self, prefix, **kwargs):
158164

159165

160166
def SessionCompleter(prefix, **kwargs):
167+
""" Return list of session names for argcomplete completer. """
161168
t = Server()
162-
return [s.get('session_name') for s in t._sessions if s.get('session_name').startswith(prefix)]
169+
return [s.get('session_name') for s in t._sessions
170+
if s.get('session_name').startswith(prefix)]
163171

164172

165173
def setupLogger(logger=None, level='INFO'):
166-
'''setup logging for CLI use.
174+
"""Setup logging for CLI use.
167175
168176
:param logger: instance of logger
169177
:type logger: :py:class:`Logger`
170-
'''
178+
179+
"""
171180
if not logger:
172181
logger = logging.getLogger()
173182
if not logger.handlers:
@@ -178,22 +187,24 @@ def setupLogger(logger=None, level='INFO'):
178187

179188

180189
def startup(config_dir):
181-
''' Initialize CLI.
190+
"""Initialize CLI.
182191
183192
:param config_dir: Config directory to search
184193
:type config_dir: string
185-
'''
194+
195+
"""
186196

187197
if not os.path.exists(config_dir):
188198
os.makedirs(config_dir)
189199

190200

191-
def build_workspace(config_file, args):
192-
''' build config workspace.
201+
def load_workspace(config_file, args):
202+
""" Build config workspace.
193203
194204
:param config_file: full path to config file
195205
:param type: string
196-
'''
206+
207+
"""
197208
logger.info('building %s.' % config_file)
198209

199210
sconfig = kaptan.Kaptan()
@@ -239,7 +250,8 @@ def build_workspace(config_file, args):
239250
return
240251

241252

242-
def subcommand_load(args):
253+
def command_load(args):
254+
""" Load a session from a tmuxp session file. """
243255
if args.list:
244256
startup(config_dir)
245257
configs_in_user = config.in_dir(config_dir)
@@ -272,14 +284,16 @@ def subcommand_load(args):
272284
file_user = os.path.join(config_dir, configfile)
273285
file_cwd = os.path.join(cwd_dir, configfile)
274286
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
275-
build_workspace(file_cwd, args)
287+
load_workspace(file_cwd, args)
276288
elif os.path.exists(file_user) and os.path.isfile(file_user):
277-
build_workspace(file_user, args)
289+
load_workspace(file_user, args)
278290
else:
279291
logger.error('%s not found.' % configfile)
280292

281293

282-
def subcommand_import_teamocil(args):
294+
def command_import_teamocil(args):
295+
""" Import teamocil config to tmuxp format. """
296+
283297
if args.list:
284298
try:
285299
configs_in_user = config.in_dir(
@@ -306,8 +320,7 @@ def subcommand_import_teamocil(args):
306320
)
307321

308322
print(output)
309-
310-
if args.config:
323+
elif args.config:
311324
configfile = os.path.relpath(args.config)
312325
configparser = kaptan.Kaptan(handler='yaml')
313326
configparser.import_config(configfile)
@@ -322,7 +335,8 @@ def subcommand_import_teamocil(args):
322335
print(newconfig)
323336

324337

325-
def subcommand_import_tmuxinator(args):
338+
def command_import_tmuxinator(args):
339+
""" Import tmuxinator config to tmuxp format. """
326340
if args.list:
327341
try:
328342
configs_in_user = config.in_dir(
@@ -365,7 +379,8 @@ def subcommand_import_tmuxinator(args):
365379
print(newconfig)
366380

367381

368-
def subcommand_convert(args):
382+
def command_convert(args):
383+
""" Convert tmuxp config to and from JSON and YAML. """
369384

370385
try:
371386
configfile = args.config
@@ -412,7 +427,8 @@ def subcommand_convert(args):
412427
print('written new config to <%s>.' % (newfile))
413428

414429

415-
def subcommand_attach_session(args):
430+
def command_attach_session(args):
431+
""" Command to attach / switch client to a tmux session."""
416432
commands = []
417433
ctext = args.session_name
418434

@@ -438,7 +454,8 @@ def subcommand_attach_session(args):
438454
print('Attaching client.')
439455

440456

441-
def subcommand_kill_session(args):
457+
def command_kill_session(args):
458+
""" Command to kill a tmux session."""
442459
commands = []
443460
ctext = args.session_name
444461

@@ -462,20 +479,21 @@ def subcommand_kill_session(args):
462479
logger.error(e)
463480

464481

465-
def cli_parser():
482+
def get_parser():
483+
""" Return :py:class:`argparse.ArgumentParser` instance for CLI. """
466484

467485
parser = argparse.ArgumentParser(
468486
description='''\
469487
Launch tmux workspace. Help documentation: <http://tmuxp.rtfd.org>.
470488
''',
471489
)
472490

473-
subparsers = parser.add_subparsers(title='subcommands',
474-
description='valid subcommands',
491+
subparsers = parser.add_subparsers(title='commands',
492+
description='valid commands',
475493
help='additional help')
476494

477495
kill_session = subparsers.add_parser('kill-session')
478-
kill_session.set_defaults(callback=subcommand_kill_session)
496+
kill_session.set_defaults(callback=command_kill_session)
479497

480498
kill_session.add_argument(
481499
dest='session_name',
@@ -484,7 +502,7 @@ def cli_parser():
484502
).completer = SessionCompleter
485503

486504
attach_session = subparsers.add_parser('attach-session')
487-
attach_session.set_defaults(callback=subcommand_attach_session)
505+
attach_session.set_defaults(callback=command_attach_session)
488506

489507
attach_session.add_argument(
490508
dest='session_name',
@@ -513,8 +531,8 @@ def cli_parser():
513531
will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
514532
will also check for any ./*.yaml and ./*.json.
515533
''' % (cwd_dir + '/', config_dir),
516-
).completer = ConfigCompleter(allowednames=('.yaml', '.json'), directories=False)
517-
load.set_defaults(callback=subcommand_load)
534+
).completer = ConfigFileCompleter(allowednames=('.yaml', '.json'), directories=False)
535+
load.set_defaults(callback=command_load)
518536

519537
convert = subparsers.add_parser('convert')
520538

@@ -530,13 +548,13 @@ def cli_parser():
530548
will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
531549
will also check for any ./*.yaml and ./*.json.
532550
''' % (cwd_dir + '/', config_dir)
533-
).completer = ConfigCompleter(allowednames=('.yaml', '.json'), directories=False)
551+
).completer = ConfigFileCompleter(allowednames=('.yaml', '.json'), directories=False)
534552

535-
convert.set_defaults(callback=subcommand_convert)
553+
convert.set_defaults(callback=command_convert)
536554

537555
importparser = subparsers.add_parser('import')
538-
importsubparser = importparser.add_subparsers(title='subcommands',
539-
description='valid subcommands',
556+
importsubparser = importparser.add_subparsers(title='commands',
557+
description='valid commands',
540558
help='additional help')
541559

542560
import_teamocil = importsubparser.add_parser('teamocil')
@@ -556,7 +574,7 @@ def cli_parser():
556574
Checks current ~/.teamocil and current directory for yaml files.
557575
'''
558576
).completer = TeamocilCompleter(allowednames=('.yml'), directories=False)
559-
import_teamocil.set_defaults(callback=subcommand_import_teamocil)
577+
import_teamocil.set_defaults(callback=command_import_teamocil)
560578

561579
import_tmuxinator = importsubparser.add_parser('tmuxinator')
562580

@@ -576,7 +594,7 @@ def cli_parser():
576594
'''
577595
).completer = TmuxinatorCompleter(allowednames=('.yml'), directories=False)
578596

579-
import_tmuxinator.set_defaults(callback=subcommand_import_tmuxinator)
597+
import_tmuxinator.set_defaults(callback=command_import_tmuxinator)
580598

581599
parser.add_argument('--log-level', dest='log_level', default='INFO',
582600
metavar='log-level',
@@ -599,7 +617,7 @@ def cli_parser():
599617

600618
def main():
601619

602-
parser = cli_parser()
620+
parser = get_parser()
603621

604622
argcomplete.autocomplete(parser, always_complete_options=False)
605623

@@ -615,17 +633,17 @@ def main():
615633

616634
util.oh_my_zsh_auto_title()
617635

618-
if args.callback is subcommand_load:
619-
subcommand_load(args)
620-
elif args.callback is subcommand_convert:
621-
subcommand_convert(args)
622-
elif args.callback is subcommand_import_teamocil:
623-
subcommand_import_teamocil(args)
624-
elif args.callback is subcommand_import_tmuxinator:
625-
subcommand_import_tmuxinator(args)
626-
elif args.callback is subcommand_attach_session:
627-
subcommand_attach_session(args)
628-
elif args.callback is subcommand_kill_session:
629-
subcommand_kill_session(args)
636+
if args.callback is command_load:
637+
command_load(args)
638+
elif args.callback is command_convert:
639+
command_convert(args)
640+
elif args.callback is command_import_teamocil:
641+
command_import_teamocil(args)
642+
elif args.callback is command_import_tmuxinator:
643+
command_import_tmuxinator(args)
644+
elif args.callback is command_attach_session:
645+
command_attach_session(args)
646+
elif args.callback is command_kill_session:
647+
command_kill_session(args)
630648
else:
631649
parser.print_help()

0 commit comments

Comments
 (0)