Commit 07e4f051 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[plotters] pre-commit cleanup

parent 4f3c311d
......@@ -91,37 +91,71 @@ def pull_impl(webapi, prefix, names, force, indentation, format_cache):
"""
if indentation == 0:
indentation = 4
# download required plotter
status, names = common.pull(webapi, prefix, 'plotter', names,
['declaration', 'code'], force,
indentation)
pull_status, names = common.pull(
webapi, prefix, "plotter", names, ["declaration", "code"], force, indentation
)
# check dataformat, libraries, default plotterparameter used by plotter
dataformats = []
libraries = []
for name in names:
data = common.fetch_object(webapi, 'plotter', name, ['declaration', 'uses'])
data = simplejson.loads(data['declaration'], object_pairs_hook=collections.OrderedDict)
libraries_json = simplejson.loads(simplejson.dumps(data['uses']))
data = common.fetch_object(webapi, "plotter", name, ["declaration", "uses"])
data = simplejson.loads(
data["declaration"], object_pairs_hook=collections.OrderedDict
)
libraries_json = simplejson.loads(simplejson.dumps(data["uses"]))
for key, val in libraries_json.items():
libraries.append(val)
lb_status = libraries_pull(webapi, prefix, libraries, force,
indentation + 2, format_cache)
dataformats.append(data['dataformat'])
lb_status = libraries_pull(
webapi, prefix, libraries, force, indentation, format_cache
)
dataformats.append(data["dataformat"])
# downloads any formats to which we depend on
df_status = dataformats_pull(webapi, prefix, dataformats, force,
indentation + 2, format_cache)
df_status = dataformats_pull(
webapi, prefix, dataformats, force, indentation, format_cache
)
# download default plotterparameter
defaultplotters = common.fetch_object(webapi, 'defaultplotter', None, ['parameter'])
defaultplotters = common.fetch_object(
webapi, "defaultplotter", None, ["parameter"]
)
plp_status = 0
for df in defaultplotters:
if name == df['plotter']:
plp_status = plotterparameters_pull(webapi, prefix, [df['parameter']], ['data', 'short_description', 'plotter'], force, indentation + 2)
code = status + lb_status + df_status
if name == df["plotter"]:
status = plotterparameters_pull(
webapi,
prefix,
[df["parameter"]],
["data", "short_description", "plotter"],
force,
indentation,
)
plp_status += status
code = pull_status + lb_status + df_status + plp_status
return code
def plot_impl(webapi, prefix, names, show, force, need_data_sample, inputdata, outputimage, plotterparameter, indentation, format_cache):
def plot_impl(
webapi,
prefix,
names,
show,
force,
need_data_sample,
inputdata,
outputimage,
plotterparameter,
indentation,
format_cache,
):
"""plot sample plot from the server.
Parameters:
......@@ -166,42 +200,46 @@ def plot_impl(webapi, prefix, names, show, force, need_data_sample, inputdata, o
"""
indent = indentation * ' '
if indentation == 0:
indentation = 4
name = names[0]
# create a core_plotter
core_plotter = plotter.Plotter(prefix, name)
if not core_plotter.valid:
message = 'Plotter %s is invalid' % core_plotter.name
message = "Plotter %s is invalid" % core_plotter.name
print(message)
sys.exit()
sample_data = None
# get sample data
if need_data_sample:
sample_data = common.fetch_object(webapi, 'plotter', name, ['sample_data'])
sample_data = simplejson.loads(sample_data['sample_data'])
sample_data = common.fetch_object(webapi, "plotter", name, ["sample_data"])
sample_data = simplejson.loads(sample_data["sample_data"])
# provide data
if inputdata is None and not need_data_sample:
message = 'Error: Missing --sample_data argument or inputdata'
message = "Error: Missing --sample_data argument or inputdata"
print(message)
sys.exit()
elif inputdata is not None:
if type(inputdata) is str:
with open(os.path.join(prefix, inputdata), 'r') as f:
with open(os.path.join(prefix, inputdata), "r") as f:
sample_data = simplejson.load(f)
f.closed
elif type(inputdata) is dict:
sample_data = inputdata
else:
message = 'Error: inputdata should be dict or str type'
message = "Error: inputdata should be dict or str type"
print(message)
sys.exit()
# output
plotter_path = os.path.join(prefix, common.TYPE_PLURAL['plotter'], name.rsplit('/', 1)[0])
plotter_path = os.path.join(
prefix, common.TYPE_PLURAL["plotter"], name.rsplit("/", 1)[0]
)
outputimage_name = "output_image.png"
if outputimage is not None:
outputimage_name = outputimage
......@@ -210,35 +248,50 @@ def plot_impl(webapi, prefix, names, show, force, need_data_sample, inputdata, o
# plot
corefmt = dataformat.DataFormat(prefix, core_plotter.dataformat.name)
sample_data = corefmt.type().from_dict(sample_data, casting='unsafe')
sample_data = corefmt.type().from_dict(sample_data, casting="unsafe")
# plotterparameter
data = None
if plotterparameter is not None:
try:
with open(os.path.join(prefix, common.TYPE_NOSTORAGE['plotterparameter'], plotterparameter + '.json'), 'r') as f:
with open(
os.path.join(
prefix,
common.TYPE_NOSTORAGE["plotterparameter"],
plotterparameter + ".json",
),
"r",
) as f:
data = simplejson.load(f, object_pairs_hook=collections.OrderedDict)
f.closed
except:
except Exception:
logger.error("Error: unknown plotterparameter path")
else:
# download default plotterparameter
defaultplotters = common.fetch_object(webapi, 'defaultplotter', None, ['parameter'])
defaultplotters = common.fetch_object(
webapi, "defaultplotter", None, ["parameter"]
)
for df in defaultplotters:
if name == df['plotter']:
data = common.fetch_object(webapi, 'plotterparameter', df['parameter'], ['data'])
if name == df["plotter"]:
data = common.fetch_object(
webapi, "plotterparameter", df["parameter"], ["data"]
)
if data is not None:
to_remove = [key for key in data['data'].keys() if key not in core_plotter.parameters]
to_remove = [
key for key in data["data"].keys() if key not in core_plotter.parameters
]
for key in to_remove:
del data['data'][key]
del data["data"][key]
data['data'].setdefault('content_type', 'image/png') #in case not set
data["data"].setdefault("content_type", "image/png") # in case not set
runnable = core_plotter.runner()
runnable.setup(data['data'])
assert runnable.ready
data_to_plot = [('sample_plot', sample_data)]
runnable.setup(data["data"])
if not runnable.ready:
raise RuntimeError("Plotter runner is not ready")
data_to_plot = [("sample_plot", sample_data)]
fig = runnable.process(data_to_plot)
if show:
im = Image.open(io.BytesIO(fig))
......@@ -246,7 +299,7 @@ def plot_impl(webapi, prefix, names, show, force, need_data_sample, inputdata, o
output_name = os.path.join(prefix, outputimage_name)
with open(output_name, "wb") as fh:
fh.write(fig)
logger.info("%ssaved image `%s'...", indent, output_name)
logger.info("%ssaved image `%s'...", indentation * " ", output_name)
else:
logger.error("Error: data has not been set")
return 1
......@@ -254,7 +307,6 @@ def plot_impl(webapi, prefix, names, show, force, need_data_sample, inputdata, o
return 0
@click.group(cls=AliasedGroup)
@click.pass_context
def plotters(ctx):
......@@ -262,160 +314,165 @@ def plotters(ctx):
pass
@plotters.command()
@click.option('--remote', help='Only acts on the remote copy of the list.',
is_flag=True)
@click.option(
"--remote", help="Only acts on the remote copy of the list.", is_flag=True
)
@click.pass_context
@raise_on_error
def list(ctx, remote):
'''Lists all the plotters available on the platform.
"""Lists all the plotters available on the platform.
To list all existing plotters on your local prefix:
$ beat plotters list
'''
"""
if remote:
with common.make_webapi(ctx.meta['config']) as webapi:
return common.display_remote_list(webapi, 'plotter')
with common.make_webapi(ctx.meta["config"]) as webapi:
return common.display_remote_list(webapi, "plotter")
else:
return common.display_local_list(ctx.meta['config'].path, 'plotter')
return common.display_local_list(ctx.meta["config"].path, "plotter")
@plotters.command()
@click.argument('names', nargs=-1)
@click.argument("names", nargs=-1)
@click.pass_context
@raise_on_error
def path(ctx, names):
'''Displays local path of plotters files
"""Displays local path of plotters files
Example:
$ beat plotters path xxx
'''
return common.display_local_path(ctx.meta['config'].path, 'plotter', names)
"""
return common.display_local_path(ctx.meta["config"].path, "plotter", names)
@plotters.command()
@click.argument('name', nargs=1)
@click.argument("name", nargs=1)
@click.pass_context
@raise_on_error
def edit(ctx, name):
'''Edit local plotter file
"""Edit local plotter file
Example:
$ beat plotters edit xxx
'''
return common.edit_local_file(ctx.meta['config'].path,
ctx.meta['config'].editor, 'plotter',
name)
"""
return common.edit_local_file(
ctx.meta["config"].path, ctx.meta["config"].editor, "plotter", name
)
@plotters.command()
@click.argument('names', nargs=-1)
@click.argument("names", nargs=-1)
@click.pass_context
@raise_on_error
def check(ctx, names):
'''Checks a local plotter for validity.
"""Checks a local plotter for validity.
$ beat plotters check xxx
'''
return common.check(ctx.meta['config'].path, 'plotter', names)
"""
return common.check(ctx.meta["config"].path, "plotter", names)
@plotters.command()
@click.argument('names', nargs=-1)
@click.option('--force', help='Performs operation regardless of conflicts',
is_flag=True)
@click.argument("names", nargs=-1)
@click.option(
"--force", help="Performs operation regardless of conflicts", is_flag=True
)
@click.pass_context
def pull(ctx, names, force):
'''Downloads the specified plotters from the server.
"""Downloads the specified plotters from the server.
$ beat plotters pull xxx.
'''
with common.make_webapi(ctx.meta['config']) as webapi:
name = common.make_up_remote_list(webapi, 'plotter', names)
"""
with common.make_webapi(ctx.meta["config"]) as webapi:
name = common.make_up_remote_list(webapi, "plotter", names)
if name is None:
return 1 #error
return pull_impl(webapi, ctx.meta['config'].path, name, force, 0, {})
return 1 # error
return pull_impl(webapi, ctx.meta["config"].path, name, force, 0, {})
@plotters.command()
@click.argument('names', nargs=-1)
@click.option('--show', help='Show...',
is_flag=True)
@click.option('--force', help='Performs operation regardless of conflicts',
is_flag=True)
@click.option('--sample-data', help='Sample data...',
is_flag=True)
@click.option('--input-data', help='<filename.json>',
type=click.Path(exists=True))
@click.option('--output-image', help='<filename.png>')
@click.option('--plotter-parameter', help='<plotterparameter>')
@click.argument("names", nargs=-1)
@click.option("--show", help="Show...", is_flag=True)
@click.option(
"--force", help="Performs operation regardless of conflicts", is_flag=True
)
@click.option("--sample-data", help="Sample data...", is_flag=True)
@click.option("--input-data", help="<filename.json>", type=click.Path(exists=True))
@click.option("--output-image", help="<filename.png>")
@click.option("--plotter-parameter", help="<plotterparameter>")
@click.pass_context
@raise_on_error
def plot(ctx, names, show, force, sample_data, input_data, output_image,
plotter_parameter):
'''Plots an image.
def plot(
ctx, names, show, force, sample_data, input_data, output_image, plotter_parameter
):
"""Plots an image.
$ beat plotters plot [--show] [--force] [--sample_data]
[--input-data=<filename.json>] [--output-image=<filename.png>]
[--plotter-parameter=<plotterparameter>] [<name>]...
'''
with common.make_webapi(ctx.meta['config']) as webapi:
"""
with common.make_webapi(ctx.meta["config"]) as webapi:
return plot_impl(
webapi, ctx.meta['config'].path, names, show, force, sample_data,
input_data, output_image, plotter_parameter, 0, {}
webapi,
ctx.meta["config"].path,
names,
show,
force,
sample_data,
input_data,
output_image,
plotter_parameter,
0,
{},
)
@plotters.command()
@click.argument('names', nargs=-1)
@click.argument("names", nargs=-1)
@click.pass_context
@raise_on_error
def create(ctx, names):
'''Creates a new local plotter.
"""Creates a new local plotter.
$ beat plotters create xxx
'''
return common.create(ctx.meta['config'].path, 'plotter', names)
"""
return common.create(ctx.meta["config"].path, "plotter", names)
@plotters.command()
@click.argument('name', nargs=1)
@click.argument("name", nargs=1)
@click.pass_context
@raise_on_error
def version(ctx, name):
'''Creates a new version of an existing plotter.
"""Creates a new version of an existing plotter.
$ beat plotters version xxx
'''
return common.new_version(ctx.meta['config'].path, 'plotter', name)
"""
return common.new_version(ctx.meta["config"].path, "plotter", name)
@plotters.command()
@click.argument('src', nargs=1)
@click.argument('dst', nargs=1)
@click.argument("src", nargs=1)
@click.argument("dst", nargs=1)
@click.pass_context
@raise_on_error
def fork(ctx, src, dst):
'''Forks a local plotter.
"""Forks a local plotter.
$ beat plotters fork xxx yyy
'''
return common.fork(ctx.meta['config'].path, 'plotter', src, dst)
"""
return common.fork(ctx.meta["config"].path, "plotter", src, dst)
@plotters.command()
@click.argument('names', nargs=-1)
@click.argument("names", nargs=-1)
@click.pass_context
@raise_on_error
def rm(ctx, names):
'''Deletes a local plotter.
"""Deletes a local plotter.
$ beat plotters rm xxx
'''
return common.delete_local(ctx.meta['config'].path, 'plotter', names)
"""
return common.delete_local(ctx.meta["config"].path, "plotter", names)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment