Ejemplo n.º 1
0
    def run(self):
        set_classes(self.options)

        # FFS why so complex
        svg = qrcode.make(self.arguments[0],
                          image_factory=qrcode.image.svg.SvgPathFillImage)
        f = io.BytesIO()
        svg.save(f)
        svg = f.getvalue().decode('utf-8')

        # Compress a bit, remove cruft
        svg = svg.replace('L ', 'L').replace('M ', 'M').replace(' z', 'z')
        svg = svg.replace(' id="qr-path"', '')

        attribs = ' class="{}"'.format(
            ' '.join(['m-image'] + self.options.get('classes', [])))

        if 'size' in self.options:
            size = self.options['size']
        else:
            size = None

        def preamble_repl(match):
            return _svg_preamble_dst.format(
                attribs=attribs,
                size=size if size else
                # The original size is in mm, convert that to pixels on 96 DPI
                # and then to rem assuming 1 rem = 16px
                '{:.2f}rem'.format(
                    float(match.group('width')) * 9.6 / 2.54 / 16.0, 2),
                viewBox=match.group('viewBox'))

        svg = _svg_preamble_src.sub(preamble_repl, svg)
        return [nodes.raw('', svg, format='html')]
Ejemplo n.º 2
0
Archivo: qr.py Proyecto: mosra/m.css
    def run(self):
        set_classes(self.options)

        # FFS why so complex
        svg = qrcode.make(self.arguments[0], image_factory=qrcode.image.svg.SvgPathFillImage)
        f = io.BytesIO()
        svg.save(f)
        svg = f.getvalue().decode('utf-8')

        # Compress a bit, remove cruft
        svg = svg.replace('L ', 'L').replace('M ', 'M').replace(' z', 'z')
        svg = svg.replace(' id="qr-path"', '')

        attribs = ' class="{}"'.format(' '.join(['m-image'] + self.options.get('classes', [])))

        if 'size' in self.options:
            size = self.options['size']
        else:
            size = None

        def preamble_repl(match): return _svg_preamble_dst.format(
            attribs=attribs,
            size=size if size else
                # The original size is in mm, convert that to pixels on 96 DPI
                # and then to rem assuming 1 rem = 16px
                '{:.2f}rem'.format(float(match.group('width'))*9.6/2.54/16.0, 2),
            viewBox=match.group('viewBox'))
        svg = _svg_preamble_src.sub(preamble_repl, svg)
        return [nodes.raw('', svg, format='html')]
Ejemplo n.º 3
0
	def qrCode(self, data, errorCorrection='L', boxSize='10', borderWidth='4'):
		parsedErrorCorrection = {
			'L': qrcode.constants.ERROR_CORRECT_L,
			'M': qrcode.constants.ERROR_CORRECT_M,
			'Q': qrcode.constants.ERROR_CORRECT_Q,
			'H': qrcode.constants.ERROR_CORRECT_H,
		}.get(errorCorrection, qrcode.constants.ERROR_CORRECT_L)

		try:
			parsedBoxSize = int(boxSize)
		except ValueError:
			parsedBoxSize = 10

		try:
			parsedBorderWidth = int(borderWidth)
		except ValueError:
			parsedBorderWidth = 4

		#Generate the image
		svg = qrcode.make(data,
			error_correction=parsedErrorCorrection,
			box_size=parsedBoxSize,
			border=parsedBorderWidth,
			image_factory=qrcode.image.svg.SvgImage)

		#Output it to a string
		output = io.BytesIO()
		svg.save(output)
		image = output.getvalue()

		cherrypy.response.headers['Content-Type'] = 'image/svg+xml'
		return image
Ejemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     qr = qrcode.QRCode()
     qr.add_data(self.get_url()[0])
     qr.make(fit=True)
     svg = qr.make_image(image_factory=qrcode.image.svg.SvgPathFillImage)
     svg_bytes = io.BytesIO()
     svg.save(svg_bytes)
     return HttpResponse(svg_bytes.getvalue(), content_type="image/svg+xml")
Ejemplo n.º 5
0
def test_generate_qr_code_svg():
    import qrcode.image.svg

    qr = cwa.generate_qr_code(full_event_description)
    svg = qr.make_image(image_factory=qrcode.image.svg.SvgPathFillImage)

    svg_bytes = io.BytesIO()
    svg.save(svg_bytes)

    assert svg_bytes.getvalue().startswith(b'<?xml')
Ejemplo n.º 6
0
def get_qr_code(data=None, border=2, box_size=7):
    qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgImage,
                       border=border,
                       box_size=box_size)
    qr.add_data(data)
    svg = qr.make_image()
    svg_content = io.BytesIO()
    svg.save(svg_content)
    content = svg_content.getvalue().decode("utf-8")
    return content
Ejemplo n.º 7
0
def qr_code_image(request, slug):

    # Generate SVG once a runtime
    image_path = os.path.join(tempfile.gettempdir(), '{}.svg'.format(slug))
    if not os.path.exists(image_path):
        poll_url = accessible_url(request, reverse('vote', args=[slug]))
        svg = qrcode.make(poll_url, image_factory=qrcode.image.svg.SvgPathImage)
        svg.save(image_path)

    # Render SVG
    response = HttpResponse(content_type='image/svg+xml')
    with open(image_path) as svg:
        response.write(svg.read())
    return response
Ejemplo n.º 8
0
def qr_code_image(request, slug):

    # Generate SVG once a runtime
    image_path = os.path.join(tempfile.gettempdir(), '{}.svg'.format(slug))
    if not os.path.exists(image_path):
        poll_url = accessible_url(request, reverse('vote', args=[slug]))
        svg = qrcode.make(poll_url,
                          image_factory=qrcode.image.svg.SvgPathImage)
        svg.save(image_path)

    # Render SVG
    response = HttpResponse(content_type='image/svg+xml')
    with open(image_path) as svg:
        response.write(svg.read())
    return response
Ejemplo n.º 9
0
 def _generate(cls, uri):
     code = qrcode.QRCode(version=3,
                          error_correction=qrcode.constants.ERROR_CORRECT_L,
                          box_size=5,
                          border=3,
                          image_factory=cls)
     code.add_data(uri)
     svg = code.make_image()
     stream = io.BytesIO()
     svg.save(stream)
     stream.seek(0)
     data = stream.read()
     if len(cls.YUU_CACHE) > MAX_QR_CACHE_ENTRIES:
         cls.YUU_CACHE.popitem()
     cls.YUU_CACHE[uri] = data
     return data
Ejemplo n.º 10
0
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)

        url, df, dt = self.get_url()
        qr = qrcode.QRCode()
        qr.add_data(url)
        qr.make(fit=True)
        svg = qr.make_image(image_factory=qrcode.image.svg.SvgPathFillImage)
        svg_bytes = io.BytesIO()
        svg.save(svg_bytes)
        ctx["svg"] = re.sub(r"<\?[^?]+\?>", "", svg_bytes.getvalue().decode())
        ctx["event"] = self.request.event
        ctx["df"] = df
        ctx["dt"] = dt

        ctx["refresh"] = max(min((dt - now()).seconds - 60, 600), 15)

        return ctx
Ejemplo n.º 11
0
 def _generate(cls, uri):
     code = qrcode.QRCode(
         version=3,
         error_correction=qrcode.constants.ERROR_CORRECT_L,
         box_size=5,
         border=3,
         image_factory=cls
     )
     code.add_data(uri)
     svg = code.make_image()
     stream = io.BytesIO()
     svg.save(stream)
     stream.seek(0)
     data = stream.read()
     if len(cls.YUU_CACHE) > MAX_QR_CACHE_ENTRIES:
         cls.YUU_CACHE.popitem()
     cls.YUU_CACHE[uri] = data
     return data