Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Readers > Sony Reader > Sony Reader Dev Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 02-19-2007, 12:44 AM   #1
Falstaff
Member
Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.
 
Posts: 21
Karma: 289
Join Date: Dec 2006
Device: Sony eReader
Create LRS and LRF files from Python with pylrs

I have just uploaded a Python package that I created called pylrs. It is available from the main page of http://www.falstaffshouse.com.

The package can create both lrs anf lrf files. The lrf files are created directly, without the use of a compiler. An example of creating an lrf file using the package is:

Code:
from pylrs.pylrs import *

b = Book()
b.Page().TextBlock().Paragraph("Hello, World!")
b.renderLrf("hello.lrf")
This will create an lrf file with a single page containing the line "Hello, World!"

There are several example programs included in the distribution that illustrate the various features. I created the package in order to format the plays of Shakespeare, which are also available from the same web page.

The source code is included in the distribution.

The package is optimistically versioned 1.0, but it's never been used by anyone but me, so assume it's an early alpha version at best.

The software has only been tested on Windows XP, Python 2.4 and 2.5, and the PRS-500.

Last edited by Falstaff; 02-19-2007 at 12:49 AM.
Falstaff is offline   Reply With Quote
Old 02-19-2007, 01:51 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Atlast! I am really, really happy you did this. Could I ask you to upload the python module to the python package index http://www.python.org/pypi

That will make it easy for other projects to depend on it. If that's too much trouble, I'd be willing to do it for you.

Also can you add some platform detection to set the default encoding. Something like
if 'linux' in sys.platform.lower() or 'darwin' in sys.platform.lower():
default_encoding = 'utf-8'

Thanks and great work.

EDIT: Browsing through the code it doesn't look like you have support for embedded images, or did I miss it?
kovidgoyal is offline   Reply With Quote
Advert
Old 02-19-2007, 09:23 AM   #3
Falstaff
Member
Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.
 
Posts: 21
Karma: 289
Join Date: Dec 2006
Device: Sony eReader
You can have embedded images in an ImageBlock, so they can be cover pages or illustrations. You can't use them by themselves in a Header or on a Canvas yet.

Yes, I forgot the default source encoding was set to the windows character set. Is "utf-8" or "latin-1" a better default for non-Windows 8 bit strings?

For any real projects, you should probably set the encoding explicitly with Book(sourceencoding="<codec>"). See the russanbook.py example program for an example of this.

The code can be uploaded anywhere -- there are no restictions on it's use. But I only plan to maintain the latest version on the web page.

Last edited by Falstaff; 02-19-2007 at 10:13 AM.
Falstaff is offline   Reply With Quote
Old 02-19-2007, 12:32 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
On most modern Linux systems, utf-8 is a good choice for default encoding. Not so sure about OSX.
kovidgoyal is offline   Reply With Quote
Old 03-15-2007, 02:49 AM   #5
tor
Junior Member
tor began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Mar 2007
Device: PRS-500
Quote:
Originally Posted by kovidgoyal
On most modern Linux systems, utf-8 is a good choice for default encoding. Not so sure about OSX.
Firstly, the standard forum newbie "hi all".

Yeah, OSX has it's own ver of UTF 8 ( known as UTF 8 MAC) and can cause headaches ( i.e. samba for one ).

And thanks for the python scripts, my readers should hopefully be here tomorrow, as an OS X user it looks as if I'm in for a bit of fun.
tor is offline   Reply With Quote
Advert
Old 03-23-2007, 10:59 AM   #6
utrost
Junior Member
utrost began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Mar 2007
Device: Sony reader
Jiha!

Hi Mike,

I've played arround with your scripts, and I am really, really happy about them.
The only problem is, that I dont know Python at all But hey - it cant be that difficult

Uwe, tryin' to build a HTML2LRF-script the very moment.
utrost is offline   Reply With Quote
Old 03-27-2007, 08:25 PM   #7
Falstaff
Member
Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.
 
Posts: 21
Karma: 289
Join Date: Dec 2006
Device: Sony eReader
Sorry, I was out of town and didn't see your message. I think it would be fairly hard to use these scripts without knowing python pretty well, but if you have any specific questions, let me know and I'll try to answer them.
Falstaff is offline   Reply With Quote
Old 03-31-2007, 07:42 PM   #8
ashkulz
Addict
ashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enough
 
ashkulz's Avatar
 
Posts: 350
Karma: 705
Join Date: Dec 2006
Location: Mumbai, India
Device: Kindle 1/REB 1200
Falstaff: I made some changes in your code, in file pylrs.py:587
Code:
if not isinstance(textBlock, TextBlock) and not isinstance(textBlock, ImageBlock):
I needed support for referring to ImageBlocks from a TOC for my PDFRead project.

Thanks for the very wonderful library!
ashkulz is offline   Reply With Quote
Old 04-01-2007, 07:32 PM   #9
Falstaff
Member
Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.Falstaff has a complete set of Star Wars action figures.
 
Posts: 21
Karma: 289
Join Date: Dec 2006
Device: Sony eReader
Thanks for the fix -- I'll incorporate it in the next version. I'm glad the code is of use to people.
Falstaff is offline   Reply With Quote
Old 04-06-2007, 04:33 PM   #10
EatingPie
Blueberry!
EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.
 
EatingPie's Avatar
 
Posts: 888
Karma: 133343
Join Date: Mar 2007
Device: Sony PRS-500 (RIP); PRS-600 (Good Riddance); PRS-505; PRS-650; PRS-350
Falstaff.

Would you mind providing the code for the conversion project you did?

The samples are handy, but a more complete example (one that opens files, for example) would be great for a Python newb like myself.

-Pie
EatingPie is offline   Reply With Quote
Old 04-19-2007, 01:21 AM   #11
EatingPie
Blueberry!
EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.
 
EatingPie's Avatar
 
Posts: 888
Karma: 133343
Join Date: Mar 2007
Device: Sony PRS-500 (RIP); PRS-600 (Good Riddance); PRS-505; PRS-650; PRS-350
BUMP.

Still hoping for sample code that reads a file for conversion!

-Pie
EatingPie is offline   Reply With Quote
Old 04-19-2007, 08:35 AM   #12
ashkulz
Addict
ashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enoughashkulz will become famous soon enough
 
ashkulz's Avatar
 
Posts: 350
Karma: 705
Join Date: Dec 2006
Location: Mumbai, India
Device: Kindle 1/REB 1200
Quote:
Originally Posted by EatingPie
Still hoping for sample code that reads a file for conversion!-Pie
Don't know if it will help you, but you can look at the code for generating LRF files for PDFRead (look at the LrfOutput class, line 182).
ashkulz is offline   Reply With Quote
Old 04-22-2007, 01:48 AM   #13
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
@Falstaff

It may be a good idea to change the default blockStyle for an ImageBlock to
BlockStyle(blockrule='block-fixed') that way the reader doesn't scale images off the page when going from S->M->L
kovidgoyal is offline   Reply With Quote
Old 04-23-2007, 02:48 PM   #14
EatingPie
Blueberry!
EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.EatingPie puts his or her pants on both legs at a time.
 
EatingPie's Avatar
 
Posts: 888
Karma: 133343
Join Date: Mar 2007
Device: Sony PRS-500 (RIP); PRS-600 (Good Riddance); PRS-505; PRS-650; PRS-350
Quote:
Originally Posted by ashkulz
Don't know if it will help you, but you can look at the code for generating LRF files for PDFRead (look at the LrfOutput class, line 182).
Thanks for the pointers, I'll give it a go.

Now another questions...

I have code to generate UTF-16 smart quotes, which I would want to embed into pylrs. I noted that just pre-pending a "u" to a string flags it as UTF-16, from the (super ultra crappy) Python reference I used. Problem is, this foregoes any algorithmic conversion. EG...

text = u"This is text";

But I need to scan the text and look for preceding and following characters to decide which curly-quote to apply (open or close). Once a string has the "u" prepended, I have no idea what I'm dealing with.

My lack of python knowledge really shows here. So let me break this down into two basic questions.

(1) Where would I parse text and convert it to UTF-16?

(2) Is there a GOOD Python reference on the Internet? The one I used was the Python Library Reference, is literally the worse documentation I've seen, ever! (http://docs.python.org/lib/lib.html).

I really prefer makelrf3, but I have several bugs (and lack of features) that makes pylrs look far more viable... if I can just figure out how to parse a string to apply my curly-quotes algorithm.

-Pie
EatingPie is offline   Reply With Quote
Old 04-23-2007, 04:23 PM   #15
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
pydoc codecs.decode
pydoc codecs.encode

and u'my string' is a python unicode object not a utf-16 encoded string.
kovidgoyal is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pissed off with LRF formatting: LRF/LRS clean tool? grimborg LRF 8 02-15-2010 01:14 PM
How do I Create a Python Plugin? Sydney's Mom Plugins 25 01-27-2010 06:26 AM
Converting LRS to LRF mcortez Sony Reader 1 11-28-2007 11:26 AM
LRS to LRF on Mac OSX - any suggestions? Lime2K Sony Reader 3 07-29-2007 10:15 PM
PRS-500 Span tags in LRS and LRF files -- do I understand them? Falstaff Sony Reader Dev Corner 2 01-31-2007 10:34 AM


All times are GMT -4. The time now is 01:23 PM.


MobileRead.com is a privately owned, operated and funded community.