Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 90)

Forget about "incognito mode", use a throwaway browser!

It's not be­cause I wrote it (ok, yes, it's be­cause I wrote it) but if you ev­er need a "clean" browser, with­out cook­ies etc for test­s, you can do worse than us­ing my De­vi­cen­zo like this:

rm -f ~/.config/ralsina/devicenzo.conf
curl https://devicenzo.googlecode.com/svn/trunk/devicenzo.py | python

The first line re­moves all con­fig­u­ra­tion, cook­ies, etc, you may have and the sec­ond one down­loads the lat­est ver­sion (don't wor­ry, it takes about 2 sec­ond­s) and launch­es it.

And voilá, a com­plete­ly fresh out­-of-the-box, we­bkit-based browser, with no pre­vi­ous his­to­ry, cook­ies, or con­fig­u­ra­tion, fair­ly fea­ture-­com­plete.

Serbo-Croatian version of PyQt By Example!

A while ago I got an email from An­ja Skr­ba ask­ing me for per­mis­sion to trans­late PyQt by Ex­am­ple in­to Ser­bo-Croa­t­ian.

And here it is all nice and trans­lat­ed. Lots of thanks to An­ja for the hard work!

Using rst2pdf in Different Ways

This was an idea by Dinu Gher­man: you can use rst2pdf as a flow­able gen­er­a­tor for re­port­lab. Sup­pose you want to cre­ate, in a re­port­lab "s­to­ry", a bunch of para­graph­s, with em­pha­sis, links, etc, and per­haps a ta­ble.

Us­ing re­struc­tured tex­t, it's some­thing like this:

This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

  + with a nested item here

It is, of course, per­fect­ly pos­si­ble to gen­er­ate a bunch of re­port­lab (or rather platy­pus) flow­ables to rep­re­sent all this. It will just mean some 75 lines of code. And if you change any­thing, then you have to ed­it code!

Or you can take ad­van­tage of rst2pdf and do this:

from docutils.core import publish_doctree
from rst2pdf.createpdf import RstToPdf
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Frame

rest_text = """
This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

    + with a nested item here
"""
r2p = RstToPdf()
doctree = publish_doctree(rest_text)
story = r2p.gen_elements(doctree)
canv = Canvas("platypus-rest.pdf")
f = Frame(2 * cm, 2 * cm, 16 * cm, 18 * cm, showBoundary=True)
f.addFromList(story, canv)
canv.save()

This pro­duces this pdf. And of course edit­ing it is rather eas­i­er than edit­ing code. Since you are not us­ing rst2pdf to do the fi­nal PDF gen­er­a­tion, you can use these flow­ables in your own doc­u­ments.

The bad news

Some things will not work, like head­ings, since rst2pdf cre­ates flow­ables that do a ton of things like adding them­selves on in­dex­es and such. If you want a head­ing-­like thing you can use class­es:

.. class:: heading1

This will look like a heading

This is a regular paragraph.

Oth­er ran­dom re­struc­tured text fea­tures may or may not work, like foot­notes or ci­ta­tion­s.

The Password Is Password (follow the link to see what I mean)

I think this is a first in the world of stat­ic site gen­er­a­tors :-)

Now Niko­la sup­ports pass­word-pro­tect­ed pages. How does it work?

If you add a pass­word meta­da­ta field, then it will "Just Work". Like this:

I think this is a first in the world of stat­ic site gen­er­a­tors :-) And the re­sult is what you are see­ing now.

How is it im­ple­ment­ed? Niko­la will en­crypt the con­tent us­ing RC4, then wrap it in a <di­v>, and tack a form at the end that trig­gers Javascript code to de­crypt it, and show it.

Is it se­cure? Well, I am not a cryp­tog­ra­pher, so as­sume no. Specif­i­cal­ly, while RC4 is con­sid­ered se­cure, I am not dis­card­ing the be­gin­ning of they keystream, and the im­ple­men­ta­tions I am us­ing are not au­dit­ed.

So, don't use this for any­thing that could get you in trou­ble. Have fun!

UP­DATE Re­mem­ber when I asked "Is it se­cure?" well, re­al­ly, no it's not. It's not stupid in the sense that de­cod­ing what's writ­ten in the post will re­quire at least a mod­icum of ef­fort by who­ev­er is so in­ter­est­ed in read­ing what you are post­ing in your site, but peo­ple with cryp­to chops will crack it like a WEP-se­cured AP, mm­mmkay? It's al­so stupid­ly easy to brute­force this, so be smart about pass­word­s.

OTO­H, it's more se­cure than HTTP sim­ple au­th, since you can't sniff it (not that sim­ple auth is se­cure) and it can hide a piece of the page, which us­ing server-based auth can't.

I may do a more se­cure ver­sion even­tu­al­ly, but this is not it. There­fore, use for fun stuff, not to hide im­por­tan­t/il­le­gal stuff.


Contents © 2000-2023 Roberto Alsina