ó
|c‹Ac           @   si   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d Z	 d Z
 d d d	 „  ƒ  YZ d
 S(   sA    TopicMap class
    http://www.topicmaps.org/xtm/#desc-topic-map
iÿÿÿÿ(   t   Topic(   t   Association(   t   IdExistsError(   t   minidoms   http://www.w3.org/1999/xlinks!   http://www.topicmaps.org/xtm/1.0/t   TopicMapc           B   s}   e  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 RS(   sI    TopicMap class
        http://www.topicmaps.org/xtm/#desc-topic-map
    s   utf-8c         C   s(   | |  _  | |  _ i  |  _ i  |  _ d S(   sL   Initialize TopicMap object. (id, encoding="utf-8") - set id and xml encodingN(   t   _idt	   _encodingt   topicst   associations(   t   selft   idt   encoding(    (    s   lib/xtm/TopicMap.pyt   __init__   s    			c         C   sh   | rd zM t  | d ƒ } t j t |  ƒ ƒ } | j d d |  j ƒ } | j | ƒ Wd | j ƒ  Xn  d S(   s(   Save TopicMap xml representation to filet   ws   	s   
N(   t   openR   t   parseStringt   strt   toprettyxmlR   t   writet   close(   R	   t   filenamet   ft   domTMt   content(    (    s   lib/xtm/TopicMap.pyt	   exportxml   s    c         C   s   |  j  S(   s   Return TopicMap id(   R   (   R	   (    (    s   lib/xtm/TopicMap.pyt   getId&   s    c         C   sB   t  | t ƒ r |  j | ƒ n  t  | t ƒ r> |  j | ƒ n  d S(   s#   Add object (:Topic or :Association)N(   t
   isinstanceR    t   addTopicR   t   addAssociation(   R	   t   child(    (    s   lib/xtm/TopicMap.pyt   addChild*   s    c         C   sA   | j  ƒ  } | |  j j ƒ  k r0 t | ƒ ‚ n  | |  j | <d S(   sU   Add Topic object. Throws IdExistsError exception if Topic with such id already existsN(   R   R   t   keysR   (   R	   t   topicR
   (    (    s   lib/xtm/TopicMap.pyR   1   s    c         C   sA   | j  ƒ  } | |  j j ƒ  k r0 t | ƒ ‚ n  | |  j | <d S(   sa   Add Association object. Throws IdExistsError exception if Association with such id already existsN(   R   R   R   R   (   R	   t   associationR
   (    (    s   lib/xtm/TopicMap.pyR   9   s    c         C   s   |  j  | =d S(   s    Remove Association with given idN(   R   (   R	   R
   (    (    s   lib/xtm/TopicMap.pyt   removeAssociationA   s    c         C   s   |  j  j ƒ  S(   s   Return list of Topics(   R   t   values(   R	   (    (    s   lib/xtm/TopicMap.pyt	   getTopicsE   s    c         C   s   |  j  j ƒ  S(   s   Return list of Associations(   R   R#   (   R	   (    (    s   lib/xtm/TopicMap.pyt   getAssociationsI   s    c         C   s6   |  j  j ƒ  |  j j ƒ  } | r+ | g p1 d g d S(   s2   Return list list of Topics and Associations or "" t    i    (   R   R#   R   (   R	   t   children(    (    s   lib/xtm/TopicMap.pyt   getChildrenM   s    c         C   sE   d j  g  |  j ƒ  D] } t | ƒ ^ q ƒ } d t t |  j | f S(   s0   Return XTM:str representation of TopicMap objectR&   s;   <topicMap xmlns="%s" xmlns:xlink="%s" id="%s">%s</topicMap>(   t   joinR(   R   t   ns_xtmt   ns_xlinkR   (   R	   R   t   childs(    (    s   lib/xtm/TopicMap.pyt   __str__R   s    .c         C   s   |  j  | =d S(   s   Remove Topic with given id:strN(   R   (   R	   R
   (    (    s   lib/xtm/TopicMap.pyt   removeTopicX   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R"   R$   R%   R(   R-   R.   (    (    (    s   lib/xtm/TopicMap.pyR      s   										N(    (   R1   t	   xtm.TopicR    t   xtm.AssociationR   t   xtm.ExceptionsR   t   xml.domR   R+   R*   R   (    (    (    s   lib/xtm/TopicMap.pyt   <module>   s   