Example #1
0
def getAssayFromSIDs(AID, SIDs=[]):
    """
  Returns a pandas DataFrame containing the assay data for AID. This is useful
  when an assay has more than 10000 associated SIDs and can't be retrieved with
  getAssayFromAID due to PUG data restrictions.  SIDs can be a list of the 
  prefetched SIDs for the assay or it can be an empty list, in which case the 
  SIDs for the given assay will be fetched automatically.
  @param AID  The AID to search on.
  @param SIDs  The SIDs for the given AID
  """
    response = ""
    pos = 0
    groupSz = 9999
    if len(SIDs) < 1:
        SIDs = getSIDsFromAID(AID)
    while pos < len(SIDs):
        url = PROLOG + "/assay/aid/CSV"
        payload = {'aid': AID, 'sid': ",".join(SIDs[pos:pos + groupSz])}
        pos = pos + groupSz + 1
        if len(response) == 0:
            response += post(url, payload)
        else:
            data = post(url, payload)
            response += data[data.index('\n'):]
    response = StringIO(response.encode(ENCODING))
    return pd.DataFrame.from_csv(response, index_col=False, header=0)
Example #2
0
    def __call__(self):
        pdf = StringIO()
        html = self._render_template()
        html = StringIO(html.encode('utf-8'))

        # Construct a trojan horse and hide context inside it
        path = ContextString(self.context.absolute_url())
        path.context = self.context

        # Generate the pdf
        pisadoc = pisa.CreatePDF(html, pdf, path=path, encoding='utf-8',
                                 raise_exception=False)
        assert pdf.len != 0, 'Pisa PDF generation returned empty PDF!'
        html.close()
        pdfcontent = pdf.getvalue()
        pdf.close()

        self.request.response.setHeader("Content-Disposition",
                                        "attachment; filename=assessment-%s.pdf" 
                                         % self.context.id)
        self.request.response.setHeader("Content-Type", "application/pdf")
        self.request.response.setHeader("Content-Length", len(pdfcontent))
        self.request.response.setHeader("Cache-Control", "no-store")
        self.request.response.setHeader("Pragma", "no-cache")
        self.request.response.write(pdfcontent)
        return pdfcontent
Example #3
0
    def read(self, request):
        """Method to get records from database
        """
        try:
            
            colname,dbname,collection,database,recno = get_database_info(request)

            options = get_form_parameters(request)    
            records = database[options.start:options.end]
            
            if options.reverse:
                records.reverse()
            
            result = StringIO()
            for field in records:
                result.write(field.to_xml())

            result = result.getvalue() or XML_EMPTY
            
            result = XML_HEADER % database.config.OUTPUT_ENCODING + result
            #FIXME: apply gizmo only data    
            return HttpResponse(result.encode(options.gizmo))
        
        except Exception, e:
            return HttpResponse(XML_ERROR % str(e))
Example #4
0
 def hashScore(self):
     exporter = AsciiExport.Exporter(self, ASCIISettings.ASCIISettings(),
                                     False)
     scoreString = StringIO()
     exporter.export(scoreString)
     scoreString = scoreString.getvalue()
     return hashlib.md5(scoreString.encode('utf-8')).digest()
Example #5
0
def tweets(text):
    tweets_list = []
    if not isinstance(text, StringIO):
        text = StringIO(text.encode('UTF-8'))
    for line in text.readlines():
        tokens = tokenize(line)
        if len(tokens) > 0:
            tweets_list.extend(tokens)
    else:
        return tweets_list
Example #6
0
    def tocsv(self, encoding='unicode'):
        out = StringIO()
        c = csv.writer(out, dialect=csv.excel)

        for item in self.items:
            c.writerow([getattr(item, x) for x in item.dic])

        out.seek(0)
        out = out.read()

        if encoding != 'unicode':
            out = out.encode(encoding)

        return out
Example #7
0
 def to_pdf(self, html):
     """Converts a html in pdf file"""
     html = StringIO(html.encode('utf-8'))
     return self.converter.convert(html)
Example #8
0
 def to_pdf(self, html):
     """Converts a html in pdf file"""
     html = StringIO(html.encode('utf-8'))
     return self.converter.convert(html)
Example #9
0
 def hashScore(self):
     exporter = AsciiExport.Exporter(self, ASCIISettings.ASCIISettings(), False)
     scoreString = StringIO()
     exporter.export(scoreString)
     scoreString = scoreString.getvalue()
     return hashlib.md5(scoreString.encode('utf-8')).digest()
Example #10
0
def run_cli_native(verb, *args, **kwargs):
    """
    Run a Tahoe-LAFS CLI command specified as bytes (on Python 2) or Unicode
    (on Python 3); basically, it accepts a native string.

    Most code should prefer ``run_cli_unicode`` which deals with all the
    necessary encoding considerations.

    :param runner.Options options: The options instance to use to parse the
        given arguments.

    :param native_str verb: The command to run.  For example,
        ``"create-node"``.

    :param [native_str] args: The arguments to pass to the command.  For
        example, ``("--hostname=localhost",)``.

    :param [native_str] nodeargs: Extra arguments to pass to the Tahoe
        executable before ``verb``.

    :param bytes|unicode stdin: Text or bytes to pass to the command via stdin.

    :param NoneType|str encoding: The name of an encoding which stdout and
        stderr will be configured to use.  ``None`` means matching default
        behavior for the given Python version.

    :param bool return_bytes: If False, stdout/stderr is native string,
        matching native behavior.  If True, stdout/stderr are returned as
        bytes.
    """
    options = kwargs.pop("options", runner.Options())
    nodeargs = kwargs.pop("nodeargs", [])
    encoding = kwargs.pop("encoding", None) or getattr(sys.stdout,
                                                       "encoding") or "utf-8"
    return_bytes = kwargs.pop("return_bytes", False)
    verb = maybe_unicode_to_argv(verb)
    args = [maybe_unicode_to_argv(a) for a in args]
    nodeargs = [maybe_unicode_to_argv(a) for a in nodeargs]
    precondition(
        all(
            isinstance(arg, argv_type)
            for arg in [verb] + nodeargs + list(args)),
        "arguments to run_cli must be {argv_type} -- convert using unicode_to_argv"
        .format(argv_type=argv_type),
        verb=verb,
        args=args,
        nodeargs=nodeargs,
    )
    argv = ["tahoe"] + nodeargs + [verb] + list(args)
    stdin = kwargs.get("stdin", "")
    if PY2:
        # The original behavior, the Python 2 behavior, is to accept either
        # bytes or unicode and try to automatically encode or decode as
        # necessary.  This works okay for ASCII and if LANG is set
        # appropriately.  These aren't great constraints so we should move
        # away from this behavior.
        #
        # The encoding attribute doesn't change StringIO behavior on Python 2,
        # but it's there for realism of the emulation.
        stdin = StringIO(stdin)
        stdin.encoding = encoding
        stdout = StringIO()
        stdout.encoding = encoding
        stderr = StringIO()
        stderr.encoding = encoding
    else:
        # The new behavior, the Python 3 behavior, is to accept unicode and
        # encode it using a specific encoding. For older versions of Python 3,
        # the encoding is determined from LANG (bad) but for newer Python 3,
        # the encoding is either LANG if it supports full Unicode, otherwise
        # utf-8 (good). Tests can pass in different encodings to exercise
        # different behaviors.
        if isinstance(stdin, str):
            stdin = stdin.encode(encoding)
        stdin = TextIOWrapper(BytesIO(stdin), encoding)
        stdout = TextIOWrapper(BytesIO(), encoding)
        stderr = TextIOWrapper(BytesIO(), encoding)
    d = defer.succeed(argv)
    d.addCallback(
        partial(
            runner.parse_or_exit,
            options,
        ),
        stdout=stdout,
        stderr=stderr,
    )
    d.addCallback(
        runner.dispatch,
        stdin=stdin,
        stdout=stdout,
        stderr=stderr,
    )

    def _done(rc, stdout=stdout, stderr=stderr):
        if return_bytes and PY3:
            stdout = stdout.buffer
            stderr = stderr.buffer
        return 0, _getvalue(stdout), _getvalue(stderr)

    def _err(f, stdout=stdout, stderr=stderr):
        f.trap(SystemExit)
        if return_bytes and PY3:
            stdout = stdout.buffer
            stderr = stderr.buffer
        return f.value.code, _getvalue(stdout), _getvalue(stderr)

    d.addCallbacks(_done, _err)
    return d