Best Python code snippet using molecule_python
email.py
Source:email.py  
1from config import misago2from utils import (3    get_bool_display,4    input_bool,5    input_choice,6    print_setup_changed_message,7    serialize_bool,8)9PROVIDER_CONSOLE = "console"10PROVIDER_GMAIL = "gmail"11PROVIDER_MAILJET = "mailjet"12PROVIDER_SENDINBLUE = "sendinblue"13PROVIDER_SMTP = "smtp"14def run_email_wizard(env_file):15    email_provider_prompt = [16        "Which e-mail provider do you want to use?",17        "",18        "0 - None (disables sending e-mails)",19        "1 - SMTP",20        "2 - Gmail (gmail.com)",21        "3 - Mailjet (mailjet.com)",22        "4 - SendinBlue (sendinblue.com)",23        "",24        "Enter choice's number",25    ]26    email_provider = input_choice(27        "\n".join(email_provider_prompt), "012345", coerce_to=int28    )29    choices_values = {30        0: disable_sending_emails,31        1: run_smtp_wizard,32        2: run_gmail_wizard,33        3: run_mailjet_wizard,34        4: run_sendinblue_wizard,35    }36    email_provider_wizard = choices_values[email_provider]37    email_provider_wizard(env_file)38def disable_sending_emails(env_file):39    env_file["MISAGO_EMAIL_PROVIDER"] = PROVIDER_CONSOLE40def run_smtp_wizard(env_file):41    email_host_prompt = 'Enter your SMTP host (eg. "smtp.myprovider.com"): '42    email_host = None43    while not email_host:44        email_host = input(email_host_prompt).strip()45        if not email_host:46            email_host = None47            print("You have to enter a SMTP host.")48            print()49    email_user_prompt = "Enter your SMTP user name: "50    email_user = None51    while not email_user:52        email_user = input(email_user_prompt).strip()53        if not email_user:54            email_user = None55            print("You have to enter a SMTP user name.")56            print()57    email_password_prompt = "Enter your SMTP user password (optinal but recommended): "58    email_password = input(email_password_prompt).strip()59    email_ssl = input_bool("Enable SSL", default=False)60    email_tls = input_bool("Enable TLS", default=False)61    email_port_prompt = "Enter your SMTP port: "62    email_port = None63    while not email_port:64        email_port = input(email_port_prompt).strip()65        if not email_port:66            email_port = None67            print("You have to enter a SMTP port.")68            print()69    email_address_prompt = "Enter your site's e-mail address: "70    email_address = input(email_address_prompt).strip()71    while not email_address:72        email_address = input(email_address_prompt).strip()73        if not email_address:74            email_address = None75            print("You have to enter e-mail address.")76            print()77    email_from_prompt = 'Enter your sender name (eg. "Misago Forums", optional): '78    email_from = input(email_from_prompt).strip()79    env_file["MISAGO_EMAIL_PROVIDER"] = PROVIDER_SMTP80    env_file["MISAGO_EMAIL_HOST"] = email_host81    env_file["MISAGO_EMAIL_PORT"] = email_port82    env_file["MISAGO_EMAIL_USER"] = email_user83    env_file["MISAGO_EMAIL_PASSWORD"] = email_password84    env_file["MISAGO_EMAIL_USE_SSL"] = serialize_bool(email_ssl)85    env_file["MISAGO_EMAIL_USE_TLS"] = serialize_bool(email_tls)86    if email_from:87        env_file["MISAGO_DEFAULT_FROM_EMAIL"] = "%s <%s>" % (email_from, email_address)88    else:89        env_file["MISAGO_DEFAULT_FROM_EMAIL"] = email_address90def run_gmail_wizard(env_file):91    email_address_prompt = "Enter your Gmail e-mail address: "92    email_address = None93    while not email_address:94        email_address = input(email_address_prompt).strip()95        if not email_address:96            email_address = None97            print("You have to enter an e-mail address.")98            print()99    print()100    print(101        "Gmail requires that each application connecting to its servers "102        "uses dedicated password."103    )104    print(105        "Your gmail.com or google account password WILL NOT WORK. "106        "Use one of following options to generate dedicated password for "107        "your Misago site to use when sending e-mails:"108    )109    print()110    print(111        "If you have 2-Step Verification enabled:         "112        "https://security.google.com/settings/security/apppasswords"113    )114    print(115        "If you DON'T have 2-Step Verification enabled:   "116        "https://myaccount.google.com/lesssecureapps"117    )118    print()119    password_prompt = "Enter your Gmail application password: "120    password = None121    while not password:122        password = input(password_prompt).strip()123        if not password:124            password = None125            print("You have to enter a password.")126            print()127    email_from_prompt = 'Enter your sender name (eg. "Misago Forums", optional): '128    email_from = input(email_from_prompt).strip()129    env_file["MISAGO_EMAIL_PROVIDER"] = PROVIDER_GMAIL130    env_file["MISAGO_GMAIL_USER"] = email_address131    env_file["MISAGO_GMAIL_PASSWORD"] = password132    if email_from:133        env_file["MISAGO_DEFAULT_FROM_EMAIL"] = "%s <%s>" % (email_from, email_address)134    else:135        env_file["MISAGO_DEFAULT_FROM_EMAIL"] = email_address136def run_mailjet_wizard(env_file):137    api_key_prompt = "Enter your Mailjet API key: "138    api_key = None139    while not api_key:140        api_key = input(api_key_prompt).strip()141        if not api_key:142            api_key = None143            print("You have to enter an API key.")144            print()145    api_secret_prompt = "Enter your Mailjet secret key: "146    api_secret = None147    while not api_secret:148        api_secret = input(api_secret_prompt).strip()149        if not api_secret:150            api_secret = None151            print("You have to enter an secret key.")152            print()153    env_file["MISAGO_EMAIL_PROVIDER"] = PROVIDER_MAILJET154    env_file["MISAGO_MAILJET_API_KEY_PUBLIC"] = api_key155    env_file["MISAGO_MAILJET_API_KEY_PRIVATE"] = api_secret156    env_file["MISAGO_DEFAULT_FROM_EMAIL"] = ""157def run_sendinblue_wizard(env_file):158    api_key_prompt = "Enter your SendinBlue API key: "159    api_key = None160    while not api_key:161        api_key = input(api_key_prompt).strip()162        if not api_key:163            api_key = None164            print("You have to enter an API key.")165            print()166    env_file["MISAGO_EMAIL_PROVIDER"] = PROVIDER_SENDINBLUE167    env_file["MISAGO_SENDINBLUE_API_KEY"] = api_key168    env_file["MISAGO_DEFAULT_FROM_EMAIL"] = ""169def print_email_setup(env_file):170    if env_file.get("MISAGO_EMAIL_PROVIDER") == PROVIDER_CONSOLE:171        print("Sending e-mails is currently disabled.")172    if env_file.get("MISAGO_EMAIL_PROVIDER") == PROVIDER_SMTP:173        print_smtp_setup(env_file)174    if env_file.get("MISAGO_EMAIL_PROVIDER") == PROVIDER_GMAIL:175        print_gmail_setup(env_file)176    if env_file.get("MISAGO_EMAIL_PROVIDER") == PROVIDER_MAILJET:177        print_mailjet_setup(env_file)178    if env_file.get("MISAGO_EMAIL_PROVIDER") == PROVIDER_SENDINBLUE:179        print_sendinblue_setup(env_file)180def print_smtp_setup(env_file):181    print("Using custom SMTP configuration to send e-mails:")182    print()183    print("From:        %s" % env_file.get("MISAGO_DEFAULT_FROM_EMAIL"))184    print("User:        %s" % env_file.get("MISAGO_EMAIL_USER", ""))185    print("Password:    %s" % env_file.get("MISAGO_EMAIL_PASSWORD", ""))186    print("Host:        %s" % env_file.get("MISAGO_EMAIL_HOST", ""))187    print("Port:        %s" % env_file.get("MISAGO_EMAIL_PORT", ""))188    print("SSL:         %s" % get_bool_display(env_file.get("MISAGO_EMAIL_USE_SSL")))189    print("TLS:         %s" % get_bool_display(env_file.get("MISAGO_EMAIL_USE_TLS")))190def print_gmail_setup(env_file):191    print("Using Gmail to send e-mails:")192    print()193    print("From:        %s" % env_file.get("MISAGO_DEFAULT_FROM_EMAIL"))194    print("User:        %s" % env_file.get("MISAGO_GMAIL_USER", ""))195    print("Password:    %s" % env_file.get("MISAGO_GMAIL_PASSWORD", ""))196def print_mailjet_setup(env_file):197    print("Using Mailjet to send e-mails:")198    print()199    print("API key:     %s" % env_file.get("MISAGO_MAILJET_API_KEY_PUBLIC"))200    print("Secret key:  %s" % env_file.get("MISAGO_MAILJET_API_KEY_PRIVATE"))201def print_sendinblue_setup(env_file):202    print("Using SendinBlue to send e-mails:")203    print()204    print("API key:     %s" % env_file.get("MISAGO_SENDINBLUE_API_KEY"))205def change_email_setup(env_file):206    print_email_setup(misago)207    print()208    if input_bool("Change e-mail configuration?", default=False):209        run_email_wizard(env_file)210        env_file.save()211        print_setup_changed_message()212if __name__ == "__main__":213    if misago.is_file():214        try:215            change_email_setup(misago)216        except KeyboardInterrupt:...encryption.py
Source:encryption.py  
1import base642from collections import OrderedDict3import boto34from .yaml_utils import UnencryptedTag, EncryptedTag5## encrypt6def encrypt_value(client, config, value):7    config['Plaintext'] = str(value.value).encode('utf-8')8    response = client.encrypt(**config)9    config['Plaintext'] = None10    return EncryptedTag(base64.b64encode(response['CiphertextBlob']).decode('utf-8'))11def encrypt_file(env_file):12    client = boto3.client('kms')13    for env in env_file['environments']:14        config = {15            'KeyId': env_file['environments'][env]['kms_key'],16            'EncryptionContext': {17                'name': env_file['name'],18                'env': env19            }20        }21        for variable in env_file['environments'][env]['variables']:22            value = env_file['environments'][env]['variables'][variable]23            if isinstance(value, UnencryptedTag):24                env_file['environments'][env]['variables'][variable] = encrypt_value(client, config, value)25## decrypt26def decrypt_value(client, config, value):27    config['CiphertextBlob'] = base64.b64decode(value.value)28    response = client.decrypt(**config)29    config['CiphertextBlob'] = None30    return UnencryptedTag(response['Plaintext'].decode('utf-8'))31def decrypt_env(client, env_file, env):32    config = {33        'EncryptionContext': {34            'name': env_file['name'],35            'env': env36        }37    }38    for variable in env_file['environments'][env]['variables']:39        value = env_file['environments'][env]['variables'][variable]40        if isinstance(value, EncryptedTag):41            env_file['environments'][env]['variables'][variable] = decrypt_value(client, config, value)42def decrypt_file(env_file, env=None):43    client = boto3.client('kms')44    if env != None:45        decrypt_env(client, env_file, env)46    else:47        for env in env_file['environments']:48            decrypt_env(client, env_file, env)...postgres.py
Source:postgres.py  
1from utils import get_random_string2def run_postgres_wizard(env_file):3    if env_file.is_file():4        update_postgres_env_file(env_file)5    else:6        generate_default_postgres_env_file(env_file)7def generate_default_postgres_env_file(env_file):8    env_file["POSTGRES_USER"] = "misago_%s" % get_random_string(16)9    env_file["POSTGRES_PASSWORD"] = "%s" % get_random_string(80)10    env_file.save()11    print("PostgreSQL configuration has been saved to %s" % env_file.path)12def update_postgres_env_file(env_file):13    changes = []14    if not env_file.get("POSTGRES_USER"):15        env_file["POSTGRES_USER"] = "misago_%s" % get_random_string(16)16        changes.append("User:       %s" % env_file["POSTGRES_USER"])17    if not env_file.get("POSTGRES_PASSWORD"):18        env_file["POSTGRES_PASSWORD"] = "%s" % get_random_string(80)19        changes.append("Password:   %s" % env_file["POSTGRES_PASSWORD"])20    if changes:21        env_file.save()22        print(23            "PostgreSQL configuration file has been updated with following database settings:"24        )25        print()26        print("\n".join(changes))...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
