Best Python code snippet using playwright-python
autoreload.py
Source:autoreload.py  
...579        WatchmanReloader.check_availability()580    except WatchmanUnavailable:581        return StatReloader()582    return WatchmanReloader()583def start_roundbox(reloader, main_func, *args, **kwargs):584    """585    :param reloader:586    :param main_func:587    :param args:588    :param kwargs:589    :return:590    """591    ensure_echo_on()592    main_func = check_errors(main_func)593    roundbox_main_thread = threading.Thread(594        target=main_func, args=args, kwargs=kwargs, name="roundbox-main-thread"595    )596    roundbox_main_thread.daemon = True597    roundbox_main_thread.start()598    while not reloader.should_stop:599        try:600            reloader.run(roundbox_main_thread)601        except WatchmanUnavailable as ex:602            # It's possible that the watchman service shuts down or otherwise603            # becomes unavailable. In that case, use the StatReloader.604            reloader = StatReloader()605            logger.error("Error connecting to Watchman: %s", ex)606            logger.info("Watching for file changes with %s", reloader.__class__.__name__)607def run_with_reloader(main_func, *args, **kwargs):608    """609    :param main_func:610    :param args:611    :param kwargs:612    :return:613    """614    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))615    try:616        if os.environ.get(ROUNDBOX_AUTORELOAD_ENV) == "true":617            reloader = get_reloader()618            logger.info("Watching for file changes with %s", reloader.__class__.__name__)619            start_roundbox(reloader, main_func, *args, **kwargs)620        else:621            exit_code = restart_with_reloader()622            sys.exit(exit_code)623    except KeyboardInterrupt:...__init__.py
Source:__init__.py  
1#  -*- coding: utf-8 -*-2# This library is heavy inspired by the Django management core module.3# Praise the OpenSource and devs from Django framework and the community4# url: https://github.com/django/django/tree/main/django/core/management5import functools6import os7import pkgutil8import sys9from collections import defaultdict10from difflib import get_close_matches11from importlib import import_module12import RoundBox13from RoundBox.apps import apps14from RoundBox.conf.project_settings import settings15from RoundBox.const import __version__16from RoundBox.core.cliparser.base import (17    BaseCommand,18    CommandError,19    CommandParser,20    handle_default_options,21)22from RoundBox.core.cliparser.color import color_style23from RoundBox.core.exceptions import ImproperlyConfigured24from RoundBox.utils import autoreload25def find_commands(cliparser_dir):26    """27    Given a path to a management directory, return a list of all the command28    names that are available.29    """30    command_dir = os.path.join(cliparser_dir, "commands")31    return [32        name33        for _, name, is_pkg in pkgutil.iter_modules([command_dir])34        if not is_pkg and not name.startswith("_")35    ]36def load_command_class(app_name, name):37    """38    Given a command name and an application name, return the Command39    class instance. Allow all errors raised by the import process40    (ImportError, AttributeError) to propagate.41    """42    module = import_module("%s.cliparser.commands.%s" % (app_name, name))43    return module.Command()44@functools.lru_cache(maxsize=None)45def get_commands():46    """Return the script's main help text, as a string.47        Look for a cliparser.commands package in RoundBox.core, and in each48        installed application -- if a commands package exists, register all49        commands in that package.50        Core commands are always included. If a settings module has been51        specified, also include user-defined commands.52        The dictionary is in the format {command_name: app_name}. Key-value53        pairs from this dictionary can then be used in calls to54        load_command_class(app_name, command_name)55        The dictionary is cached on the first call and reused on subsequent56        calls.57    :return:58    """59    commands = {name: "RoundBox.core" for name in find_commands(__path__[0])}60    if not settings.configured:61        return commands62    for app_config in reversed(apps.get_app_configs()):63        path = os.path.join(app_config.path, "cliparser")64        commands.update({name: app_config.name for name in find_commands(path)})65    return commands66class CliParserUtility:67    """ """68    def __init__(self, argv=None):69        """70        :param argv:71        """72        self.argv = argv or sys.argv[:]73        self.prog_name = os.path.basename(self.argv[0])74        if self.prog_name == "__main__.py":75            self.prog_name = "python3 -m roundbox"76        self.settings_exception = None77    def main_help_text(self, commands_only=False):78        """79        :param commands_only:80        :return:81        """82        if commands_only:83            usage = sorted(get_commands())84        else:85            usage = [86                "",87                "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name,88                "",89                "Available subcommands:",90            ]91            commands_dict = defaultdict(lambda: [])92            for name, app in get_commands().items():93                if app == "RoundBox.core":94                    app = "RoundBox"95                else:96                    app = app.rpartition(".")[-1]97                commands_dict[app].append(name)98            style = color_style()99            for app in sorted(commands_dict):100                usage.append("")101                usage.append(style.NOTICE("[%s]" % app))102                for name in sorted(commands_dict[app]):103                    usage.append("    %s" % name)104            # Output an extra note if settings are not properly configured105            if self.settings_exception is not None:106                usage.append(107                    style.NOTICE(108                        "Note that only RoundBox core commands are listed "109                        "as settings are not properly configured (error: %s)."110                        % self.settings_exception111                    )112                )113        return "\n".join(usage)114    def fetch_command(self, subcommand):115        """Try to fetch the given subcommand, printing a message with the116        appropriate command called from the command line if it can't be found.117        :param subcommand:118        :return:119        """120        # Get commands outside of try block to prevent swallowing exceptions121        commands = get_commands()122        try:123            app_name = commands[subcommand]124        except KeyError:125            if os.environ.get("ROUNDBOX_SETTINGS_MODULE"):126                # If `subcommand` is missing due to misconfigured settings, the127                # following line will retrigger an ImproperlyConfigured exception128                # (get_commands() swallows the original one) so the user is129                # informed about it.130                settings.INSTALLED_APPS131            elif not settings.configured:132                sys.stderr.write("No RoundBox settings specified.\n")133            possible_matches = get_close_matches(subcommand, commands)134            sys.stderr.write("Unknown command: %r" % subcommand)135            if possible_matches:136                sys.stderr.write(". Did you mean %s?" % possible_matches[0])137            sys.stderr.write("\nType '%s help' for usage.\n" % self.prog_name)138            sys.exit(1)139        if isinstance(app_name, BaseCommand):140            # If the command is already loaded, use it directly.141            klass = app_name142        else:143            klass = load_command_class(app_name, subcommand)144        return klass145    def autocomplete(self):146        """Output completion suggestions for BASH.147        The output of this function is passed to BASH's `COMREPLY` variable and148        treated as completion suggestions. `COMREPLY` expects a space149        separated string as the result.150        The `COMP_WORDS` and `COMP_CWORD` BASH environment variables are used151        to get information about the cli input. Please refer to the BASH152        man-page for more information about this variables.153        Subcommand options are saved as pairs. A pair consists of154        the long option string (e.g. '--exclude') and a boolean155        value indicating if the option requires arguments. When printing to156        stdout, an equal sign is appended to options which require arguments.157        Note: If debugging this function, it is recommended to write the debug158        output in a separate file. Otherwise the debug output will be treated159        and formatted as potential completion suggestions.160        """161        # Don't complete if user hasn't sourced bash_completion file.162        if "ROUNDBOX_AUTO_COMPLETE" not in os.environ:163            return164        cwords = os.environ["COMP_WORDS"].split()[1:]165        cword = int(os.environ["COMP_CWORD"])166        try:167            curr = cwords[cword - 1]168        except IndexError:169            curr = ""170        subcommands = [*get_commands(), "help"]171        options = [("--help", False)]172        # subcommand173        if cword == 1:174            print(" ".join(sorted(filter(lambda x: x.startswith(curr), subcommands))))175        # subcommand options176        # special case: the 'help' subcommand has no options177        elif cwords[0] in subcommands and cwords[0] != "help":178            subcommand_cls = self.fetch_command(cwords[0])179            parser = subcommand_cls.create_parser("", cwords[0])180            options.extend(181                (min(s_opt.option_strings), s_opt.nargs != 0)182                for s_opt in parser._actions183                if s_opt.option_strings184            )185            # filter out previously specified options from available options186            prev_opts = {x.split("=")[0] for x in cwords[1 : cword - 1]}187            options = (opt for opt in options if opt[0] not in prev_opts)188            # filter options by current input189            options = sorted((k, v) for k, v in options if k.startswith(curr))190            for opt_label, require_arg in options:191                # append '=' to options which require args192                if require_arg:193                    opt_label += "="194                print(opt_label)195        # Exit code of the bash completion function is never passed back to196        # the user, so it's safe to always exit with 0.197        # For more details see #25420.198        sys.exit(0)199    def exec(self):200        """Given the command-line arguments, figure out which subcommand is being201        run, create a parser appropriate to that command, and run it.202        :return:203        """204        try:205            subcommand = self.argv[1]206        except IndexError:207            subcommand = "help"  # Display help if no arguments were given.208        # Preprocess options to extract --settings and --pythonpath.209        # These options could affect the commands that are available, so they210        # must be processed early.211        parser = CommandParser(212            prog=self.prog_name,213            usage="%(prog)s subcommand [options] [args]",214            add_help=False,215            allow_abbrev=False,216        )217        parser.add_argument("--settings")218        parser.add_argument("--pythonpath")219        parser.add_argument("args", nargs="*")  # catch-all220        try:221            options, args = parser.parse_known_args(self.argv[2:])222            handle_default_options(options)223        except CommandError:224            pass  # Ignore any option errors at this point225        try:226            settings.INSTALLED_APPS227        except ImproperlyConfigured as exc:228            self.settings_exception = exc229        except ImportError as exc:230            self.settings_exception = exc231        # @TODO: check if in the future this section is function for current use232        if settings.configured:233            # Start the auto-reloading dev server even if the code is broken.234            # The hardcoded condition is a code small, but we can't rely on a235            # flag on the command class because we haven't located it yet.236            if subcommand == 'runserver' and "--noreload" not in self.argv:237                try:238                    autoreload.check_errors(RoundBox.setup)()239                except Exception:240                    # The exception will be raised later in the child process241                    # started by the autoreloader. Pretend it didn't happen by242                    # loading an empty list of applications.243                    apps.all_models = defaultdict(dict)244                    apps.app_configs = {}245                    apps.apps_ready = apps.models_ready = apps.ready = True246                    # Remove options not compatible with the built-in runserver247                    # (e.g. options for the contrib.staticfiles' runserver).248                    # Changes here require manually testing as described in249                    # #27522.250                    _parser = self.fetch_command("runserver").create_parser(251                        "RoundBox", "runserver"252                    )253                    _options, _args = _parser.parse_known_args(self.argv[2:])254                    for _arg in _args:255                        self.argv.remove(_arg)256            # In all other cases, django.setup() is required to succeed.257            else:258                RoundBox.setup()259        self.autocomplete()260        if subcommand == "help":261            if "--commands" in args:262                sys.stdout.write(self.main_help_text(commands_only=True) + "\n")263            elif not options.args:264                sys.stdout.write(self.main_help_text() + "\n")265            else:266                self.fetch_command(options.args[0]).print_help(self.prog_name, options.args[0])267        # special case for --version and --help to work, for backwards compatibility268        elif subcommand == "version" or self.argv[1:] == ["--version"]:269            sys.stdout.write(__version__ + "\n")270        elif self.argv[1:] in (["--help"], ["-h"]):271            sys.stdout.write(self.main_help_text() + "\n")272        else:273            self.fetch_command(subcommand).run_from_argv(self.argv)274def exec_from_cli(argv=None):275    """Execute the CliParserUtility276    :param argv:277    :return:278    """279    utility = CliParserUtility(argv)...BlockTool.py
Source:BlockTool.py  
1from panda3d.core import Point3, Vec42from .BoxTool import BoxTool3from .ToolOptions import ToolOptions4from direct.foundry.Create import MultiCreate5from direct.foundry.Select import Select, Deselect6from direct.foundry.ActionGroup import ActionGroup7from direct.foundry.ChangeSelectionMode import ChangeSelectionMode8from direct.foundry.SelectionType import SelectionType9from direct.foundry.GridSettings import GridSettings10from direct.foundry.KeyBind import KeyBind11from direct.foundry.IDGenerator import IDGenerator12from direct.foundry import MaterialPool, LEGlobals, LEConfig13from PyQt5 import QtWidgets, QtCore14class BlockToolOptions(ToolOptions):15    GlobalPtr = None16    @staticmethod17    def getGlobalPtr():18        self = BlockToolOptions19        if not self.GlobalPtr:20            self.GlobalPtr = BlockToolOptions()21        return self.GlobalPtr22    def __init__(self):23        ToolOptions.__init__(self)24        self.roundVertices = True25        shapeBase = QtWidgets.QWidget(self)26        self.layout().addWidget(shapeBase)27        shapeBase.setLayout(QtWidgets.QFormLayout())28        typeLbl = QtWidgets.QLabel("Shape:", shapeBase)29        self.typeCombo = QtWidgets.QComboBox(shapeBase)30        self.typeCombo.currentIndexChanged.connect(self.__selectBrush)31        shapeBase.layout().addRow(typeLbl, self.typeCombo)32        self.roundBox = QtWidgets.QCheckBox("Round created vertices", shapeBase)33        self.roundBox.setChecked(self.roundVertices)34        self.roundBox.stateChanged.connect(self.__changeRound)35        shapeBase.layout().addRow(None, self.roundBox)36        self.currentControls = None37        self.selectedBrush = base.brushMgr.brushes[0]38        for brush in base.brushMgr.brushes:39            self.typeCombo.addItem(brush.Name)40    def __changeRound(self, state):41        self.roundVertices = (state != QtCore.Qt.Unchecked)42        if self.tool:43            self.tool.maybeUpdatePreviewBrushes()44    def __selectBrush(self, index):45        if self.currentControls:46            self.layout().removeWidget(self.currentControls)47            self.currentControls.setParent(None)48        brush = base.brushMgr.brushes[index]49        self.selectedBrush = brush50        if len(brush.controls) > 0:51            self.currentControls = brush.controlsGroup52            self.layout().addWidget(self.currentControls)53        else:54            self.currentControls = None55        self.roundBox.setEnabled(self.selectedBrush.CanRound)56        if self.tool:57            self.tool.maybeUpdatePreviewBrushes()58class BlockTool(BoxTool):59    Name = "Block"60    KeyBind = KeyBind.BlockTool61    ToolTip = "Block tool"62    Icon = "icons/editor-block.png"63    Draw3DBox = False64    def __init__(self, mgr):65        BoxTool.__init__(self, mgr)66        self.box.setColor(LEGlobals.PreviewBrush2DColor)67        self.lastBox = None68        self.options = BlockToolOptions.getGlobalPtr()69        self.previewBrushes = []70    def activate(self):71        BoxTool.activate(self)72        self.acceptGlobal('brushValuesChanged', self.onBrushValuesChanged)73    def onBrushValuesChanged(self, brush):74        if brush == self.options.selectedBrush:75            self.maybeUpdatePreviewBrushes()76    def enable(self):77        BoxTool.enable(self)78        solids = []79        for sel in base.selectionMgr.selectedObjects:80            if sel.ObjectName == "solid":81                solids.append(sel)82        if len(solids) > 0:83            mins = Point3()84            maxs = Point3()85            solids[len(solids) - 1].np.calcTightBounds(mins, maxs, base.render)86            self.lastBox = [mins, maxs]87        elif self.lastBox is None:88            self.lastBox = [Point3(0), Point3(GridSettings.DefaultStep)]89    def disable(self):90        self.removePreviewBrushes()91        BoxTool.disable(self)92    def removePreviewBrushes(self):93        for brush in self.previewBrushes:94            brush.delete()95        self.previewBrushes = []96    def updatePreviewBrushes(self):97        self.removePreviewBrushes()98        self.previewBrushes = self.options.selectedBrush.create(IDGenerator(), self.state.boxStart,99            self.state.boxEnd, self.determineMaterial(), 2 if self.options.roundVertices else 0, True)100    def cleanup(self):101        self.lastBox = None102        self.previewBrushes = None103        BoxTool.cleanup(self)104    def leftMouseDownToDraw(self):105        BoxTool.leftMouseDownToDraw(self)106        vp = base.viewportMgr.activeViewport107        if self.lastBox is not None:108            self.state.boxStart += vp.getUnusedCoordinate(self.lastBox[0])109            self.state.boxEnd += vp.getUnusedCoordinate(self.lastBox[1])110        else:111            self.state.boxEnd += vp.getUnusedCoordinate(Point3(GridSettings.DefaultStep))112        self.onBoxChanged()113    def onBoxChanged(self):114        BoxTool.onBoxChanged(self)115        self.maybeUpdatePreviewBrushes()116    def maybeUpdatePreviewBrushes(self):117        if (not self.state.boxStart or not self.state.boxEnd) or \118        (self.state.boxStart[0] == self.state.boxEnd[0] or self.state.boxStart[1] == self.state.boxEnd[1]119            or self.state.boxStart[2] == self.state.boxEnd[2]):120            self.removePreviewBrushes()121            return122        self.updatePreviewBrushes()123        self.doc.updateAllViews()124    def determineMaterial(self):125        if MaterialPool.ActiveMaterial:126            return MaterialPool.ActiveMaterial127        else:128            return MaterialPool.getMaterial(LEConfig.default_material.getValue())129    def boxDrawnConfirm(self):130        self.removePreviewBrushes()131        box = [self.state.boxStart, self.state.boxEnd]132        if box[0].x != box[1].x and box[0].y != box[1].y and box[0].z != box[1].z:133            solids = self.options.selectedBrush.create(base.document.idGenerator, self.state.boxStart, self.state.boxEnd,134                self.determineMaterial(), 2)135            creations = []136            for solid in solids:137                creations.append((base.document.world.id, solid))138            base.actionMgr.performAction("Create %i solid(s)" % len(creations),139                ActionGroup([140                    Deselect(all = True),141                    MultiCreate(creations),142                    ChangeSelectionMode(SelectionType.Groups),143                    Select(solids, False)144                ])145            )146            self.lastBox = box147    def boxDrawnCancel(self):148        self.lastBox = [self.state.boxStart, self.state.boxEnd]...roundbox.py
Source:roundbox.py  
1# Copyright 2014, Sugar Labs2#3# This program is free software; you can redistribute it and/or modify4# it under the terms of the GNU General Public License as published by5# the Free Software Foundation; either version 2 of the License, or6# (at your option) any later version.7#8# This program is distributed in the hope that it will be useful,9# but WITHOUT ANY WARRANTY; without even the implied warranty of10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the11# GNU General Public License for more details.12#13# You should have received a copy of the GNU General Public License14# along with this program; if not, write to the Free Software15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA16import math17from gi.repository import Gtk18from sugar3.graphics import style19_BORDER_DEFAULT = style.LINE_WIDTH20class RoundBox(Gtk.HBox):21    __gtype_name__ = 'RoundBox'22    def __init__(self, **kwargs):23        Gtk.HBox.__init__(self, **kwargs)24        self._radius = style.zoom(15)25        self.border_color = style.COLOR_BLACK26        self.tail = None27        self.background_color = None28        self.set_resize_mode(Gtk.ResizeMode.PARENT)29        self.set_reallocate_redraws(True)30        self.connect('draw', self.__expose_cb)31        self.connect('add', self.__add_cb)32    def __add_cb(self, child, params):33        child.set_border_width(style.zoom(5))34    def __expose_cb(self, widget, cr):35        rect = self.get_allocation()36        hmargin = style.zoom(15)37        x = hmargin38        y = 039        width = rect.width - _BORDER_DEFAULT * 2. - hmargin * 240        if self.tail is None:41            height = rect.height - _BORDER_DEFAULT * 2.42        else:43            height = rect.height - _BORDER_DEFAULT * 2. - self._radius44        cr.move_to(x + self._radius, y)45        cr.arc(x + width - self._radius, y + self._radius,46               self._radius, math.pi * 1.5, math.pi * 2)47        tail_height = style.zoom(5)48        if self.tail == 'right':49            cr.arc(x + width - self._radius, y + height - self._radius * 2,50                   self._radius, 0, math.pi * 0.5)51            cr.line_to(x + width - self._radius, y + height)52            cr.line_to(x + width - tail_height * self._radius,53                       y + height - self._radius)54            cr.arc(x + self._radius, y + height - self._radius * 2,55                   self._radius, math.pi * 0.5, math.pi)56        elif self.tail == 'left':57            cr.arc(x + width - self._radius, y + height - self._radius * 2,58                   self._radius, 0, math.pi * 0.5)59            cr.line_to(x + self._radius * tail_height,60                       y + height - self._radius)61            cr.line_to(x + self._radius, y + height)62            cr.line_to(x + self._radius, y + height - self._radius)63            cr.arc(x + self._radius, y + height - self._radius * 2,64                   self._radius, math.pi * 0.5, math.pi)65        else:66            cr.arc(x + width - self._radius, y + height - self._radius,67                   self._radius, 0, math.pi * 0.5)68            cr.arc(x + self._radius, y + height - self._radius,69                   self._radius, math.pi * 0.5, math.pi)70        cr.arc(x + self._radius, y + self._radius, self._radius,71               math.pi, math.pi * 1.5)72        cr.close_path()73        if self.background_color is not None:74            r, g, b, __ = self.background_color.get_rgba()75            cr.set_source_rgb(r, g, b)76            cr.fill_preserve()77        if self.border_color is not None:78            r, g, b, __ = self.border_color.get_rgba()79            cr.set_source_rgb(r, g, b)80            cr.set_line_width(_BORDER_DEFAULT)81            cr.stroke()82        return False83if __name__ == '__main__':84    win = Gtk.Window()85    win.connect('destroy', Gtk.main_quit)86    win.set_default_size(450, 450)87    vbox = Gtk.VBox()88    box1 = RoundBox()89    box1.tail = 'right'90    vbox.add(box1)91    label1 = Gtk.Label("Test 1")92    box1.add(label1)93    rbox = RoundBox()94    rbox.tail = 'left'95    rbox.background_color = style.Color('#FF0000')96    vbox.add(rbox)97    label2 = Gtk.Label("Test 2")98    rbox.add(label2)99    bbox = RoundBox()100    bbox.background_color = style.Color('#aaff33')101    bbox.border_color = style.Color('#ff3300')102    vbox.add(bbox)103    win.add(vbox)104    win.show_all()...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
