/ lib / email / charset.pyc
charset.pyc
  1  o

  2  4�Hc|D�	@s�gd�ZddlmZddlZddlZddlmZddlmZdZ	dZ
  3  dZd	Zd
  4  Z
dZdZid
e	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�de	e	df�d
  5  d�de
  6  e
  7  df�de
  8  e
  9  df�de
 10  ddf�de
 11  ddf�e
 12  ddfe
 13  e
 14  dfee
 15  dfd ��Zid!d
�d"d
�d#d�d$d�d%d�d&d�d'd�d(d�d)d�d*d�d+d�d,d�d-d�d.d�d/d�d0d�d1d�dddd2dd3d
 16  d4��Zd5d6dd7�Zdd8d9�Zd:d;�Zd<d=�Zd>d?�ZGd@dA�dA�ZdS)B)�Charset�	add_alias�add_charset�	add_codec�)�partialN)�errors)�encode_7or8bit�����us-asciizunknown-8bit�z
 17  iso-8859-1z
 18  iso-8859-2z
 19  iso-8859-3z
 20  iso-8859-4z
 21  iso-8859-9ziso-8859-10ziso-8859-13ziso-8859-14ziso-8859-15ziso-8859-16zwindows-1252�viscii)NNN�big5�gb2312zeuc-jp�iso-2022-jp�	shift_jis�utf-8)rzkoi8-rr�latin_1zlatin-1�latin_2zlatin-2�latin_3zlatin-3�latin_4zlatin-4�latin_5zlatin-5�latin_6zlatin-6�latin_7zlatin-7�latin_8zlatin-8�latin_9zks_c_5601-1987zeuc-kr)zlatin-9�latin_10zlatin-10�cp949�euc_jp�euc_kr�ascii�eucgb2312_cn�big5_tw)rrr
cCs"|tkrtd��|||ft|<dS)a>Add character set properties to the global registry.
 22  
 23      charset is the input character set, and must be the canonical name of a
 24      character set.
 25  
 26      Optional header_enc and body_enc is either charset.QP for
 27      quoted-printable, charset.BASE64 for base64 encoding, charset.SHORTEST for
 28      the shortest of qp or base64 encoding, or None for no encoding.  SHORTEST
 29      is only valid for header_enc.  It describes how message headers and
 30      message bodies in the input charset are to be encoded.  Default is no
 31      encoding.
 32  
 33      Optional output_charset is the character set that the output should be
 34      in.  Conversions will proceed from input charset, to Unicode, to the
 35      output charset when the method Charset.convert() is called.  The default
 36      is to output in the same character set as the input.
 37  
 38      Both input_charset and output_charset must have Unicode codec entries in
 39      the module's charset-to-codec mapping; use add_codec(charset, codecname)
 40      to add codecs the module does not know about.  See the codecs module's
 41      documentation for more information.
 42      z!SHORTEST not allowed for body_encN)�SHORTEST�
 43  ValueError�CHARSETS)�charset�
 44  header_enc�body_enc�output_charset�r,�uC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\email\charset.pyrmsrcC�|t|<dS)z�Add a character set alias.
 45  
 46      alias is the alias name, e.g. latin-1
 47      canonical is the character set's canonical name, e.g. iso-8859-1
 48      N)�ALIASES)�alias�	canonicalr,r,r-r��rcCr.)a$Add a codec that map characters in the given charset to/from Unicode.
 49  
 50      charset is the canonical name of a character set.  codecname is the name
 51      of a Python codec, as appropriate for the second argument to the unicode()
 52      built-in, or to the encode() method of a Unicode string.
 53      N)�	CODEC_MAP)r(�	codecnamer,r,r-r�srcCs|tkr
 54  |�dd�S|�|�S)Nr"�surrogateescape)�UNKNOWN8BIT�encode)�string�codecr,r,r-�_encode�s
 55  r:c@s\eZdZdZefdd�Zdd�Zdd�Zdd	�Zd
 56  d�Z	dd
�Z
 57  dd�Zdd�Zdd�Z
dS)ra@	Map character sets to their email properties.
 58  
 59      This class provides information about the requirements imposed on email
 60      for a specific character set.  It also provides convenience routines for
 61      converting between character sets, given the availability of the
 62      applicable codecs.  Given a character set, it will do its best to provide
 63      information on how to use that character set in an email in an
 64      RFC-compliant way.
 65  
 66      Certain character sets must be encoded with quoted-printable or base64
 67      when used in email headers or bodies.  Certain character sets must be
 68      converted outright, and are not allowed in email.  Instances of this
 69      module expose the following information about a character set:
 70  
 71      input_charset: The initial character set specified.  Common aliases
 72                     are converted to their `official' email names (e.g. latin_1
 73                     is converted to iso-8859-1).  Defaults to 7-bit us-ascii.
 74  
 75      header_encoding: If the character set must be encoded before it can be
 76                       used in an email header, this attribute will be set to
 77                       charset.QP (for quoted-printable), charset.BASE64 (for
 78                       base64 encoding), or charset.SHORTEST for the shortest of
 79                       QP or BASE64 encoding.  Otherwise, it will be None.
 80  
 81      body_encoding: Same as header_encoding, but describes the encoding for the
 82                     mail message's body, which indeed may be different than the
 83                     header encoding.  charset.SHORTEST is not allowed for
 84                     body_encoding.
 85  
 86      output_charset: Some character sets must be converted before they can be
 87                      used in email headers or bodies.  If the input_charset is
 88                      one of them, this attribute will contain the name of the
 89                      charset output will be converted to.  Otherwise, it will
 90                      be None.
 91  
 92      input_codec: The name of the Python codec used to convert the
 93                   input_charset to Unicode.  If no conversion codec is
 94                   necessary, this attribute will be None.
 95  
 96      output_codec: The name of the Python codec used to convert Unicode
 97                    to the output_charset.  If no conversion codec is necessary,
 98                    this attribute will have the same value as the input_codec.
 99      cCs�zt|t�r|�d�nt|d�}Wntyt�|��w|��}t�||�|_	t
100  �|j	ttdf�\}}}|s<|j	}||_
||_t�||�|_t�|j	|j	�|_t�|j|j�|_dS)Nr")�
101  isinstance�strr7�UnicodeErrorr�CharsetError�lowerr/�get�
input_charsetr'r%�BASE64�header_encoding�
body_encodingr+r3�input_codec�output_codec)�selfrA�henc�benc�convr,r,r-�__init__�s0
102  
103  �
104  �
105  ��
106  �zCharset.__init__cCs
107  |j��S�N)rAr?�rGr,r,r-�__repr__�s
108  zCharset.__repr__cCst|�t|���kSrL)r<r?)rG�otherr,r,r-�__eq__�szCharset.__eq__cCs.|jtksJ�|jtkrdS|jtkrdStS)aPReturn the content-transfer-encoding used for body encoding.
109  
110          This is either the string `quoted-printable' or `base64' depending on
111          the encoding used, or it is a function in which case you should call
112          the function with a single argument, the Message object being
113          encoded.  The function should then set the Content-Transfer-Encoding
114          header itself to whatever is appropriate.
115  
116          Returns "quoted-printable" if self.body_encoding is QP.
117          Returns "base64" if self.body_encoding is BASE64.
118          Returns conversion function otherwise.
119          zquoted-printable�base64)rDr%�QPrBrrMr,r,r-�get_body_encoding�s
120  
121  zCharset.get_body_encodingcCs|jp|jS)z�Return the output character set.
122  
123          This is self.output_charset if that is not None, otherwise it is
124          self.input_charset.
125          )r+rArMr,r,r-�get_output_charsetr2zCharset.get_output_charsetcCs6|jpd}t||�}|�|�}|dur|S|�||�S)a�Header-encode a string by converting it first to bytes.
126  
127          The type of encoding (base64 or quoted-printable) will be based on
128          this charset's `header_encoding`.
129  
130          :param string: A unicode string for the header.  It must be possible
131              to encode this string to bytes using the character set's
132              output codec.
133          :return: The encoded string, with RFC 2047 chrome.
134          r
N)rFr:�_get_encoder�
header_encode)rGr8r9�header_bytes�encoder_moduler,r,r-rVs
135  
136  
137  zCharset.header_encodecCs|jpd}t||�}|�|�}t|j|d�}|��}t|�t}g}	g}
138  t|�|}|D]F}|
139  �	|�t
140  �|
141  �}
|�t|
|��}||krr|
142  �
�|	sR|
143  sR|	�	d�n|	rVdnd}t
144  �|
145  �}t||�}|	�	||��|g}
146  t|�|}q,t
147  �|
148  �}t||�}|	�	||��|	S)afHeader-encode a string by converting it first to bytes.
149  
150          This is similar to `header_encode()` except that the string is fit
151          into maximum line lengths as given by the argument.
152  
153          :param string: A unicode string for the header.  It must be possible
154              to encode this string to bytes using the character set's
155              output codec.
156          :param maxlengths: Maximum line length iterator.  Each element
157              returned from this iterator will provide the next maximum line
158              length.  This parameter is used as an argument to built-in next()
159              and should never be exhausted.  The maximum line lengths should
160              not count the RFC 2047 chrome.  These line lengths are only a
161              hint; the splitter does the best it can.
162          :return: Lines of encoded strings, each with RFC 2047 chrome.
163          r
)r(N� r)rFr:rUrrVrT�len�RFC2047_CHROME_LEN�next�append�EMPTYSTRING�join�
header_length�pop)rGr8�
164  maxlengthsr9rWrX�encoderr(�extra�lines�current_line�maxlen�	character�	this_line�length�	separator�joined_liner,r,r-�header_encode_lines*s8
165  
166  
167  
168  
169  
170  
171  �
172  
173  zCharset.header_encode_linescCsZ|jtkrtjS|jtkrtjS|jtkr+tj�|�}tj�|�}||kr(tjStjSdSrL)rCrB�email�
174  base64mimerR�
175  quoprimimer%r`)rGrW�len64�lenqpr,r,r-rUhs
176  
177  
178  zCharset._get_encodercCs�|s|S|jturt|t�r|�|j�}tj�|�S|jt	ur5t|t�r*|�|j�}|�
179  d�}tj�|�St|t�rC|�|j��
180  d�}|S)avBody-encode a string by converting it first to bytes.
181  
182          The type of encoding (base64 or quoted-printable) will be based on
183          self.body_encoding.  If body_encoding is None, we assume the
184          output charset is a 7bit encoding, so re-encoding the decoded
185          string using the ascii codec produces the correct string version
186          of the content.
187          �latin1r")rDrBr;r<r7r+rnro�body_encoderR�decoderp)rGr8r,r,r-rtws	
188  
189  
190  
191  
192  
193  zCharset.body_encodeN)�__name__�
194  __module__�__qualname__�__doc__�DEFAULT_CHARSETrKrNrPrSrTrVrmrUrtr,r,r,r-r�s+!>r)�__all__�	functoolsr�email.base64mimern�email.quoprimimer�email.encodersrrRrBr%r[rzr6r^r'r/r3rrrr:rr,r,r,r-�<module>s�
195  �
196  �
197  �
198  �
199  
200  �
201  �
202  
�
203  �
204  �
205  �
206  �
207  ��
208  �
209  �
210  �
211  ����������	�
212  ���
�������
213