Mini Shell

Direktori : /usr/lib/python3.6/site-packages/simpleline/render/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/simpleline/render/__pycache__/containers.cpython-36.pyc

3

<�Zc�>�@s�ddlmZddlmZmZmZddlmZdddgZe�Z	Gdd�de�Z
Gd	d�de
�ZGd
d�de
�ZGdd�de�Z
Gdd
�d
e�ZGdd�de�ZdS)�)�ceil)�Widget�
TextWidget�SeparatorWidget)�get_simpleline_logger�ListRowContainer�ListColumnContainer�WindowContainercsbeZdZdZd�fdd�	Zedd��Zedd	��Zejd
d	��Zddd�Z	d
d�Z
dd�Z�ZS)�	ContainerzCBase class for containers which will do positioning of the widgets.NTcsPt�j�d|_g|_|r8x|D]}|jjt|��q W|rFt�|_nd|_dS)aHConstruct Container.

        :param items: List of items for positioning in this Container. Callback can't be specified this way.
        :type items: List of items for rendering.

        :param numbering: Enable/disable automatic numbering (labels) for items. Enabled by default (True).
        :type numbering: bool
        N)�super�__init__�_key_pattern�_items�append�
ContainerItem�
KeyPattern)�self�items�	numbering�i)�	__class__�� /usr/lib/python3.6/containers.pyr"s	


zContainer.__init__cCs
t|j�S)zReturn items count.)�lenr)rrrr�size7szContainer.sizecCs|jS)zkReturn key pattern which will be used for items numbering.

        Will return `None` if not set.
        )r
)rrrr�key_pattern<szContainer.key_patterncCs
||_dS)z~Set the key pattern object which will be used for items numbering.

        Setting `None` will stop doing numbering.
        N)r
)rrrrrrDscCs"|jjt|||��t|j�dS)aNAdd item to the Container.

        :param item: Add item to this container.
        :type item: Could be item (based on `simpleline.render.widgets.Widget`)
                    or other container (based on `simpleline.render.containers.Container`).

        :param callback: Add callback for this item. This callback will be called when user activate this `item`.
        :type callback: function ``func(data)``.

        :param data: Data which will be passed to the callback.
        :param data: Anything.

        :returns: ID of the item in this Container.
        :rtype: int
        �)rrrr)r�item�callback�datarrr�addLsz
Container.addcCsr|jst|�tkrdS|jj|�}|dk	rn|dkrny$|j|}|jdk	rV|j|j�dStk
rldSXdS)a=Process input from the user if any of the items in the list was called.

        This method must be called in `UIScreen.input()` method if list widget should call the callbacks.

        :param key: Key pressed from user.
        :type key: str

        :returns: True if key was processed. False otherwise.
        FNrT)r
�type�str�translate_input_to_widget_idrrr�
IndexError)r�key�resrrrr�process_user_input_s


zContainer.process_user_inputcCst|jj|��}|S)aCreate TextWidget from KeyPattern.

        :param item_id: Create label for item with this id.
        :type item_id: int

        :returns: Widget with label for the item with item_id.
        :rtype: `simpleline.render.widgets.TextWidget` instance.
        )rr
�get_widget_label)r�item_id�
number_widgetrrr�create_number_labelxs	zContainer.create_number_label)NT)NN)
�__name__�
__module__�__qualname__�__doc__r�propertyrr�setterr r'r+�
__classcell__rr)rrr
s
r
csVeZdZdZd�fdd�	Zddd�Zddd	�Zed
d��Z�fdd
�Z	dd�Z
�ZS)r	z^Base container for screens.

    This can hold other containers or Widgets for rendering.
    Ncst�jdd�||_dS)aConstruct base container for screens.

        This container doesn't have numbering support. Input other containers in it to allow numbering
        and input processing.

        :param title: Title line with separator after this title.
        :type title: str
        F)rN)rr�_title)r�title)rrrr�s	zWindowContainer.__init__rcCs|j|||�}|j|�|S)a?Add widget and after widget add blank line.

        This method will call
        `self.add(item, callback, data)`
        `self.add_separator(lines)`

        :param item: Add item to this container.
        :type item: Could be item (based on `simpleline.render.widgets.Widget`)
                    or other container (based on `simpleline.render.containers.Container`).

        :param callback: Add callback for this item. This callback will be called when user activate this `item`.
        :type callback: function ``func(data)``.

        :param data: Data which will be passed to the callback.
        :param data: Anything.

        :param blank_lines: How many blank lines should be printed.
        :type blank_lines: int greater than 0.

        :returns: ID of the item in this Container.
        :rtype: int
        )r �
add_separator)rrrrZblank_linesr)rrr�add_with_separator�s
z"WindowContainer.add_with_separatorcCs|jt|��dS)z�Add blank lines between widgets.

        :param lines: How many blank lines should be printed.
        :type lines: int greater than 0.
        N)r r)r�linesrrrr5�szWindowContainer.add_separatorcCs|jS)zTitle of WindowContainer.)r3)rrrrr4�szWindowContainer.titlecsVt�j|�|jdd�|jr(|j|�x(|jD]}|j}|j|�|j|�q0WdS)z�Render widgets to it's internal buffer.

        :param width: the maximum width the item can use
        :type width: int

        :return: nothing
        rN)r�render�set_cursor_positionr3�_draw_title_and_separatorr�widget�draw)r�widthrr;)rrrr8�s

zWindowContainer.rendercCs<t|j�}t�}|j|�|j|�|j|�|j|�dS)N)rr3rr8r<)rr=Ztitle_widget�seprrrr:�s



z)WindowContainer._draw_title_and_separator)N)NNr)r)r,r-r.r/rr6r5r0r4r8r:r2rr)rrr	�s

csNeZdZdZd�fdd�	Z�fdd�Zd	d
�Zdd�Zd
d�Zdd�Z	�Z
S)ra�Place widgets in rows automatically.

    Compared to the ColumnWidget this is able to handle word wrapping correctly.

    There is numbering N) automatically for all items. To disable this feature call `self.key_pattern = None`.
    If you want other numbering then look on `KeyPattern` class.

    Widgets will be placed based on the number of columns in the following way:

    1) w1  2) w2  3) w3
    4) w4  5) w5  6) w6
    ....
    N�Tcs*t�j||�||_||_||_g|_dS)a�Create ListWidget with specific number of columns.

        :param columns: How many columns we want.
        :type columns: int, bigger than 0

        :param items: List of items for positioning in this Container. Callback can't be specified this way.
        :type items: List of items for rendering.

        :param columns_width: Width of every column. If nothing specified the maximum width will be distributed
                              to columns.
        :type columns_width: int or None

        :param spacing: Set the spacing between columns.
        :type spacing: int

        :param numbering: Enable/disable automatic numbering (labels) for items. Enabled by default (True).
        :type numbering: bool
        N)rr�_columns�_columns_width�_spacing�_numbering_widgets)r�columnsrZ
columns_widthZspacingr)rrrr�s
zListRowContainer.__init__cst�j|�|jdkr>|jd}||j}t|||j�|_|j�}|j|�}d}x�|D]�}d}x�t|�D]v\}	}
|j	|
}|j
}|j||�|jdk	r�|j
|
}
t|
j�}|j|
�|j|||�|j|dd�|||	}qlWt||j|j�|j}qZWdS)z�Render widgets to it's internal buffer.

        :param width: the maximum width the item can use
        :type width: int

        :return: nothing
        NrrT)�block)rr8rAr@rB�int�_get_ordered_map�_lines_per_every_row�	enumeraterr;r9r
rCr�textr<�maxr=)rr=Zspaces_between_columnsZsum_spacingZordered_mapZlines_per_rowsZcol_pos�colZrow_pos�row_idr)Z	containerr;r*Zwidget_width)rrrr8s,









zListRowContainer.rendercCsp|j�g}x^|D]V}xPt|�D]D\}}|j|}t|�|krH|jd�t||t|jj���||<q WqW|S)Nr)�_render_all_itemsrIrrrrKr;�	get_lines)rrZ
lines_per_rowZcolumn_itemsrMr)rrrrrH4s


$z%ListRowContainer._lines_per_every_rowcCs�x�t|j�D]r\}}|j}|dkr*td��|jrr|j|�}t|j�}|j|�|j	j
|�||8}|dkrrtd��|jj|�qWdS)Nrz5Widget can't be rendered! Columns width is too small.zWWidget can't be rendered with numbering on! Increase column width or disable numbering.)rIrrA�
ValueErrorr
r+rrJr8rCrr;)rr)rZ
item_widthr*Znumber_widthrrrrNDs


z"ListRowContainer._render_all_itemscCs4|j�}x&t|j�D]}|||jj|�qW|S)z�Return list of identifiers (index) to the original item list.

        .. NOTE: Use of ``self._prepare_list()` is encouraged to create output list and just fill up this list.
        )�
_prepare_list�rangerr@r)r�ordering_mapr)rrrrGZsz!ListRowContainer._get_ordered_mapcCsttdd�td|j���S)z}Prepare list for items ordering to rows and columns.

        List will be prepared as ([column 1], [column 2], ...)
        cSsgS)Nr)�xrrr�<lambda>lsz0ListRowContainer._prepare_list.<locals>.<lambda>r)�list�maprRr@)rrrrrQgszListRowContainer._prepare_list)NNr?T)r,r-r.r/rr8rHrNrGrQr2rr)rrr�s
,
c@seZdZdZdd�ZdS)ra�Place widgets in columns automatically.

    Compared to the ColumnWidget this is able to handle word wrapping correctly.

    There is numbering N) automatically for all items. To disable this feature call `self.key_pattern = None`.
    If you want other numbering then look on `KeyPattern` class.

    Widgets will be placed based on the number of columns in the following way:

    1) w1  4) w4  7) w7
    2) w2  5) w5  8) w8
    3) w3  6) w6  9) w9
    cCsN|j�}tt|j�|j�}x,t|j�D]}t||�}||j|�q(W|S)N)	rQrrrr@rRrrFr)rrSZitems_in_columnr)Zcol_positionrrrrG~sz$ListColumnContainer._get_ordered_mapN)r,r-r.r/rGrrrrros
c@s*eZdZdZddd�Zdd�Zdd	�Zd
S)rz0Pattern for automatic key printing before items.�{:d}) rcCs||_||_dS)aCreate the pattern class.

        For enabling greater functionality than python 3 format is able to do, feel free to override this class and
        use your subclass instead.

        :param pattern: Set pattern which will be called for every item.
        :type pattern: Strings format method. See https://docs.python.org/3.3/library/string.html#format-string-syntax.

        :param offset: Set the offset for numbering items. Default is 1 to start indexing naturally for user.
        :type offset: int
        N)�_pattern�_offset)r�pattern�offsetrrrr�szKeyPattern.__init__cCs|jj||j�S)z�Get widget identifier for user input description.

        It should be something similar to the pattern.

        :param item_id: Position of the widget in the list.
        :type item_id: int starts from 0.
        )rY�formatrZ)rr)rrrr(�szKeyPattern.get_widget_labelcCs2yt|�dStk
r,tjd|�dSXdS)a:Get id of the widget from the user input.

        This is reverse translation to `self.get_widget_identifier()`.

        :param user_input: Input from user:
        :type user_input: str

        :return: ID of the widget in the list or None if the input can't be translated.
        :rtype: int or None
        rz(No callback registered for user input %sN)rFrP�log�debug)rZ
user_inputrrrr#�s
z'KeyPattern.translate_input_to_widget_idN)rXr)r,r-r.r/rr(r#rrrrr�s

rc@seZdZdZddd�ZdS)rz�Item used inside of containers to store widgets callbacks and data.

    Internal representation for Containers. Do not use this class directly.
    NcCs||_||_||_dS)aConstruct WidgetContainer.

        :param widget: Any item from `simpleline.render.widgets` or `Container`.
        :type widget: Class subclassing the `simpleline.render.widgets.Widget` class
                    or `simpleline.render.containers.Container`.

        :param callback: This callback will be called as reaction on user input.
        :type callback: Function with one data parameter: `def func(data):`.

        :param data: Params which will be passed to callback.
        :type data: Anything.
        N)r;rr)rr;rrrrrr�s
zContainerItem.__init__)NN)r,r-r.r/rrrrrr�srN)ZmathrZsimpleline.render.widgetsrrrZsimpleline.loggingr�__all__r^r
r	rr�objectrrrrrr�<module>s
f[.