def cli(filename, f_min, f_max, output, quality_factor, spring_constant, resonance_frequency, temperature): # This function needs to be broken up into component pieces. # For example, the reproduce python, reproduce cli bits can easily be their # own functions (and in fact, may make more sense as __repr__ methods of # classes) # The report generator should also be its own function. if resonance_frequency is None: resonance_frequency = (f_min + f_max) / 2.0 file_path = os.path.abspath(filename) with h5py.File(filename, 'r') as f: date = f.attrs['date'] time = f.attrs['time'].replace(':', '') if output is None: cli_reproduce = """calck "{file_path}" {f_min} {f_max} -Q {Q} -k {k} -f {f} -T {T}""".format(file_path=file_path, f_min=f_min, f_max=f_max, Q=quality_factor, k=spring_constant, f=resonance_frequency, T=temperature) else: cli_reproduce = """calck "{file_path}" {f_min} {f_max} -o {output} -Q {Q} -k {k} -f {f} -T {T}""".format(file_path=file_path, f_min=f_min, f_max=f_max, Q=quality_factor, k=spring_constant, f=resonance_frequency, T=temperature, output=output) python_reproduce = """\ import brownian._calck bmf = brownian._calck.calck("{file_path}", f_min={f_min}, f_max={f_max}, quality_factor={Q}, spring_constant={k}, resonance_frequency={f}, temperature={T}) print(bmf.report()) """.format(file_path=file_path, f_min=f_min, f_max=f_max, Q=quality_factor, k=spring_constant, f=resonance_frequency, T=temperature) bmf = calck(filename, f_min, f_max, quality_factor, spring_constant, resonance_frequency, temperature) if output is None: text = u"""\ Generated by brownian version {version}. Reproduce this analysis with the following shell command: {cli_reproduce} or with the following python code: {python_reproduce} {report} """.format(cli_reproduce=cli_reproduce, python_reproduce=python_reproduce, report = bmf.rst_report(), version=brownian.__version__) click.echo(text) return 0 else: files = Bunch(fit="fit.png", reduced_residuals="reduced_residuals.png", cdf='cdf.png') bmf.plot_fit(files.fit) bmf.plot_reduced_residuals(files.reduced_residuals) bmf.plot_cdf(files.cdf) ReST = u"""\ Report ====== {report} .. image:: {files.fit} .. image:: {files.reduced_residuals} .. image:: {files.cdf} Reproduce this analysis with the following shell command: .. code:: bash {cli_reproduce} or with the following python code: .. code:: python {python_reproduce} Generated by ``brownian`` version ``{version}``. """.format(files=files, cli_reproduce=cli_reproduce, python_reproduce=python_reproduce, report = bmf.rst_report(), version=brownian.__version__) ext = file_extension(output) if ext not in ('zip', 'html'): click.echo('output file extension must be zip or html, not {0}'.format(ext)) return -1 elif ext == 'html': image_dependent_html = docutils.core.publish_string(ReST, writer_name='html') self_contained_html = unicode(img2uri(image_dependent_html), 'utf8') # io module instead of built-in open because it allows specifying # encoding. See http://stackoverflow.com/a/22288895/2823213 with io.open(output, 'w', encoding='utf8') as f: f.write(self_contained_html) for filename in files.values(): silentremove(filename) return 0 elif ext == 'zip': files.html = date+'-'+time+'.html' files.rst = date+'-'+time+'.rst' image_dependent_html = unicode( docutils.core.publish_string(ReST, writer_name='html'), 'utf8' ) with io.open(files.html, 'w', encoding='utf8') as f: f.write(image_dependent_html) with io.open(files.rst, 'w', encoding='utf8') as f: f.write(ReST) with zipfile.ZipFile(output, 'w') as z: for f in files.values(): z.write(f) # Delete non-zipped version of the files for f in files.values(): silentremove(f) return 0
def tearDown(self): silentremove(self.testv1) silentremove(self.testv2) silentremove(self.converted)
def tearDown(self): self.fh.close() silentremove(self.filename)