Best Python code snippet using robotframework
main_window.py
Source:main_window.py  
...79        self._init_workspace()80        self._init_toolbars()81        self._init_menus()82        self._init_plugins()83        self._init_library_docs()84        self._init_url_scheme_handler()85        self.workspace.plugins.on_workspace_initialized(self)86        self._init_shortcuts()87        self._init_flirt_signatures()88        self._run_daemon(use_daemon=use_daemon)89        # I'm ready to show off!90        if show:91            self.showMaximized()92            self.windowHandle().screenChanged.connect(self.on_screen_changed)93            self.show()94        self.status = "Ready."95    def sizeHint(self, *args, **kwargs):  # pylint: disable=unused-argument,no-self-use96        return QSize(1200, 800)97    #98    # Properties99    #100    @property101    def caption(self):102        return self.getWindowTitle()103    @caption.setter104    def caption(self, v):105        self.setWindowTitle(v)106    @property107    def status(self):108        return self._status109    @status.setter110    def status(self, v):111        self._status = v112        self.statusBar().showMessage(v)113    @property114    def progress(self):115        return self._progress116    @progress.setter117    def progress(self, v):118        self._progress = v119        self._progressbar.show()120        self._progressbar.setValue(v)121    #122    # Dialogs123    #124    def open_mainfile_dialog(self):125        # pylint: disable=assigning-non-slot126        # https://github.com/PyCQA/pylint/issues/3793127        file_path, _ = QFileDialog.getOpenFileName(self, "Open a binary", Conf.last_used_directory,128                                                   "All executables (*);;"129                                                   "Windows PE files (*.exe);;"130                                                   "Core Dumps (*.core);;"131                                                   "angr database (*.adb)",132                                                   )133        Conf.last_used_directory = os.path.dirname(file_path)134        return file_path135    def _pick_image_dialog(self):136        try:137            prompt = LoadDockerPrompt(parent=self)138        except LoadDockerPromptError:139            return None140        if prompt.exec_() == 0:141            return None  # User canceled142        return prompt.textValue()143    def open_load_plugins_dialog(self):144        dlg = LoadPlugins(self.workspace.plugins)145        dlg.setModal(True)146        dlg.exec_()147    def open_newstate_dialog(self):148        if self.workspace.instance.project.am_none:149            QMessageBox.critical(self,150                                 "Cannot create new states",151                                 "Please open a binary to analyze first.")152            return153        new_state_dialog = NewState(self.workspace.instance, parent=self, create_simgr=True)154        new_state_dialog.exec_()155    def open_doc_link(self):156        QDesktopServices.openUrl(QUrl("https://docs.angr.io/", QUrl.TolerantMode))157    def open_about_dialog(self):158        dlg = LoadAboutDialog()159        dlg.exec_()160    #161    # Widgets162    #163    def _init_statusbar(self):164        self._progressbar = QProgressBar()165        self._progressbar.setMinimum(0)166        self._progressbar.setMaximum(100)167        self._progressbar.hide()168        self.statusBar().addPermanentWidget(self._progressbar)169    def _init_toolbars(self):170        for cls in (FileToolbar, DebugToolbar):171            self.toolbar_manager.show_toolbar_by_class(cls)172    #173    # Menus174    #175    def _init_menus(self):176        self._file_menu = FileMenu(self)177        self._analyze_menu = AnalyzeMenu(self)178        self._view_menu = ViewMenu(self)179        self._help_menu = HelpMenu(self)180        self._plugin_menu = PluginMenu(self)181        # TODO: Eventually fix menu bars to have native support on MacOS182        # if on a Mac, don't use the native menu bar (bug mitigation from QT)183        if sys.platform == 'darwin':184            self.menuBar().setNativeMenuBar(False)185        self.menuBar().addMenu(self._file_menu.qmenu())186        self.menuBar().addMenu(self._view_menu.qmenu())187        self.menuBar().addMenu(self._analyze_menu.qmenu())188        self.menuBar().addMenu(self._plugin_menu.qmenu())189        self.menuBar().addMenu(self._help_menu.qmenu())190    #191    # Workspace192    #193    def _init_workspace(self):194        """195        Initialize workspace196        :return:    None197        """198        self.central_widget = QMainWindow()199        self.setCentralWidget(self.central_widget)200        wk = Workspace(self, Instance())201        self.workspace = wk202        self.workspace.view_manager.tabify_center_views()203        self.central_widget.setTabPosition(Qt.RightDockWidgetArea, QTabWidget.North)204        self.central_widget.setDockNestingEnabled(True)205        def set_caption(**kwargs):  # pylint: disable=unused-argument206            if self.workspace.instance.project.am_none:207                self.caption = ''208            elif self.workspace.instance.project.filename is None:209                self.caption = "Loaded from stream"210            else:211                self.caption = os.path.basename(self.workspace.instance.project.filename)212        self.workspace.instance.project.am_subscribe(set_caption)213        self.tab = self.central_widget.findChild(QTabBar)214        self.tab.tabBarClicked.connect(self.on_center_tab_clicked)215    #216    # Shortcuts217    #218    def interrupt_current_job(self):219        self.workspace.instance.interrupt_current_job()220    def _init_shortcuts(self):221        """222        Initialize shortcuts223        :return:    None224        """225        center_dockable_views = self.workspace.view_manager.get_center_views()226        for i in range(1, len(center_dockable_views) + 1):227            QShortcut(QKeySequence('Ctrl+' + str(i)), self, center_dockable_views[i - 1].raise_)228        QShortcut(QKeySequence("Ctrl+I"), self, self.interrupt_current_job)229        # Raise the DisassemblyView after everything has initialized230        center_dockable_views[0].raise_()231        # Toggle exec breakpoint232        QShortcut(QKeySequence(Qt.Key_F2), self, self.workspace.toggle_exec_breakpoint)233        # Single step234        QShortcut(QKeySequence(Qt.Key_F7), self, self.workspace.step_forward)235        # Run236        QShortcut(QKeySequence(Qt.Key_F9), self, self.workspace.continue_forward)237    #238    # Plugins239    #240    def _init_plugins(self):241        self.workspace.plugins.discover_and_initialize_plugins()242    #243    # FLIRT Signatures244    #245    def _init_flirt_signatures(self):246        if Conf.flirt_signatures_root:247            # if it's a relative path, it's relative to the angr-management package248            if os.path.isabs(Conf.flirt_signatures_root):249                flirt_signatures_root = Conf.flirt_signatures_root250            else:251                if is_pyinstaller():252                    flirt_signatures_root = os.path.join(app_root(), Conf.flirt_signatures_root)253                else:254                    # when running as a Python package, we should use the git submodule, which is on the same level255                    # with (instead of inside) the angrmanagement module directory.256                    flirt_signatures_root = os.path.join(app_root(), "..", Conf.flirt_signatures_root)257            flirt_signatures_root = os.path.normpath(flirt_signatures_root)258            _l.info("Loading FLIRT signatures from %s.", flirt_signatures_root)259            angr.flirt.load_signatures(flirt_signatures_root)260    #261    # Library docs262    #263    def _init_library_docs(self):264        GlobalInfo.library_docs = LibraryDocs()265        if Conf.library_docs_root:266            GlobalInfo.library_docs.load_func_docs(Conf.library_docs_root)267    #268    # Daemon269    #270    def _run_daemon(self, use_daemon=None):271        if use_daemon is None:272            # Load it from the configuration file273            use_daemon = Conf.use_daemon274        if not use_daemon:275            return276        # connect to daemon (if there is one)277        if not daemon_exists():...library.py
Source:library.py  
1# -*- coding: utf-8 -*-2from collections import defaultdict3from operator import itemgetter4import TEItransformer as TEIT5from app_tei_auxiliary import *6import sqlite37from sqlite3 import Error8APP_DIR = os.getcwd()9CONFIG = read_yaml("config/library_tei_docs.yaml")10class Library:11    """12    Collect library.13    """14    @staticmethod15    def __load_library_paths():16        """17        Get document names and extensions form the library dir.18        :return: dict19        """20        files = defaultdict(list)21        for filename in os.listdir(CONFIG['PATHS']['library']):22            if not filename.startswith('.'):23                fname, fextension = os.path.splitext(filename)24                fname = os.path.split(fname)[-1]25                files[fname].append(fextension)26        return files27    @staticmethod28    def __create_link(cur_dir, new_dir, filename):29        """30        Create soft links to app_tei directories.31        :param cur_dir: str32        :param new_dir: str33        :param filename: str34        :return: None35        """36        os.chdir(new_dir)37        os.system('ln -s {}/{}'.format(cur_dir, filename))38        os.chdir(APP_DIR)39    def __transfer_files(self, fullname, output_format='html'):40        """41        Create soft links to transformed files to app_tei directories.42        :param fullname: str43        :param output_format: str44        :return: None45        """46        self.__create_link(47            CONFIG['PATHS']['library_docs'],48            CONFIG['PATHS']['static'], fullname)49        if output_format == 'html':50            self.__create_link(51                CONFIG['PATHS']['library_templates'],52                CONFIG['PATHS']['templates'], fullname)53    def __transform_format(self, TT, filename, files, output_format, **kwargs):54        """55        Transform file to a format.56        :param TT: TEItransformer object57        :param filename: str58        :param files: dict59        :param output_format: str60        :param kwargs: kwargs for transform method61        :return: None62        """63        full_name = "{}.{}".format(filename, output_format)64        output_filename = "{}/{}".format(65            CONFIG['PATHS']['library'], filename)66        TT.transform(67            output_format=output_format,68            output_filename=output_filename,69            enable_valid=False, **kwargs)70        self.__transfer_files(full_name, output_format=output_format)71        files[filename].append('.' + output_format)72    def __parse_xml(self, TT, filename, files, schema_path=None, **kwargs):73        """74        Parse xml file.75        :param TT: TEItransformer object76        :param filename: str77        :param files: dict78        :param schema_path: str79        :param kwargs: kwargs for transform method80        :return: dict81        """82        full_name = "{}.xml".format(filename)83        tei_path = os.path.join(CONFIG['PATHS']['library'], full_name)84        if schema_path:85            schema_path = os.path.join(CONFIG['PATHS']['library'], schema_path)86        TT.load_tei(tei_path, schema_path=schema_path)87        self.__transfer_files(full_name, output_format='xml')88        self.__transform_format(TT, filename, files, 'html', full_page=True, **kwargs)89        self.__transform_format(TT, filename, files, 'docx', **kwargs)90        self.__transform_format(TT, filename, files, 'json')91        return files92    def parse_library(self, scenario='drama', **kwargs):93        """94        Parse library files.95        :param scenario: str96        :param kwargs: kwargs for transform method97        :return: dict98        """99        TT = TEIT.TEITransformer(scenario=scenario)100        files = self.__load_library_paths()101        for filename in files:102            formats = files[filename]103            if '.xml' in formats:104                files = self.__parse_xml(TT, filename, files, **kwargs)105        return files106class LibraryDB(Library):107    """108    Library creation interface.109    """110    def __init__(self):111        self.create_db()112    def create_db(self):113        """114        Create db and tables.115        """116        self.conn = None117        try:118            self.conn = sqlite3.connect(119                CONFIG['PATHS']['dbname'])120            self.cur = self.conn.cursor()121            self.create_tables()122        except Error as e:123            print(e)124    def create_tables(self):125        """126        Create tables.127        """128        self.conn.execute(CONFIG['SQL_QUERIES']['drop_documents'])129        self.conn.execute(CONFIG['SQL_QUERIES']['drop_authors'])130        self.conn.execute(CONFIG['SQL_QUERIES']['create_documents'])131        self.conn.execute(CONFIG['SQL_QUERIES']['create_authors'])132    @staticmethod133    def get_element_text(soup, *args, findall=False, **kwargs):134        """135        Try to extract text from tag.136        :param soup: bs4 object137        :param args: args for findAll method138        :param findall: whether to find all the child nodes139        :param kwargs: kwargs for findAll method140        :return: str141        """142        if findall:143            element = soup.findAll(*args, **kwargs)144        else:145            element = soup.find(*args, **kwargs)146            element = [element]147        if not element or element == [None]:148            return 'Not defined'149        text = [i.text for i in element]150        text = ', '.join(text)151        return text152    def __html_search_metadata(self, filename):153        full_name = "{}.{}".format(filename, 'html')154        path = os.path.join(CONFIG['PATHS']['templates'], full_name)155        soup = read_html(path)156        metadata = self.create_metadata(filename)157        for tagset in CONFIG['METAINFO_TAGS']:158            tag_value = self.__parse_tagset_class(soup, tagset)159            metadata[tagset[-3] + '_' + tagset[-2]] = tag_value160        metadata['inner_id_author'] = generate_id(metadata['titleStmt_author'])161        return metadata162    @staticmethod163    def __find_tag(sp, tagset):164        """165        Find tag for meta information166        :param sp: b4s element167        :param tagset: list168        :return: b4s element169        """170        if sp:171            for tag in tagset[:-2]:172                sp = sp.find(class_=tag)173                if not sp: break174        return sp175    def __parse_tagset_class(self, soup, tagset):176        """177        Parse tags for metadata.178        :param soup: bs4 soup179        :param tagset: list180        :return: str181        """182        sp = soup.find(class_='teiHeader')183        sp = self.__find_tag(sp, tagset)184        if sp:185            tag_value = self.get_element_text(186                sp, class_=tagset[-2], findall=tagset[-1])187        else: tag_value = 'Not defined'188        return tag_value189    # def __parse_tagset(self, soup, tagset):190    #     """191    #     Parse tags for metadata.192    #     :param soup: bs4 soup193    #     :param tagset: list194    #     :return: str195    #     """196    #     sp = soup.find('teiHeader')197    #     for tag in tagset[:-2]:198    #         sp = sp.find(tag)199    #         if not sp: break200    #     if sp:201    #         tag_value = self.get_element_text(202    #             sp, tagset[-2], findall=tagset[-1])203    #     else:204    #         tag_value = 'Not defined'205    #     return tag_value206    @staticmethod207    def __add_formats(formats, metadata):208        """209        Add formats to metadata210        :param formats: list211        :param metadata: dict212        :return:213        """214        for frm in formats:215            col_name = "{}_format".format(frm[1:])216            metadata[col_name] = 1217        return metadata218    @staticmethod219    def create_metadata(filename):220        """221        Create metadata template222        :param filename: str223        :return: dict224        """225        return {226            'docx_format': 0,227            'html_format': 0,228            'xml_format': 0,229            'json_format': 0,230            'filename': filename,231            'inner_id_document': generate_id(filename)232        }233    def collect_data(self, **kwargs):234        """235        Collect data from library files.236        :param kwargs: kwargs for parse_library function237        :return: list238        """239        files = self.parse_library(**kwargs)240        meta_info = []241        for filename in files:242            formats = files[filename]243            if '.html' in formats:244                metadata = self.__html_search_metadata(filename)245                metadata = self.__add_formats(formats, metadata)246                self.add_document_info(metadata)247        return meta_info248    def add_document_info(self, metadata):249        """250        Add data to DB tables251        :param metadata: dict252        :return: None253        """254        document = itemgetter(*CONFIG['DB_COLUMNS']['document'])(metadata)255        author = itemgetter(*CONFIG['DB_COLUMNS']['author'])(metadata)256        q_doc = CONFIG['SQL_QUERIES']['insert_document'].format(*document*2)257        q_author = CONFIG['SQL_QUERIES']['insert_author'].format(*author*2)258        self.cur.execute(q_doc)259        self.cur.execute(q_author)...tasks.py
Source:tasks.py  
...71def print_version(ctx):72    """Print the current project version."""73    print(Version(path=VERSION_PATH, pattern=VERSION_PATTERN))74@task75def library_docs(ctx, name):76    """Generate standard library documentation.77    Args:78        name:  Name of the library or `all` to generate docs for all libs.79               Name is case-insensitive and can be shortened as long as it80               is a unique prefix. For example, `b` is equivalent to81               `BuiltIn` and `di` equivalent to `Dialogs`.82    """83    libraries = ['BuiltIn', 'Collections', 'DateTime', 'Dialogs',84                 'OperatingSystem', 'Process', 'Screenshot', 'String',85                 'Telnet', 'XML']86    name = name.lower()87    if name != 'all':88        libraries = [lib for lib in libraries if lib.lower().startswith(name)]89        if len(libraries) != 1:...bundle.py
Source:bundle.py  
1#!/usr/bin/env python32import sys3import os4# for finding various libs5import angrmanagement6import capstone7import unicorn8import pyvex9import angr10import cle11import z312import zmq13import parso14import debugpy15if sys.platform == "linux":16    import archr17def make_common_options(for_chess=False):18    """19    Create the pyinstaller command.20    """21    am_repo_dir = os.path.dirname(os.path.dirname(angrmanagement.__file__))22    # any dynamically-loaded modules have to be explicitly added23    included_data = [24        (25            os.path.join(os.path.dirname(angrmanagement.__file__), "resources"),26            "angrmanagement/resources",27        ),28        (29            os.path.join(os.path.dirname(angrmanagement.__file__), "resources/images"),30            "angrmanagement/resources/images",31        ),32        (33            os.path.join(os.path.dirname(angrmanagement.__file__), "plugins"),34            "angrmanagement/plugins",35        ),36        (37            os.path.join(os.path.dirname(angrmanagement.__file__), "config"),38            "angrmanagement/config",39        ),40        (41            os.path.join(os.path.dirname(cle.__file__), "backends/pe/relocation"),42            "cle/backends/pe/relocation",43        ),44        (45            os.path.join(os.path.dirname(cle.__file__), "backends/elf/relocation"),46            "cle/backends/elf/relocation",47        ),48        (49            os.path.join(50                os.path.dirname(angr.__file__), "analyses/identifier/functions"51            ),52            "angr/analyses/identifier/functions",53        ),54        (os.path.join(os.path.dirname(angr.__file__), "procedures"), "angr/procedures"),55        (os.path.join(os.path.dirname(parso.__file__), "python"), "parso/python"),56        (os.path.join(am_repo_dir, "flirt_signatures"), "flirt_signatures"),57        (os.path.join(am_repo_dir, "library_docs"), "library_docs"),58        (os.path.join(os.path.dirname(debugpy.__file__), "_vendored"), "debugpy/_vendored"),59    ]60    if sys.platform == "linux":61        included_data.append(62            (63                os.path.join(os.path.dirname(archr.__file__), "implants"),64                "archr/implants",65            )66        )67    if sys.platform != "darwin":68        included_data.append(69            (70                os.path.join(os.path.dirname(zmq.__file__), os.pardir, "pyzmq.libs"),71                "pyzmq.libs",72            )73        )74    # dynamically-loaded DLLs have to be explicitly added. We just include the entire lib dir.75    included_libs = [76        (os.path.join(os.path.dirname(angr.__file__), "lib"), "angr/lib"),77        (os.path.join(os.path.dirname(pyvex.__file__), "lib"), "pyvex/lib"),78        (os.path.join(os.path.dirname(unicorn.__file__), "lib"), "unicorn/lib"),79        (capstone._path, "capstone/lib"),80        (os.path.join(os.path.dirname(z3.__file__), "lib"), "z3/lib"),81    ]82    if sys.platform == "linux":83        import keystone84        included_libs.append((os.path.dirname(keystone.__file__), "keystone"))85    all_mappings = [86        (";" if sys.platform.startswith("win") else ":").join(mapping)87        for mapping in (included_data + included_libs)88    ]89    # include ipython because it's not autodetected for some reason90    hidden_import = [91        "--hidden-import=ipykernel.datapub",92        "--hidden-import=pkg_resources.py2_warn",93        "--hidden-import=sqlalchemy.sql.default_comparator",94        "--hidden-import=pyxdg",95        "--hidden-import=pyzmq",96        "--hidden-import=xmlrpc.server"97    ]98    if for_chess:99        hidden_import.append("--hidden-import=slacrs")100        hidden_import.append("--hidden-import=getmac")101        hidden_import.append("--hidden-import=qtterm")102    args = [103        "pyinstaller",104        ] + hidden_import + [105        "--name=angr-management",106        "-w",107        "-i",108        os.path.join(109            os.path.dirname(angrmanagement.__file__), "resources", "images", "angr.ico"110        ),111    ]112    for mapping in all_mappings:113        args.append("--add-data")114        args.append(mapping)115    args.append("start.py")116    return args117def make_bundle(onefile=True, for_chess=False):118    """119    Execute the pyinstaller command.120    """121    args = make_common_options(for_chess=for_chess)122    if onefile:123        args.append("--onefile")124        args.append("--distpath")125        args.append("onefile")126    if sys.platform in ("linux", "win32", "darwin"):127        print(f"Creating bundle for {sys.platform}")128        os.system(" ".join(args))129    else:130        print(f"Unsupported platform: {sys.platform}")131def main():132    for_chess = "--chess" in sys.argv133    if "--onefile" in sys.argv:134        make_bundle(onefile=True, for_chess=for_chess)135    if "--onedir" in sys.argv:136        make_bundle(onefile=False, for_chess=for_chess)137if __name__ == "__main__":...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!!
