[Scribus] Scripter using Python calendar modules - prcal, prmonth

Craig Ringer craig
Mon Aug 21 02:21:48 CEST 2006


Dion Vansevenant wrote:

> import calendar
> newDoc(PAPER_LETTER, (50, 10, 10, 10), PORTRAIT, 1, UNIT_POINTS, 
> FACINGPAGES, FIRSTPAGERIGHT)
> cal = calendar.prmonth(2006,8)
> ob = createText(100, 60, 140, 130)
> setText(cal, ob)
> 
> 
> When I ran it, I got a new document, and empty text box, and the 
> following console output:
> 
> 1
>     August 2006
> Mo Tu We Th Fr Sa Su
>     1  2  3  4  5  6
>  7  8  9 10 11 12 13
> 14 15 16 17 18 19 20
> 21 22 23 24 25 26 27
> 28 29 30 31
> Traceback (most recent call last):
>   File "<console>", line 1, in ?
> TypeError: coercing to Unicode: need string or buffer, NoneType found
> 
> It would seem that setText needs a string, so I changed that line from:
> 
> setText(cal, ob)
> 
> to:
> 
> setText(str(cal), ob)

The Python interface expects strings in utf-8 encoding. To be safe, you 
should therefore always use:

str(cal, "utf-8").

One of the things I'd like to see in a replacement interface is more 
correct handling of text encodings using Python `Unicode' strings 
natively (I actually have a transparent Python unicode <-> QString 
converter working for Boost::Python).

> and ran again, getting this in the console:
> 
> 1
>     August 2006
> Mo Tu We Th Fr Sa Su
>     1  2  3  4  5  6
>  7  8  9 10 11 12 13
> 14 15 16 17 18 19 20
> 21 22 23 24 25 26 27
> 28 29 30 31

Have you examined the value of `cal'? I'd say it'll be None, because 
calendar.prmonth will have printed its output to the console rather than 
returning a string of it.

> But now my document has a text box with "None" in it.

That's because you passed it str(None) which returns the string 
representation of the value `None', which is "None".

> I have limited programming knowledge, but can muddle my way through some 
> stuff. Is it possible to get the Scripter to use this output? If so, 
> what am I missing to use it properly?

You need to find a function in the calendar module that _returns_ the 
formatted calendar rather than printing it. If there really isn't one 
(rather unlikely) it's possible to capture the output using cStringIO 
and stdout redirection, but I really don't think you'll need to do that.

It's often helpful to use the console to examine the value of your 
variables and to run individual statements, helping you understand what 
your script is doing. In this case, for example, just type:

cal

and run that "script"; it'll print the value of the variable cal in 
repr() form. It used to be easier to use the console interactively, but 
that functionality was made harder to use in exchange for syntax 
highlighting, a better editor, and more useful facilities for editing 
multi-statement snippets. There's a shortcut that runs the command in 
the console and clears the console input, though, and that's pretty 
handy when poking around for variables. (Does it still have a shortcut?).

--
Craig Ringer



More information about the scribus mailing list