def sendmsg(messagetext, to): global current if current != me: current = me ANSI.ansi("A") print me + ":" ANSI.ansi("0K") print mymessage + messagetext message = xmpp.protocol.Message(to, mymessage + messagetext, typ="chat") jabber.send(message)
def test_torturet(self): s = ANSI.ANSI(24, 80) sample_text = open('torturet.vt').read() for c in sample_text: s.process(c) assert s.pretty() == torture_target, 'processed: \n' + s.pretty( ) + '\nexpected:\n' + torture_target
def test_write (self): s = ANSI.ANSI (6,65) s.fill('.') s.cursor_home() for c in write_text: s.write (c) assert str(s) == write_target
def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"): self.child = pexpect.spawn(engine) self.term = ANSI.ANSI() # self.child.expect ('Chess') # if self.child.after != 'Chess': # raise IOError, 'incompatible chess program' # self.term.process_list (self.before) # self.term.process_list (self.after) self.last_computer_move = ''
def progressbar(left, index, total, right=None, skip=False, charset=3, mid_color=None): if right is None: right = '%s/%s ' % (index + 1, total ) # Because index starts at 0 by default barload = COLS - len(right) if skip: div = int(total / barload) if div <= 0: div = 1 if index > 1 and index % div != 0: return # Only print if progress bar advances left = '(Skipping) %s' % left # Progress section spaced = False # Put spaces in between progress bar characters advance = ['▮', '█', '▰', '⣿', '◼', '.', '⬜'][charset] remain = ['▯', '░', '▱', '⣀', '▭', ' ', '⣿'][charset] load = COLS - len(right) - len(left) mod = 1 if load % 2 == 0 else 0 progress = int(index * barload / total) - len(left) if progress < 0: progress = 0 # Left bar = ANSI.color(left, 'yellow') # Middle if mid_color is not None: bar += ANSI.color(mid_color) for i in range(load): if i == progress and mid_color is not None: bar += ANSI.color('reset') if spaced and i % 2 == mod: bar += ' ' elif i < progress: bar += advance else: bar += remain # Right bar += ANSI.color(right, 'cyan') ANSI.move_column(1) print(bar, end='') sys.stdout.flush()
def load_data(): def load(): try: with open(displays.DATA_FILE, 'rb') as file: global ARRANGEMENT ARRANGEMENT = pickle.load(file) return True except IOError: return False success = load() if success: print(ANSI.color('Successfully loaded!', 'green')) elif displays.find(): success = load() return success
def __init__(self, ip=None): self.buffer = ANSI.ANSI(self.Rows, self.Cols) self.record = file('record.txt', 'wb') self.drac_ip = ip self.ControllerCards = { 'SAS': ('C', 3, 'PercH200Controller'), 'PERC': ('R', 18, 'Perc6_i_Controller'), } try: reset_conn(self) except socket.error, e: print "Error in serial connection ", str(e) self.conn.close() del self.conn #self.conn.shutdown(1) for i in xrange(10): if not self.thread.killed and self.thread.isAlive(): self.thread.kill() time.sleep(10) reset_conn(self)
if '--username' in options: username = options['--username'] print "Login for %s@%s:%s" % (username, hostname, port) if '--password' in options: password = options['--password'] else: password = getpass.getpass('password: '******'/dev/null','/tmp/daemon.log','/tmp/daemon.log') sys.stdout.write('server started with pid %d\n' % os.getpid()) virtual_screen = ANSI.ANSI(24, 80) child = pxssh.pxssh() child.login(hostname, username, password) print 'created shell. command line prompt is', child.PROMPT #child.sendline ('stty -echo') #child.setecho(False) virtual_screen.write(child.before) virtual_screen.write(child.after) if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock") s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) localhost = '127.0.0.1' s.bind('/tmp/mysock') os.chmod('/tmp/mysock', 0777) print 'Listen' s.listen(1)
def main(): try: optlist, args = getopt.getopt(sys.argv[1:], 'h?d', [ 'help', 'h', '?', 'hostname=', 'username='******'password='******'port=', 'watch' ]) except Exception as e: print str(e) exit_with_usage() command_line_options = dict(optlist) options = dict(optlist) # There are a million ways to cry for help. These are but a few of them. if [ elem for elem in command_line_options if elem in ['-h', '--h', '-?', '--?', '--help'] ]: exit_with_usage(0) hostname = "127.0.0.1" port = 1664 username = os.getenv('USER') password = "" daemon_mode = False if '-d' in options: daemon_mode = True if '--watch' in options: watch_mode = True else: watch_mode = False if '--hostname' in options: hostname = options['--hostname'] if '--port' in options: port = int(options['--port']) if '--username' in options: username = options['--username'] print "Login for %s@%s:%s" % (username, hostname, port) if '--password' in options: password = options['--password'] else: password = getpass.getpass('password: '******'/dev/null','/tmp/daemon.log','/tmp/daemon.log') sys.stdout.write('server started with pid %d\n' % os.getpid()) virtual_screen = ANSI.ANSI(24, 80) child = pxssh.pxssh() child.login(hostname, username, password) print 'created shell. command line prompt is', child.PROMPT #child.sendline ('stty -echo') # child.setecho(False) virtual_screen.write(child.before) virtual_screen.write(child.after) if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock") s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) localhost = '127.0.0.1' s.bind('/tmp/mysock') os.chmod('/tmp/mysock', 0o777) print 'Listen' s.listen(1) print 'Accept' #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #localhost = '127.0.0.1' #s.bind((localhost, port)) # print 'Listen' # s.listen(1) r = roller(0.01, endless_poll, (child, child.PROMPT, virtual_screen)) r.start() print "screen poll updater started in background thread" sys.stdout.flush() try: while True: conn, addr = s.accept() print 'Connected by', addr data = conn.recv(1024) if data[0] != ':': cmd = ':sendline' arg = data.strip() else: request = data.split(' ', 1) if len(request) > 1: cmd = request[0].strip() arg = request[1].strip() else: cmd = request[0].strip() if cmd == ':exit': r.cancel() break elif cmd == ':sendline': child.sendline(arg) # child.prompt(timeout=2) time.sleep(0.2) shell_window = str(virtual_screen) elif cmd == ':send' or cmd == ':xsend': if cmd == ':xsend': arg = arg.decode("hex") child.send(arg) time.sleep(0.2) shell_window = str(virtual_screen) elif cmd == ':cursor': shell_window = '%x%x' % (virtual_screen.cur_r, virtual_screen.cur_c) elif cmd == ':refresh': shell_window = str(virtual_screen) response = [] response.append(shell_window) #response = add_cursor_blink (response, row, col) sent = conn.send('\n'.join(response)) if watch_mode: print '\n'.join(response) if sent < len(response): print "Sent is too short. Some data was cut off." conn.close() finally: r.cancel() print "cleaning up socket" s.close() if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock") print "done!"
def split(): print('Splitting images...') start = time.time() converted = 0 total = len(IMAGES) for index, image_file in enumerate(IMAGES): fname, ext = os.path.splitext(image_file) base = os.path.basename(fname) skip = False # Check if the glob gets expanded to existing files. # If it does, skip this image (already tiles for it) for some_file in glob.iglob('%s%s_*' % (PREFIX, base)): skip = True break else: # Split the image try: image = Image.open(image_file) except IOError: progressclear() print( ANSI.color('Error: could not open "%s"' % image_file, 'red')) continue tiles = [] imgw, imgh = image.size arrw, arrh = DIMENSIONS # Scale arrangement to best fit image # =================================== # Ratios imgr = imgw * 1.0 / imgh arrr = arrw * 1.0 / arrh scalew = False scaleh = False scale = 1 xoffset = 0 yoffset = 0 new_arrw = 0 new_arrh = 0 # Image is smaller than arrangement if imgw < arrw and imgh < arrh: if imgr <= arrr: scalew = True elif imgr > arrr: scaleh = True # Image is larger than arrangement elif imgw > arrw and imgh > arrh: if imgr <= arrr: scalew = True elif imgr > arrr: scaleh = True # Image is wider than arrangement elif imgw > arrw: scaleh = True # Image is longer than arrangement elif imgh > arrh: scalew = True if scalew: scale = imgw * 1.0 / arrw new_arrh = int(arrh * scale) new_arrw = imgw elif scaleh: scale = imgh * 1.0 / arrh new_arrw = int(arrw * scale) new_arrh = imgh # Center arrangement if image is wider or longer if new_arrw < imgw: xoffset = int((imgw - new_arrw) / 2.0) if new_arrh < imgh: yoffset = int((imgh - new_arrh) / 2.0) new_arrangement = copy.deepcopy(ARRANGEMENT) for display in new_arrangement: display.w = int(display.w * scale) display.h = int(display.h * scale) display.x = int(display.x * scale) + xoffset display.y = int(display.y * scale) + yoffset for tile_index, display in enumerate(new_arrangement): area = ( display.x, # Start x display.y, # Start y display.x + display.w, # End x display.y + display.h # End y ) tile = image.crop(area) tile.save('%s%s_%d%s' % (PREFIX, base, tile_index, ext)) converted += 1 progressbar('"%s"' % image_file, index, total, skip=skip) end = time.time() progressclear() print(ANSI.color('Complete!', 'green')) duration = int(end - start) print('Tiled %s images in %s seconds' % (ANSI.color( str(converted), 'cyan'), ANSI.color(str(duration), 'cyan')))
def progressclear(): ANSI.clear(1)
def __init__(self): ConfigMixIn.__init__(self) self.child = None self.term = ANSI.ANSI() super(BaseConnection, self).__init__()
def test_tetris (self): s = ANSI.ANSI (24,80) tetris_text = open ('tetris.data').read() for c in tetris_text: s.process (c) assert str(s) == tetris_target
def print_arrangement(_arrangement, max_height=12): lines_printed = 0 # Make a copy of the array to keep the originals intact arrangement = copy.deepcopy(_arrangement) # Scale down dimensions to display in terminal maxh = max([display.h for display in arrangement]) div = maxh / max_height for display in arrangement: display.w = int(display.w / div) display.h = int(display.h / div) display.x = int(display.x / div) display.y = int(display.y / div) # Draw displays from left to right arrangement.sort(key=lambda display: display.x) _arrangement.sort(key=lambda display: display.x) print_inside = all([len(str(_arrangement[i])) <= arrangement[i].w - 2 for i in range(len(arrangement))]) if not print_inside: print(ANSI.color(' '.join([str(display) for display in _arrangement]), 'cyan')) lines_printed += 1 total_lines = max([display.y + display.h for display in arrangement]) lines = {num : ('', 0) for num in range(total_lines)} for index in range(len(arrangement)): display = arrangement[index] _display = _arrangement[index] (w, h, x, y) = (display.w, display.h, display.x, display.y) box = { 'top_left' : '┌', 'top_right' : '┐', 'bottom_left' : '└', 'bottom_right': '┘', 'horiz' : '─', 'vert' : '│' } # Because python 2 cannot use 'nonlocal' keyword outer = { 'index': 0 } def prnt_line(text, num): current_line = lines[ outer['index'] ] current_text = current_line[0] current_num = current_line[1] # Multi-layer arrangements overlap with padding that gets printed new_start = (x - 1) * 2 if ( new_start > 0 and current_num > new_start and current_text[new_start:].strip(' ') == '' # Content is only spaces (OK to overwrite) ): current_text = current_text[:new_start] current_num = new_start lines[ outer['index'] ] = (current_text + text, current_num + num) outer['index'] += 1 # y offset for i in range(y): n = (w - 1) * 2 prnt_line(' ' * n, n) # First line n = (w - 2) * 2 prnt_line(box['top_left'] + box['horiz'] * n + box['top_right'], n + 2) # Middle lines vlines = h - 2 vmid = int(vlines / 2) + (0 if vlines % 2 == 0 else 1) for i in range(vlines): text = '' n = (w - 2) * 2 if (print_inside and i + 1 == vmid): dims = str(_display) text = dims.center(n, ' ') if display.mirrored: text = text.replace(dims, ANSI.color(dims, 'yellow', 'black')) else: text = ' ' * n prnt_line(box['vert'] + text + box['vert'], n + 2) # Last line n = (w - 2) * 2 prnt_line(box['bottom_left'] + box['horiz'] * n + box['bottom_right'], n + 2) # x padding for future displays which go further down while outer['index'] < total_lines: n = (w - 1) * 2 prnt_line(' ' * n, n) for num, line in lines.items(): print(line[0]) lines_printed += total_lines if any([display.mirrored for display in arrangement]): print('Note: %s displays are mirrored' % ANSI.color('highlighted', 'yellow', 'black')) lines_printed += 1 return lines_printed
def find(prefs=None): # Setup # =================================== pbuddy = '/usr/libexec/PlistBuddy' if prefs is None: prefs = '/Library/Preferences/com.apple.windowserver.plist' if not os.path.isfile(pbuddy): print(ANSI.color('Error: could not locate PlistBuddy', 'red')) exit(1) if not os.path.isfile(prefs): print(ANSI.color('Error: could not locate display preferences file', 'red')) exit(1) def test(command): try: subprocess.check_call( command, stdout=DEVNULL, stderr=DEVNULL, shell=True ) except subprocess.CalledProcessError: return False return True # =================================== total_arrangements = 0 pbuddy_print = lambda n: 'print :DisplayAnyUserSets:%d:' % n # Determine number of arrangements while test('"%s" -c "%s" "%s"' % (pbuddy, pbuddy_print(total_arrangements), prefs)): total_arrangements += 1 print('Find your display setup:') found = False for arrangement_index in range(total_arrangements): arrangement = [] print_left = pbuddy_print(arrangement_index) display = 0 while test('"%s" -c "%s%d" "%s"' % (pbuddy, print_left, display, prefs)): display_attr = lambda attr: int( subprocess.check_output( '"%s" -c "%s%d:%s" "%s"' % (pbuddy, print_left, display, attr, prefs), universal_newlines=True, shell=True ).rstrip() ) mirrored = display_attr('Mirrored') == 1 prefix = '' if mirrored else 'Unmirrored' width = display_attr(prefix + 'Width') height = display_attr(prefix + 'Height') originX = display_attr(prefix + 'OriginX') originY = display_attr(prefix + 'OriginY') arrangement.append(Display(width, height, originX, originY, mirrored)) display += 1 # Normalize before printing or saving arrangement = normalize(arrangement) print() num_lines = print_arrangement(arrangement) print() num_lines += 2 # Two print() 's prog = '(%d/%d)' % (arrangement_index + 1, total_arrangements) prog = ANSI.color(prog, 'cyan') read = input('%s Is this your arrangement? [y/N] ' % prog) num_lines += 2 # One for printing the string and one for when you hit enter if read and read in 'yY': found = True fname = DATA_FILE try: with open(fname, 'wb') as file: pickle.dump(arrangement, file, protocol=2) print(ANSI.color('Successfully recorded!', 'green')) except Exception: print(ANSI.color('Error storing arrangement data in "%s"' % fname, 'red')) break else: ANSI.clear(num_lines) if not found: print(ANSI.color('No arrangement chosen. Nothing recorded.', 'red')) return found
exit(0) else: # Base64 shared arrangement import base64 fname = displays.DATA_FILE decoded = '' try: decoded = base64.b64decode(config) except Exception: pass if decoded != '': try: with open(fname, 'wb') as file: file.write(decoded) print(ANSI.color('Successfully recorded!', 'green')) exit(0) except Exception: pass print( ANSI.color( 'Error storing arrangement data in "%s"' % fname, 'red')) else: print(usage) exit(0) elif command == 'clean': if num_args == 2: pass elif num_args == 3:
class Shell(Pane.Pane): ''' Shell panel, start and handles a shell (~ bash) ''' nbLines = 18 nbCols = 64 #nbLines=10; #nbCols=20; ansi = ANSI.ANSI(nbLines - 1, nbCols - 1) textColor = (255, 255, 0) yOffset = 17 yStep = 8 def __init__(self, main): Pane.Pane.__init__(self) self.main = main self.screen = main.screen self.kb = VirtualKB.VirtualKB(main) self.font2 = pygame.font.Font(self.main.skinFolder + "/font.ttf", 8) self.font = pygame.font.Font(None, 12) self.str = "" self.bash = pexpect.spawn("/bin/bash") self.shellReader = ShellReader(self) self.shellReader.start() self.shellLines = [] self.shellImage = pygame.image.load(self.main.skinFolder + "/shell.png") self.editPane = pygame.Surface((320, 240)) def handleEvent(self, value): key = self.kb.handleClickKb2((value.x, value.y)) char = key.getKey() if (char != None): # note: this will callback "updateshell" as necessary self.bash.send(char) self.main.debug("Got key: ", char) else: self.updateScreen() def updateScreen(self): self.editPane.blit(self.shellImage, (0, 0)) if (self.shellLines != None): y = self.yOffset cpt = 0 for line in self.shellLines: if (cpt > len(self.shellLines) - self.nbLines): line = line.replace('\t', ' ') ln = self.font2.render(line, True, self.textColor) self.editPane.blit(ln, (2, y)) y = y + self.yStep cpt = cpt + 1 self.screen.blit(self.editPane, (0, 0)) self.kb.showKB2(self.screen, (0, 240 - 80)) self.main.paintMenuBar() pygame.display.flip() def appendLine(self, line): if (line != None and len(line) > 0): #self.showHex(line); self.ansi.write(line) self.main.debug(self.ansi.pretty()) self.shellLines = str(self.ansi).splitlines() self.updateScreen() def handleShutdown(self): self.shellReader.shutdown() self.bash.close() def showHex(self, line): s = "" for c in line: s += hex(ord(c)) + "(" + c + ") " print s