³ò
Pt¡Ic           @   sè   d  Z  d d k Z y d d k l Z WnH e j
 o< y d d k l Z Wqq e j
 o d GHd Z qq Xn Xd d k Z d d k Z d d k	 Z	 d d k
 Z
 e i i e i d ƒ Z e i i e i d ƒ Z d d	 d „  ƒ  YZ d S(
   s<   librarybackend.py - Comic book library backend using sqlite.iÿÿÿÿN(   t   dbapi2s+   ! Could neither find pysqlite2 nor sqlite3.s
   library.dbt   library_coverst   LibraryBackendc           B   s  e  Z d  Z d „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s_   The LibraryBackend handles the storing and retrieval of library
    data to and from disk.
    c         C   s¬   d „  } t  i t ƒ |  _ | |  i _ t |  i _ |  i i d ƒ i ƒ  p |  i	 ƒ  n |  i i d ƒ i ƒ  p |  i
 ƒ  n |  i i d ƒ i ƒ  p |  i ƒ  n d  S(   Nc         S   s#   t  | ƒ d j o | d Sn | S(   s\   Return rows as sequences only when they have more than
            one element.
            i   i    (   t   len(   t   cursort   row(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   row_factory   s    s   pragma table_info(Book)s   pragma table_info(Collection)s   pragma table_info(Contain)(   R    t   connectt   _db_patht   _conR   t   strt   text_factoryt   executet   fetchallt   _create_table_bookt   _create_table_collectiont   _create_table_contain(   t   selfR   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   __init__   s    	c         C   s¢   | d j oC | d j o |  i i d ƒ } q˜ |  i i d d | f ƒ } nI | d j o |  i i d | f ƒ } n  |  i i d | d | f ƒ } | i ƒ  S(   sÙ   Return a sequence with all the books in <collection>, or *ALL*
        books if <collection> is None. If <filter_string> is not None, we
        only return books where the <filter_string> occurs in the path.
        s5   select id from Book
                    order by paths[   select id from Book
                    where path like ?
                    order by paths   %%%s%%s…   select id from Book
                    where id in (select book from Contain where collection = ?)
                    order by paths©   select id from Book
                    where id in (select book from Contain where collection = ?)
                    and path like ?
                    order by pathN(   t   NoneR	   R   R   (   R   t
   collectiont   filter_stringt   cur(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_books_in_collection0   s    				c      	   C   s~   y" |  i  i d | f ƒ i ƒ  } Wn  t j
 o d | GHd Sn Xt i | d t d t ƒ} | d j o d | GHn | S(   sr   Return a pixbuf with a thumbnail of the cover of <book>, or
        None if the cover can not be fetched.
        s2   select path from Book
                where id = ?s   ! Non-existant book #%dt   createt   dst_dirs   ! Could not get cover for %sN(	   R	   R   t   fetchonet	   ExceptionR   t	   thumbnailt   get_thumbnailt   Truet
   _cover_dir(   R   t   bookt   patht   thumb(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_coverI   s    			c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   s^   Return the filesystem path to <book>, or None if <book> isn't
        in the library.
        s.   select path from Book
            where id = ?(   R	   R   R   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_pathX   s    	c         C   sF   |  i  i d | f ƒ } | i ƒ  } | d j o d Sn t i | ƒ S(   sS   Return the name of <book>, or None if <book> isn't in the
        library.
        s.   select name from Book
            where id = ?N(   R	   R   R   R   t   encodingt
   to_unicode(   R   R    R   t   name(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_name`   s    	c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   s^   Return the number of pages in <book>, or None if <book> isn't
        in the library.
        s/   select pages from Book
            where id = ?(   R	   R   R   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_pagesk   s    	c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   s]   Return the archive format of <book>, or None if <book> isn't
        in the library.
        s0   select format from Book
            where id = ?(   R	   R   R   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_formats   s    	c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   s\   Return the size of <book> in bytes, or None if <book> isn't
        in the library.
        s.   select size from Book
            where id = ?(   R	   R   R   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_book_size{   s    	c         C   sE   | d j o |  i i d ƒ } n |  i i d | f ƒ } | i ƒ  S(   s…   Return a sequence with all the subcollections in <collection>,
        or all top-level collections if <collection> is None.
        sd   select id from Collection
                where supercollection isnull
                order by namesa   select id from Collection
                where supercollection = ?
                order by nameN(   R   R	   R   R   (   R   R   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_collections_in_collectionƒ   s    		c         C   s   |  i  i d ƒ } | i ƒ  S(   sˆ   Return a sequence with all collections (flattened hierarchy).
        The sequence is sorted alphabetically by collection name.
        s3   select id from Collection
            order by name(   R	   R   R   (   R   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_all_collections‘   s    		c         C   sC   |  i  i d | f ƒ } | i ƒ  } | d j o d Sn t | ƒ S(   se   Return the name field of the <collection>, or None if the
        collection does not exist.
        s4   select name from Collection
            where id = ?N(   R	   R   R   R   t   unicode(   R   R   R   R'   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_collection_name™   s    	c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   sŸ   Return the collection called <name>, or None if no such
        collection exists. Names are unique, so at most one such collection
        can exist.
        s4   select id from Collection
            where name = ?(   R	   R   R   (   R   R'   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_collection_by_name¤   s    	c         C   s"   |  i  i d | f ƒ } | i ƒ  S(   s+   Return the supercollection of <collection>.s?   select supercollection from Collection
            where id = ?(   R	   R   R   (   R   R   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   get_supercollection­   s    	c   
   
   C   sP  t  i i | ƒ } t  i i | ƒ } t i | ƒ } | d j o t Sn | \ } } } t i	 | d t
 d t ƒ|  i i d | f ƒ i ƒ  } yY | d j	 o& |  i i d | | | | | f ƒ n# |  i i d | | | | | f ƒ Wn# t i j
 o d | GHt Sn X| d j	 o2 |  i i d | f ƒ i ƒ  }	 |  i |	 | ƒ n t
 S(	   sä   Add the archive at <path> to the library. If <collection> is
        not None, it is the collection that the books should be put in.
        Return True if the book was successfully added (or was already
        added).
        R   R   s.   select id from Book
            where path = ?sp   update Book set
                    name = ?, pages = ?, format = ?, size = ?
                    where path = ?sq   insert into Book
                    (name, path, pages, format, size)
                    values (?, ?, ?, ?, ?)s&   ! Could not add book %s to the librarys2   select id from Book
                where path = ?N(   t   osR!   t   abspatht   basenamet   archivet   get_archive_infoR   t   FalseR   R   R   R   R	   R   R   R    t   Errort   add_book_to_collection(
   R   R!   R   R'   t   infot   formatt   pagest   sizet   oldR    (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   add_book³   s0    						c         C   sD   y |  i  i d | f ƒ t SWn t i j
 o d | GHn Xt S(   sw   Add a new collection with <name> to the library. Return True
        if the collection was successfully added.
        s8   insert into Collection
                (name) values (?)s   ! Could not add collection %s(   R	   R   R   R    R8   R7   (   R   R'   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   add_collectionÕ   s    	c         C   s^   y |  i  i d | | f ƒ Wn: t i j
 o n& t i j
 o d | | f GHn Xd S(   s   Put <book> into <collection>.sD   insert into Contain
                (collection, book) values (?, ?)s(   ! Could not add book %s to collection %sN(   R	   R   R    t   DatabaseErrorR8   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyR9   á   s    	c         C   sD   | d j o |  i i d | f ƒ n |  i i d | | f ƒ d S(   s}   Put <subcollection> into <supercollection>, or put
        <subcollection> in the root if <supercollection> is None.
        sY   update Collection
                set supercollection = NULL
                where id = ?sV   update Collection
                set supercollection = ?
                where id = ?N(   R   R	   R   (   R   t   subcollectiont   supercollection(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   add_collection_to_collectionì   s
    		c         C   s\   y! |  i  i d | | f ƒ t SWn4 t i j
 o n  t i j
 o d | GHn Xt S(   s_   Rename the <collection> to <name>. Return True if the renaming
        was successful.
        s;   update Collection set name = ?
                where id = ?s#   ! Could not rename collection to %s(   R	   R   R   R    RA   R8   R7   (   R   R   R'   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   rename_collectionù   s    	c         C   s½   |  i  | ƒ } | d j o t Sn | d t d ƒ } x) |  i | ƒ o | d t d ƒ } q; W|  i | ƒ d j o t Sn |  i i d | f ƒ i ƒ  } |  i i d | | f ƒ t	 S(   sš   Duplicate the <collection> by creating a new collection
        containing the same books. Return True if the duplication was
        successful.
        t    s   (Copy)s4   select id from Collection
            where name = ?sy   insert or ignore into Contain (collection, book)
            select ?, book from Contain
            where collection = ?N(
   R/   R   R7   t   _R0   R@   R	   R   R   R   (   R   R   R'   t	   copy_namet   copy_collection(    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   duplicate_collection  s     		c         C   sc   |  i  | ƒ } | d j	 o t i | d t ƒn |  i i d | f ƒ |  i i d | f ƒ d S(   s#   Remove the <book> from the library.R   s   delete from Book where id = ?s"   delete from Contain where book = ?N(   R$   R   R   t   delete_thumbnailR   R	   R   (   R   R    R!   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   remove_book  s
    c         C   sF   |  i  i d | f ƒ |  i  i d | f ƒ |  i  i d | f ƒ d S(   s6   Remove the <collection> (sans books) from the library.s#   delete from Collection where id = ?s(   delete from Contain where collection = ?sR   update Collection set supercollection = NULL
            where supercollection = ?N(   R	   R   (   R   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   remove_collection#  s
    
	c         C   s   |  i  i d | | f ƒ d S(   s    Remove <book> from <collection>.sA   delete from Contain
            where book = ? and collection = ?N(   R	   R   (   R   R    R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyt   remove_book_from_collection+  s    	c         C   s   |  i  i ƒ  |  i  i ƒ  d S(   s!   Commit changes and close cleanly.N(   R	   t   committ   close(   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyRP   0  s    c         C   s   |  i  i d ƒ d  S(   Nsî   create table book (
            id integer primary key,
            name string,
            path string unique,
            pages integer,
            format integer,
            size integer,
            added date default current_date)(   R	   R   (   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyR   5  s    	c         C   s   |  i  i d ƒ d  S(   Ns‚   create table collection (
            id integer primary key,
            name string unique,
            supercollection integer)(   R	   R   (   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyR   ?  s    	c         C   s   |  i  i d ƒ d  S(   NsŽ   create table contain (
            collection integer not null,
            book integer not null,
            primary key (collection, book))(   R	   R   (   R   (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyR   E  s    	N(   t   __name__t
   __module__t   __doc__R   R   R   R#   R$   R(   R)   R*   R+   R,   R-   R/   R0   R1   R?   R@   R9   RD   RE   RJ   RL   RM   RN   RP   R   R   R   (    (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pyR      s6   												"										
	(    (   RS   R2   t   sqlite3R    t   ImportErrort	   pysqlite2R   R5   t	   constantsR%   R   R!   t   joint   DATA_DIRR   R   R   (    (    (    sN   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/librarybackend.pys   <module>   s    