Skip to content

Commit 8598bd6

Browse files
Clean up install.py
1 parent e60ff79 commit 8598bd6

File tree

1 file changed

+68
-51
lines changed

1 file changed

+68
-51
lines changed

assembly/zip/install.py

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,28 @@
66
from jupyter_client.kernelspec import KernelSpecManager
77

88
ALIASES = {
9-
"JJAVA_CLASSPATH": {
9+
'JJAVA_CLASSPATH': {
1010
},
11-
"JJAVA_COMPILER_OPTS": {
11+
'JJAVA_COMPILER_OPTS': {
1212
},
13-
"JJAVA_STARTUP_SCRIPTS_PATH": {
13+
'JJAVA_STARTUP_SCRIPTS_PATH': {
1414
},
15-
"JJAVA_STARTUP_SCRIPT": {
15+
'JJAVA_STARTUP_SCRIPT': {
1616
},
17-
"JJAVA_TIMEOUT": {
18-
"NO_TIMEOUT": "-1",
19-
},
20-
17+
'JJAVA_TIMEOUT': {
18+
'NO_TIMEOUT': '-1',
19+
}
2120
}
2221

2322
NAME_MAP = {
24-
"classpath": "JJAVA_CLASSPATH",
25-
"comp-opts": "JJAVA_COMPILER_OPTS",
26-
"startup-scripts-path": "JJAVA_STARTUP_SCRIPTS_PATH",
27-
"startup-script": "JJAVA_STARTUP_SCRIPT",
28-
"timeout": "JJAVA_TIMEOUT",
29-
23+
'classpath': 'JJAVA_CLASSPATH',
24+
'comp-opts': 'JJAVA_COMPILER_OPTS',
25+
'startup-scripts-path': 'JJAVA_STARTUP_SCRIPTS_PATH',
26+
'startup-script': 'JJAVA_STARTUP_SCRIPT',
27+
'timeout': 'JJAVA_TIMEOUT'
3028
}
3129

30+
3231
def type_assertion(name, type_fn):
3332
env = NAME_MAP[name]
3433
aliases = ALIASES.get(env, {})
@@ -37,15 +36,19 @@ def checker(value):
3736
alias = aliases.get(value, value)
3837
type_fn(alias)
3938
return alias
39+
4040
setattr(checker, '__name__', getattr(type_fn, '__name__', 'type_fn'))
4141
return checker
4242

43+
4344
class EnvVar(argparse.Action):
4445
def __init__(self, option_strings, dest, aliases=None, name_map=None, list_sep=None, **kwargs):
4546
super(EnvVar, self).__init__(option_strings, dest, **kwargs)
4647

47-
if aliases is None: aliases = {}
48-
if name_map is None: name_map = {}
48+
if aliases is None:
49+
aliases = {}
50+
if name_map is None:
51+
name_map = {}
4952

5053
self.aliases = aliases
5154
self.name_map = name_map
@@ -55,8 +58,7 @@ def __init__(self, option_strings, dest, aliases=None, name_map=None, list_sep=N
5558
if name.lstrip('-') not in name_map:
5659
raise ValueError('Name "%s" is not mapped to an environment variable' % name.lstrip('-'))
5760

58-
59-
def __call__(self, parser, namespace, value, option_string=None):
61+
def __call__(self, arg_parser, namespace, value, option_string=None):
6062
if option_string is None:
6163
raise ValueError('option_string is required')
6264

@@ -75,8 +77,10 @@ def __call__(self, parser, namespace, value, option_string=None):
7577

7678
setattr(namespace, self.dest, env)
7779

80+
7881
if __name__ == '__main__':
79-
parser = argparse.ArgumentParser(description='Install the java kernel.')
82+
parser = argparse.ArgumentParser(description='Install the java kernel.',
83+
formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=30))
8084

8185
install_location = parser.add_mutually_exclusive_group()
8286
install_location.add_argument(
@@ -99,74 +103,87 @@ def __call__(self, parser, namespace, value, option_string=None):
99103
)
100104

101105
parser.add_argument(
102-
'--replace',
103-
help='Replace any existing kernel spec with this name.',
104-
action='store_true'
105-
)
106-
107-
parser.add_argument(
108-
"--classpath",
109-
dest="env",
106+
'--classpath',
107+
dest='env',
110108
action=EnvVar,
111109
aliases=ALIASES,
112110
name_map=NAME_MAP,
113-
help="A file path separator delimited list of classpath entries that should be available to the user code. **Important:** no matter what OS, this should use forward slash \"/\" as the file separator. Also each path may actually be a simple glob.",
114-
type=type_assertion("classpath", str),
111+
help='''
112+
A file path separator delimited list of classpath entries that should be available to the user code.
113+
**Important:** no matter what OS, this should use forward slash \"/\" as the file separator.
114+
Also each path may actually be a simple glob.
115+
''',
116+
type=type_assertion('classpath', str),
115117
list_sep=os.pathsep,
116118
)
117119
parser.add_argument(
118-
"--comp-opts",
119-
dest="env",
120+
'--comp-opts',
121+
dest='env',
120122
action=EnvVar,
121123
aliases=ALIASES,
122124
name_map=NAME_MAP,
123-
help="A space delimited list of command line options that would be passed to the `javac` command when compiling a project. For example `-parameters` to enable retaining parameter names for reflection.",
124-
type=type_assertion("comp-opts", str),
125-
list_sep=" ",
125+
help='''
126+
A space delimited list of command line options that would be passed to the `javac` command
127+
when compiling a project. For example `-parameters` to enable retaining parameter names for reflection.
128+
''',
129+
type=type_assertion('comp-opts', str),
130+
list_sep=' ',
126131
)
127132
parser.add_argument(
128-
"--startup-scripts-path",
129-
dest="env",
133+
'--startup-scripts-path',
134+
dest='env',
130135
action=EnvVar,
131136
aliases=ALIASES,
132137
name_map=NAME_MAP,
133-
help="A file path separator delimited list of `.jshell` scripts to run on startup. This includes jjava-jshell-init.jshell and jjava-display-init.jshell. **Important:** no matter what OS, this should use forward slash \"/\" as the file separator. Also each path may actually be a simple glob.",
134-
type=type_assertion("startup-scripts-path", str),
138+
help='''
139+
A file path separator delimited list of `.jshell` scripts to run on startup.'
140+
This includes jjava-jshell-init.jshell and jjava-display-init.jshell.
141+
**Important:** no matter what OS, this should use forward slash \"/\" as the file separator.
142+
Also each path may actually be a simple glob.
143+
''',
144+
type=type_assertion('startup-scripts-path', str),
135145
list_sep=os.pathsep,
136146
)
137147
parser.add_argument(
138-
"--startup-script",
139-
dest="env",
148+
'--startup-script',
149+
dest='env',
140150
action=EnvVar,
141151
aliases=ALIASES,
142152
name_map=NAME_MAP,
143-
help="A block of java code to run when the kernel starts up. This may be something like `import my.utils;` to setup some default imports or even `void sleep(long time) { try {Thread.sleep(time); } catch (InterruptedException e) { throw new RuntimeException(e); }}` to declare a default utility method to use in the notebook.",
144-
type=type_assertion("startup-script", str),
153+
help='''
154+
A block of java code to run when the kernel starts up.
155+
This may be something like `import my.utils;` to setup some default imports
156+
or even `void sleep(long time) { try {Thread.sleep(time); } catch (InterruptedException e)
157+
{ throw new RuntimeException(e); }}` to declare a default utility method to use in the notebook.
158+
''',
159+
type=type_assertion('startup-script', str),
145160
)
146161
parser.add_argument(
147-
"--timeout",
148-
dest="env",
162+
'--timeout',
163+
dest='env',
149164
action=EnvVar,
150165
aliases=ALIASES,
151166
name_map=NAME_MAP,
152-
help="A duration specifying a timeout (in milliseconds by default) for a _single top level statement_. If less than `1` then there is no timeout. If desired a time may be specified with a `TimeUnit` may be given following the duration number (ex `\"30 SECONDS\"`).",
153-
type=type_assertion("timeout", str),
167+
help='''
168+
A duration specifying a timeout (in milliseconds by default) for a _single top level statement_.
169+
If less than `1` then there is no timeout.
170+
If desired a time may be specified with a `TimeUnit` may be given following the duration number
171+
(ex `\"30 SECONDS\"`).
172+
''',
173+
type=type_assertion('timeout', str),
154174
)
155-
156175

157176
args = parser.parse_args()
158177

159-
if not hasattr(args, "env") or getattr(args, "env") is None:
160-
setattr(args, "env", {})
161-
178+
if not hasattr(args, 'env') or getattr(args, 'env') is None:
179+
setattr(args, 'env', {})
162180

163181
# Install the kernel
164182
install_dest = KernelSpecManager().install_kernel_spec(
165183
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'java'),
166184
kernel_name='java',
167185
user=args.user,
168186
prefix=sys.prefix if args.sys_prefix else args.prefix,
169-
replace=args.replace
170187
)
171188

172189
# Connect the self referencing token left in the kernel.json to point to it's install location.

0 commit comments

Comments
 (0)