Best Python code snippet using fMBT_python
tasks.py
Source:tasks.py  
1"""2Tasks for managing bnbalsamo.github.io3"""4import os5from datetime import datetime6from pathlib import Path7from uuid import uuid48from invoke import Collection, task9from werkzeug.utils import secure_filename10# Change the CWD to the repo root.11_LAST_DIR = None12while not Path("./tasks.py").exists():13    os.chdir("..")14    _CURRENT_DIR = Path(".").resolve()15    if _CURRENT_DIR == _LAST_DIR:16        # We hit the FS root :(17        raise FileNotFoundError("Could not find the repository root.")18# Relevant directories, relative to the repo root.19DIRECTORIES = {20    "drafts": Path("./_drafts"),21    "posts": Path("./_posts"),22    "presentations": Path("./_presentations"),23    "speaker_notes": Path("./_speaker_notes"),24}25# Used in some helper prints - never actually called.26EDITOR = os.environ.get("EDITOR") or "nano"27speaker_notes_header = """---28layout: page29---30"""31def parse_for_notes(p):32    note_lines = []33    with open(p) as f:34        capturing = False35        for line in f.readlines():36            if line == "???\n":37                capturing = True38                continue39            if line == "---\n":40                # Ignore the header41                if capturing:42                    note_lines.append(line)43                capturing = False44                continue45            if capturing:46                note_lines.append(line)47    notes_markdown = "".join(note_lines)48    return notes_markdown49def title_to_filename(title):50    """51    Convert a post or presentation title to a file name.52    """53    filename = title.lower()54    filename = secure_filename(filename)55    return filename56@task(name="post")57def new_post(c, title):58    """59    Create a new post.60    """61    print("Creating draft post")62    print(f"Title: {title}")63    filepath = DIRECTORIES["drafts"] / (title_to_filename(title) + ".md")64    filepath = filepath.resolve()65    print(f"File Path: {filepath}")66    if filepath.exists():67        raise FileExistsError(filepath.resolve())68    with open(filepath, "w") as f:69        f.write(f"---\nlayout: post\ntitle: {title}\n---\n\n")70    print("Open your new post for editing with...")71    print(f"{EDITOR} {filepath}")72@task(name="presentation")73def new_presentation(c, title):74    """75    Create a new presentation.76    """77    print("!!! Presentations don't support drafts, this will be published !!!")78    print("Creating presentation")79    print(f"Title: {title}")80    filepath = DIRECTORIES["presentations"] / (title_to_filename(title) + ".html")81    filepath = filepath.resolve()82    print(f"File Path: {filepath}")83    if filepath.exists():84        raise FileExistsError(filepath)85    with open(filepath, "w") as f:86        f.write(87            "---\n"88            "layout: presentation\n"89            f"pres_title: {title}\n---\n\n"90            "class: center, middle\n"91            f"# {title}\n\n"92            "---\n\n"93        )94    print("Open your new presentation for editing with...")95    print(f"{EDITOR} {filepath}")96@task(name="post")97def publish_post(c, title, year=None, month=None, day=None):98    """99    Publish a post from drafts.100    """101    print(f"Publishing Post: {title}")102    draft_filepath = DIRECTORIES["drafts"] / (title_to_filename(title) + ".md")103    draft_filepath = draft_filepath.resolve()104    print(f"Draft File Path: {draft_filepath}")105    if not draft_filepath.exists():106        raise FileNotFoundError(draft_filepath)107    if year is None:108        # Zero padding here might be overkill, but it's consistent!109        year = f"{datetime.now().year:04}"110    if month is None:111        month = f"{datetime.now().month:02}"112    if day is None:113        day = f"{datetime.now().day:02}"114    post_filepath = DIRECTORIES["posts"] / (115        f"{year}-{month}-{day}-{title_to_filename(title)}" + ".md"116    )117    post_filepath = post_filepath.resolve()118    print(f"Post File Path: {post_filepath}")119    if post_filepath.exists():120        raise FileExistsError(post_filepath)121    draft_filepath.rename(post_filepath)122@task(name="site")123def export_site(c):124    """125    Build and export the site to the local host.126    """127    print("Building site...")128    c.run("sudo docker run --rm -v $(pwd):/srv/jekyll jekyll/jekyll jekyll build")129    print(f"Site available in `{Path('./_site').resolve()}`")130@task(name="dockerimage")131def export_docker_image(c, image_name="bnbalsamo.github.io:latest"):132    """133    Build and export the docker image to the local host.134    """135    c.run(f"sudo docker build . -t {image_name}")136@task(name="speakernotes")137def export_speaker_notes(c):138    """139    Build and export speaker notes to the local host.140    """141    for x in Path("./_presentations").iterdir():142        notes_markdown = parse_for_notes(x)143        with open(DIRECTORIES["speaker_notes"] / (x.name[0:-5] + ".md"), "w") as f:144            f.write(speaker_notes_header + notes_markdown)145@task(name="dockerimage")146def run_docker_image(c, image_name="bnbalsamo.github.io:latest", port=80, build=True):147    """148    Run the docker image locally.149    """150    uid = uuid4().hex151    if build:152        export_docker_image(c, image_name=image_name)153    print(f"Running container. Access site at localhost:{port}")154    print(f"After exiting the server run docker rm {uid} to clean up the container.")155    c.run(f"sudo docker run -p {port}:80 {image_name} --name {uid}")156@task(name="testsite")157def run_test_site(c, port=4000):158    """159    Run the test site locally.160    """161    uid = uuid4().hex162    print(f"Running test site on port {port}...")163    c.run(164        f"sudo docker run -d --name test_site_{uid} -p {port}:4000 "165        "-v $(pwd):/srv/jekyll jekyll/jekyll jekyll serve --watch -D"166    )167    print("Run the following to end the test server instance...")168    print(f"sudo docker stop test_site_{uid} && " f"sudo docker rm test_site_{uid}")169# Organize into our subcommands...170ns = Collection()  # Make implicit root explicit171# Add "new" subcommand172new_ns = Collection("new")173new_ns.add_task(new_post)174new_ns.add_task(new_presentation)175ns.add_collection(new_ns)176# Add "publish" subcommand177publish_ns = Collection("publish")178publish_ns.add_task(publish_post)179ns.add_collection(publish_ns)180# Add "export" subcommand181export_ns = Collection("export")182export_ns.add_task(export_site)183export_ns.add_task(export_docker_image)184export_ns.add_task(export_speaker_notes)185ns.add_collection(export_ns)186# Add "run" subcommand187run_ns = Collection("run")188run_ns.add_task(run_docker_image)189run_ns.add_task(run_test_site)...mediawiki_export_reading.py
Source:mediawiki_export_reading.py  
1import bz22from dataclasses import dataclass3from functools import cache4from typing import Any, Callable, Iterator, Optional, TextIO, Union5from lxml import etree  # type: ignore6from mediawiki_export_constants import (7    EXPORT_NS,8    FORMAT,9    MEDIAWIKI,10    MODEL,11    PAGE,12    TEXT,13    TITLE,14)15QFORMAT = etree.QName(EXPORT_NS, FORMAT)16QMEDIAWIKI = etree.QName(EXPORT_NS, MEDIAWIKI)17QMODEL = etree.QName(EXPORT_NS, MODEL)18QPAGE = etree.QName(EXPORT_NS, PAGE)19QTEXT = etree.QName(EXPORT_NS, TEXT)20QTITLE = etree.QName(EXPORT_NS, TITLE)21@dataclass22class Page:23    title: Optional[str]24    model: Optional[str]25    format: Optional[str]26    text: Optional[str]27    def to_lxml_etree_element(self) -> etree.Element:28        page = etree.Element(QPAGE)29        title = etree.Element(QTITLE)30        title.text = self.title31        page.append(title)32        model = etree.Element(QMODEL)33        model.text = self.model34        page.append(model)35        format = etree.Element(QFORMAT)36        format.text = self.format37        page.append(format)38        text = etree.Element(QTEXT)39        text.text = self.text40        page.append(text)41        return page42def opensesame(*args, **kwargs):43    try:44        f = bz2.open(*args, **kwargs)45        f.peek(0)46        return f47    except OSError as e:48        if e.args != ("Invalid data stream",):49            raise50    return open(*args, **kwargs)51@cache52def get_text_property(53    el: etree.Element,54    property_uri: str,55    property_local_name: str,56) -> Optional[str]:57    q = etree.QName(property_uri, property_local_name)58    for child in el:59        if child.tag == q:60            return get_element_as_text(child)61    return None62def get_element_as_text(el: etree.Element) -> Optional[str]:63    if len(el):64        return None65    return el.text or None66def pages(67    xmlfile,68    /,69    matcher: Callable[[Page], bool] = lambda page: True,70) -> Iterator[Page]:71    for page_el in page_elements(xmlfile):72        title = get_text_property(page_el, EXPORT_NS, TITLE)73        model = get_text_property(page_el, EXPORT_NS, MODEL)74        format_ = get_text_property(page_el, EXPORT_NS, FORMAT)75        text = get_text_property(page_el, EXPORT_NS, TEXT)76        page = Page(title, model, format_, text)77        if matcher(page):78            yield page79TextPropertiesDict = dict[str, Any]80def get_text_properties_as_dicts(el: etree.Element) -> Optional[TextPropertiesDict]:81    d = {}82    for child in el:83        q = etree.QName(child)84        if len(child):85            value = get_text_properties_as_dicts(child)86        else:87            value = child.text88        if value:89            d[q.localname] = value90    return d or None91def pages_as_dicts(xmlfile) -> Iterator[Optional[TextPropertiesDict]]:92    for page_el in page_elements(xmlfile):93        page_dict = get_text_properties_as_dicts(page_el)94        yield page_dict95def page_elements(xmlfile: TextIO) -> Iterator[etree.Element]:96    for _, page_el in etree.iterparse(xmlfile, tag=QPAGE):97        yield page_el...publish.py
Source:publish.py  
1from . import Job2import mirror3import logging4class Publish(Job):5	def __init__(self, pages = None, export_ns = '', publisher = None):6		Job.__init__(self)7		self.pages = pages8		self.export_ns = export_ns9		self.publisher = publisher10	11	def summary(self):12		return "Publishing pages %s" % self.pages13	14	def required(self):15		return True16	def perform(self, fidoc):17		dw = fidoc.get_wiki()18		19		export_ns = []20		21		dw.resolve(self.export_ns, [], export_ns)22		logging.info("Export to namespace %s" % export_ns)23		# print(export_ns)24		# sys.exit()25		restpub = mirror.create_restrictedwikipublisher(fidoc, export_ns)26		27		pages = []28		if self.pages is not None:29			all_pages_info = dw.allpages()30			rx_pages = [re.compile(p) for p in self.pages]31			for info in all_pages_info:32				p = dw.resolve(info['id'])33				if p is None:34					continue35			36				for rx in rx_pages:37					if rx.match(p) is not None:38						pages.append(p)39						break40		else:41			# rx_pages = mirror.public_pages()42			pages = mirror.list_all_public_pages(dw, restpub)43		# print(pages)44		45		46		pages.sort()47		48		mirror.publish_pages(dw, restpub, pages, export_ns)49		logging.info("Finished!")50		return True51		52	def responsible(self, fidoc):...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!!
