Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import textwrap
  2.  
  3. import smtplib
  4. from email.mime.text import MIMEText
  5.  
  6. class NameThingy:
  7.     def __init__(self, name):
  8.         self.name = name
  9.     def __getattribute__(self, attrname):
  10.         name = object.__getattribute__(self, 'name')
  11.         return NameThingy(f'{name}.{attrname}')
  12.     def __matmul__(self, other):
  13.         name = object.__getattribute__(self, 'name')
  14.         othername = object.__getattribute__(other, 'name')
  15.         return NameThingy(f'{name}@{othername}')
  16.  
  17. class EmailMetaNamespace(dict):
  18.     def __missing__(self, index):
  19.         if isinstance(index, str) and not index.startswith('_'):
  20.             return NameThingy(index)
  21.         raise KeyError(index)
  22.  
  23. class EmailMeta(type):
  24.     @classmethod
  25.     def __prepare__(cls, name, bases, **kwargs):
  26.         return EmailMetaNamespace()
  27.     def __new__(cls, name, bases, namespace, **kwargs):
  28.         annotations = namespace['__annotations__']
  29.         subject = annotations['Subject']
  30.         from_ = object.__getattribute__(annotations['From'], 'name')
  31.         to = object.__getattribute__(annotations['To'], 'name')
  32.         body = textwrap.dedent(annotations['Body'])
  33.        
  34.         msg = MIMEText(body)
  35.         msg['Subject'] = subject
  36.         msg['From'] = from_
  37.         msg['To'] = to
  38.        
  39.         print('Pretending to send the following email:')
  40.         print(msg.as_string())
  41.  
  42. Email = type.__new__(EmailMeta, 'Email', (), {})
  43.  
  44. class Message(Email):
  45.     From:    me@gmail.com
  46.     To:      you@gmail.com
  47.     Subject: "Let's get nachos"
  48.     Body: '''\
  49.        Man, wouldn't nachos be nice?
  50.        Let's go get some.'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement