B
    EDb,                 @   s<   d dl Z d dlT d dlmZmZmZmZ G dd deZdS )    N)*)PORTABLE_BYTESPORTABLE_STRINGpytype_supportsafe_conversionsc               @   s.   e Zd ZdZdddZddd	Zdd
dZdS )TypeConverterzThe type converter mainly serves as a tool for the dispatcher providing the means to
	validate and convert between the interface and function layers
	UTF-8FTc             C   s   || _ || _|| _dS )aq  In the TypeConverter constructor you can set conversion properties like which
		encoding to use when converting forth and back between unicode to str (or bytes and
		str for python 3). The default encoding is UTF-8.
		allow_unsafe_conversion is a boolean that tells the TypeConverter if unsafe conversions are allowed,
		An example of an unsafe conversion is float to int, as any floating point part will be truncated
		in the conversion.
		only_strings_to_unicode tells the converter to let types other than unicode and str (or bytes and
		str in python 3) be converted to the expected type when calling to_unicode_string
		N)encodingallow_unsafe_conversiononly_strings_to_unicode)selfr	   r
   r    r   9G:\easytimepro\master/mysite/ladon\types\typeconverter.py__init__   s    zTypeConverter.__init__Nc          
   C   s  |rx|D ]}||}q
W t |}|tkr>|tkr>t|}t}|tksRtt ||ttgkr|tkrr|| j}|rx|D ]}||}q|W |S yT|tkr|d  tdks| dkrd}n||}|rx|D ]}||}qW |S  t	k
r } zt
|t|W dd}~X Y nX dS )z-Convert a unicode string to certain types.
		r   F0FN)typeboolr   ZNonUnicodeErrorr   encoder	   upperstrip	ExceptionZFromUnicodeConversionErrorstr)r   valtyp
prefilterspostfiltersfuncZvtyper   r   r   from_unicode_string   s4    

(
z!TypeConverter.from_unicode_stringc          
   C   s  |rx|D ]}||}q
W t |}|ttgkrt|tkr8n|tkrNt|| j}nt|}|rpx|D ]}||}q`W |S | js|t| krt||y||}W n2 tk
r } zt||t	|W dd}~X Y nX | j
rn|tkrt| }t|}|rx|D ]}||}q W |S )zuConvert a primitive type to unicode. The method uses TypeConverter
		options in the decition making, and encoding.
		N)r   r   r   r	   r
   r   ZUnsafeConversionDisabledr   ZUnsafeConversionErrorr   r   r   lower)r   r   Zexpected_typr   r   r   r   r   r   r   r   to_unicode_stringI   s<    


"
zTypeConverter.to_unicode_string)r   FT)NN)NN)__name__
__module____qualname____doc__r   r   r!   r   r   r   r   r      s   

.r   )	sysZmysite.ladon.exceptions.typesZmysite.ladon.compatr   r   r   r   objectr   r   r   r   r   <module>   s   