Exemplo n.º 1
0
def qstring_to_unicode(qstr):
    """Convert QString to unicode
    """
    if isinstance(qstr, unicode):
        return qstr
    else:
        return common.to_unicode(qstr.toUtf8().data(), 'utf-8')
Exemplo n.º 2
0
    def createRequest(self, operation, request, data):
        if operation == self.GetOperation:
            if self.is_forbidden(request):
                # deny GET request for banned media type by setting dummy URL
                # XXX abort properly
                request.setUrl(QUrl(QString('forbidden://localhost/')))
            else:
                common.logger.debug(
                    common.to_unicode(
                        request.url().toString().toUtf8().data()).encode(
                            'utf-8'))

        #print request.url().toString(), operation
        request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                             QNetworkRequest.PreferCache)
        reply = QNetworkAccessManager.createRequest(self, operation, request,
                                                    data)
        reply.error.connect(self.catch_error)

        #add Base-Url header, then we can get it from QWebView
        if isinstance(request.originatingObject(), QWebFrame):
            try:
                reply.setRawHeader(
                    QByteArray('Base-Url'),
                    QByteArray('').append(request.originatingObject().page().
                                          mainFrame().baseUrl().toString()))
            except Exception, e:
                common.logger.debug(e)
Exemplo n.º 3
0
 def __init__(self, reply):
     # save shortcuts to URL details
     self.url = reply.url()
     self.host = self.url.host()
     self.path = self.url.path()
     self.qs = self.url.queryItems()
     self.data = reply.data
     self.content_type = reply.content_type
     self.content = common.to_unicode(str(reply.content))
     try:
         self.parsed_content = parser.parse(self.content, self.content_type)
     except ValueError as e:
         print 'Error parsing URL with lxml: {}'.format(self.url.toString())
         self.parsed_content = None
     self.columns = None
     self.cookies = QNetworkCookie.parseCookies(reply.rawHeader('Set-Cookie'))
     # map of Qt verbs
     verbs = {
         QNetworkAccessManager.HeadOperation: 'HEAD',
         QNetworkAccessManager.GetOperation: 'GET',
         QNetworkAccessManager.PutOperation: 'PUT',
         QNetworkAccessManager.PostOperation: 'POST',
         QNetworkAccessManager.DeleteOperation: 'DELETE',
         QNetworkAccessManager.CustomOperation: 'CUSTOM',
     }
     self.verb = verbs[reply.operation()]
     # save request details
     request = reply.orig_request
     self.request_headers = [(header, request.rawHeader(header)) for header in request.rawHeaderList()]
     self.response_headers = [(header, request.rawHeader(header)) for header in reply.rawHeaderList()]
Exemplo n.º 4
0
def qstring_to_unicode(qstr):
    """Convert QString to unicode
    """
    if isinstance(qstr, unicode):
        return qstr
    else:
        return common.to_unicode(qstr.toUtf8().data(), 'utf-8')
    def __ge__(self, other):
        """
        :type self: service_message
        :type other: service_message
        :rtype: bool
        """
        if self.name != other.name:
            return False

        for p in other.params:
            if p in self.params:
                v1 = self.params[p]
                v2 = other.params[p]
                if to_unicode(v1) != to_unicode(v2):
                    return False
            else:
                return False
        return True
    def __ge__(self, other):
        """
        :type self: service_message
        :type other: service_message
        :rtype: bool
        """
        if self.name != other.name:
            return False

        for p in other.params:
            if p in self.params:
                v1 = self.params[p]
                v2 = other.params[p]
                if to_unicode(v1) != to_unicode(v2):
                    return False
            else:
                return False
        return True
Exemplo n.º 7
0
 def save(self):
     """Save the current HTML state to disk
     """
     for i in itertools.count(1):
         filename = os.path.join(OUTPUT_DIR, 'state{}.html'.format(i))
         if not os.path.exists(filename):
             html = self.current_html()
             open(filename, 'w').write(common.to_unicode(html))
             print 'save', filename
             break
Exemplo n.º 8
0
 def is_forbidden(self, request):
     """Returns whether this request is permitted by checking URL extension and regex
     XXX head request for mime?
     """
     forbidden = False
     url = common.to_unicode(request.url().toString().toUtf8().data()).encode('utf-8')
     if common.get_extension(url) in self.banned_extensions:
         forbidden = True
     elif re.match(self.allowed_regex, url) is None:
         forbidden = True
     return forbidden
Exemplo n.º 9
0
Arquivo: v5.py Projeto: w4lker/Antix
 def is_forbidden(self, request):
     """Returns whether this request is permitted by checking URL extension and regex
     XXX head request for mime?
     """
     forbidden = False
     url = common.to_unicode(request.url().url())
     if common.get_extension(url) in self.banned_extensions:
         forbidden = True
     elif re.match(self.allowed_regex, url) is None:
         forbidden = True
     return forbidden
Exemplo n.º 10
0
def load_data():
    """Load sample vertical data into a dictionary with a key for each column
    """
    common.logger.debug('Start loading verticals data')
    global vertical_data
    for filename in glob.glob('verticals/*.csv'):
        for row in csv.reader(open(filename)):
            for i, v in enumerate(row):
                if v:
                    vertical_data['{}-field{}'.format(
                        filename, i)][hash_key(v)] = common.to_unicode(v)
    common.logger.debug('Completed loading verticals data')
Exemplo n.º 11
0
    def convert(self, intext):
        """Fill the stage from a commands file (given as text).

        Arguments:
            intext (str): Content of the commands file.
        """
        init_size = len(self._stg)
        intext = to_unicode(intext)
        try:
            eoi, text = change_text(intext, self._strict)
        except Exception as exc: # pragma pylint: disable=broad-except
            details = traceback.format_exc()
            raise ConversionError(exc, details, '?', '?')
        text = "{0}\n_post_conversion() # {1}\n".format(text, MARK)
        self._exec_ctxt = self._setup()
        try:
            exec text in self._exec_ctxt # pragma pylint: disable=exec-used
        except Exception as exc: # pragma pylint: disable=broad-except
            self._teardown()
            details = traceback.format_exc()
            eoprev, lineno, line = _locate_error(exc, eoi, text)
            if self._strict & ConversionLevel.Partial:
                debug_message("Conversion failed, split after line", eoprev)
                # remove the unfinished command
                if self._unfinish:
                    del self._stg[self._unfinish]
                # _post_conversion was not called
                try:
                    self._post_conversion()
                except Exception: # pragma pylint: disable=broad-except
                    # some commands have not be named...
                    pass
                part1, astext = split_text(intext, eoprev, remove=MARK)
                # debug_message("+" * 50)
                # debug_message(part1)
                # debug_message("-" * 50)
                # debug_message(astext)
                # debug_message("=" * 50)
                # in case of SyntaxError, nothing has been added
                if isinstance(exc, SyntaxError):
                    debug_message("SyntaxError: convert first part separately")
                    comm2study(part1, self._stg, self._strict)
                if astext:
                    self.add_text_stage(astext)
            else:
                # revert the input stage as it was
                while len(self._stg) > init_size:
                    del self._stg[-1]
                raise ConversionError(exc, details, lineno, line)
        finally:
            self._teardown()
Exemplo n.º 12
0
    def export(self, file_name):
        """
        Export Stage to a COMM file.

        Arguments:
            file_name (str): Path to the COMM file.
        """
        from common import to_unicode
        utext = to_unicode(self.get_text(enclosed=True))
        if not utext.endswith('\n'):
            utext += '\n'

        with open(file_name, 'w') as handle:
            handle.write(to_str(utext))
            handle.flush()
Exemplo n.º 13
0
 def createRequest(self, operation, request, data):
     if operation == self.GetOperation:
         if self.is_forbidden(request):
             # deny GET request for banned media type by setting dummy URL
             # XXX abort properly
             request.setUrl(QUrl('forbidden://localhost/'))
         else:
             common.logger.debug(common.to_unicode(request.url().toString()).encode('utf-8'))
     
     #print request.url().toString(), operation
     request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache)
     reply = QNetworkAccessManager.createRequest(self, operation, request, data)
     reply.error.connect(self.catch_error)
     
     # add Base-Url header, then we can get it from QWebView
     if isinstance(request.originatingObject(), QWebFrame):
         try:
             reply.setRawHeader(QByteArray('Base-Url'), QByteArray('').append(request.originatingObject().page().mainFrame().baseUrl().toString()))
         except Exception, e:
             common.logger.debug(e)
Exemplo n.º 14
0
    def refresh(self):
        """Refresh state of currently processed (calculated) result."""
        if self.is_finished() or not self.is_started():
            return
        job = self.current.job
        res = self.hdlr.getJobState(job.jobid_int)
        self.current.state = convert_launcher_state(res)
        debug_message2('Job {0}: status is {1}: {2}'
                       .format(job.jobid, res, SO.name(self.current.state)))
        if self.current.state & SO.Finished:
            job.end_time = current_time()
            self.get_job_results(job)
            # parse message file if it exists
            stage = self.current_stage
            output = glob(osp.join(stage.folder, "logs", "command_*.log"))
            if output:
                self.current.state = convert_state_from_message(res, output[0])
                with open(output[0], 'rb') as fileout:
                    text = to_unicode(fileout.read())
                    self.current.add_messages(extract_messages(text))

            self.save_jobs()
            self._update_result()
            # refresh next if any
            self.refresh()
        else:
            self.console("\nLast {0} lines at {1}..."
                         .format(self._nbline, current_time()))
            salome_job = self.hdlr.getJobParameters(job.jobid_int)
            text = remote_tail(self._infos.server_username(job.server),
                               self._infos.server_hostname(job.server),
                               osp.join(salome_job.work_directory,
                                        "logs", "command_*.log"),
                               self._nbline)
            self.current.add_messages(extract_messages(text))
            self.console(text)
 def as_unicode(self):
     buf = "[" + self.name
     for k, v in self.params.items():
         buf += ' ' + k + "='" + v + "'"
     buf += "]"
     return to_unicode(buf)
Exemplo n.º 16
0
 def current_text(self):
     """Return text from the current rendered HTML
     """
     return common.to_unicode(unicode(
         self.page().mainFrame().toPlainText()))
Exemplo n.º 17
0
def _text2unicode(text):
    """Conditionally convert text to unicode."""
    if behavior().editor_use_unicode:
        return to_unicode(text)
    else:
        return text
Exemplo n.º 18
0
 def _convert(item):
     if isinstance(item, list):
         return [to_unicode(i.value) for i in item]
     if item.filename:
         return MultipartFile(item)
     return to_unicode(item.value)
Exemplo n.º 19
0
 def __init__(self, storage):
     self.filename = to_unicode(storage.filename)
     self.file = storage.file
Exemplo n.º 20
0
 def as_unicode(self):
     buf = "[" + self.name
     for k, v in self.params.items():
         buf += ' ' + k + "='" + v + "'"
     buf += "]"
     return to_unicode(buf)
Exemplo n.º 21
0
 def current_html(self):
     """Return current rendered HTML
     """
     return common.to_unicode(unicode(self.page().mainFrame().toHtml()))