def urls_from_md(md): ans = list(md.urls()) if md.hasText(): # Chromium returns the url as text/plain on drag and drop of image text = md.text() if text and text.lstrip().partition(':')[0] in {'http', 'https', 'ftp'}: u = QUrl(text.strip()) if u.isValid(): ans.append(u) return ans
class LoadRequest(object): # enum Operation GetOperation = 0 PostOperation = 1 def __init__(self, urlOrReq=QUrl(), op=GetOperation, data=QByteArray()): if isinstance(urlOrReq, self.__class__): otherReq = urlOrReq self._url = QUrl(otherReq._url) self._op = otherReq._op self._data = QByteArray(otherReq._data) else: url = QUrl(urlOrReq) self._url = url self._op = op self._data = data def isValid(self): return self._url.isValid() def url(self): return self._url def setUrl(self, url): self._url = url def urlString(self): ''' @return: String ''' return QUrl.fromPercentEncoding(self._url.toEncoded()) def operation(self): return self._op def setOperation(self, op): self._op = op def data(self): return self._data def setData(self, data): self._data = data def webRequest(self): ''' @return: QWebEngineHttpRequest ''' if self._op == self.GetOperation: method = QWebEngineHttpRequest.Get else: method = QWebEngineHttpRequest.Post req = QWebEngineHttpRequest(self._url, method) req.setPostData(self._data) return req
def parse_link(self, link): link = link.strip() if link and os.path.exists(link): return QUrl.fromLocalFile(link) has_schema = re.match(r'^[a-zA-Z]+:', link) if has_schema is not None: url = QUrl(link, QUrl.TolerantMode) if url.isValid(): return url if os.path.exists(link): return QUrl.fromLocalFile(link) if has_schema is None: first, _, rest = link.partition('.') prefix = 'http' if first == 'ftp': prefix = 'ftp' url = QUrl(prefix + '://' + link, QUrl.TolerantMode) if url.isValid(): return url return QUrl(link, QUrl.TolerantMode)
def parse_link(self, link): link = link.strip() if link and os.path.exists(link): return QUrl.fromLocalFile(link) has_schema = re.match(r'^[a-zA-Z]+:', link) if has_schema is not None: url = QUrl(link, QUrl.TolerantMode) if url.isValid(): return url if os.path.exists(link): return QUrl.fromLocalFile(link) if has_schema is None: first, _, rest = link.partition('.') prefix = 'http' if first == 'ftp': prefix = 'ftp' url = QUrl(prefix +'://'+link, QUrl.TolerantMode) if url.isValid(): return url return QUrl(link, QUrl.TolerantMode)