Passed
Push — master ( c25839...d5f461 )
by Christophe
01:15
created

aiscalator.jupyter.cli.update()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
# -*- coding: utf-8 -*-
2
# Apache Software License 2.0
3
#
4
# Copyright (c) 2018, Christophe Duong
5
#
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
#
10
# http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
"""
18
CLI module for Jupyter related commands.
19
"""
20
import logging
21
import os
22
import sys
23
24
import click
25
26
from aiscalator import __version__
27
from aiscalator.core.config import AiscalatorConfig
28
from aiscalator.jupyter import command
29
30
31
@click.group()
32
@click.version_option(version=__version__)
33
def jupyter():
34
    """Notebook environment to explore and handle data."""
35
    pass
36
37
38
@jupyter.command()
39
@click.version_option(version=__version__)
40
def setup():
41
    """Setup the current docker image to run notebooks."""
42
    # TODO to implement
43
    logging.error("Not implemented yet")
44
45
46
@jupyter.command()
47
@click.version_option(version=__version__)
48
def update():
49
    """
50
    Checks and tries to update the current docker image
51
    to run notebooks to a newer version.
52
53
    Initiates a docker pull of the latest images we are depending on
54
    and build the next aiscalator images from there.
55
    Before replacing the version tags in the Dockerfile, we make sure
56
    to do a maximum in the background while still having a working
57
    image in the meantime.
58
59
    """
60
    # TODO to implement
61
    logging.error("Not implemented yet")
62
63
64
@jupyter.command()
65
@click.option('--name', prompt='What is the name of your step?',
66
              help="Name of the new step to create",
67
              metavar='<STEP>')
68
@click.option('-f', '--format', 'output_format',
69
              help="format of the configuration file (default is hocon)",
70
              type=click.Choice(['json', 'hocon']),
71
              default='hocon')
72
# TODO: import an existing notebook and create a new aiscalate step from it
73
@click.argument('path', type=click.Path())
74
@click.version_option(version=__version__)
75
def new(name, output_format, path):
76
    """Create a new notebook associated with a new aiscalate step config."""
77
    file_conf = os.path.join(path, name, name) + '.conf'
78
    file_json = os.path.join(path, name, name) + '.json'
79
    if os.path.exists(file_conf):
80
        prompt_edit(file_conf)
81
    elif os.path.exists(file_json):
82
        prompt_edit(file_json)
83
    else:
84
        click.echo(command.jupyter_new(name, path,
85
                                       output_format=output_format))
86
87
88
def prompt_edit(file):
89
    """
90
    When creating a new step, if it is already defined,
91
    ask to edit instead
92
93
    Parameters
94
    ----------
95
    file : str
96
        existing configuration file
97
98
    """
99
    msg = file + ' already exists. Did you mean to run:\n'
100
    for i in sys.argv:
101
        if i != "new":
102
            msg += i + ' '
103
        else:
104
            break
105
    msg += "edit " + file + " instead?"
106
    if click.confirm(msg, abort=True):
107
        conf = AiscalatorConfig(config=file)
108
        click.echo(command.jupyter_edit(conf))
109
110
111 View Code Duplication
@jupyter.command()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
112
@click.argument('conf', type=click.Path(exists=True))
113
@click.argument('notebook', nargs=-1)
114
@click.version_option(version=__version__)
115
# TODO add parameters override from CLI
116
def edit(conf, notebook):
117
    """Edit the notebook from an aiscalate config with JupyterLab."""
118
    if len(notebook) < 2:
119
        notebook = notebook[0] if notebook else None
120
        app_config = AiscalatorConfig(config=conf,
121
                                      step_selection=notebook)
122
        click.echo(command.jupyter_edit(app_config))
123
    else:
124
        raise click.BadArgumentUsage("Expecting one or less notebook names")
125
126
127 View Code Duplication
@jupyter.command()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
128
@click.argument('conf', type=click.Path(exists=True))
129
@click.argument('notebook', nargs=-1)
130
@click.version_option(version=__version__)
131
# TODO add parameters override from CLI
132
def run(conf, notebook):
133
    """Run the notebook from an aiscalate config without GUI."""
134
    if notebook:
135
        for note in notebook:
136
            app_config = AiscalatorConfig(config=conf,
137
                                          step_selection=note)
138
            click.echo(command.jupyter_run(app_config))
139
    else:
140
        app_config = AiscalatorConfig(config=conf)
141
        click.echo(command.jupyter_run(app_config))
142