|
| 1 | +import os |
| 2 | + |
| 3 | +import nptdms |
| 4 | +from nptdms import TdmsFile |
| 5 | +from mako.lookup import TemplateLookup |
| 6 | + |
| 7 | +from mfr.core import extension |
| 8 | + |
| 9 | +# class EscapeHtml(Extension): |
| 10 | +# def extendMarkdown(self, md, md_globals): |
| 11 | +# del md.preprocessors['html_block'] |
| 12 | +# del md.inlinePatterns['html'] |
| 13 | + |
| 14 | + |
| 15 | +class TdmsRenderer(extension.BaseRenderer): |
| 16 | + |
| 17 | + TEMPLATE = TemplateLookup( |
| 18 | + directories=[ |
| 19 | + os.path.join(os.path.dirname(__file__), 'templates') |
| 20 | + ]).get_template('viewer.mako') |
| 21 | + |
| 22 | + def __init__(self, *args, **kwargs): |
| 23 | + super().__init__(*args, **kwargs) |
| 24 | + self.metrics.add('nptdms_version', nptdms.version.__version__) |
| 25 | + |
| 26 | + def render(self): |
| 27 | + """Render a tdms file to html.""" |
| 28 | + |
| 29 | + tdms_file = TdmsFile.open(self.file_path, raw_timestamps=True) |
| 30 | + |
| 31 | + body = "<div><ul>" |
| 32 | + for property, value in tdms_file.properties.items(): |
| 33 | + body += "<li><b>File</b> = " + str(value) + "</li>\n\n" |
| 34 | + body += "</ul></div><div><ul>" |
| 35 | + for group in tdms_file.groups(): |
| 36 | + body += "<li><b>Channel group</b> = " + group.name + "</li>\n" |
| 37 | + body += "<ul>" |
| 38 | + for channel in group.channels(): |
| 39 | + body += "<li><b>Channel</b> = " + channel.name + "</li>\n" |
| 40 | + body += "<ul>" |
| 41 | + # Access dictionary of properties: |
| 42 | + for property, value in channel.properties.items(): |
| 43 | + body += "<li>" + property + " = " + str(value) + "</li>\n\n" |
| 44 | + |
| 45 | + # Access numpy array of data for channel: |
| 46 | + # body = body + str(channel[:]) + "" |
| 47 | + body += "</ul>" |
| 48 | + body += "</ul>" |
| 49 | + body += "</ul></div>" |
| 50 | + |
| 51 | + return self.TEMPLATE.render(base=self.assets_url, body=body) |
| 52 | + |
| 53 | + @property |
| 54 | + def file_required(self): |
| 55 | + return True |
| 56 | + |
| 57 | + @property |
| 58 | + def cache_result(self): |
| 59 | + return True |
0 commit comments