def compile_email_spt(fpath): r = {} with open(fpath) as f: pages = list(split_and_escape(f.read())) for i, page in enumerate(pages, 1): tmpl = b'\n' * page.offset + page.content content_type, renderer = parse_specline(page.header) key = 'subject' if i == 1 else content_type env = jinja_env_html if content_type == 'text/html' else jinja_env r[key] = SimplateLoader(fpath, tmpl).load(env, fpath) return r
def extract_spt(fileobj, *args, **kw): pages = list(split_and_escape(fileobj.read())) npages = len(pages) for i, page in enumerate(pages, 1): f = BytesIO(b'\n' * page.offset + page.content) content_type, renderer = parse_specline(page.header) extractor = None if (i == npages and not page.header) or content_type in ('text/html', 'text/plain'): extractor = jinja2.ext.babel_extract elif i < 3: extractor = extract_python if extractor: for match in extractor(f, *args, **kw): yield match
def extract_spt(fileobj, *args, **kw): pages = list(split_and_escape(fileobj.read().decode('utf8'))) npages = len(pages) for i, page in enumerate(pages, 1): f = BytesIO(b'\n' * page.offset + page.content.encode('utf8')) content_type, renderer = parse_specline(page.header) extractor = None if (i == npages and not page.header) or content_type in ('text/html', 'text/plain'): extractor = jinja2.ext.babel_extract elif i < 3: extractor = extract_python if extractor: for match in extractor(f, *args, **kw): yield match
def extract_spt(fileobj, *args, **kw): pages = list(split_and_escape(fileobj.read().decode('utf8'))) npages = len(pages) for i, page in enumerate(pages, 1): f = BytesIO(b'\n' * page.offset + page.content.encode('utf8')) content_type, renderer = parse_specline(page.header) extractor = None if i < 3 and i < npages and not page.header: extractor = extract_python else: extractor = jinja2.ext.babel_extract if extractor: for match in extractor(f, *args, **kw): yield match
def extract_spt(fileobj, *args, **kw): pages = list(split_and_escape(fileobj.read().decode('utf8'))) npages = len(pages) for i, page in enumerate(pages, 1): f = BytesIO(b'\n' * page.offset + page.content.encode('utf8')) content_type, renderer = parse_specline(page.header) extractor = None python_page = i < 3 and i < npages and not page.header json_page = renderer == 'json_dump' if python_page or json_page: extractor = extract_python_custom else: extractor = extract_jinja2_custom if extractor: for match in extractor(f, *args, **kw): yield match
def compile_email_spt(fpath): """Compile an email simplate. Args: fpath (str): filesystem path of the simplate to compile Returns: dict: the compiled pages of the simplate, keyed by content type (the first page gets the special key `subject`) """ r = {} with open(fpath, 'rb') as f: pages = list(split_and_escape(f.read().decode('utf8'))) for i, page in enumerate(pages, 1): tmpl = '\n' * page.offset + page.content content_type, renderer = parse_specline(page.header) key = 'subject' if i == 1 else content_type env = jinja_env_html if content_type == 'text/html' else jinja_env r[key] = SimplateLoader(fpath, tmpl).load(env, fpath) return r
def check_specline(header, media_type, renderer): assert pagination.parse_specline(header) == (media_type, renderer)