ó
b=0Yc           @   sØ   d  d l  Z  d  d l Z d  d l Z y d  d l Z Wn e k
 rM d Z n Xd  d l m Z d  d l m Z m	 Z	 d Z
 d   Z d Z d Z d Z d d e d	  Z e d
  Z d   Z d   Z d e d  Z d S(   iÿÿÿÿN(   t	   NoPackage(   t   NotSupportedt   NoDBAccessDatat
   ISPManagerc           C   s"   t  j j d  p! t  j j d  S(   Ns   /usr/local/ispmgr/bin/ispmgrs   /usr/local/mgr5/sbin/mgrctl(   t   ost   patht   isfile(    (    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt   detect   s    s!   /usr/local/ispmgr/etc/ispmgr.confs   (\S+) "([^"]+)" {([^}]+)}s   (\S+)\s+(\S+)c         C   s  t    } d } | r  t   } x | D]t } | j   } t |  d k rs | j t  | g   | j | d  q% t |  d k r% | j | d  q% q% Wn  t |  } t   }	 xÖ t j	 t
 | j    D]Œ \ }
 } } t j	 t |  } |
 |  k rÎ | d | f k rÎ t  g  | D]0 \ } } | d k s;| | k r| | f ^ q } t | j    j t | j     r|	 j |  qqÎ qÎ W| j   |	 S(   sK  
    /usr/local/ispmgr/etc/ispmgr.conf parser
    :param sectype: Type sector for example: Service or DbServer or Account
    :param secname: Name sector. May be different
    :param seckeys: Name key for retrieving and filtering
    :param path: path to config file default /usr/local/ispmgr/etc/ispmgr.conf
    :return: list
    i   i    i   N(   t   dictt   Nonet   listt   splitt   lent   updatet   appendt   opent   ret   findallt   SECTION_PATTERNt   readt   KEYWORDS_PATTERNt   sett   itemst   issubsett   close(   t   sectypet   secnamet   seckeysR   t   seckeys_filtert   seckeys_extractedt   key_valt   key_val_splitedt   conf_streamt   result_listt   stypet   snamet   sbodyt   blstt   kt   vt   result(    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt	   conf_pars   s.    				(-'
c         C   s   t    } t d d d d d |   } x^ | D]V } y< | d | d	 <| d | d
 <| d | d <d | d <| SWq+ t k
 r q+ Xq+ Wt d |  f   d  S(   NR   t   DbServerR   t   Hostnamet   Passwords
   Type mysqlt   UserR   t   passt   logint   hostt   mysqlt   dbsA   Can not find database access data for localhost in config file %s(   R+   R,   s
   Type mysqlR-   (   R   R)   t   KeyErrorR   (   t
   _conf_patht   accesst   access_listt   access_from_conf(    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt	   db_access@   s    	
c      	   C   s   t  s t d   n  |  j d d  } |  d } |  d } t  j d | d | d | d d	  } | j   } d
 } | j |  | j   } | j   | S(   Ns@   Can not connect to database; MySQL-python package not installed.R0   t	   localhostR/   R.   t   usert   passwdR2   R1   s6   SELECT db.Db, db.User FROM db  GROUP BY db.User, db.Db(   t   MySQLdbR    t   gett   connectt   cursort   executet   fetchallR   (   R5   t   dbhostt   dblogint   dbpassR2   R?   t   sqlt   data(    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt   _dbname_dblogin_pairsO   s    

$
c           C   s   t  d   d  S(   Nsc   Getting all users registered in the Control Panel is not currently supported. Is under development.(   R   (    (    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt   cpusers^   s    c         c   s<  t  |  } t g  t j   D] } | d | d f ^ q  } xì t |  D]Þ \ } } | j d  rL | j   } t |  d k r*| d \ } }	 y: | j t	 |	   }
 |
 d k rÎ d |	 | | f GHn  Wn& t k
 r÷ d | | f GHd }
 n X|
 r'|  d k	 s|
 |  k r'| |
 f Vq'q*qL qL W| j   d S(   s   
    Extract (database name <=> control panel login) pairs from ISPmanager config file
    :param cplogin_lst:
    :param _conf_path:
    :return:
    i   i    s	   DbAssign i   s?   WARNING: can not find group name with id %s; line %d in file %ss(   WARNING: can not pars line %d in file %sN(   R   R   t   grpt   getgrallt	   enumeratet
   startswithR   R   R=   t   intR	   t
   ValueErrorR   (   t   cplogin_lstR4   R    t	   grp_tuplet   grpid_login_dictt	   line_numbt   linet   line_splitedt   dbnamet   user_uidt   cplogin(    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt   _dbname_cplogin_pairs_iterc   s$    3
(   R   R   RI   R<   t   ImportErrorR	   t   clcommon.cpapi.cpapiexceptionsR    R   R   t
   __cpname__R   t   ISP_DB_CONFR   R   R)   R8   RG   RH   RX   (    (    (    sE   /usr/lib/python2.7/site-packages/clcommon/cpapi/plugins/ispmanager.pyt   <module>   s$   
	$		