def main(): parser = argparse.ArgumentParser(description="Templater for config files") parser.add_argument('--template-args', type=str, nargs="*", help="Special Arguments to be sent to templates") parser.add_argument('--committed', type=str, nargs="*", help="Key:Value mappings to be slotted into template") parser.add_argument('template', type=str, help="The file path of the jinja template") parser.add_argument('dest', type=str, help="The file path of the rendered template") args = parser.parse_args() lazy = LazySysAdmin(SysAdminClient("127.0.0.1", 4000)) lazy.prefetch() renderer = ConfigTemplateRenderer(args.template) fullArgs = {'values': lazy} fullArgs['args'] = getattr(args, 'template_args', None) fullArgs['committed'] = getattr(args, 'committed', None) rendered = renderer.renderTemplate(fullArgs) dirname = os.path.dirname(args.dest) try: if not os.path.exists(dirname): os.makedirs(dirname) except OSError as e: print "Error making dirs for template %s" % args.dest with open(args.dest, "w") as f: f.write(rendered) f.flush() os.fsync(f.fileno())
def test_prefetch(self): s = LazySysAdmin(ClientWrapper()) s["network"].prefetch() call_count = s.sysadmin.call_counts["get"] self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.1", s["network"]["dhcp"]["startip"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"]) self.assertEqual("192.168.1.100", s["network"]["dhcp"]["endip"]) self.assertEqual(True, s["my"]["service"]["restarter"]) self.assertLess(call_count, s.sysadmin.call_counts["get"]) s.prefetch() call_count = s.sysadmin.call_counts["get"] self.assertEqual("waffles", s["default"]["network"]["breakfast"]) self.assertEqual(call_count, s.sysadmin.call_counts["get"])