B
    dv'                 @   s   d Z ddlZddlZddlmZ dZejZejZ	G dd dej
Zede G dd deZG d	d
 d
eZG dd dZG dd dZdS )a|  lightweight database dumper
Copyright (C) 2003 Duncan Brown
This file is part of the lightweight datapase dumper (ldbd)

The ldbd module provides classes for manipulating LIGO metadata database
tables.

References:
http://www.ligo.caltech.edu/docs/T/T990101-02.pdf
http://www.ligo.caltech.edu/docs/T/T990023-01.pdf
http://ldas-sw.ligo.caltech.edu/doc/db2/doc/html/ilwdformat.html
    N)git_versionz&Duncan Brown <dbrown@ligo.caltech.edu>c               @   s.   e Zd ZdZdZdZdZdZdZe	j
ZdZdS )	LIGOLWStreamz=
  Create a csv parser dialect for parsing LIGO_LW streams
  ,F\
"TN)__name__
__module____qualname____doc__	delimiterdoublequote
escapecharlineterminator	quotecharcsv	QUOTE_ALLquotingskipinitialspace r   r   V/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/glue/ldbd.pyr   )   s   r   c               @   s   e Zd ZdZdS )LIGOLwParseErrorz'Error parsing LIGO lightweight XML fileN)r   r	   r
   r   r   r   r   r   r   8   s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	XlatorzL
  All in one multiple string substitution class from the python cookbook
  c             C   s   t dtt j|  S )zC
    Build a re object based on keys in the current dictionary
    |)recompilejoinmapescapekeys)selfr   r   r   _make_regexA   s    zXlator._make_regexc             C   s   | | d S )z.
    Handler invoked for each regex match
    r   )group)r    matchr   r   r   __call__G   s    zXlator.__call__c             C   s   |   | |S )z3
    Translate text, returns the modified text
    )r!   sub)r    textr   r   r   xlatM   s    zXlator.xlatN)r   r	   r
   r   r!   r$   r'   r   r   r   r   r   =   s   r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )LIGOLwParserzr
  Provides methods for parsing the data from a LIGO lightweight XML
  file parsed with pyRXP into a dictionary
  c             C   s   t d| _t d| _t d| _t d| _t d| _t d| _t d| _t d| _	d	| _
tttttttttt| j| j| jd
| _tddddd| _d	S )z
    Initializes a LIGO lightweight XML parser with the necessary
    regular expressions and function for tuple translation
    z&(\A[a-z0-9_]+:|\A)([a-z0-9_]+):table\Zz0(\A[a-z0-9_]+:|\A)([a-z0-9_]+:|\A)([a-z0-9_]+)\Zz\A\s*"z"\s*\Zz\A\s+"z"*\s*\Zz\A\\[0-9][0-9][0-9]z\\,N)Zint2sZint_2sZint4sZint_4sZint8sZint_8sZreal4Zreal_4Zreal8Zreal_8Zlstringz	ilwd:charzilwd:char_u&><r   )z&amp;z&gt;z&lt;z\\)r   r   tabrxcolrxllsrxrlsrxlicrxricrxoctrxdlmrxuniqueintfloat_LIGOLwParser__lstring_LIGOLwParser__ilwdchartypesr   xmltostr)r    r   r   r   __init__Z   s.    zLIGOLwParser.__init__c             C   s:   | j d|}| jd|}| j|}| jd|}|S )z
    Returns a parsed lstring by stripping out and instances of
    the escaped delimiter. Sometimes the raw lstring has whitespace
    and a double quote at the beginning or end. If present, these
    are removed.
     r   )r.   r%   r/   r:   r'   r3   )r    Zlstrr   r   r   Z	__lstringy   s
    zLIGOLwParser.__lstringc             C   s|   |}| j d|}| jd|}| j|r>td| d  n:y| j|}W n( tk
rv   | jsj|}nt	dY nX |S )z
    If the ilwd:char field contains octal data, it is translated
    to a binary string and returned. Otherwise a lookup is done
    in the unique id dictionary and a binary string containing the
    correct unique id is returned.
    r<   zistr = ''z(unique id table has not been initialized)
r0   r%   r1   r2   r#   execr4   lookupAttributeErrorr   )r    ZistrZ	istr_origr   r   r   Z
__ilwdchar   s    zLIGOLwParser.__ilwdcharc                s  i }d}x^|d D ]P}|d dks4|d dkr^|d d   }y| j|d}W n  tk
rz   td| Y nX |i dd	d
||< x|d D ]}|d dks|d dkr|d d   }y| j|d}W n  tk
r   td| Y nX y|d d   }W n" tk
r2   td| Y nX ||| d |< || dg 	| qW |d7 }qW xF|
 D ]8}x.|d || d  d D ]}|d dks|d dkry|d d }	W n tk
r   tdY nX |	dkr td|	 t|d dkr |d 	d	 |d d }
|
|	|	 rN|
dt|	  }
tttdd	|
gt}t|}t|| d  t| \}}|dkrtd fddt|D }xt|D ]}t| \}}yZ|| d | }t|| dkrd|| |< n&| j|| d |  || || |< W n\ ttfk
r } z8dt| }|d|||| t|| f 7 }t|W dd}~X Y nX qW ttt||| d< qW qvW |S ) z
    Parse an XML tuple returned by pyRXP into a dictionary
    of LIGO metadata elements. The dictionary contains one
    entry for each table found in the XML tuple.
    r      Tabletable   Namez#unable to parse a valid table name r   r<   )poscolumnstreamqueryColumnrG      z$unable to parse a valid column name Typeztype is missing for column Z
orderedcolrF   ZStreamrH   	Delimiterzstream is missing delimiterr   z#unable to handle stream delimiter: Nz\nz'invalid stream length for given columnsc                s   g | ]}d g  qS )Nr   ).0i)ntypr   r   
<listcomp>   s    z+LIGOLwParser.parsetuple.<locals>.<listcomp>zstream translation error (%s) z#for column %s in table %s: %s -> %s)lowerr,   r#   r"   r@   r   r-   KeyError
setdefaultappendr   lenendswithnextr   readerr   r%   r   divmodranger9   
ValueErrorstrlistr   tuple)r    ZxmltuplerC   ZtupleidxtagtabZsubtagcoltypdelimrawrH   slenmlenZlftlstrO   jkthiscolerrmsgmsgr   )rP   r   
parsetuple   s|    "

* "zLIGOLwParser.parsetupleN)r   r	   r
   r   r;   r7   r8   rn   r   r   r   r   r(   T   s
   r(   c               @   s"   e Zd ZdZdddZdd ZdS )LIGOMetadataz
  LIGO Metadata object class. Contains methods for parsing a LIGO
  lightweight XML file and inserting it into a database, executing
  and SQL query to retrive data from the database and writing it
  to a LIGO lightweight XML file
  Nc             C   s8   || _ || _|rd| j_i | _tdddddd| _dS )z
    Connects to the database and creates a cursor. Initializes the unique
    id table for this LIGO lw document.

    xmlparser = pyRXP XML to tuple parser object
    lwtparser = LIGOLwParser object (tuple parser)
    Nz&amp;z&gt;z&lt;z\\z\")r)   r*   r+   r   r   )	xmlparser	lwtparserr4   rC   r   Zstrtoxml)r    rp   rq   r   r   r   r;     s    zLIGOMetadata.__init__c             C   sX   | j std| jstdddd |dD }|  |d}| j|| _dS )	zz
    Parses an XML document into a form read for insertion into the database

    xml = the xml document to be parsed
    zpyRXP parser not initializedz$LIGO_LW tuple parser not initializedr<   c             S   s   g | ]}|  qS r   )strip)rN   xr   r   r   rQ     s    z&LIGOMetadata.parse.<locals>.<listcomp>r   zutf-8N)rp   r   rq   r   splitencodern   rC   )r    xmlZ	ligolwtupr   r   r   parse  s    zLIGOMetadata.parse)NN)r   r	   r
   r   r;   rw   r   r   r   r   ro      s   
ro   )r   r   r   Zgluer   
__author__date__date__id__version__Dialectr   register_dialect	Exceptionr   dictr   r(   ro   r   r   r   r   <module>   s    ,