ó
#yRc           @   s   d  d l  Z  d  d l Z d  d l m Z m Z m Z d e f d     YZ d e f d     YZ d e f d     YZ	 d	 e
 f d
     YZ d S(   i˙˙˙˙N(   t   Elementt
   SubElementt   tostringt   ArtifactExceptionc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyR   &   s   t   ArtifactFormatExceptionc           B   s   e  Z RS(    (   R   R   (    (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyR   )   s   t   ArtifactValidationExceptionc           B   s   e  Z RS(    (   R   R   (    (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyR   ,   s   t   Artifactc           B   s   e  Z d  Z d d d d d  Z d   Z d   Z e d  Z d d  Z d d  Z	 e
 e
 e
 d	  Z e d
    Z e d d   Z e d d   Z RS(   sÖ   
    Simplified representation of Maven artifact for purpose of packaging

    Package consists mostly of simple properties and string formatting and
    loading functions to prevent code duplication elsewhere
    t    c         C   s^   | j    |  _ | j    |  _ | j    |  _ | j    |  _ | j    |  _ | j    |  _ d  S(   N(   t   stript   groupIdt
   artifactIdt	   extensiont
   classifiert   versiont	   namespace(   t   selfR   R   R   R   R   R   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   __init__7   s    c         C   s7   d j  d |  j d |  j d |  j d |  j d |  j  S(   Nu   {gid}:{aid}:{ext}:{cls}:{ver}t   gidt   aidt   extt   clst   ver(   t   formatR   R   R   R   R   (   R   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   __unicode__@   s
    			c         C   s   t  |   j t j    S(   N(   t   unicodet   encodet   syst   getfilesystemencoding(   R   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   __str__G   s    c         C   s  d } |  j  r |  j  d } n  d j d |  j d |  j  } |  j rb | d j d |  j  } n  |  j r |  j s | d } n  | d	 j d
 |  j  } n  | ré |  j sÍ t d j d t |      n  | d j d |  j  } n |  j sű |  j r| d } n  d j d | d |  S(   s6  Return representation of artifact as used in RPM dependencies

        versioned -- return artifact string including version

        Example outputs:
        mvn(commons-logging:commons-logging)
        mvn(commons-logging:commons-logging:1.2) # versioned
        mvn(commons-logging:commons-logging:war:)
        mvn(commons-logging:commons-logging:war:1.2) # versioned
        mvn(commons-logging:commons-logging:war:test-jar:)
        mvn(commons-logging:commons-logging:war:test-jar:1.3) # versioned
        maven31-mvn(commons-logging:commons-logging)
        t   mvns   -mvns   {gid}:{aid}R   R   s   :{ext}R   t   :s   :{clas}t   classC   Cannot create versioned string from artifact without version: {art}t   arts   :{ver}R   s   {namespace}({mvnstr})R   t   mvnstr(	   R   R   R   R   R   R   R   R   t   str(   R   t	   versionedR   R#   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   get_rpm_strJ   s(    					t   artifactc         C   sc   t  |  } xP d D]H } t |  |  r t |  |  r t | |  } t |  |  | _ q q W| S(   sH   
        Return XML Element node representation of the Artifact
        R   R   R   R   R   R   (   s
   artifactIds   groupIds	   extensions   versions
   classifiers	   namespace(   R    t   hasattrt   getattrR   t   text(   R   t   roott   keyt   item(    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   get_xml_elements   s    
c         C   s   |  j  |  } t | d t S(   sL   
        Return XML formatted string representation of the Artifact
        t   pretty_print(   R.   R   t   True(   R   R+   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   get_xml_str   s    c   
      C   sę   t  } t } t } t j d  } xn d D]f } t |  |  }	 |	 sI q( n  |	 rX t } n  |	 j d  d	 k rv t  } n  | j |	  r( t  } q( q( W| rŽ | rŽ t d
   n  | rĘ | rĘ t d   n  | rć | rć t d   n  t  S(   s   
        Function to validate current state of artifact with regards to
        wildcards, empty parts and backreference usage
        s   @\d+R   R   R   R   R   R   t   *i˙˙˙˙s   All parts of artifact are emptys   Wildcard used in artifacts   Backreference used in artifact(   s
   artifactIds   groupIds	   extensions   versions
   classifiers	   namespace(   R0   t   Falset   ret   compileR)   t   findt   matchR   (
   R   t   allow_emptyt   allow_wildcardst   allow_backreft	   all_emptyt   wildcard_usedt   backref_usedt
   backref_reR,   t   val(    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   validate   s,    
		c         C   sm   |  | j  | j | j | j | j | j  } x9 d D]1 } t | |  s4 t | | t | |   q4 q4 W| S(   s¤   
        Merge two artifacts into one. Information missing in dominant artifact will
        be copied from recessive artifact. Returns new merged artifact
        R   R   R   R   R   R   (   s
   artifactIds   groupIds	   extensions   versions
   classifiers	   namespace(   R   R   R   R   R   R   R)   t   setattr(   R   t   dominantt	   recessivet   retR,   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   merge_artifacts¤   s    
 c         C   s  i d d 6d d 6d d 6d d 6d d 6} xR | D]J } | j  d |  } | d
 k	 r0 | j d
 k	 r0 | j j   | | <q0 q0 W| d s | d rŁ t d   n  | sß | j  d	  } | d
 k	 rÖ | j j   } qß d } n  |  | d | d | d | d | d |  S(   s}   
        Create Artifact from xml.etree.ElementTree.Element as contained
        within pom.xml or a dependency map.
        R	   R   R   R   R   R   s   ./sI   Empty groupId or artifactId encountered. This is a bug, please report it!s   ./namespaceN(   R6   t   NoneR*   R
   R   (   R   t   xmlnodeR   t   partsR,   t   node(    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   from_xml_element˛   s&    

	c   	      C   sü   | j  d  } t |  d k  r< t d j d |    n  t |  d k ri t d j d |    n  | d } | d } t |  d	 k r | d n d
 } t |  d k rť | d n d
 } t |  d k rÝ | d n d
 } |  | | | | | |  S(   s÷   
        Create Artifact from Maven-style definition

        The string should be in the format of:
           groupId:artifactId[:extension[:classifier]][:version]

        Where last part is always considered to be version unless empty
        R    i   sH   Artifact string '{mvnstr}' does not contain ':' character. Can not parseR#   i   sB   Artifact string '{mvnstr}' contains too many colons. Can not parsei    i   i   R	   i   i˙˙˙˙(   t   splitt   lenR   R   (	   R   R#   R   t   tupR   R   R   R   R   (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   from_mvn_strÓ   s    


"""(   R   R   t   __doc__R   R   R   R3   R&   R.   R1   R0   R@   t   classmethodRE   RJ   RN   (    (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyR   /   s   		) (   R4   R   t
   lxml.etreeR    R   R   t	   ExceptionR   R   R   t   objectR   (    (    (    s9   /usr/lib/python2.7/site-packages/javapackages/artifact.pyt   <module>!   s   