from __future__ import with_statement

EnsurePythonVersion(2, 5)  # For 'with' statement


import urllib
import urllib2


def qr_code(target, source, env):
    """Generate the QR code for the SL4A script using the Google Chart API.

    The docs on the chart API can be found at
    http://code.google.com/apis/chart/docs/gallery/qr_codes.html

    The zxing project's online QR code generator is at
    http://zxing.appspot.com/generator/

    """
    if len(target) != 1:
        raise ValueError("only one target file allowed")
    elif len(source) != 1:
        raise ValueError("only one source file allowed")

    google_charts_api = 'http://chart.apis.google.com/chart'
    args = {'cht': 'qr', 'chs': '350x350'}
    with open(str(source[0]), 'rb') as file:
        args['chl'] = 'oplop.py\n' + file.read()
    query = urllib.urlencode(args)
    url = urllib2.urlopen('?'.join([google_charts_api, query]))
    with open(str(target[0]), 'wb') as file:
        file.write(url.read())


env = Environment()
env.Command('qr_code.png', 'oplop.min.py', qr_code)
