python: Normalize quotes with Black.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:20:45 -08:00
committed by Tim Abbott
parent 11741543da
commit 6e4c3e41dc
989 changed files with 32792 additions and 32792 deletions

View File

@@ -10,20 +10,20 @@ from scripts.lib.setup_path import setup_path
setup_path()
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
import argparse
import configparser
import uuid
os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
os.chdir(os.path.join(os.path.dirname(__file__), "..", ".."))
# Standard, 64-bit tokens
AUTOGENERATED_SETTINGS = [
'avatar_salt',
'rabbitmq_password',
'shared_secret',
'thumbor_key',
"avatar_salt",
"rabbitmq_password",
"shared_secret",
"thumbor_key",
]
@@ -56,7 +56,7 @@ def generate_django_secretkey() -> str:
# This helps optimize noop provision performance.
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
return get_random_string(50, chars)
@@ -79,7 +79,7 @@ def generate_secrets(development: bool = False) -> None:
lines: List[str] = []
if len(current_conf) == 0:
lines = ['[secrets]\n']
lines = ["[secrets]\n"]
def need_secret(name: str) -> bool:
return name not in current_conf
@@ -104,17 +104,17 @@ def generate_secrets(development: bool = False) -> None:
# The core Django SECRET_KEY setting, used by Django internally to
# secure sessions. If this gets changed, all users will be logged out.
if need_secret('secret_key'):
if need_secret("secret_key"):
secret_key = generate_django_secretkey()
add_secret('secret_key', secret_key)
add_secret("secret_key", secret_key)
# To prevent Django ImproperlyConfigured error
from zproject import settings
settings.SECRET_KEY = secret_key
# Secret key for the Camo HTTPS proxy.
if need_secret('camo_key'):
add_secret('camo_key', random_string(64))
if need_secret("camo_key"):
add_secret("camo_key", random_string(64))
if not development:
# The memcached_password and redis_password secrets are only
@@ -169,16 +169,16 @@ def generate_secrets(development: bool = False) -> None:
# accessing the Zulip mobile push notifications service.
# * zulip_org_key is generated using os.urandom().
# * zulip_org_id only needs to be unique, so we use a UUID.
if need_secret('zulip_org_key'):
add_secret('zulip_org_key', random_string(64))
if need_secret('zulip_org_id'):
add_secret('zulip_org_id', str(uuid.uuid4()))
if need_secret("zulip_org_key"):
add_secret("zulip_org_key", random_string(64))
if need_secret("zulip_org_id"):
add_secret("zulip_org_id", str(uuid.uuid4()))
if len(lines) == 0:
print("generate_secrets: No new secrets to generate.")
return
with open(OUTPUT_SETTINGS_FILENAME, 'a') as f:
with open(OUTPUT_SETTINGS_FILENAME, "a") as f:
# Write a newline at the start, in case there was no newline at
# the end of the file due to human editing.
f.write("\n" + "".join(lines))
@@ -186,18 +186,18 @@ def generate_secrets(development: bool = False) -> None:
print(f"Generated new secrets in {OUTPUT_SETTINGS_FILENAME}.")
if __name__ == '__main__':
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--development', action='store_true', help='For setting up the developer env for zulip'
"--development", action="store_true", help="For setting up the developer env for zulip"
)
group.add_argument(
'--production',
action='store_false',
dest='development',
help='For setting up the production env for zulip',
"--production",
action="store_false",
dest="development",
help="For setting up the production env for zulip",
)
results = parser.parse_args()

View File

@@ -6,25 +6,25 @@ from cssutils import profile
from cssutils.profiles import Profiles, macros, properties
from premailer import Premailer
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../')
EMAIL_TEMPLATES_PATH = os.path.join(ZULIP_PATH, 'templates', 'zerver', 'emails')
COMPILED_EMAIL_TEMPLATES_PATH = os.path.join(EMAIL_TEMPLATES_PATH, 'compiled')
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
EMAIL_TEMPLATES_PATH = os.path.join(ZULIP_PATH, "templates", "zerver", "emails")
COMPILED_EMAIL_TEMPLATES_PATH = os.path.join(EMAIL_TEMPLATES_PATH, "compiled")
CSS_SOURCE_PATH = os.path.join(EMAIL_TEMPLATES_PATH, "email.css")
def configure_cssutils() -> None:
# These properties are not supported by cssutils by default and will
# result in warnings when premailer package is run.
properties[Profiles.CSS_LEVEL_2]['-ms-interpolation-mode'] = r'none|bicubic|nearest-neighbor'
properties[Profiles.CSS_LEVEL_2]['-ms-text-size-adjust'] = r'none|auto|{percentage}'
properties[Profiles.CSS_LEVEL_2]['mso-table-lspace'] = r'0|{num}(pt)'
properties[Profiles.CSS_LEVEL_2]['mso-table-rspace'] = r'0|{num}(pt)'
properties[Profiles.CSS_LEVEL_2]['-webkit-text-size-adjust'] = r'none|auto|{percentage}'
properties[Profiles.CSS_LEVEL_2]['mso-hide'] = 'all'
properties[Profiles.CSS_LEVEL_2]['pointer-events'] = (
r'auto|none|visiblePainted|'
r'visibleFill|visibleStroke|'
r'visible|painted|fill|stroke|all|inherit'
properties[Profiles.CSS_LEVEL_2]["-ms-interpolation-mode"] = r"none|bicubic|nearest-neighbor"
properties[Profiles.CSS_LEVEL_2]["-ms-text-size-adjust"] = r"none|auto|{percentage}"
properties[Profiles.CSS_LEVEL_2]["mso-table-lspace"] = r"0|{num}(pt)"
properties[Profiles.CSS_LEVEL_2]["mso-table-rspace"] = r"0|{num}(pt)"
properties[Profiles.CSS_LEVEL_2]["-webkit-text-size-adjust"] = r"none|auto|{percentage}"
properties[Profiles.CSS_LEVEL_2]["mso-hide"] = "all"
properties[Profiles.CSS_LEVEL_2]["pointer-events"] = (
r"auto|none|visiblePainted|"
r"visibleFill|visibleStroke|"
r"visible|painted|fill|stroke|all|inherit"
)
profile.addProfiles(
@@ -36,7 +36,7 @@ configure_cssutils()
def inline_template(template_source_name: str) -> None:
template_name = template_source_name.split('.source.html')[0]
template_name = template_source_name.split(".source.html")[0]
template_path = os.path.join(EMAIL_TEMPLATES_PATH, template_source_name)
compiled_template_path = os.path.join(
os.path.dirname(template_path), "compiled", os.path.basename(template_name) + ".html"
@@ -57,32 +57,32 @@ def inline_template(template_source_name: str) -> None:
# template, since we'll end up with 2 copipes of those tags.
# Thus, we strip this stuff out if the template extends
# another template.
if template_name not in ['email_base_default', 'macros']:
if template_name not in ["email_base_default", "macros"]:
output = strip_unnecesary_tags(output)
if (
'zerver/emails/compiled/email_base_default.html' in output
or 'zerver/emails/email_base_messages.html' in output
"zerver/emails/compiled/email_base_default.html" in output
or "zerver/emails/email_base_messages.html" in output
):
assert output.count('<html>') == 0
assert output.count('<body>') == 0
assert output.count('</html>') == 0
assert output.count('</body>') == 0
assert output.count("<html>") == 0
assert output.count("<body>") == 0
assert output.count("</html>") == 0
assert output.count("</body>") == 0
with open(compiled_template_path, 'w') as final_template_file:
with open(compiled_template_path, "w") as final_template_file:
final_template_file.write(output)
def escape_jinja2_characters(text: str) -> str:
escaped_jinja2_characters = [('%7B%7B%20', '{{ '), ('%20%7D%7D', ' }}'), ('&gt;', '>')]
escaped_jinja2_characters = [("%7B%7B%20", "{{ "), ("%20%7D%7D", " }}"), ("&gt;", ">")]
for escaped, original in escaped_jinja2_characters:
text = text.replace(escaped, original)
return text
def strip_unnecesary_tags(text: str) -> str:
end_block = '</body>\n</html>'
start_block = '{% extends'
end_block = "</body>\n</html>"
start_block = "{% extends"
start = text.find(start_block)
end = text.rfind(end_block)
if start != -1 and end != -1:
@@ -95,7 +95,7 @@ def strip_unnecesary_tags(text: str) -> str:
def get_all_templates_from_directory(directory: str) -> Set[str]:
result = set()
for f in os.listdir(directory):
if f.endswith('.source.html'):
if f.endswith(".source.html"):
result.add(f)
return result