def parse(path): html = filescheme.dirbrowser_html(path).decode('utf-8') soup = bs4.BeautifulSoup(html, 'html.parser') with testutils.ignore_bs4_warning(): print(soup.prettify()) container = soup('div', id='dirbrowserContainer')[0] parent_elem = container('ul', class_='parent') if not parent_elem: parent = None else: parent = parent_elem[0].li.a.string folders = [] files = [] for li in container('ul', class_='folders')[0]('li'): item = self.Item(link=li.a['href'], text=str(li.a.string)) folders.append(item) for li in container('ul', class_='files')[0]('li'): item = self.Item(link=li.a['href'], text=str(li.a.string)) files.append(item) return self.Parsed(parent=parent, folders=folders, files=files)
def parse(quteproc): """Parse the dirbrowser content from the given quteproc. Args: quteproc: The quteproc fixture. """ html = quteproc.get_content(plain=False) soup = bs4.BeautifulSoup(html, 'html.parser') with testutils.ignore_bs4_warning(): print(soup.prettify()) title_prefix = 'Browse directory: ' # Strip off the title prefix to obtain the path of the folder that # we're browsing path = pathlib.Path(soup.title.string[len(title_prefix):]) container = soup('div', id='dirbrowserContainer')[0] parent_elem = container('ul', class_='parent') if not parent_elem: parent = None else: parent = pathlib.Path(QUrl(parent_elem[0].li.a['href']).toLocalFile()) folders = [] files = [] for css_class, list_ in [('folders', folders), ('files', files)]: for li in container('ul', class_=css_class)[0]('li'): item_path = pathlib.Path(QUrl(li.a['href']).toLocalFile()) list_.append( Item(path=item_path, link=li.a['href'], text=str(li.a.string))) return Parsed(path=path, parent=parent, folders=folders, files=files)
def test_icons(self, monkeypatch): """Make sure icon paths are correct file:// URLs.""" html = filescheme.dirbrowser_html(os.getcwd()).decode('utf-8') soup = bs4.BeautifulSoup(html, 'html.parser') with testutils.ignore_bs4_warning(): print(soup.prettify()) css = soup.html.head.style.string assert "background-image: url('qute://resource/img/folder.svg');" in css
def test_oserror(self, mocker): m = mocker.patch('qutebrowser.browser.webkit.network.filescheme.' 'os.listdir') m.side_effect = OSError('Error message') html = filescheme.dirbrowser_html('').decode('utf-8') soup = bs4.BeautifulSoup(html, 'html.parser') with testutils.ignore_bs4_warning(): print(soup.prettify()) error_msg = soup('p', id='error-message-text')[0].string assert error_msg == 'Error message'
def test_basic(self): html = filescheme.dirbrowser_html(os.getcwd()).decode('utf-8') soup = bs4.BeautifulSoup(html, 'html.parser') with testutils.ignore_bs4_warning(): print(soup.prettify()) container = soup.div assert container['id'] == 'dirbrowserContainer' title_elem = container('div', id='dirbrowserTitle')[0] title_text = title_elem('p', id='dirbrowserTitleText')[0].text assert title_text == 'Browse directory: {}'.format(os.getcwd())