Commits

Jesse London committed 2b59605 Draft

tweak to js data utility

Comments (0)

Files changed (1)

 from datetime import date
 
 
+TEMPLATE = """\
+if(typeof(defaultData) === 'undefined') {{
+  var defaultData = [];
+}}
+defaultData.push({{
+  title: "{title}",
+  version: "{version}",
+  data: {data}
+}});
+"""
+
+
 def main(target, out, title, version=None):
+    version = version or date.strftime(date.today(), '%m.%d.%Y')
     data = csv.reader(open(target))
-    version = version or date.strftime(date.today(), '%m.%d.%Y')
-    with open(out, 'w') as outfile:
-        outfile.write('var defaultDataTitle = "%s";\n' % title)
-        outfile.write('var defaultDataVersion = "%s";\n' % version)
-        outfile.write('var DefaultData = ')
-        json.dump(list(data), outfile, indent=2)
-        outfile.write(';\n')
+    code = TEMPLATE.format(
+        title=title,
+        version=version,
+        data=json.dumps(list(data), indent=4),
+    )
+    with open(out, 'a') as outfile:
+        outfile.write(code)
 
 
 if __name__ == '__main__':