Ejemplo n.º 1
0
 def setUp(self):
     self.f = StringIO()
     self.o = Logger(self.f)
     self.o.fore.as_hex = self.o.fore.ground = 'fore'
     self.o.back.as_hex = self.o.back.ground = 'back'
     self.f1 = fg_code(WHITE, False)
     self.f2 = fg_code(GREEN, True)
     self.b1 = bg_code(RED)
     self.b2 = bg_code(PURPLE)
Ejemplo n.º 2
0
 def test_receives_repeated_normal_CR_LF_in_broken_godwars_mode_fine(self):
     self.tc.fix_broken_godwars_line_endings = True
     self.tc.dataReceived("foo\r\n\r\n")
     expected = [
         simpleml("foo", fg_code(WHITE, False), bg_code(BLACK)),
         simpleml("", fg_code(WHITE, False), bg_code(BLACK))
     ]
     for ml in expected:
         ml.wrap = True
     lines = [
         line
         for ((line, ), kwargs) in self.e.metalineReceived.call_args_list
     ]
     assert lines == expected, lines
Ejemplo n.º 3
0
 def setUp(self):
     self.f = TelnetClientFactory(None, 'ascii', None)
     self.f.realm = self.e = Mock(spec=RootRealm)
     self.tc = TelnetClient(self.f)
     self.tc.transport = FakeTransport()
     self.fores = RunLengthList([(0, fg_code(WHITE, False))])
     self.backs = RunLengthList([(0, bg_code(BLACK))])
Ejemplo n.º 4
0
    def write(self, line, soft_line_start=False):
        """Write a line to the screen.
        
        This forcibly converts its argument to a Metaline.
        """
        if not isinstance(line, (basestring, Metaline)):
            line = str(line)
        if isinstance(line, basestring):
            metaline = simpleml(line, fg_code(WHITE, False), bg_code(BLACK))
            metaline.wrap = False
            metaline.soft_line_start = soft_line_start
        else:
            metaline = line
        #we don't need to close off the ends of the note, because thanks to
        #the magic of the ColourCodeParser, each new line is started by the
        #implied colour, so notes can't bleed out into text (though the
        #reverse can be true).

        #this needs to be before the futzing with NLs and GA, because textwrap
        #obliterates all other newlines.
        metaline = metaline.wrapped(self.wrapper)

        #we don't actually append newlines at the end, but the start. This
        #simplifies things, because we don't use a newline where a soft line
        #end meets a soft line start, so there's only one place in this code
        #that can add newlines.
        if self._last_line_end is not None:
            if self._last_line_end == 'hard' or not metaline.soft_line_start:
                metaline.insert(0, '\n')

        for prot in self.protocols:
            prot.metalineReceived(metaline)

        self._last_line_end = metaline.line_end
Ejemplo n.º 5
0
def prompt_time_display(match, realm):
    """Add a pink coloured timestamp to the prompt."""
    now = datetime.now()
    metaline = simpleml(
        (now.strftime("(%H:%M:%S.%%s) ") % str(now.microsecond)[:2]),
        HexFGCode(0xFF, 0xA0, 0xA0), bg_code(BLACK))
    realm.alterer.insert_metaline(match.end(1), metaline)
Ejemplo n.º 6
0
def test_ColourCodeParser_funky_real_example():
    ccp = ColourCodeParser()
    inline = '\x1b[33mfoo\x1b[0m'
    ml = ccp.parseline(inline)
    print ml.fores.items()
    assert ml.fores.items() == [(0, fg_code(YELLOW, False)),
                               (3, fg_code(WHITE, False))]
    assert ml.backs.items() == [(0, bg_code(BLACK))]
Ejemplo n.º 7
0
 def __init__(self, realm, logformat):
     self.fore = fg_code(WHITE, False)
     self.back = bg_code(BLACK)
     self._dirty = False
     realm.addProtocol(self)
     self.log = open(
         time.strftime(logformat) % {'name': realm.factory.name}, 'a')
     self.log.write(self.log_preamble)
Ejemplo n.º 8
0
 def test_fixes_LF_CR_normally(self):
     self.tc.fix_broken_godwars_line_endings = True
     self.tc.dataReceived("foo\n\r")
     expected = [simpleml("foo", fg_code(WHITE, False), bg_code(BLACK))]
     expected[0].wrap = True
     lines = [
         line
         for ((line, ), kwargs) in self.e.metalineReceived.call_args_list
     ]
     assert lines == expected, lines
Ejemplo n.º 9
0
    def connectionMade(self):
        """The MUD's been connected to."""
        message = time.strftime("Connection opened at %H:%M:%S.")
        colour = HexFGCode(0xFF, 0xAA, 0x00)  #lovely orange
        metaline = simpleml(message, colour, bg_code(BLACK))
        self.write(metaline)
        for prot in self.protocols:
            prot.connectionMade()

        self.telnet.msdp.add_listener(self.mmap)
Ejemplo n.º 10
0
    def test_no_tell_sent_doesnt_cock_up(self):
        ml = simpleml('Bar tells you, "Blah."', fg_code(WHITE, False),
                      bg_code(BLACK))
        self.fact.realm.metalineReceived(ml)
        ml_written = self.p.metalineReceived.call_args[0][0]
        colour_expecting = ml_written.fores.get_at(0)

        self.fact.realm.send("tell baz blah")
        ml = simpleml("Whom do you wish to tell to?", fg_code(WHITE, False),
                      bg_code(BLACK))
        self.fact.realm.metalineReceived(ml)

        self.fact.realm.send("tell bar blah")
        ml = simpleml('You tell Bar, "Blah."', fg_code(WHITE, False),
                      bg_code(BLACK))
        self.fact.realm.metalineReceived(ml)
        ml_written_2 = self.p.metalineReceived.call_args[0][0]

        assert ml_written_2.fores.get_at(10) == colour_expecting
Ejemplo n.º 11
0
 def parseline(self, line):
     """Interpret the VT100 codes in line and returns a Metaline, replete
     with RunLengthLists, that splits the text, foreground and background
     into three separate channels.
     """
     fores, backs, cleanline = self._parseline(line)
     rlfores = RunLengthList(((length, fg_code(colour, bold))
                              for (length, (colour, bold)) in fores),
                             _normalised=True)
     rlbacks = RunLengthList(
         ((length, bg_code(colour)) for (length, colour) in backs),
         _normalised=True)
     return Metaline(cleanline, rlfores, rlbacks)
Ejemplo n.º 12
0
 def obj_as_metaline(self, o):
     desc = self.objs[o]
     print desc.keys()
     colour = HexFGCode(0xFF, 0xAA, 0x00)
     metaline = simpleml('(', colour, bg_code(BLACK))
     metaline += simpleml(desc['type'], colour, bg_code(BLACK))
     
     if desc['weapon'] and desc['weapon'] != 'None':
         metaline += simpleml(',' + desc['weapon'], fg_code(YELLOW, True), bg_code(BLACK))
         metaline += simpleml(',' + desc['dmg_str'], fg_code(RED, True), bg_code(BLACK))
         metaline += simpleml(',' + '%s' % desc['dmg_avg'], fg_code(RED, True), bg_code(BLACK))
         
     if desc['ac']:
         metaline += simpleml(',AC:%s' % desc['ac'], fg_code(RED, True), bg_code(BLACK))
     
     if desc['prop'] != '':
         metaline += simpleml(',' + desc['prop'], fg_code(BLUE, True), bg_code(BLACK))
     
     metaline += simpleml(')', colour, bg_code(BLACK))
     return metaline
Ejemplo n.º 13
0
    def connectionLost(self):
        """The link to the MUD died.

        It is guaranteed that this will be called before close on connection
        event receivers.
        """
        message = time.strftime("Connection closed at %H:%M:%S.")
        colour = HexFGCode(0xFF, 0xAA, 0x00)  #lovely orange
        metaline = simpleml(message, colour, bg_code(BLACK))
        self.write(metaline)
        for prot in self.protocols:
            prot.connectionLost()
        #we might be waiting on the connection to die before we send out
        #close events
        if self._closing_down:
            for prot in self.protocols:
                prot.close()
        self._closing_down = True
        self.mmap.save()
Ejemplo n.º 14
0
 def anyline(self, match, realm):
     if self.opozn_flag:
         s = match.group(1).strip()
         if s != '':
             self.obj_desc = '%s\n%s' % (self.obj_desc, s)
     
     if self.parse_flag:
         
         # logging baz
         m = RE_BAZ_NEW.match(match.group(1))
         print m, match.group(1)
         if m:
             w = {
                 'lot': m.group(1).strip(),
                 'object': m.group(2).strip(),
                 'price': m.group(3).strip(),
                 'date': '%s' % datetime.datetime.now(),
                 }
             self.baz_log.write('%s\n' % json.dumps(w, encoding="utf-8"))
             self.baz_log.flush()
         
         print 'PPPPP', match.group(1).strip()
         for res in OBJ_RES:
             m = res.match(match.group(1))
             if m:
                 o = m.group(1).strip()
                 if not self.has_obj(o):
                     print 'Getting from mudportal'
                     desc = self.get_from_mudportal(o)
                     if desc != {}:
                         self.update_obj(o, desc)
                 print o
                 if self.has_obj(o):
                     realm.alterer.insert_metaline(m.start(1) + len(o) + 1, self.obj_as_metaline(o))
                 else:
                     metaline = simpleml('(?)', fg_code(RED, True), bg_code(BLACK))
                     realm.alterer.insert_metaline(m.start(1) + len(o) + 1, metaline)
Ejemplo n.º 15
0
 def info(self, message, colour=HexFGCode(0xFF, 0xAA, 0x00)):
     metaline = simpleml(message, colour, bg_code(BLACK))
     self.write(metaline)
Ejemplo n.º 16
0
def test_ColourCodeParser_no_line():
    ccp = ColourCodeParser()
    inline = ''
    ml = ccp.parseline(inline)
    assert ml.fores.items() == [(0, fg_code(WHITE, False))]
    assert ml.backs.items() == [(0, bg_code(BLACK))]
Ejemplo n.º 17
0
def test_ColourCodeParser_all_reset_fore_and_back_not_needed():
    ccp = ColourCodeParser()
    inline = "foo\x1b[0m"
    ml = ccp.parseline(inline)
    assert ml.fores == {0: fg_code(WHITE, False)}
    assert ml.backs == {0: bg_code(BLACK)}
Ejemplo n.º 18
0
def test_ColourCodeParser_add_reset_back_change_made():
    ccp = ColourCodeParser()
    inline = "\x1b[47mfoo\x1b[0m"
    ml = ccp.parseline(inline)
    assert ml.backs == {0: bg_code(WHITE),
                        3: bg_code(BLACK)}
Ejemplo n.º 19
0
def test_ColourCodeParser_redundant_back_change():
    ccp = ColourCodeParser()
    inline = 'foo\x1b[40m'
    ml = ccp.parseline(inline)
    assert ml.backs == {0: bg_code(BLACK)}, ml.backs
Ejemplo n.º 20
0
def ml(m, c):
    return simpleml(m, c, bg_code(BLACK))
Ejemplo n.º 21
0
def test_ColourCodeParser_bg_change():
    ccp = ColourCodeParser()
    inline = 'foo\x1b[46mbar'
    ml = ccp.parseline(inline)
    assert ml.backs.items() == [(0, bg_code(BLACK)),
                                               (3, bg_code(CYAN))]
Ejemplo n.º 22
0
def test_ColOurCodeParser_redundant_back_reset():
    ccp = ColourCodeParser()
    inline = "foo\x1b[48m"
    ml = ccp.parseline(inline)
    assert ml.backs == {0: bg_code(BLACK)}