Example #1
0
def main():
    suite = unittest.TestLoader().loadTestsFromTestCase(TestPothosModule)
    sio = StringIO()
    result = unittest.TextTestRunner(stream=sio, verbosity=2).run(suite)
    print(sio.getvalue())
    if not result.wasSuccessful():
        raise Exception("unittest FAIL")
Example #2
0
 def checkRoundtrip(self, t):
     s = StringIO()
     t.write(s)
     s.seek(0)
     t2 = xpt.Typelib.read(s)
     self.assert_(t2 is not None)
     self.assertEqualTypelibs(t, t2)
Example #3
0
def readZip(f, **kwargs):
    from mien.parsers.nmpml import elements as dialect

    f = ZipFile(f, "r")
    xml = f.read("xml")
    xml = StringIO(xml)
    doc = xm.readTree(xml)
    xml.close()
    doc = xm.assignClasses(doc, dialect)
    try:
        dat = f.read("data")
    except:
        print "No data archive in zip file"
        return doc
    from mien.parsers.datahash import readMD

    dat = StringIO(dat)
    dat = readMD(dat, return_raw_hash=True)
    des = doc.getElements("Data")
    for de in des:
        try:
            d, h = dat[de.upath()]
        except:
            print "can't find data for element %s" % (de.upath(),)
            d, h = (zeros(0), {})
        de.datinit(d, h)
    f.close()
    return doc
Example #4
0
File: handler.py Project: moul/junk
    def send(self, content="", code=200, message="OK", headers={}):
        if not "Content-Type" in headers:
            headers["Content-Type"] = "text/html; charset=utf-8"

        # headers['Expires'] = 'Sun, 25 Jan 1988 13:15:00 GMT'
        # headers['Last-Modified'] = 'Sun, 25 Jan 1988 13:15:00 GMT'
        # headers['Cache-Control'] = 'must-revalidate'
        # headers['X-Powered-By'] = 'jambe'

        headers["Content-Length"] = len(content)
        if "gzip" in self.headers.get("Accept-Encoding", "none").split(","):
            from StringIO import StringIO
            import gzip

            headers["Content-Encoding"] = "gzip"
            f = StringIO()
            gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
            gzf.write(content)
            gzf.close()
            content = f.getvalue()
            f.close()
            new_content_length = len(content)
            self.log_message("Gzip: old-length=%s, new-length=%s" % (headers["Content-Length"], new_content_length))
            headers["Content-Length"] = new_content_length

        self.send_response(code, message)
        for key in headers:
            self.send_header(key, headers[key])
        self.end_headers()

        if self.command != "HEAD" and code >= 200 and code not in (204, 304):
            self.wfile.write(content)
Example #5
0
 def export(self):
     self.save()
     self.merge()
     io = StringIO()
     self.output.write(io)
     self.close()
     return io.getvalue()
Example #6
0
    def _send_command(self, command, host, port, timeout):
        sock = socket.socket()
        sock.settimeout(timeout)
        buf = StringIO()
        chunk_size = 1024
        # try-finally and try-except to stay compatible with python 2.4
        try:
            try:
                # Connect to the zk client port and send the stat command
                sock.connect((host, port))
                sock.sendall(command)

                # Read the response into a StringIO buffer
                chunk = sock.recv(chunk_size)
                buf.write(chunk)
                num_reads = 1
                max_reads = 10000
                while chunk:
                    if num_reads > max_reads:
                        # Safeguard against an infinite loop
                        raise Exception("Read %s bytes before exceeding max reads of %s. " % (buf.tell(), max_reads))
                    chunk = sock.recv(chunk_size)
                    buf.write(chunk)
                    num_reads += 1
            except (socket.timeout, socket.error):
                raise ZKConnectionFailure()
        finally:
            sock.close()
        return buf
Example #7
0
        def leessheet(self, loc):
            """Lees de data uit een sheet.

            Dit leest de configuratie uit het sheet loc. retourneert een filepointer-achtige StringIO(). Dit is op zich
            een overbodige kopie, maarach. Het idee is toch dat deze funcie maar een keert wordt uitgevoerd.
            """
            s = Sheet_ro(loc, 0, 0, 4, laatsteboeking(loc))
            f = StringIO()
            for r in s.data:
                if r[0] == "lid":
                    try:

                        try:
                            naam = r[2].strip('"')
                        except:
                            naam = ""
                        try:
                            woonplaats = r[3].strip('"')
                        except:
                            woonplaats = ""
                        try:
                            reknr = int(r[4])
                        except:
                            reknr = str(r[4])

                        f.write("%s,%s,%s,%s,%s\n" % (r[0], r[1], naam, woonplaats, reknr))
                    except Exception, e:
                        print e
                        raise Fout("mogelijk probleem met missende rijen in relatie bestand")
                else:
                    try:
                        f.write("%s,%s\n" % (r[0], r[1]))
                    except Exception, e:
                        print e
                        raise Fout("mogelijk probleem met missende rijen in relatie bestand")
Example #8
0
    def test_multi_line(self):
        block = """
    if test:
        print ''' this is a block of stuff.
this is more stuff in the block.
and more block.
'''
        do_more_stuff(g)
"""
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.write_indented_block(block)
        printer.close()
        # print stream.getvalue()
        assert (
            stream.getvalue()
            == """
if test:
    print ''' this is a block of stuff.
this is more stuff in the block.
and more block.
'''
    do_more_stuff(g)

"""
        )
Example #9
0
 def __str__(self):
     out = StringIO()
     alignment = self.alignment
     tree = self.tree
     # write the taxa block
     print >> out, "#NEXUS"
     print >> out, ""
     for comment in self.comments:
         print >> out, "[%s]" % comment
     print >> out, ""
     print >> out, "BEGIN TAXA;"
     print >> out, "  DIMENSIONS ntax = %d;" % len(alignment.headers)
     print >> out, "  TAXLABELS %s;" % " ".join(alignment.headers)
     print >> out, "END;"
     # write the tree block
     print >> out, ""
     print >> out, "BEGIN TREES;"
     print >> out, "  TREE primates = %s" % tree.get_newick_string()
     print >> out, "END;"
     # write the alignment block
     print >> out, ""
     print >> out, "BEGIN CHARACTERS;"
     print >> out, "  DIMENSIONS nchar = %d;" % len(alignment.columns)
     print >> out, "  FORMAT datatype = DNA;"
     print >> out, "  MATRIX"
     max_header_length = max(len(header) for header in alignment.headers)
     for header, sequence in zip(alignment.headers, alignment.sequences):
         print >> out, "    %s %s" % (Monospace.left_justify(header, max_header_length, " "), sequence)
     print >> out, "  ;"
     print >> out, "END;"
     return out.getvalue()
class DummyController:
    nohelp = "no help on %s"

    def __init__(self, options):
        self.options = options
        self.topics_printed = []
        self.stdout = StringIO()

    def upcheck(self):
        return True

    def get_supervisor(self):
        return self.get_server_proxy("supervisor")

    def get_server_proxy(self, namespace=None):
        proxy = self.options.getServerProxy()
        if namespace is None:
            return proxy
        else:
            return getattr(proxy, namespace)

    def output(self, data):
        self.stdout.write(data + "\n")

    def print_topics(self, doc_headers, cmds_doc, rows, cols):
        self.topics_printed.append((doc_headers, cmds_doc, rows, cols))
Example #11
0
    def test_backslash_line(self):
        block = """
            # comment
    if test:
        if (lala + hoho) + \\
(foobar + blat) == 5:
            print "hi"
    print "more indent"
"""
        stream = StringIO()
        printer = PythonPrinter(stream)
        printer.write_indented_block(block)
        printer.close()
        assert (
            stream.getvalue()
            == """
            # comment
if test:
    if (lala + hoho) + \\
(foobar + blat) == 5:
        print "hi"
print "more indent"

"""
        )
Example #12
0
    def importXLS(self, action):
        """ Create and handle form button."""

        # Extract form field values and errors from HTTP request
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        try:
            io = StringIO(data["xls_file"].data)
            workbook = xlrd.open_workbook(file_contents=io.read())
        except (KeyError, TypeError, xlrd.XLRDError):
            self.abort_action(action, (_(u"Invalid XLS file"),))
            return

        errors = []
        report_error = lambda err: errors.append(err)

        records = len(import_xls(self.context, workbook, report_error))

        if errors:
            self.abort_action(action, errors)
        else:
            self.status = _(u"Imported ${number} items", mapping={u"number": records})
 def tell(self):
     with self.lock:
         pos = StringIO.tell(self)
         if pos < len(self.getvalue()) - 1:
             return pos
     self.done.wait()
     return StringIO.tell(self)
Example #14
0
    def test_view_zip(self):
        """ Custom view of zipfile """
        from zipfile import ZipFile

        zfile = open(os.path.join(os.path.dirname(__file__), "fixtures", "one_file_zip.zip"), "rb")

        upload_file = StringIO(zfile.read())
        upload_file.filename = "test.zip"
        upload_file.headers = {"content-type": "application/zip"}

        namelist = ZipFile(zfile).namelist()
        zfile.close()
        addNyBFile(
            self.portal.myfolder,
            submitted=1,
            contributor="contributor",
            id="test",
            title="test",
            uploaded_file=upload_file,
        )
        transaction.commit()

        # The result is a simple html with the contents of the zipfile (filepaths)
        self.browser.go("http://localhost/portal/myfolder/test/download/1/test.zip?action=view")
        html_content = self.browser.get_html()

        for filepath in namelist:
            self.assertTrue(filepath in html_content)
        zfile.close()
Example #15
0
    def __str__(self):
        out = StringIO()
        print >> out, "## LogoData"
        print >> out, "# First column is position number, counting from zero"
        print >> out, "# Subsequent columns are raw symbol counts"
        print >> out, "# Entropy is mean entropy measured in nats."
        print >> out, "# Low and High are the 95% confidence limits."
        print >> out, "# Weight is the fraction of non-gap symbols in the column."
        print >> out, "#\t"
        print >> out, "#\t",
        for a in self.alphabet:
            print >> out, a, "\t",
        print >> out, "Entropy\tLow\tHigh\tWeight"

        for i in range(self.length):
            print >> out, i + 1, "\t",
            for c in self.counts[i]:
                print >> out, c, "\t",
            print >> out, "%6.4f" % self.entropy[i], "\t",
            if self.entropy_interval is not None:
                print >> out, "%6.4f" % self.entropy_interval[i][0], "\t",
                print >> out, "%6.4f" % self.entropy_interval[i][1], "\t",
            else:
                print >> out, "\t", "\t",
            if self.weight is not None:
                print >> out, "%6.4f" % self.weight[i],
            print >> out, ""
        print >> out, "# End LogoData"

        return out.getvalue()
Example #16
0
    def runTest(self):
        test = self._dt_test
        runner = self._dt_runner

        old = sys.stdout
        new = StringIO()
        optionflags = self._dt_optionflags

        if not (optionflags & REPORTING_FLAGS):
            # The option flags don't include any reporting flags,
            # so add the default reporting flags
            optionflags |= _unittest_reportflags

        try:
            # Save our current directory and switch out to the one where the
            # test was originally created, in case another doctest did a
            # directory change.  We'll restore this in the finally clause.
            curdir = os.getcwdu()
            # print 'runTest in dir:', self._ori_dir  # dbg
            os.chdir(self._ori_dir)

            runner.DIVIDER = "-" * 70
            failures, tries = runner.run(test, out=new.write, clear_globs=False)
        finally:
            sys.stdout = old
            os.chdir(curdir)

        if failures:
            raise self.failureException(self.format_failure(new.getvalue()))
Example #17
0
def _bitmap_formatter(data, format, fout, device):
    feps = StringIO()
    eps_formatter(data, format, feps)
    feps.seek(0)

    gs = GhostscriptAPI()
    gs.convert(device, feps, fout, format.logo_width, format.logo_height, format.resolution)
Example #18
0
def install(portal):
    out = StringIO()

    checkPAS(portal, out)
    registerConfiglet(portal, out)

    return out.getvalue()
def installFinalSteps(context):
    """Additional install steps.
    """
    # Only run step if a flag file is present in profiles/default
    # see http://maurits.vanrees.org/weblog/archive/2007/06/discovering-genericsetup
    if context.readDataFile("aaolm_customviews_various.txt") is None:
        return
    out = StringIO()
    out.write("Adjusting allow_discussion on content types...\n")

    portal = context.getSite()
    portal_types = getToolByName(portal, "portal_types")

    # here are the content types that we want to make discussable
    news_item = getattr(portal_types, "News Item")
    page = portal_types.Document
    event = portal_types.Event
    link = portal_types.Link

    # allow discussion on Pages, Links, Events, News Items by default
    news_item.allow_discussion = True
    page.allow_discussion = True
    event.allow_discussion = True
    link.allow_discussion = True

    print >> out, "Turned on discussion for News Items, Pages, Events, and Links."
    return out.getvalue()
Example #20
0
def process(hud_lines, matpheno_lines):
    """
    @param hud_lines: lines of a .hud file
    @param matpheno_lines: lines of a MAT_pheno.txt file
    @return: contents of an .ind file
    """
    # get the ordered names from the .hud file
    names, hud_data = hud.decode(hud_lines)
    # get case and control status from the matpheno file
    cases = set()
    controls = set()
    for line in iterutils.stripped_lines(matpheno_lines):
        name, classification = line.split(None, 1)
        if classification == "1":
            cases.add(name)
        elif classification == "2":
            controls.add(name)
        elif classification in ("12", "null"):
            # skip individuals classified like this
            pass
        else:
            msg = "invalid MAT_pheno classification: " + classification
            raise Exception(msg)
    # write the .ind file contents
    out = StringIO()
    for name in names:
        gender = "U"
        classification = "Ignore"
        if name in cases:
            classification = "Case"
        elif name in controls:
            classification = "Control"
        row = [name, gender, classification]
        print >> out, "\t".join(row)
    return out.getvalue().rstrip()
Example #21
0
    def testDeepCopyCanInvalidate(self):
        """
        Tests regression for invalidation problems related to missing
        readers and writers values in cloned objects (see
        http://mail.zope.org/pipermail/zodb-dev/2008-August/012054.html)
        """
        import ZODB.MappingStorage

        database = DB(ZODB.blob.BlobStorage("blobs", ZODB.MappingStorage.MappingStorage()))
        connection = database.open()
        root = connection.root()
        transaction.begin()
        root["blob"] = Blob()
        transaction.commit()

        stream = StringIO()
        p = Pickler(stream, 1)
        p.dump(root["blob"])
        u = Unpickler(stream)
        stream.seek(0)
        clone = u.load()
        clone._p_invalidate()

        # it should also be possible to open the cloned blob
        # (even though it won't contain the original data)
        clone.open()

        # tearDown
        database.close()
Example #22
0
class DummyPrivateRequest(object):
    def __init__(self, uri="/"):
        self.code = None
        self.method = "GET"
        self.clientproto = "HTTP/1.0"
        self.path = uri
        self.uri = uri
        self.client = address.IPv4Address("TCP", "localhost", 12345)
        self.host = address.IPv4Address("TCP", "localhost", 12345)
        self.content = StringIO()
        self.request_headers = {}
        self.response_headers = {}
        self._finished = defer.Deferred()

    def setResponseCode(self, code):
        self.code = code

    def finish(self):
        self._finished.callback(self)

    def getHeader(self, name):
        return self.request_headers.get(name.lower(), None)

    def setHeader(self, name, value):
        self.response_headers[name.lower()] = value

    def write(self, data):
        self.content.write(data)

    def notifyFinish(self):
        d = defer.Deferred()
        self._finished.addCallback(defer.drop_param, d.callback, self)
        return d
Example #23
0
def colified(elts, **options):
    """Invokes the colify() function but returns the result as a string
       instead of writing it to an output string."""
    sio = StringIO()
    options["output"] = sio
    colify(elts, **options)
    return sio.getvalue()
Example #24
0
    def export_csv(cls):
        import csv
        from StringIO import StringIO

        s = StringIO()
        fields = (
            "name",
            "classification",
            "depth",
            "children_count",
            "email",
            "parent",
            "description",
            "url",
            "website_dump",
            "contact",
            "address",
        )
        writer = csv.DictWriter(s, fields)
        for pb in PublicBody.objects.all():
            d = {}
            for field in fields:
                value = getattr(pb, field)
                if value is None:
                    d[field] = value
                elif isinstance(value, unicode):
                    d[field] = value.encode("utf-8")
                else:
                    d[field] = unicode(value).encode("utf-8")
            writer.writerow(d)
        s.seek(0)
        return s.read()
Example #25
0
def test_csv_annotations():
    exporter = ExportCSV(MagicMock(), MagicMock())
    out = StringIO()
    metadata = {
        "description": "description text",
        "section_title": "Definition and scopes:",
        "indicators_details_url": "http://url.to/indicators",
        "blocks": [
            {
                "definition": "dèfinitioñ ţext",
                "filter_label": "Indicator",
                "label": "long label text",
                "source_label": "source label text",
                "source_url": "http://url.to/source",
                "source_definition": "source definition text",
                "note": "note text",
            }
        ],
    }
    exporter.write_annotations(out, metadata)
    out.seek(0)
    output = out.read().split("\r\n")
    assert output[0] == "Definition and scopes:"
    assert output[1] == "Indicator:,long label text"
    assert output[2] == "Definition:,dèfinitioñ ţext"
    assert output[3] == "Notes:,note text"
    assert output[4] == "Source:,source definition text"
    assert output[5] == "List of available indicators:,http://url.to/indicators"
Example #26
0
def eval_in_context(expression, globals, locals):
    result = None
    try:
        result = eval(expression, globals, locals)
    except Exception:
        s = StringIO()
        traceback.print_exc(file=s)
        result = s.getvalue()

        try:
            try:
                etype, value, tb = sys.exc_info()
                result = value
            finally:
                etype = value = tb = None
        except:
            pass

        result = ExceptionOnEvaluate(result)

        # Ok, we have the initial error message, but let's see if we're dealing with a name mangling error...
        try:
            if "__" in expression:
                # Try to handle '__' name mangling...
                split = expression.split(".")
                curr = locals.get(split[0])
                for entry in split[1:]:
                    if entry.startswith("__") and not hasattr(curr, entry):
                        entry = "_%s%s" % (curr.__class__.__name__, entry)
                    curr = getattr(curr, entry)

                result = curr
        except:
            pass
    return result
Example #27
0
def process(args, raw_hud_lines):
    """
    @param args: user options from the web or cmdline
    @param hud_lines: raw lines of a .hud file
    @return: results in convenient text form
    """
    out = StringIO()
    names, data = hud.decode(raw_hud_lines)
    # normalize the names of the isolates
    if args.clean_isolates:
        names = [Carbone.clean_isolate_element(x) for x in names]
    # get the pcs
    C_full = np.array(data, dtype=float)
    pcs = eigenpop.get_scaled_eigenvectors(C_full, args.diploid_and_biallelic)
    # check for sufficient number of eigenvectors
    if len(pcs) < args.npcs:
        msg_a = "the number of requested principal components "
        msg_b = "must be no more than the number of OTUs"
        raise ValueError(msg_a + msg_b)
    # create the R frame
    headers = ["otu"] + ["pc%d" % (i + 1) for i in range(args.npcs)]
    print >> out, "\t".join(headers)
    for i, name in enumerate(names):
        typed_row = [name] + [pcs[j][i] for j in range(args.npcs)]
        if args.add_indices:
            typed_row = [i + 1] + typed_row
        row = [str(x) for x in typed_row]
        print >> out, "\t".join(row)
    return out.getvalue()
Example #28
0
 def Decode(self, rawstring):
     Stream = StringIO(rawstring)
     (
         self.PDUType,
         self.Reserved1,
         self.PDULength,
         self.ProtocolVersion,
         self.Reserved2,
         self.Reserved3,
         self.Reserved4,
     ) = unpack("> B B I H H 16s 16s", Stream.read(42))
     self.Reserved5 = unpack(">8I", Stream.read(32))
     while 1:
         Type = NextType(Stream)
         if Type == 0x10:
             tmp = ApplicationContextItem()
         elif Type == 0x21:
             tmp = PresentationContextItemAC()
         elif Type == 0x50:
             tmp = UserInformationItem()
         elif Type is None:
             break
         else:
             raise "InvalidVariableItem"
         tmp.Decode(Stream)
         self.VariableItems.append(tmp)
Example #29
0
    def expand_macro(self, formatter, name, content):
        min_depth, max_depth = 1, 6
        title = None
        inline = 0
        if content:
            argv = [arg.strip() for arg in content.split(",")]
            if len(argv) > 0:
                depth = argv[0]
                if "-" in depth:
                    min_depth, max_depth = [int(d) for d in depth.split("-", 1)]
                else:
                    min_depth = max_depth = int(depth)
                if len(argv) > 1:
                    title = argv[1].strip()
                    if len(argv) > 2:
                        inline = argv[2].strip().lower() == "inline"

        # TODO: - integrate the rest of the OutlineFormatter directly here
        #       - use formatter.wikidom instead of formatter.source
        out = StringIO()
        oformatter = OutlineFormatter(self.env, formatter.context)
        oformatter.format(formatter.source, out, max_depth, min_depth, shorten=not inline)
        outline = Markup(out.getvalue())

        if title:
            outline = tag.h4(title) + outline
        if not inline:
            outline = tag.div(outline, class_="wiki-toc")
        return outline
Example #30
0
    def test_integrate(self):
        p_outer = Pipeline()
        p_inner = Pipeline()

        out0 = OneCellLambdaStage(lambda: "hamster,elderberry")
        out1 = OneCellLambdaStage(lambda x: "".join(sorted(x.replace(",", ""))) + "_out1")
        sio = StringIO()
        out2 = OneCellLambdaStage(lambda x, y: "[{},{}]".format(x, y), fout=sio)

        in0 = OneCellLambdaStage(lambda x: x.split(","), n_results=2)
        in1 = OneCellLambdaStage(lambda x: "".join(sorted(x)) + "_in1")
        in2 = OneCellLambdaStage(lambda x: "".join(sorted(x)) + "_in2")
        in3 = OneCellLambdaStage(lambda x, y: "({},{})".format(x, y))

        in_nodes = [p_inner.add(s) for s in (in0, in1, in2, in3)]
        out_nodes = [p_outer.add(s) for s in (out0, out1, out2)]

        in_nodes[0]["fx0"] > in_nodes[1]["x"]
        in_nodes[0]["fx1"] > in_nodes[2]["x"]
        in_nodes[1]["fx"] > in_nodes[3]["x"]
        in_nodes[2]["fx"] > in_nodes[3]["y"]

        in_node_proxy = p_outer._Pipeline__integrate(None, p_inner, in_nodes[0], in_nodes[3])

        out_nodes[0]["fx"] > in_node_proxy["x"]
        out_nodes[0]["fx"] > out_nodes[1]["x"]
        in_node_proxy["fx"] > out_nodes[2]["x"]
        out_nodes[1]["fx"] > out_nodes[2]["y"]

        self.run_pipeline(p_outer)

        control = "[(aehmrst_in1,bdeeelrrry_in2),abdeeeehlmrrrrsty_out1]"

        self.assertEqual(sio.getvalue(), control)