Best Python code snippet using autotest_python
source.py
Source:source.py  
...80                    # compose absolute URL if relative "href" found81                    url = urlparse.urljoin(self.url, value)82                    if self.pattern.match(url):83                        self.links.append(url)84    def get_ahref_list(self, url, pattern):85        self.reset(url, pattern)86        self.feed(urllib2.urlopen(url).read())87        self.close()88        return self.links89class url_source(source):90    """91    A simple URL based source that parses HTML to find references to92    kernel files.93    """94    _extension_pattern = re.compile(r'.*\.[^/.]+$')95    def __init__(self, database, prefix):96        super(url_source, self).__init__(database)97        self.prefix = prefix98        self.urls = []99    def add_url(self, url, pattern):100        """101        Add a URL path to a HTML document with links to kernel files.102        :param url: URL path to a HTML file with links to kernel files103                (can be either an absolute URL or one relative to self.prefix)104        :param pattern: regex pattern to filter kernel files links out of105                all othe links found in the HTML document106        """107        # if it does not have an extension then it's a directory and it needs108        # a trailing '/'. NOTE: there are some false positives such as109        # directories named "v2.6" where ".6" will be assumed to be extension.110        # In order for these to work the caller must provide a trailing /111        if url[-1:] != '/' and not self._extension_pattern.match(url):112            url = url + '/'113        self.urls.append((url, re.compile(pattern)))114    @staticmethod115    def _get_item(url):116        """117        Get a database.item object by fetching relevant HTTP information118        from the document pointed to by the given url.119        """120        try:121            info = urllib2.urlopen(url).info()122        except IOError, err:123            # file is referenced but does not exist124            print 'WARNING: %s' % err125            return None126        size = info.get('content-length')127        if size:128            size = int(size)129        else:130            size = -1131        timestamp = int(time.mktime(info.getdate('date')))132        if not timestamp:133            timestamp = 0134        return database.item(url, size, timestamp)135    def get_new_files(self):136        parser = _ahref_parser()137        files = {}138        for url, pattern in self.urls:139            links = parser.get_ahref_list(urlparse.urljoin(self.prefix, url),140                                          pattern)141            for link in links:142                item = self._get_item(link)143                if item:144                    files[item.name] = item145        return self._get_new_files(files)146class directory_source(source):147    """148    Source that finds kernel files by listing the contents of a directory.149    """150    def __init__(self, database, path):151        """152        Initialize a directory_source instance.153        :param database: Persistent database with known kernels information....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!!
