Ejemplo n.º 1
0
    def search(self, description):
        if description.help_ref:
            ref = description.help_ref
        else:
            raise KeyError()

        url = QUrl(self.baseurl).resolved(QUrl(ref))
        if url.isLocalFile():
            path = url.toLocalFile()
            fragment = url.fragment()
            if os.path.isfile(path):
                return url
            elif os.path.isfile("{}.html".format(path)):
                url = QUrl.fromLocalFile("{}.html".format(path))
                url.setFragment(fragment)
                return url
            elif os.path.isdir(path) and \
                    os.path.isfile(os.path.join(path, "index.html")):
                url = QUrl.fromLocalFile(os.path.join(path, "index.html"))
                url.setFragment(fragment)
                return url
            else:
                raise KeyError()
        else:
            if url.scheme() in ["http", "https"]:
                path = url.path()
                if not (path.endswith(".html") or path.endswith("/")):
                    url.setPath(path + ".html")
        return url
Ejemplo n.º 2
0
 def data(self):
     """Return all the data for this model (including dependencies) in a structured dict
     """
     empty_template = '{}'
     param_map = {PATH: 'Path', GET: 'GET', POST: 'POST', COOKIE: 'Cookie'}
     url = QUrl(self.transition.url)
     path_dict = path_to_dict(url.path())
     get_keys, post_keys = set(), set()
     output = {}
     if self.override:
         output['variables'] = []
         for param_type, key, template, parent_model in self.override:
             parent = parent_model if isinstance(
                 parent_model, basestring) else parent_model.data()
             output['variables'].append({
                 'key': key,
                 'origin': param_map[param_type],
                 'source': parent,
                 'template': template,
             })
             if param_type == PATH:
                 path_dict[key] = empty_template
             elif param_type == GET:
                 get_keys.add(key)
             elif param_type == POST:
                 post_keys.add(key)
     url.setPath(dict_to_path(path_dict))
     qs_items = [(key, empty_template if key in get_keys else value)
                 for (key, value) in url.queryItems()
                 if (GET, key) not in self.ignored]
     url.setEncodedQueryItems(qs_items)
     output['url'] = url.toString()
     data = dict([(key, empty_template if key in post_keys else value)
                  for (key, value) in self.transition.data
                  if (POST, key) not in self.ignored])
     if data:
         output['data'] = data
     #if self.ignored:
     #    output['ignored'] = [(param_map[param_type], value) for (param_type, value) in self.ignored]
     if self.selector is not None:
         output['columns'] = {'data': str(self.selector)}
     elif self.columns is not None:
         output['columns'] = {
             field: str(selector)
             for (field, selector) in self.columns.items()
         }
     output['headers'] = [(str(key), str(value))
                          for (key,
                               value) in self.transition.request_headers
                          if str(key).lower() not in ('content-length', )]
     output['verb'] = self.transition.verb
     return output
Ejemplo n.º 3
0
    def show_change_log(self, path):
        """
        Show STDM change log window if the user have never
        seen it before.
        :return: None
        :rtype: NoneType
        """
        change_log_path = '{}/html/change_log.htm'.format(path)
        change_log_url = QUrl()
        change_log_url.setPath(change_log_path)
        change_log_html = file_text(change_log_path)

        self.webView.setHtml(change_log_html, change_log_url)

        self.exec_()
Ejemplo n.º 4
0
 def get_authorization_url(self, scopes):
     host, port = config.setting['server_host'], config.setting['server_port']
     url = QUrl()
     if (host in MUSICBRAINZ_SERVERS and port == 80) or port == 443:
         url.setScheme("https")
     else:
         url.setScheme("http")
         if port != 80:
             url.setPort(port)
     url.setHost(host)
     url.setPath("/oauth2/authorize")
     url.addQueryItem("response_type", "code")
     url.addQueryItem("client_id", MUSICBRAINZ_OAUTH_CLIENT_ID)
     url.addQueryItem("redirect_uri", "urn:ietf:wg:oauth:2.0:oob")
     url.addQueryItem("scope", scopes)
     return str(url.toEncoded())
Ejemplo n.º 5
0
    def show_change_log(self, path):
        """
        Show STDM change log window if the user have never
        seen it before.
        :return: None
        :rtype: NoneType
        """
        change_log_path = '{}/html/change_log.htm'.format(path)
        change_log_url = QUrl()
        change_log_url.setPath(change_log_path)
        change_log_html = file_text(change_log_path)

        self.webView.setHtml(
            change_log_html, change_log_url
        )

        self.exec_()
Ejemplo n.º 6
0
def gen_request(transition, override_params=None, ignored=None):
    """Generate a request modifying the transitions for this model with the provided parameters
    
    Parameters
    ----------
    transition: Transition.
            A specific transition to use rather than the first one for this model
    override_params: a list of (param_type, key, value) pairs to override the parameters for this transition
    ignored: list of (param_type, key)
        a list of (param_type, key) pairs of parameters that can be left out
    
    Returns
    -------
        dictionary with data sufficient for sending a query in the web browser
    
    """
    override_params = override_params or []
    ignored = ignored or []
    url = QUrl(transition.url)
    get_dict = dict([(key, value)
                     for (param_type, key, value) in override_params
                     if param_type == GET])
    post_dict = dict([(key, value)
                      for (param_type, key, value) in override_params
                      if param_type == POST])
    path_dict = path_to_dict(transition.path)
    for param_type, key, value in override_params:
        if param_type == PATH:
            path_dict[key] = value
    url.setPath(dict_to_path(path_dict))
    qs_items = transition.qs
    data_items = transition.data

    qs_items = [(key, urllib.quote_plus(get_dict[key].encode('utf-8'))
                 if key in get_dict else value) for (key, value) in qs_items
                if (GET, key) not in ignored]
    url.setEncodedQueryItems(qs_items)
    data_items = [(key, post_dict[key] if key in post_dict else value)
                  for (key, value) in data_items if (POST, key) not in ignored]
    return dict(url=url,
                headers=transition.request_headers,
                data=encode_data(data_items, transition.content_type))
Ejemplo n.º 7
0
    def urlFromValue(self, value):
        variable = value.variable
        origin = variable.attributes.get("origin", "")
        if origin and QDir(origin).exists():
            origin = QUrl.fromLocalFile(origin)
        elif origin:
            origin = QUrl(origin)
            if not origin.scheme():
                origin.setScheme("file")
        else:
            origin = QUrl("")
        base = origin.path()
        if base.strip() and not base.endswith("/"):
            origin.setPath(base + "/")

        name = QUrl(str(value))
        url = origin.resolved(name)
        if not url.scheme():
            url.setScheme("file")
        return url
Ejemplo n.º 8
0
    def urlFromValue(self, value):
        variable = value.variable
        origin = variable.attributes.get("origin", "")
        if origin and QDir(origin).exists():
            origin = QUrl.fromLocalFile(origin)
        elif origin:
            origin = QUrl(origin)
            if not origin.scheme():
                origin.setScheme("file")
        else:
            origin = QUrl("")
        base = origin.path()
        if base.strip() and not base.endswith("/"):
            origin.setPath(base + "/")

        name = QUrl(str(value))
        url = origin.resolved(name)
        if not url.scheme():
            url.setScheme("file")
        return url
Ejemplo n.º 9
0
    def setContentList(self):

        url = QUrl(self.url())
        if not url.path().endsWith(u"/"):
            url.setPath(url.path() + u"/")

        base_url = self.url().toString()
        base_path = (url.path())

        self.open(self.ReadOnly | self.Unbuffered)
        content = (u"<html>\n"
                   u"<head>\n"
                   u"  <title>" + self.html(base_url) + u"</title>\n"
                   u'  <style type="text/css">\n'
                   u"  th { background-color: #aaaaaa;\n"
                   u"       color: black }\n"
                   u"  table { border: solid 1px #aaaaaa }\n"
                   u"  tr.odd { background-color: #dddddd;\n"
                   u"           color: black\n }\n"
                   u"  tr.even { background-color: white;\n"
                   u"           color: black\n }\n"
                   u"  </style>\n"
                   u"</head>\n\n"
                   u"<body>\n"
                   u"<h1>Listing for " + base_path + u"</h1>\n\n")

        lines = self.input.splitlines()

        for line in lines:

            pieces = line.split("\t")
            if pieces == ["."]:
                break
            try:
                type, path, host, port = pieces[:4]
            except ValueError:
                # This isn't a listing. Let's try returning data instead.
                self.setContentData()
                return

            if type[0] == "i":
                content += u"<p>" + self.html(type[1:]) + u"</p>"
            elif type[0] == "h" and path.startswith(u"URL:"):
                content += u"<p><a href=\"" + path[4:] + u"\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "0":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=text\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "1":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "4":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=binhex\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "5":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=dos\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "6":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=uuencoded\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "9":
                content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html(
                    type[1:]) + u"</a></p>"
            elif type[0] == "g":
                content += u"<img src=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html(
                    type[1:]) + u"</img>"
            elif type[0] == "I":
                content += u"<img src=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html(
                    type[1:]) + u"</img>"

        content += (u"</body>\n" u"</html>\n")

        self.content = content.encode("utf-8")

        self.setHeader(QNetworkRequest.ContentTypeHeader,
                       QVariant("text/html; charset=UTF-8"))
        self.setHeader(QNetworkRequest.ContentLengthHeader,
                       QVariant(len(self.content)))
        self.readyRead.emit()
        self.finished.emit()