$ python
>>> "{}".format(u'ł')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
position 0: ordinal not in range(128)
$ python
>>> from __future__ import unicode_literals
>>> "{}".format(u'ł')
u'\u0142'
I think it will crash without that.
On Wed, Mar 18, 2015 at 11:14 PM, Roderick Smith
<email address hidden> wrote:
> Roderick Smith has proposed merging lp:~rodsmith/hwcert-tools/get-ytd into lp:~hardware-certification/hwcert-tools/reporting-tools.
>
> Requested reviews:
> Zygmunt Krynicki (zkrynicki)
> Jeff Lane (bladernr)
>
> For more details, see:
> https://code.launchpad.net/~rodsmith/hwcert-tools/get-ytd/+merge/253447
>
> New script to generate YTD reports on server certificates issued.
>
> Third resubmission addresses most of Zygmunt's concerns.
> --
> You are requested to review the proposed merge of lp:~rodsmith/hwcert-tools/get-ytd into lp:~hardware-certification/hwcert-tools/reporting-tools.
>
> === added file 'certification_reports/get_ytd_makes.py'
> --- certification_reports/get_ytd_makes.py 1970-01-01 00:00:00 +0000
> +++ certification_reports/get_ytd_makes.py 2015-03-18 22:13:39 +0000
> @@ -0,0 +1,151 @@
> +#!/usr/bin/python2
> +"""Script to summarize make and model certification for a specified year."""
> +
> +# Copyright (c) 2015 Canonical Ltd.
> +
> +# Author: Rod Smith <email address hidden>
> +
> +from __future__ import print_function, absolute_import
> +
> +import codecs
> +import locale
> +import sys
> +import time
> +from argparse import ArgumentParser
> +
> +from api_utils import APIQuery, QueryError
> +
> +sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
> +
> +C3_URL = "https://certification.canonical.com"
> +CERTIFICATE_API = C3_URL + "/api/v1/certificates"
> +
> +SERVER_FF = ['Expansion Chassis',
> + 'Main Server Chassis',
> + 'Multi-system',
> + 'Blade',
> + 'Rack Mount Chassis',
> + 'Server']
> +
> +api = APIQuery(C3_URL)
> +
> +
> +def get_server_certs(username, api_key, release, year, certnum):
> + """Retrieve certificate information from C3.
> +
> + :param username:
> + C3 username
> + :param api_key:
> + C3 API key, obtainable from https://certification.canonical.com/me/
> + :param release:
> + Ubuntu release codename (e.g., "precise", "trusty")
> + :param year:
> + Year for which the report is desired
> + :param certnum:
> + Certificate number (e.g., "1502-7107" or "all" for all certificates)
> + :returns:
> + Dictionary with manufacturer names as keys and list of models as values
> + """
> + request_url = CERTIFICATE_API
> + summaries = {}
> +
> + if certnum == "all":
> + request_params = {"username": username,
> + "api_key": api_key}
> + else:
> + request_params = {"username": username,
> + "api_key": api_key,
> + "name": certnum}
> +
> + certs = []
> + try:
> + certs = api.batch_query(request_url, params=request_params)
> + except QueryError:
> + raise SystemExit("Unable to get certificates")
> + certs = [cert for cert in certs if cert['name']]
> +
> + for cert in certs:
> + if int(cert['created_at'][0:4]) == year:
> + machine = api.single_query(C3_URL + cert['machine'],
> + params=request_params)
> + if (machine['platform']['form_factor'] in SERVER_FF and
> + cert['release']['codename'] == release):
> + make = machine['account']['name']
> + details = {'model': machine['platform']['name'],
> + 'certnum': cert['name'],
> + 'cid': machine['canonical_id'].encode('utf-8')}
> + try:
> + summaries[make].append(details)
> + except LookupError:
> + summaries[make] = [details]
> +
> + return summaries
> +
> +
> +def main():
> + """Generate summary and detailed report."""
> + parser = ArgumentParser(
> + description=("Script to summarize make and model certificates "
> + "issued for the year to date or for a specified year. "
> + "Produces a list of makes, the number of certificates "
> + "issued for each make, and a list of the models "
> + "certified for that make."))
> + parser.add_argument(
> + "username", help="Launchpad username used to access C3.")
> + parser.add_argument(
> + "api_key", metavar="API_KEY",
> + help=("API key used to access C3, from "
> + "https://certification.canonical.com/me/"))
> + parser.add_argument(
> + "release", help="Ubuntu release ('trusty', 'precise', etc.)")
> + parser.add_argument("year", help="The year to summarize", type=int)
> + parser.add_argument(
> + "-c", "--cert_num",
> + help="Certificate number or 'all' (used for debugging)",
> + default="all")
> + args = parser.parse_args()
> +
> + summaries = get_server_certs(args.username, args.api_key, args.release,
> + args.year, args.cert_num)
> +
> + print("YTD server certificates for {0} in {1}".format(
> + args.release, args.year))
> + print()
> + print("Report generated: {0}".format(time.strftime("%c")))
> + print()
> +
> + # First pass: Generate "executive summary"
> + print("Summary:")
> + print("--------")
> + print()
> + grand_total = 0
> + sorted_make = sorted(summaries)
> + for make in sorted_make:
> + num_of_make = 0
> + for model in sorted(summaries[make]):
> + num_of_make += 1
> + grand_total += 1
> + print(make + ": " + str(num_of_make))
> + print()
> + print("Grand total: ", grand_total)
> +
> + # Second pass: Generate detailed report
> + print()
> + print()
> + print("Detailed information:")
> + print("---------------------")
> + print()
> + for make in sorted_make:
> + print(make + ":")
> + num_of_make = 0
> + for model in sorted(summaries[make]):
> + num_of_make += 1
> + print(" * {0} (CID {1}):".format(model['model'], model['cid']))
> + print(" https://certification.canonical.com/certificates/{0}".
> + format(model['certnum']))
> + print(make, " TOTAL: ", num_of_make)
> + print()
> +
> +
> +if __name__ == "__main__":
> + sys.exit(main())
>
>
Does this still work without unicode_literals?
$ python
>>> "{}".format(u'ł')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
position 0: ordinal not in range(128)
$ python
>>> from __future__ import unicode_literals
>>> "{}".format(u'ł')
u'\u0142'
I think it will crash without that.
On Wed, Mar 18, 2015 at 11:14 PM, Roderick Smith /code.launchpad .net/~rodsmith/ hwcert- tools/get- ytd/+merge/ 253447 reports/ get_ytd_ makes.py' reports/ get_ytd_ makes.py 1970-01-01 00:00:00 +0000 reports/ get_ytd_ makes.py 2015-03-18 22:13:39 +0000 getwriter( locale. getpreferredenc oding() )(sys.stdout) /certification. canonical. com" certificates" certs(username, api_key, release, year, certnum): /certification. canonical. com/me/ query(request_ url, params= request_ params) 'created_ at'][0: 4]) == year: query(C3_ URL + cert['machine'], request_ params) 'platform' ]['form_ factor' ] in SERVER_FF and ]['codename' ] == release): 'account' ]['name' ] 'platform' ]['name' ], 'canonical_ id'].encode( 'utf-8' )} make].append( details) ("Script to summarize make and model certificates " add_argument( add_argument( /certification. canonical. com/me/")) add_argument( add_argument( "year", help="The year to summarize", type=int) add_argument( certs(args. username, args.api_key, args.release, time.strftime( "%c"))) summaries[ make]): ------- ------- ------- ") summaries[ make]): format( model[' model'] , model['cid'])) /certification. canonical. com/certificate s/{0}". model[' certnum' ]))
<email address hidden> wrote:
> Roderick Smith has proposed merging lp:~rodsmith/hwcert-tools/get-ytd into lp:~hardware-certification/hwcert-tools/reporting-tools.
>
> Requested reviews:
> Zygmunt Krynicki (zkrynicki)
> Jeff Lane (bladernr)
>
> For more details, see:
> https:/
>
> New script to generate YTD reports on server certificates issued.
>
> Third resubmission addresses most of Zygmunt's concerns.
> --
> You are requested to review the proposed merge of lp:~rodsmith/hwcert-tools/get-ytd into lp:~hardware-certification/hwcert-tools/reporting-tools.
>
> === added file 'certification_
> --- certification_
> +++ certification_
> @@ -0,0 +1,151 @@
> +#!/usr/bin/python2
> +"""Script to summarize make and model certification for a specified year."""
> +
> +# Copyright (c) 2015 Canonical Ltd.
> +
> +# Author: Rod Smith <email address hidden>
> +
> +from __future__ import print_function, absolute_import
> +
> +import codecs
> +import locale
> +import sys
> +import time
> +from argparse import ArgumentParser
> +
> +from api_utils import APIQuery, QueryError
> +
> +sys.stdout = codecs.
> +
> +C3_URL = "https:/
> +CERTIFICATE_API = C3_URL + "/api/v1/
> +
> +SERVER_FF = ['Expansion Chassis',
> + 'Main Server Chassis',
> + 'Multi-system',
> + 'Blade',
> + 'Rack Mount Chassis',
> + 'Server']
> +
> +api = APIQuery(C3_URL)
> +
> +
> +def get_server_
> + """Retrieve certificate information from C3.
> +
> + :param username:
> + C3 username
> + :param api_key:
> + C3 API key, obtainable from https:/
> + :param release:
> + Ubuntu release codename (e.g., "precise", "trusty")
> + :param year:
> + Year for which the report is desired
> + :param certnum:
> + Certificate number (e.g., "1502-7107" or "all" for all certificates)
> + :returns:
> + Dictionary with manufacturer names as keys and list of models as values
> + """
> + request_url = CERTIFICATE_API
> + summaries = {}
> +
> + if certnum == "all":
> + request_params = {"username": username,
> + "api_key": api_key}
> + else:
> + request_params = {"username": username,
> + "api_key": api_key,
> + "name": certnum}
> +
> + certs = []
> + try:
> + certs = api.batch_
> + except QueryError:
> + raise SystemExit("Unable to get certificates")
> + certs = [cert for cert in certs if cert['name']]
> +
> + for cert in certs:
> + if int(cert[
> + machine = api.single_
> + params=
> + if (machine[
> + cert['release'
> + make = machine[
> + details = {'model': machine[
> + 'certnum': cert['name'],
> + 'cid': machine[
> + try:
> + summaries[
> + except LookupError:
> + summaries[make] = [details]
> +
> + return summaries
> +
> +
> +def main():
> + """Generate summary and detailed report."""
> + parser = ArgumentParser(
> + description=
> + "issued for the year to date or for a specified year. "
> + "Produces a list of makes, the number of certificates "
> + "issued for each make, and a list of the models "
> + "certified for that make."))
> + parser.
> + "username", help="Launchpad username used to access C3.")
> + parser.
> + "api_key", metavar="API_KEY",
> + help=("API key used to access C3, from "
> + "https:/
> + parser.
> + "release", help="Ubuntu release ('trusty', 'precise', etc.)")
> + parser.
> + parser.
> + "-c", "--cert_num",
> + help="Certificate number or 'all' (used for debugging)",
> + default="all")
> + args = parser.parse_args()
> +
> + summaries = get_server_
> + args.year, args.cert_num)
> +
> + print("YTD server certificates for {0} in {1}".format(
> + args.release, args.year))
> + print()
> + print("Report generated: {0}".format(
> + print()
> +
> + # First pass: Generate "executive summary"
> + print("Summary:")
> + print("--------")
> + print()
> + grand_total = 0
> + sorted_make = sorted(summaries)
> + for make in sorted_make:
> + num_of_make = 0
> + for model in sorted(
> + num_of_make += 1
> + grand_total += 1
> + print(make + ": " + str(num_of_make))
> + print()
> + print("Grand total: ", grand_total)
> +
> + # Second pass: Generate detailed report
> + print()
> + print()
> + print("Detailed information:")
> + print("
> + print()
> + for make in sorted_make:
> + print(make + ":")
> + num_of_make = 0
> + for model in sorted(
> + num_of_make += 1
> + print(" * {0} (CID {1}):".
> + print(" https:/
> + format(
> + print(make, " TOTAL: ", num_of_make)
> + print()
> +
> +
> +if __name__ == "__main__":
> + sys.exit(main())
>
>