File: //usr/lib64/python3.9/site-packages/rhsm/__pycache__/pathtree.cpython-39.opt-1.pyc
a
X�Zh�. � @ sV d dl Z d dlZd dlmZmZmZ d dlmZ d dlm Z dZ
dZG dd� d�ZdS ) � N)�Tuple�List�Any)�GhettoBitStream��HuffmanNodezPATH ENDZlistingc @ s� e Zd ZdZedd�dd�Zeed�dd�Zed �d
d�Z de
eedd�d
d�Ze
ee
ed�dd��Zeeee
ef d�dd��Zeeed�dd��Ze
eee d�dd��Zeeeed�dd��Ze
eee eeed�dd��ZdS ) �PathTreeat
This builds and makes available a tree that represents matchable paths. A
path must be matched starting from its root and the root of the tree,
matching one segment at a time.
There are three trees involved in the process, and that can get confusing.
1) Word Tree: This is a Huffman tree made from the word list provided at
the beginning of the data stream.
2) Huffman Path Tree: This is a Huffman tree made of nodes whose values
will become nodes in the Path Tree. This tree exists so there can be
a Huffman code associated with each node in the Path Tree. However,
the Path Tree itself will arrange this data much differently.
3) Path Tree: This is the tree used to match paths. Each node is a
dict where keys are path segments (the middle part of /.../) and each
value is a list of other nodes.
N)�data�returnc C sn | � |�\}}t�|� tdd� |D ��}t|�}| �|�}t�|� tdd� |D ��}| �||||�| _dS )a�
Uncompresses data into a tree that can be traversed for matching paths
:param data: binary data as read from a file or pulled directly out
of a certificate extension. Data should be compressed
with huffman coding as described for v3 entitlement
certificates
:type data: binary string
c s s | ]}|j |jfV qd S �N)�code�value��.0�node� r �3/usr/lib64/python3.9/site-packages/rhsm/pathtree.py� <genexpr>9 � z$PathTree.__init__.<locals>.<genexpr>c s s | ]}|j |fV qd S r )r r r r r r = r N)�_unpack_datar Z
build_tree�dictr �_generate_path_leaves�_generate_path_tree� path_tree)�selfr Zword_leavesZunused_bits� word_dict� bitstream�path_leaves� path_dictr r r �__init__. s
zPathTree.__init__)�pathr
c C s, |� d�std��| �| j|�d��d��S )a�
Given an absolute path, determines if the path tree contains any
complete paths that exactly equal the beginning of this path. Thus,
The tree is traversed from its root, and as long as the provided path
does not hit its end before hitting the end of a tree path, this will
return True
:param path: absolute path to match against the tree
:return: True iff there is a match, else False
�/zpath must start with "/")�
startswith�
ValueError�_traverse_treer �strip�split)r r r r r �
match_path@ s
zPathTree.match_path)r
c C s g }| � |� d�t|��S )N�
)�build_path_list�join�sorted)r �pathsr r r �__str__N s
zPathTree.__str__)�acc�tree� curr_pathr
c C sv |du rd}|du r| j }t|t�r*|g}|D ]B}|�� D ]4\}}|tkrV|�|� q:| j||d||f d� q:q.dS )aW
Expand the Huffman tree into a list of paths
:param acc: an accumulator that stores the expanded paths. Callers should provide an empty list
:param tree: A dict representing a node in the greater path tree
:param curr_path: A string representing a path that is added to as nodes are visited
N� z%s/%s)r/ r0 )r �
isinstancer �items�PATH_END�appendr) )r r. r/ r0 �branch�k�vr r r r) S s
zPathTree.build_path_list)r/ �wordsr
c C s� t |v rdS |r�g }|d |v r.|�|d � |d tkrJt|�dkrJdS t|�� �D ]}|�d�rV|�|� qV|D ].}|| D ] }| �||dd� �r� dS q�qtdS )a�
Helper method for match_path that does recursive matching
:param tree: A dict representing a node in the greater path tree
:param words: list of words to match, the result of spliting a path
by the "/" separator. Words must be sorted with the
next word to match being at words[0]
:return: True iff there is a match, else False
Tr � �$NF)r4 r5 �LISTING�len�list�keysr"