Best Python code snippet using avocado_python
vmimage.py
Source:vmimage.py  
...87            self._file_name = self.image_pattern.format(version=self.version,88                                                        build=self.build,89                                                        arch=self.arch)90        return self._file_name91    def _feed_html_parser(self, url, parser):92        try:93            data = urlopen(url).read()94            parser.feed(astring.to_text(data, self.HTML_ENCODING))95        except HTTPError:96            raise ImageProviderError('Cannot open %s' % self.url_versions)97    @staticmethod98    def get_best_version(versions):99        return max(versions)100    def get_versions(self):101        """Return all available versions for the current parameters."""102        parser = VMImageHtmlParser(self.version_pattern)103        self._feed_html_parser(self.url_versions, parser)104        resulting_versions = []105        if parser.items:106            for version in parser.items:107                # Trying to convert version to int or float so max()108                # can compare numerical values.109                try:110                    # Can it be converted to integer?111                    resulting_versions.append(int(version))112                except ValueError:113                    try:114                        # Can it be converted to float?115                        resulting_versions.append(float(version))116                    except ValueError:117                        # So it's just a string118                        resulting_versions.append(version)119        return resulting_versions120    def get_version(self):121        """Probes the higher version available for the current parameters."""122        resulting_versions = self.get_versions()123        if resulting_versions:124            self._best_version = self.get_best_version(resulting_versions)125            return self._best_version126        else:127            raise ImageProviderError('Version not available at %s' %128                                     self.url_versions)129    def get_image_url(self):130        """131        Probes the higher image available for the current parameters.132        """133        if not self.url_images or not self.image_pattern:134            raise ImageProviderError(135                "url_images and image_pattern attributes are required to get image url")136        url_images = self.url_images.format(version=self.version,137                                            build=self.build,138                                            arch=self.arch)139        image = self.image_pattern.format(version=self.version,140                                          build=self.build,141                                          arch=self.arch)142        parser = VMImageHtmlParser(image)143        self._feed_html_parser(url_images, parser)144        if parser.items:145            return url_images + max(parser.items)146        else:147            raise ImageProviderError("No images matching '%s' at '%s'. "148                                     "Wrong arch?" % (image, url_images))149    def get_image_parameters(self, image_file_name):150        """151        Computation of image parameters from image_pattern152        :param image_file_name: pattern with parameters153        :type image_file_name: str154        :return: dict with parameters155        :rtype: dict or None156        """157        keywords = re.split(r'\{(.*?)\}', self.image_pattern)[1::2]158        matches = re.match(self.file_name, image_file_name)159        if not matches:160            return None161        return {x: matches.group(x) for x in keywords}162class FedoraImageProviderBase(ImageProviderBase):163    """164    Base Fedora Image Provider165    """166    HTML_ENCODING = 'iso-8859-1'167    url_old_images = None168    def get_image_url(self):169        if int(self.version) >= 28:170            cloud = 'Cloud'171        else:172            cloud = 'CloudImages'173        if self.url_old_images and int(self.version) <= 31:174            self.url_versions = self.url_old_images175        self.url_images = self.url_versions + '{version}/' \176            + cloud + '/{arch}/images/'177        return super().get_image_url()178class FedoraImageProvider(FedoraImageProviderBase):179    """180    Fedora Image Provider181    """182    name = 'Fedora'183    def __init__(self, version='[0-9]+', build='[0-9]+.[0-9]+',184                 arch=DEFAULT_ARCH):185        super().__init__(version, build, arch)186        self.url_versions = 'https://dl.fedoraproject.org/pub/fedora/linux/releases/'187        self.url_old_images = 'https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/'188        self.image_pattern = 'Fedora-Cloud-Base-(?P<version>{version})-(?P<build>{build}).(?P<arch>{arch}).qcow2$'189class FedoraSecondaryImageProvider(FedoraImageProviderBase):190    """191    Fedora Secondary Image Provider192    """193    name = 'FedoraSecondary'194    def __init__(self, version='[0-9]+', build='[0-9]+.[0-9]+',195                 arch=DEFAULT_ARCH):196        super().__init__(version, build, arch)197        self.url_versions = 'https://dl.fedoraproject.org/pub/fedora-secondary/releases/'198        self.url_old_images = 'https://archives.fedoraproject.org/pub/archive/fedora-secondary/releases/'199        self.image_pattern = 'Fedora-Cloud-Base-(?P<version>{version})-(?P<build>{build}).(?P<arch>{arch}).qcow2$'200class CentOSImageProvider(ImageProviderBase):201    """202    CentOS Image Provider203    """204    name = 'CentOS'205    def __init__(self, version='[0-9]+', build='[0-9]{4}', arch=DEFAULT_ARCH):206        super().__init__(version, build, arch)207        self.url_versions = 'https://cloud.centos.org/centos/'208        self.url_images = self.url_versions + '{version}/images/'209        self.image_pattern = 'CentOS-(?P<version>{version})-(?P<arch>{arch})-GenericCloud-(?P<build>{build}).qcow2.xz$'210    def get_image_url(self):211        if int(self.version) >= 8:212            self.build = r'[\d\.\-]+'213            self.url_images = self.url_versions + '{version}/{arch}/images/'214            self.image_pattern = 'CentOS-(?P<version>{version})-GenericCloud-(?P<build>{build}).(?P<arch>{arch}).qcow2$'215        return super().get_image_url()216class UbuntuImageProvider(ImageProviderBase):217    """218    Ubuntu Image Provider219    """220    name = 'Ubuntu'221    def __init__(self, version='[0-9]+.[0-9]+', build=None,222                 arch=DEFAULT_ARCH):223        # Ubuntu uses 'amd64' instead of 'x86_64'224        if arch == 'x86_64':225            arch = 'amd64'226        # and 'arm64' instead of 'aarch64'227        elif arch == 'aarch64':228            arch = 'arm64'229        super().__init__(version, build, arch)230        self.url_versions = 'http://cloud-images.ubuntu.com/releases/'231        self.url_images = self.url_versions + 'releases/{version}/release/'232        self.image_pattern = 'ubuntu-(?P<version>{version})-server-cloudimg-(?P<arch>{arch}).img'233    def get_best_version(self, versions):234        """ Return best (more recent) version """235        max_float = max([float(item) for item in versions])236        return str(f'{max_float:2.2f}')237    def get_versions(self):238        """Return all available versions for the current parameters."""239        parser = VMImageHtmlParser(self.version_pattern)240        self._feed_html_parser(self.url_versions, parser)241        resulting_versions = []242        if parser.items:243            for version in parser.items:244                max_float = float(version)245                resulting_versions.append(str(f'{max_float:2.2f}'))246        return resulting_versions247class DebianImageProvider(ImageProviderBase):248    """249    Debian Image Provider250    """251    name = 'Debian'252    def __init__(self, version=None, build=r'[\d{8}\-\d{3}]', arch=DEFAULT_ARCH):253        # Debian uses 'amd64' instead of 'x86_64'254        if arch == 'x86_64':255            arch = 'amd64'256        # and 'arm64' instead of 'aarch64'257        elif arch == 'aarch64':258            arch = 'arm64'259        table_version = {260            'buster': '10',261            'bullseye': '11',262        }263        table_codename = {264            '10': 'buster',265            '11': 'bullseye',266        }267        # Default version if none was selected, should work at least until 2023 Q3268        if version is None:269            version = "bullseye"270        # User provided a numerical version271        if version in table_codename.keys():272            version = table_codename[version]273        # If version is not a codename by now, it's wrong or unknown,274        # so let's fail early275        if (version not in table_version.keys()):276            raise ImageProviderError("Unknown version", version)277        super().__init__(version, build, arch)278        self.url_versions = 'https://cloud.debian.org/images/cloud/'279        self.url_images = self.url_versions + version + '/{build}/'280        self.image_pattern = 'debian-'+table_version[version]+'-generic-(?P<arch>{arch})-{build}.qcow2$'281    def get_image_url(self):282        # Find out the build first283        parserbuild = VMImageHtmlParser(self.build)284        self._feed_html_parser(self.url_versions+self._version+"/", parserbuild)285        self.build = max(parserbuild.items)286        return super().get_image_url()287class JeosImageProvider(ImageProviderBase):288    """289    JeOS Image Provider290    """291    name = 'JeOS'292    def __init__(self, version='[0-9]+', build=None,293                 arch=DEFAULT_ARCH):294        # JeOS uses '64' instead of 'x86_64'295        if arch == 'x86_64':296            arch = '64'297        super().__init__(version, build, arch)298        self.url_versions = 'https://avocado-project.org/data/assets/jeos/'...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!!
