refactor in preparation for #11
This commit is contained in:
parent
9b32db482a
commit
be4cbdb8cb
66
jsvn
66
jsvn
@ -12,6 +12,7 @@ import time
|
||||
from subprocess import *
|
||||
import traceback
|
||||
import datetime
|
||||
import types
|
||||
|
||||
STATUS_LINE_REGEX = r'[ACDIMRX?!~ ][CM ][L ][+ ][SX ][KOTB ]..(.+)'
|
||||
|
||||
@ -109,17 +110,6 @@ def get_config(svn):
|
||||
|
||||
return config
|
||||
|
||||
def apply_aliases(config, argv):
|
||||
if not argv[0] in config['aliases']:
|
||||
return argv
|
||||
alias = config['aliases'][argv[0]]
|
||||
if type(alias) == str:
|
||||
return [alias] + argv[1:]
|
||||
elif type(alias) == list:
|
||||
return alias + argv[1:]
|
||||
sys.stderr.write('Unsupported type for alias "%s"\n' % alias)
|
||||
return argv
|
||||
|
||||
###########################################################################
|
||||
# Utility Functions #
|
||||
###########################################################################
|
||||
@ -1033,23 +1023,38 @@ def url(argv, svn, out):
|
||||
###########################################################################
|
||||
# Main #
|
||||
###########################################################################
|
||||
def main(argv):
|
||||
def do_cmd(argv, realsvn, config, expand=True):
|
||||
global using_color
|
||||
|
||||
realsvn = find_in_path('svn')
|
||||
config = get_config(realsvn)
|
||||
if config['svn']:
|
||||
realsvn = config['svn']
|
||||
out = sys.stdout
|
||||
orig_subcommand = argv[0] if len(argv) > 0 else ''
|
||||
if len(argv) > 0:
|
||||
argv = apply_aliases(config, argv)
|
||||
if len(argv) > 0 and argv[0].startswith('!'):
|
||||
if len(argv) == 0:
|
||||
Popen([realsvn]).wait()
|
||||
return
|
||||
|
||||
if expand and (argv[0] in config['aliases']):
|
||||
# expand aliases
|
||||
orig_subcommand = argv[0]
|
||||
alias = config['aliases'][argv[0]]
|
||||
if hasattr(alias, '__call__'):
|
||||
# alias is a python function, call it
|
||||
alias(argv)
|
||||
return
|
||||
elif type(alias) == types.StringType:
|
||||
argv = [alias] + argv[1:]
|
||||
elif type(alias) == types.ListType:
|
||||
argv = alias + argv[1:]
|
||||
else:
|
||||
sys.stderr.write('Unsupported type for alias "%s"\n' % alias)
|
||||
|
||||
# after expanding the alias, check if it is an external
|
||||
# command to launch
|
||||
if argv[0].startswith('!'):
|
||||
# execute an external program
|
||||
argv[0] = argv[0][1:] # strip leading '!'
|
||||
argv = [argv[0], orig_subcommand] + argv[1:]
|
||||
Popen(argv, shell=True).wait()
|
||||
return 0
|
||||
return
|
||||
|
||||
out = sys.stdout
|
||||
using_pager = False
|
||||
using_color = sys.stdout.isatty() and config['use_color']
|
||||
if sys.stdout.isatty() and config['use_pager']:
|
||||
@ -1091,22 +1096,31 @@ def main(argv):
|
||||
'stash': stash,
|
||||
}
|
||||
|
||||
do_normal_exec = True
|
||||
if len(argv) >= 1:
|
||||
do_native_exec = True
|
||||
if argv[0] in handlers:
|
||||
r = handlers[argv[0]](argv, realsvn, out)
|
||||
if r == RET_OK or r == RET_ERR:
|
||||
do_normal_exec = False
|
||||
do_native_exec = False
|
||||
elif argv[0].startswith('__'):
|
||||
# allow double-underscore commands to execute the native
|
||||
# subversion command (e.g. "__st")
|
||||
argv[0] = argv[0][2:]
|
||||
|
||||
if do_normal_exec:
|
||||
if do_native_exec:
|
||||
Popen([realsvn] + argv, stdout=out).wait()
|
||||
|
||||
if using_pager:
|
||||
out.close()
|
||||
pager_proc.wait()
|
||||
|
||||
def main(argv):
|
||||
realsvn = find_in_path('svn')
|
||||
config = get_config(realsvn)
|
||||
if config['svn']:
|
||||
realsvn = config['svn']
|
||||
|
||||
do_cmd(argv, realsvn, config)
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user