ó
|c‹Ac           @   s¹   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l Z d d d „  ƒ  YZ d S(   sS    Handler class
    Adds specific for MedusWiki funcionality to the TopicMap object
iÿÿÿÿ(   t   TopicMap(   t   Topic(   t   Association(   t   Member(   t   RoleSpec(   t   Parser(   t
   Occurrence(   t   BaseName(   t
   InstanceOfNt   Handlerc           B   sª   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d d d „ Z
 d „  Z d	 „  Z d
 „  Z d d „ Z d „  Z d „  Z d „  Z d d d „ Z RS(   s[    Handler class
        Adds specific for MedusWiki funcionality to the TopicMap object
    c         C   sR   | r6 t  j j | ƒ r6 t ƒ  } | j | ƒ |  _ n t d ƒ |  _ | |  _ d S(   s—   Initialize Handler object. Input - path to xtm file
        (filename=None) - if xtm file exists, then parse it, otherwise create new TopicMap
        t   0N(   t   ost   patht   existsR   t   parset   _tmR    t	   _filename(   t   selft   filenamet   parser(    (    s   lib/xtm/Handler.pyt   __init__   s
    	c         C   s   | |  _  d S(   s@   (tm:TopicMap) - change currently wrapped TopicMap with given oneN(   R   (   R   t   tm(    (    s   lib/xtm/Handler.pyt   setTopicMap"   s    c         C   s   |  j  j j ƒ  S(   s   Return list of topic id's (   R   t   topicst   keys(   R   (    (    s   lib/xtm/Handler.pyt	   getTopics&   s    c         C   s   |  j  S(   s   Return TopicMap(   R   (   R   (    (    s   lib/xtm/Handler.pyt   getTopicMap*   s    c         C   s   |  j  j ƒ  S(   s   Return id of decorated TopicMap(   R   t   getId(   R   (    (    s   lib/xtm/Handler.pyR   .   s    c         C   s   | |  j  j k S(   s-   (name:str) - return True if such topic exists(   R   R   (   R   t   name(    (    s   lib/xtm/Handler.pyt   hasTopic2   s    c         C   s‡   t  | ƒ } | r( | j t | ƒ ƒ n  | rD | j t | ƒ ƒ n  | r` | j t | ƒ ƒ n  |  j j | ƒ |  j j |  j ƒ d S(   s´   (topicid:str, occurence_uri:str, name:str; instance_of:str)
        (topicid, occurrence_uri=None, name=None, instance_of=None)
        create new topic, and add it to the topicmapN(	   R   t   addChildR   R   R   R   t   addTopict	   exportxmlR   (   R   t   topicidt   occurrence_uriR   t   instance_oft   topic(    (    s   lib/xtm/Handler.pyR   6   s    c         C   s'   |  j  j | ƒ |  j  j |  j ƒ d S(   s*   (topicid:str) - remove topic from topicmapN(   R   t   removeTopicR    R   (   R   R!   (    (    s   lib/xtm/Handler.pyR%   H   s    c         C   s'   |  j  j | ƒ |  j  j |  j ƒ d S(   s    (assid:str) - remove associationN(   R   t   removeAssociationR    R   (   R   t   assid(    (    s   lib/xtm/Handler.pyR&   M   s    c         C   sM   |  j  j | j ƒ  } | rI x* | D] } | j ƒ  j d k r# t Sq# Wn  t S(   s;   (id:str) - return True if topic is XTM instanceOf 'builtIn't   builtIn(   R   R   t   getInstanceOfListt   getTopicReft   idt   Truet   False(   R   R+   t	   instancest   instance(    (    s   lib/xtm/Handler.pyt	   isBuiltInR   s    c         C   s÷   g  |  j  j j ƒ  D] } t | ƒ ^ q } | rA t | ƒ d pD d } t | ƒ } t | ƒ }	 t | ƒ }
 |	 j t | ƒ ƒ | rš |
 j t | ƒ ƒ n |
 j t |  j	 | ƒ ƒ ƒ | j
 |	 ƒ | j
 |
 ƒ |  j  j | ƒ |  j  j |  j ƒ d S(   sU   (ltopicid:str, lrole:str, rtopicid:str, rrole=None):
        - create new associationi   i    N(   R   t   associationsR   t   intt   maxR   R   t   setRoleSpecR   t   getOppositeRoleR   t   addAssociationR    R   (   R   t   ltopicidt   lrolet   rtopicidt   rroleR+   t   idst   uidt   associationt   lmembert   rmember(    (    s   lib/xtm/Handler.pyR6   [   s    +c         C   s‘   xŠ |  j  | ƒ D]y \ } } } |  j j | } xW | j ƒ  D]I } | j ƒ  r< | j ƒ  j | k r< | j ƒ  j ƒ  j d k r… | Sq< q< Wq Wd S(   sÇ   (topicid:str) - return topicid, of opposite role. Opposite topic is topic, that has association
        'oppositeRole' with given one.
        If such topic doesn't exists return 'relatedTo'
        t   oppositeRolet	   relatedTo(   t   getTopicAssociationsR   R1   t   getMemberListt   getRoleSpecR*   R+   (   R   R!   R'   t   roleR$   t   asst   member(    (    s   lib/xtm/Handler.pyR5   t   s    !c   
      C   sÃ   g  } x¶ |  j  j j ƒ  D]¢ \ } } | } t } xh | j ƒ  D]Z } | j ƒ  j } | | k r’ t } | j ƒ  r‰ | j ƒ  j ƒ  j }	 q˜ d }	 q> | } q> W| r | j	 | |	 | f ƒ q q W| S(   s¼   (topicid: str) - return information about topic associations -> [(assid, role, topic)*] 
        MedusWiki adds restriction to XTM association, that it could have only two members
        t    (
   R   R1   t   itemsR-   RC   R*   R+   R,   RD   t   append(
   R   R!   t   rvalueR'   R=   t   thatt   foundRG   R$   RE   (    (    s   lib/xtm/Handler.pyRB   „   s    	
c         C   sI   g  } x< |  j  d ƒ D]+ \ } } } | d k r | j | ƒ q q W| S(   s“   return list of all possible roles -> [role_id1, role_id2,*]
        role is a topic that has association('type'-'typeOf') with topic #role
        RE   t   typeOf(   RB   RJ   (   R   t   rolesR'   RE   R$   (    (    s   lib/xtm/Handler.pyt   getRoles   s
    c      
   C   sÕ  | p	 | s g  Sg  } x¸|  j  j j ƒ  D]¤\ } } g  } x! | j ƒ  D] } | j | ƒ qH W| d j ƒ  j } | d j ƒ  j ƒ  j }	 | d j ƒ  j }
 | d j ƒ  j ƒ  j } | r| r| | k ré | j |
 | | f ƒ n  |	 | k rÍ| j | |	 |
 f ƒ qÍq) | rk| rk| | k rC| j |
 | | f ƒ n  |
 | k rÍ| j | |	 |
 f ƒ qÍq) | | k rœ| | k rœ| j |
 | | f ƒ n  |
 | k r) |	 | k r) | j | |	 |
 f ƒ q) q) W| S(   sd   return list of information about associations that fits given arguments. 
        (role|None, rtopic|None)->[(ltopic, role, rtopic),*]
        filter('AuthoredBy', 'Edvardas') == [('MedusWiki', 'AuthoredBy', 'Edvardas')]
        filter('AuthoredBy', None) == [('MedusWiki', 'AuthoredBy', 'Edvardas'),         ('ShitWiki', 'AuthoredBy', 'Petras')]
        i    i   (   R   R1   RI   RC   RJ   R*   R+   RD   (   R   RE   t   rtopicRK   R'   R=   t   membersRG   t   firstidt	   firstrolet   secondidt
   secondrole(    (    s   lib/xtm/Handler.pyt   filter©   s4     N(   t   __name__t
   __module__t   __doc__t   NoneR   R   R   R   R   R   R   R%   R&   R0   R6   R5   RB   RP   RW   (    (    (    s   lib/xtm/Handler.pyR	      s    												(    (   RZ   t   xtm.TopicMapR    t	   xtm.TopicR   t   xtm.AssociationR   t
   xtm.MemberR   t   xtm.RoleSpecR   t
   xtm.ParserR   t   xtm.OccurrenceR   t   xtm.BaseNameR   t   xtm.InstanceOfR   t   os.pathR   R	   (    (    (    s   lib/xtm/Handler.pyt   <module>   s   