#!/usr/bin/env python3 #~ import falcon #~ from models.main import get_cp #~ class AppPostalCode(object): #~ def on_get(self, req, resp): #~ values = req.params #~ req.context['result'] = get_cp(values['cp']) #~ resp.status = falcon.HTTP_200 #~ https://github.com/kennethreitz/requests/blob/v1.2.3/requests/structures.py#L37 import re import smtplib import collections from collections import OrderedDict from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email import encoders from email.utils import formatdate class CaseInsensitiveDict(collections.MutableMapping): """A case-insensitive ``dict``-like object. Implements all methods and operations of ``collections.MutableMapping`` as well as dict's ``copy``. Also provides ``lower_items``. All keys are expected to be strings. The structure remembers the case of the last key to be set, and ``iter(instance)``, ``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()`` will contain case-sensitive keys. However, querying and contains testing is case insensitive:: cid = CaseInsensitiveDict() cid['Accept'] = 'application/json' cid['aCCEPT'] == 'application/json' # True list(cid) == ['Accept'] # True For example, ``headers['content-encoding']`` will return the value of a ``'Content-Encoding'`` response header, regardless of how the header name was originally stored. If the constructor, ``.update``, or equality comparison operations are given keys that have equal ``.lower()``s, the behavior is undefined. """ def __init__(self, data=None, **kwargs): self._store = OrderedDict() if data is None: data = {} self.update(data, **kwargs) def __setitem__(self, key, value): # Use the lowercased key for lookups, but store the actual # key alongside the value. self._store[key.lower()] = (key, value) def __getitem__(self, key): return self._store[key.lower()][1] def __delitem__(self, key): del self._store[key.lower()] def __iter__(self): return (casedkey for casedkey, mappedvalue in self._store.values()) def __len__(self): return len(self._store) def lower_items(self): """Like iteritems(), but with all lowercase keys.""" return ( (lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items() ) def __eq__(self, other): if isinstance(other, collections.Mapping): other = CaseInsensitiveDict(other) else: return NotImplemented # Compare insensitively return dict(self.lower_items()) == dict(other.lower_items()) # Copy is required def copy(self): return CaseInsensitiveDict(self._store.values()) def __repr__(self): return str(dict(self.items())) class NumLet(object): def __init__(self, value, moneda, **args): self._letras = self._letters(value, moneda) @property def letras(self): return self._letras.upper() #~ def _letters(self, numero, moneda='peso', texto_inicial='-(', #~ texto_final='/100 m.n.)-', fraccion_letras=False, fraccion=''): def _letters(self, numero, moneda='peso'): texto_inicial = '' texto_final = '/100 m.n.)-' fraccion_letras = False fraccion = '' enletras = texto_inicial numero = abs(numero) numtmp = '%015d' % numero if numero < 1: enletras += 'cero ' + self._plural(moneda) + ' ' else: enletras += self._numlet(numero) if numero == 1 or numero < 2: enletras += moneda + ' ' elif int(''.join(numtmp[3:])) == 0 or int(''.join(numtmp[9:])) == 0: enletras += 'de ' + self._plural(moneda) + ' ' else: enletras += self._plural(moneda) + ' ' decimal = '%0.2f' % numero decimal = decimal.split('.')[1] #~ decimal = int((numero-int(numero))*100) if fraccion_letras: if decimal == 0: enletras += 'con cero ' + self._plural(fraccion) elif decimal == 1: enletras += 'con un ' + fraccion else: enletras += 'con ' + self._numlet(int(decimal)) + self.plural(fraccion) else: enletras += decimal enletras += texto_final return enletras def _numlet(self, numero): numtmp = '%015d' % numero co1=0 letras = '' leyenda = '' for co1 in range(0,5): inicio = co1*3 cen = int(numtmp[inicio:inicio+1][0]) dec = int(numtmp[inicio+1:inicio+2][0]) uni = int(numtmp[inicio+2:inicio+3][0]) letra3 = self.centena(uni, dec, cen) letra2 = self.decena(uni, dec) letra1 = self.unidad(uni, dec) if co1 == 0: if (cen+dec+uni) == 1: leyenda = 'billon ' elif (cen+dec+uni) > 1: leyenda = 'billones ' elif co1 == 1: if (cen+dec+uni) >= 1 and int(''.join(numtmp[6:9])) == 0: leyenda = "mil millones " elif (cen+dec+uni) >= 1: leyenda = "mil " elif co1 == 2: if (cen+dec) == 0 and uni == 1: leyenda = 'millon ' elif cen > 0 or dec > 0 or uni > 1: leyenda = 'millones ' elif co1 == 3: if (cen+dec+uni) >= 1: leyenda = 'mil ' elif co1 == 4: if (cen+dec+uni) >= 1: leyenda = '' letras += letra3 + letra2 + letra1 + leyenda letra1 = '' letra2 = '' letra3 = '' leyenda = '' return letras def centena(self, uni, dec, cen): letras = '' numeros = ["","","doscientos ","trescientos ","cuatrocientos ","quinientos ","seiscientos ","setecientos ","ochocientos ","novecientos "] if cen == 1: if (dec+uni) == 0: letras = 'cien ' else: letras = 'ciento ' elif cen >= 2 and cen <= 9: letras = numeros[cen] return letras def decena(self, uni, dec): letras = '' numeros = ["diez ","once ","doce ","trece ","catorce ","quince ","dieci","dieci","dieci","dieci"] decenas = ["","","","treinta ","cuarenta ","cincuenta ","sesenta ","setenta ","ochenta ","noventa "] if dec == 1: letras = numeros[uni] elif dec == 2: if uni == 0: letras = 'veinte ' elif uni > 0: letras = 'veinti' elif dec >= 3 and dec <= 9: letras = decenas[dec] if uni > 0 and dec > 2: letras = letras+'y ' return letras def unidad(self, uni, dec): letras = '' numeros = ["","un ","dos ","tres ","cuatro ","cinco ","seis ","siete ","ocho ","nueve "] if dec != 1: if uni > 0 and uni <= 5: letras = numeros[uni] if uni >= 6 and uni <= 9: letras = numeros[uni] return letras def _plural(self, palabra): if re.search('[aeiou]$', palabra): return re.sub('$', 's', palabra) else: return palabra + 'es' class SendMail(object): def __init__(self, config): self._config = config self._server = None self._error = '' self._is_connect = self._login() @property def is_connect(self): return self._is_connect @property def error(self): return self._error def _login(self): try: if self._config['ssl']: self._server = smtplib.SMTP_SSL( self._config['servidor'], self._config['puerto'], timeout=10) else: self._server = smtplib.SMTP( self._config['servidor'], self._config['puerto'], timeout=10) self._server.login(self._config['usuario'], self._config['contra']) return True except smtplib.SMTPAuthenticationError as e: if '535' in str(e): self._error = 'Nombre de usuario o contraseña inválidos' return False if '534' in str(e) and 'gmail' in self._config['servidor']: self._error = 'Necesitas activar el acceso a otras ' \ 'aplicaciones en tu cuenta de GMail' return False except smtplib.SMTPException as e: self._error = str(e) return False except Exception as e: self._error = str(e) return False return def send(self, options): try: message = MIMEMultipart() message['From'] = self._config['usuario'] message['To'] = options['para'] message['CC'] = options['copia'] message['Subject'] = options['asunto'] message['Date'] = formatdate(localtime=True) message.attach(MIMEText(options['mensaje'], 'html')) for f in options['files']: part = MIMEBase('application', 'octet-stream') part.set_payload(f[0]) encoders.encode_base64(part) part.add_header( 'Content-Disposition', "attachment; filename={}".format(f[1])) message.attach(part) receivers = options['para'].split(',') + options['copia'].split(',') self._server.sendmail( self._config['usuario'], receivers, message.as_string()) return '' except Exception as e: return str(e) def close(self): try: self._server.quit() except: pass return