#!/usr/bin/env python import os import sys import yaml try: import pdfkit except ImportError: print("WARNING: could not import pdfkit; PDF generation will fali.") def prettify(l): l = [ip.strip() for ip in l] ret = [ "node{}: {}".format(i+1, s) for (i, s) in zip(range(len(l)), l) ] return ret # Read settings from user-provided settings file with open(sys.argv[1]) as f: data = f.read() SETTINGS = yaml.load(data) SETTINGS['footer'] = SETTINGS['footer'].format(url=SETTINGS['url']) globals().update(SETTINGS) ############################################################################### ips = list(open("ips.txt")) print("Current settings (as defined in settings.yaml):") print(" Number of IPs: {}".format(len(ips))) print(" VMs per cluster: {}".format(clustersize)) print("Background image: {}".format(background_image)) print("---------------------------------------------") assert len(ips)%clustersize == 0 if clustersize == 1: blurb = blurb.format( cluster_or_machine="machine", this_or_each="this", machine_is_or_machines_are="machine is", workshop_name=workshop_short_name, ) else: blurb = blurb.format( cluster_or_machine="cluster", this_or_each="each", machine_is_or_machines_are="machines are", workshop_name=workshop_short_name, ) clusters = [] while ips: cluster = ips[:clustersize] ips = ips[clustersize:] clusters.append(cluster) html = open("ips.html", "w") html.write("") for i, cluster in enumerate(clusters): if i>0 and i%pagesize==0: html.write('\n') html.write("
") html.write(blurb) for s in prettify(cluster): html.write("
  • %s
  • \n"%s) html.write("

    ") html.write("

    login: {}
    password: {}

    \n".format(instance_login, instance_password)) html.write(footer) html.write("
    ") html.close() """ html.write("
    ") html.write("

    {}

    ".format(blurb)) for s in prettify(cluster): html.write("
  • {}
  • ".format(s)) html.write("

    ") html.write("
    ") html.write("

    login: {}    password: {}

    \n".format(instance_login, instance_password)) html.write("
    ") html.write(footer) html.write("
    ") html.close() """ with open('ips.html') as f: pdfkit.from_file(f, 'ips.pdf')