o �J�h�f�@s�UddlmZddlZddlmZmZmZmZmZddl Z ddl m Z ddl m Z eefZded<ed d �d D��ZGd d �d �ZGdd�d�Ze�e�e�e�dS)�)� annotationsN)�MutableMapping�Mapping�MutableSequence�Iterator�Iterable)�Any�)�replaced_by_pep8ztuple[type, ...]�str_typeccs�|]}|VqdS�N�)�.0�_r r �OC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\pyparsing\results.py� <genexpr>s�rr c@s>eZdZUded<dgZdd d �Zd d �Zd d�Zdd�ZdS)�_ParseResultsWithOffsetztuple[ParseResults, int]�tup�p1� ParseResults�p2�int�return�NonecCs||f|_dSr �r)�selfrrr r r�__init__�z _ParseResultsWithOffset.__init__cCs |j|Sr r�r�ir r r� __getitem__� z#_ParseResultsWithOffset.__getitem__cCs|jSr r�rr r r� __getstate__ sz$_ParseResultsWithOffset.__getstate__cGs|d|_dS�Nrr)r�argsr r r� __setstate__#rz$_ParseResultsWithOffset.__setstate__N)rrrrrr) �__name__� __module__� __qualname__�__annotations__� __slots__rr r#r&r r r rrs   rc@s�eZdZUdZdgdfZded<ded<ded<d ed <d ed <d ed<ded<dZGdd�de�Zdpdd�Z dddde fdqdd�Z dd�Z e fdd�Z dd �Zdrd!d"�Zdsd$d%�Zdrd&d'�Zdtd)d*�Zdtd+d,�Zd-d.�Zd/d0�Zd1d2�Zdrd3d4�Zd5d6�Zdud7d8�Zd9d:�Zd;d<�Zd=d>�Zd?d@�ZdAdB�ZdvdDdE�ZdvdFdG�Z dwdHdI�Z!dxdJdK�Z"dxdLdM�Z#dydOdP�Z$dQdR�dzdUdV�Z%d{dXdY�Z&dwdZd[�Z'dwd\d]�Z(d|d_d`�Z)d}dxdbdc�Z*ddde�Z+dfdg�Z,dhdi�Z-djdk�Z.dldm�Z/e0dudwdndo��Z1e%Z2 e&Z3 e)Z4dS)~ra{Structured parse results, to provide multiple means of access to the parsed data: - as a list (``len(results)``) - by list index (``results[0], results[1]``, etc.) - by attribute (``results.<results_name>`` - see :class:`ParserElement.set_results_name`) Example:: integer = Word(nums) date_str = (integer.set_results_name("year") + '/' + integer.set_results_name("month") + '/' + integer.set_results_name("day")) # equivalent form: # date_str = (integer("year") + '/' # + integer("month") + '/' # + integer("day")) # parse_string returns a ParseResults object result = date_str.parse_string("1999/12/31") def test(s, fn=repr): print(f"{s} -> {fn(eval(s))}") test("list(result)") test("result[0]") test("result['month']") test("result.day") test("'month' in result") test("'minutes' in result") test("result.dump()", str) prints:: list(result) -> ['1999', '/', '12', '/', '31'] result[0] -> '1999' result['month'] -> '12' result.day -> '31' 'month' in result -> True 'minutes' in result -> False result.dump() -> ['1999', '/', '12', '/', '31'] - day: '31' - month: '12' - year: '1999' Nr ztuple[Any, ...]� _null_values�str�_name�_parentzset[str]� _all_names�bool�_modalz list[Any]�_toklistzdict[str, Any]�_tokdict)r.r/r0r2r3r4c@seZdZdZddd�ZdS)zParseResults.Lista� Simple wrapper class to distinguish parsed list results that should be preserved as actual Python lists, instead of being converted to :class:`ParseResults`:: LBRACK, RBRACK = map(pp.Suppress, "[]") element = pp.Forward() item = ppc.integer element_list = LBRACK + pp.DelimitedList(element) + RBRACK # add parse actions to convert from ParseResults to actual Python collection types def as_python_list(t): return pp.ParseResults.List(t.as_list()) element_list.add_parse_action(as_python_list) element <<= item | element_list element.run_tests(''' 100 [2,3,4] [[2, 1],3,4] [(2, 1),3,4] (2,3,4) ''', post_parse=lambda s, r: (r[0], type(r[0]))) prints:: 100 (100, <class 'int'>) [2,3,4] ([2, 3, 4], <class 'list'>) [[2, 1],3,4] ([[2, 1], 3, 4], <class 'list'>) (Used internally by :class:`Group` when `aslist=True`.) NcCs:|durg}t|t�st|j�dt|�j����t�|�S)Nz* may only be constructed with a list, not )� isinstance�list� TypeErrorr'�type�__new__)�cls� containedr r rr9�s � zParseResults.List.__new__r )r'r(r)�__doc__r9r r r r�Listgs&r=cKs�t|t�r|St�|�}d|_d|_t�|_|durg|_nt|t t f�r8t|tj �r2|dd�gnt |�|_n|g|_t �|_ |Sr )r5r�objectr9r.r/�setr0r3r6�_generator_typer=�dictr4)r:�toklist�name�kwargsrr r rr9�s   ��zParseResults.__new__Trrc Cs�|||_|dus |dkrdS||t�rt|�}|s|h|_||_||jvr(dS||ttf�r2|g}|rV||t�rDt t|j �d�||<n t t|d�d�||<|||_dSz |d||<WdSt t t fyz||urt|||<YdS||_YdSw)N�r)r2rr-r0r.r,r r8rrr3�KeyErrorr7� IndexError)rrBrC�asList�modalr5r r rr�s4     �zParseResults.__init__cCsLt|ttf�r |j|S||jvr|j|ddStdd�|j|D��S)N�����rcSsg|]}|d�qS)rr �r�vr r r� <listcomp>��z,ParseResults.__getitem__.<locals>.<listcomp>)r5r�slicer3r0r4rrr r rr �s   zParseResults.__getitem__cCs�||t�r|j�|t��|g|j|<|d}n!||ttf�r'||j|<|}n|j�|g�t|d�g|j|<|}||t�rC||_dSdSr$) rr4�getr6rrOr3rr/)r�krLr5�subr r r� __setitem__�s     �  �zParseResults.__setitem__c Cs�t|ttf�s |j|=dSt|j�}|j|=t|t�r*|dkr#||7}t||d�}tt|�|���}|� �|j� �D]}|D]}t |�D]\}\}}t ||||k�||<qFq@q<dS)Nrr ) r5rrOr4�lenr3r6�range�indices�reverse�values� enumerater) rr�mylen�removed� occurrences�jrQ�value�positionr r r� __delitem__�s(    ����zParseResults.__delitem__cCs ||jvSr �r4)rrQr r r� __contains__r!zParseResults.__contains__rcC� t|j�Sr )rTr3r"r r r�__len__ r!zParseResults.__len__cCs|jp|j Sr )r3r4r"r r r�__bool__ szParseResults.__bool__rcCrcr ��iterr3r"r r r�__iter__r!zParseResults.__iter__cCst|jddd��S�NrJrfr"r r r� __reversed__szParseResults.__reversed__cCrcr )rgr4r"r r r�keysr!zParseResults.keysc��fdd����D�S)Nc3s�|]}�|VqdSr r �rrQr"r rrs�z&ParseResults.values.<locals>.<genexpr>�rkr"r r"rrX�zParseResults.valuescrl)Nc3s�|] }|�|fVqdSr r rmr"r rrs�z%ParseResults.items.<locals>.<genexpr>rnr"r r"r�itemsrozParseResults.itemscCs |j S)z� Since ``keys()`` returns an iterator, this method is helpful in bypassing code that looks for the existence of any defined results names.rar"r r r�haskeyss zParseResults.haskeyscOs�|sdg}|��D]\}}|dkr|d|f}q td|����t|dt�s2t|�dks2|d|vr?|d}||}||=|S|d}|S)a� Removes and returns item at specified index (default= ``last``). Supports both ``list`` and ``dict`` semantics for ``pop()``. If passed no argument or an integer argument, it will use ``list`` semantics and pop tokens from the list of parsed tokens. If passed a non-integer argument (most likely a string), it will use ``dict`` semantics and pop the corresponding value from any defined results names. A second default return value argument is supported, just as in ``dict.pop()``. Example:: numlist = Word(nums)[...] print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321'] def remove_first(tokens): tokens.pop(0) numlist.add_parse_action(remove_first) print(numlist.parse_string("0 123 321")) # -> ['123', '321'] label = Word(alphas) patt = label("LABEL") + Word(nums)[1, ...] print(patt.parse_string("AAB 123 321").dump()) # Use pop() in a parse action to remove named result (note that corresponding value is not # removed from list form of results) def remove_LABEL(tokens): tokens.pop("LABEL") return tokens patt.add_parse_action(remove_LABEL) print(patt.parse_string("AAB 123 321").dump()) prints:: ['AAB', '123', '321'] - LABEL: 'AAB' ['AAB', '123', '321'] rJ�defaultrz)pop() got an unexpected keyword argument r )rpr7r5rrT)rr%rDrQrL�index�ret� defaultvaluer r r�pop%s(&zParseResults.popcCs||vr||S|S)a^ Returns named result matching the given key, or if there is no such name, then returns the given ``default_value`` or ``None`` if no ``default_value`` is specified. Similar to ``dict.get()``. Example:: integer = Word(nums) date_str = integer("year") + '/' + integer("month") + '/' + integer("day") result = date_str.parse_string("1999/12/31") print(result.get("year")) # -> '1999' print(result.get("hour", "not specified")) # -> 'not specified' print(result.get("hour")) # -> None r )r�key� default_valuer r rrP]szParseResults.getcCsN|j�||�|j��D]}t|�D]\}\}}t||||k�||<qq dS)a; Inserts new element at location index in the list of parsed tokens. Similar to ``list.insert()``. Example:: numlist = Word(nums)[...] print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321'] # use a parse action to insert the parse location in the front of the parsed results def insert_locn(locn, tokens): tokens.insert(0, locn) numlist.add_parse_action(insert_locn) print(numlist.parse_string("0 123 321")) # -> [0, '0', '123', '321'] N)r3�insertr4rXrYr)rrs� ins_stringr\rQr^r_r r rryts  ���zParseResults.insertcCs|j�|�dS)a Add single element to end of ``ParseResults`` list of elements. Example:: numlist = Word(nums)[...] print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321'] # use a parse action to compute the sum of the parsed integers, and add it to the end def append_sum(tokens): tokens.append(sum(map(int, tokens))) numlist.add_parse_action(append_sum) print(numlist.parse_string("0 123 321")) # -> ['0', '123', '321', 444] N)r3�append)r�itemr r rr{�szParseResults.appendcCs(t|t�r |�|�dS|j�|�dS)a Add sequence of elements to end of ``ParseResults`` list of elements. Example:: patt = Word(alphas)[1, ...] # use a parse action to append the reverse of the matched strings, to make a palindrome def make_palindrome(tokens): tokens.extend(reversed([t[::-1] for t in tokens])) return ''.join(tokens) patt.add_parse_action(make_palindrome) print(patt.parse_string("lskdj sdlkjf lksd")) # -> 'lskdjsdlkjflksddsklfjkldsjdksl' N)r5r�__iadd__r3�extend)r�itemseqr r rr~�s zParseResults.extendcCs|jdd�=|j��dS)z7 Clear all elements and results names. N)r3r4�clearr"r r rr��s zParseResults.clearcCs2z||WSty|�d�rt|��YdSw)N�__rE)rF� startswith�AttributeError)rrCr r r� __getattr__�s   �zParseResults.__getattr__�othercCs|��}||7}|Sr )�copy)rr�rtr r r�__add__�szParseResults.__add__cs�|s|S|jr7t|j���fdd��|j��}�fdd�|D�}|D]\}}|||<t|dt�r6||d_q"|j|j7_|j|jO_|S)Ncs|dkr�S|�Sr$r )�a)�offsetr r�<lambda>�rNz'ParseResults.__iadd__.<locals>.<lambda>c s4g|]\}}|D]}|t|d�|d��f�qqS)rr )r)rrQ�vlistrL)� addoffsetr rrM�s���z)ParseResults.__iadd__.<locals>.<listcomp>r)r4rTr3rpr5rr/r0)rr�� otheritems�otherdictitemsrQrLr )r�r�rr}�s"    �  �zParseResults.__iadd__cCs"t|t�r |dkr |��S||Sr$)r5rr�)rr�r r r�__radd__�szParseResults.__radd__cCs"t|�j�d|j�d|���d�S)N�(�, �))r8r'r3�as_dictr"r r r�__repr__�s"zParseResults.__repr__cCsdd�dd�|jD��dS)N�[r�cSs&g|]}t|t�r t|�nt|��qSr )r5rr-�repr)rrr r rrM�s��z(ParseResults.__str__.<locals>.<listcomp>�])�joinr3r"r r r�__str__�s�����zParseResults.__str__rEcCsLg}|jD]}|r|r|�|�t|t�r||��7}q|�t|��q|Sr )r3r{r5r� _asStringListr-)r�sep�outr|r r rr��s   zParseResults._asStringListF)�flattenr�r6cs,�fdd�}|rg|���Sdd��jD�S)a� Returns the parse results as a nested list of matching tokens, all converted to strings. If flatten is True, all the nesting levels in the returned list are collapsed. Example:: patt = Word(alphas)[1, ...] result = patt.parse_string("sldkj lsdkj sldkj") # even though the result prints in string-like form, it is actually a pyparsing ParseResults print(type(result), result) # -> <class 'pyparsing.ParseResults'> ['sldkj', 'lsdkj', 'sldkj'] # Use as_list() to create an actual list result_list = result.as_list() print(type(result_list), result_list) # -> <class 'list'> ['sldkj', 'lsdkj', 'sldkj'] c3sN�t�g���}|r%|��}t|t�r|�|ddd��n|V|s dSdSri)� collections�deque�popleftr5r� extendleft)�pr�to_visitZto_dor"r r� flatteneds�  �z'ParseResults.as_list.<locals>.flattenedcSs"g|] }t|t�r |��n|�qSr )r5r�as_list)r�resr r rrMs��z(ParseResults.as_list.<locals>.<listcomp>)r3)rr�r�r r"rr��s  �zParseResults.as_listrAcs&�fdd��t�fdd�|��D��S)a� Returns the named parse results as a nested dictionary. Example:: integer = Word(nums) date_str = integer("year") + '/' + integer("month") + '/' + integer("day") result = date_str.parse_string('12/31/1999') print(type(result), repr(result)) # -> <class 'pyparsing.ParseResults'> (['12', '/', '31', '/', '1999'], {'day': [('1999', 4)], 'year': [('12', 0)], 'month': [('31', 2)]}) result_dict = result.as_dict() print(type(result_dict), repr(result_dict)) # -> <class 'dict'> {'day': '1999', 'year': '12', 'month': '31'} # even though a ParseResults supports dict-like access, sometime you just need to have a dict import json print(json.dumps(result)) # -> Exception: TypeError: ... is not JSON serializable print(json.dumps(result.as_dict())) # -> {"month": "31", "day": "1999", "year": "12"} cs0t|t�r|��r |��S�fdd�|D�S|S)Ncsg|]}�|��qSr r rK��to_itemr rrM7rNz9ParseResults.as_dict.<locals>.to_item.<locals>.<listcomp>)r5rrqr���objr�r rr�5s "z%ParseResults.as_dict.<locals>.to_itemc3s �|] \}}|�|�fVqdSr r �rrQrLr�r rr;��z'ParseResults.as_dict.<locals>.<genexpr>)rArpr"r r�rr� s zParseResults.as_dictcCs:t|j�}|j��|_|j|_|j|jO_|j|_|S)a Returns a new shallow copy of a :class:`ParseResults` object. `ParseResults` items contained within the source are shared with the copy. Use :class:`ParseResults.deepcopy()` to create a copy with its own separate content values. )rr3r4r�r/r0r.)rrtr r rr�=s  zParseResults.copycCs�|��}t|j�D]R\}}t|t�r|��|j|<q t|ttf�r"q t|t�rHt |��|j|<}|� �D]\}}t|t�rB|��n|||<q5q t|t �r[t |�dd�|D��|j|<q |S)zL Returns a new deep copy of a :class:`ParseResults` object. css&�|]}t|t�r |��n|VqdSr )r5r�deepcopyrKr r rr[s� �z(ParseResults.deepcopy.<locals>.<genexpr>) r�rYr3r5rr�r-�bytesrr8rpr)rrtrr��destrQrLr r rr�Ks"  �  ��zParseResults.deepcopy� str | Nonecs��jr�jS�jr�j}|j��}t�fdd�|D�d�St��dkrBt�j�dkrBtt�j����dddvrBtt�j����SdS)a Returns the results name for this token expression. Useful when several different expressions might match at a particular location. Example:: integer = Word(nums) ssn_expr = Regex(r"\d\d\d-\d\d-\d\d\d\d") house_number_expr = Suppress('#') + Word(nums, alphanums) user_data = (Group(house_number_expr)("house_number") | Group(ssn_expr)("ssn") | Group(integer)("age")) user_info = user_data[1, ...] result = user_info.parse_string("22 111-22-3333 #221B") for item in result: print(item.get_name(), ':', item[0]) prints:: age : 22 ssn : 111-22-3333 house_number : 221B c3s.�|]\}}|D] \}}|�ur|VqqdSr r )rrQr�rL�locr"r rrs����z(ParseResults.get_name.<locals>.<genexpr>Nr r)rrJ) r.r/r4rp�nextrTrgrXrk)r�parZparent_tokdict_itemsr r"r�get_name`s   �� zParseResults.get_namercCs�g}d}|�|r|t|���nd�|sd�|�S|��rktdd�|��D��}|D]?\}} |r6|�|�|�|�d|�d|�d��t| t�sR|�t | ��q+| s\|�t| ��q+|�| j ||||dd ��q+t d d�|D��syd�|�S|} d} d} t | �D]O\} } t| t�r�| j ||||dd �}|�| �|�| |�d | �d | �|�| |d�|�� �q�|�| �|�| |�d | �d | �|�| |d�| �� �q�d�|�S) aM Diagnostic method for listing out the contents of a :class:`ParseResults`. Accepts an optional ``indent`` argument so that this string can be embedded in a nested display of other data. Example:: integer = Word(nums) date_str = integer("year") + '/' + integer("month") + '/' + integer("day") result = date_str.parse_string('1999/12/31') print(result.dump()) prints:: ['1999', '/', '12', '/', '31'] - day: '31' - month: '12' - year: '1999' � rEcss �|] \}}t|�|fVqdSr )r-r�r r rr�r�z$ParseResults.dump.<locals>.<genexpr>z z- z: r )�indent�full� include_list�_depthcss�|]}t|t�VqdSr )r5r)r�vvr r rr�s�r�z]:) r{r-r�r�rq�sortedrpr5rr��dump�anyrY)rr�r�r�r�r��NLrprQrL�incr�nlrr�Zvv_dumpr r rr��sZ    ��  �2�2� zParseResults.dumpcOs tj|��g|�Ri|��dS)a$ Pretty-printer for parsed results as a list, using the `pprint <https://docs.python.org/3/library/pprint.html>`_ module. Accepts additional positional or keyword args as defined for `pprint.pprint <https://docs.python.org/3/library/pprint.html#pprint.pprint>`_ . Example:: ident = Word(alphas, alphanums) num = Word(nums) func = Forward() term = ident | num | Group('(' + func + ')') func <<= ident + Group(Optional(DelimitedList(term))) result = func.parse_string("fna a,b,(fnb c,d,200),100") result.pprint(width=40) prints:: ['fna', ['a', 'b', ['(', 'fnb', ['c', 'd', '200'], ')'], '100']] N)�pprintr�)rr%rDr r rr��s zParseResults.pprintcCs|j|j��d|j|jffSr )r3r4r�r0r.r"r r rr#�s��zParseResults.__getstate__cCs*|\|_\|_}}|_t|�|_d|_dSr )r3r4r.r?r0r/)r�stater�� inAccumNamesr r rr&s  zParseResults.__setstate__cCs |j|jfSr )r3r.r"r r r�__getnewargs__s zParseResults.__getnewargs__cCstt|��t|���Sr )�dirr8r6rkr"r r r�__dir__ szParseResults.__dir__cCsrdd�}|g�}|��D]\}}t|t�r||j||d�7}q |||g|||�d�7}q |dur7||g|d�}|S)z� Helper classmethod to construct a ``ParseResults`` from a ``dict``, preserving the name-value relations as results names. If an optional ``name`` argument is given, a nested ``ParseResults`` will be returned. cSs.zt|�Wn tyYdSwt|t� S)NF)rg� Exceptionr5r r�r r r� is_iterables   � z+ParseResults.from_dict.<locals>.is_iterable)rC)rCrHN)rpr5r� from_dict)r:r�rCr�rtrQrLr r rr� s  zParseResults.from_dict)NN)rr)rr1)rr)rrr )r�rrr)rr)rr-)rE)r�r1rr6)rrA)rr�)rETTr)5r'r(r)r<r,r*r+r6r=r9r5rr rSr`rbrdrerhrjrkrXrprqrvrPryr{r~r�r�r�r}r�r�r�r�r�r�r�r�r�r�r�r#r&r�r�� classmethodr�rH�asDict�getNamer r r rr'st - 2  �'        8       "    0J r)� __future__rr��collections.abcrrrrrr��typingr�utilr r-r�r r*r8r@rr�registerr r r r�<module>s$   
Memory