def _display(self): '''Return the current status of the thread pools and queues.''' output = '' output += '{0:^67} | {1:^50}\n'.format('Ganga user threads:', 'Ganga monitoring threads:') output += '{0:^67} | {1:^50}\n'.format('------------------', '------------------------') output += '{0:<26} {1:<26} {2:<13} | {0:<26} {1:<28} {2:<10}\n'.format('Name', 'Command', 'Timeout') output += '{0:<26} {1:<26} {2:<13} | {0:<26} {1:<28} {2:<10}\n'.format('----', '-------', '-------') for u, m in zip(self._user_threadpool.worker_status(), self._monitoring_threadpool.worker_status()): # name has extra spaces as colour characters are invisible but # still count if u[0] is not None: # User task has no ganga name name_user = getColour('fg.red') + str(u[0]) + getColour('fg.normal') else: # User task has a ganga name name_user = getColour('fg.red') + str(u[1]) + getColour('fg.normal') name_monitor = getColour('fg.red') + str(m[0]) + getColour('fg.normal') if u[1] == 'idle': name_user = name_user.replace(getColour('fg.red'), getColour('fg.green')) if m[1] == 'idle': name_monitor = name_monitor.replace(getColour('fg.red'), getColour('fg.green')) output += '{0:<35} {1:<26} {2:<15} | {3:<35} {4:<28} {5:<10}\n'.format( name_user, u[1][:30].replace("\n", "\\n"), u[2], name_monitor, m[1][:30].replace("\n", "\\n"), m[2]) output += '\n' output += "Ganga user queue:\n" output += "----------------\n" output += str([self._display_element(elem) for elem in self._user_threadpool.get_queue()]) output += '\n' output += "Ganga monitoring queue:\n" output += "----------------------\n" output += str([self._display_element(elem) for elem in self._monitoring_threadpool.get_queue()]) return output
def _display(self): """Return the current status of the thread pools and queues.""" output = "" output += "{0:^67} | {1:^50}\n".format("Ganga user threads:", "Ganga monitoring threads:") output += "{0:^67} | {1:^50}\n".format("------------------", "------------------------") output += "{0:<26} {1:<26} {2:<13} | {0:<26} {1:<28} {2:<10}\n".format("Name", "Command", "Timeout") output += "{0:<26} {1:<26} {2:<13} | {0:<26} {1:<28} {2:<10}\n".format("----", "-------", "-------") for u, m in zip(self._user_threadpool.worker_status(), self._monitoring_threadpool.worker_status()): # name has extra spaces as colour characters are invisible but # still count if u[0] is not None: # User task has no ganga name name_user = getColour("fg.red") + str(u[0]) + getColour("fg.normal") else: # User task has a ganga name name_user = getColour("fg.red") + str(u[1]) + getColour("fg.normal") name_monitor = getColour("fg.red") + str(m[0]) + getColour("fg.normal") if u[1] == "idle": name_user = name_user.replace(getColour("fg.red"), getColour("fg.green")) if m[1] == "idle": name_monitor = name_monitor.replace(getColour("fg.red"), getColour("fg.green")) output += "{0:<35} {1:<26} {2:<15} | {3:<35} {4:<28} {5:<10}\n".format( name_user, u[1][:30].replace("\n", "\\n"), u[2], name_monitor, m[1][:30].replace("\n", "\\n"), m[2] ) output += "\n" output += "Ganga user queue:\n" output += "----------------\n" output += str([self._display_element(elem) for elem in self._user_threadpool.get_queue()]) output += "\n" output += "Ganga monitoring queue:\n" output += "----------------------\n" output += str([self._display_element(elem) for elem in self._monitoring_threadpool.get_queue()]) return output
def _display(self, colour): from Ganga.Utility.ColourText import ANSIMarkup, NoMarkup, getColour, Foreground, Effects import Ganga.Utility.external.textwrap as textwrap if colour: markup = ANSIMarkup() else: markup = NoMarkup() fg = Foreground() fx = Effects() display_config = getConfig('Display') name_colour = getColour(display_config['config_name_colour']) docstring_colour = getColour( display_config['config_docstring_colour']) # fg.boldgrey value_colour = getColour( display_config['config_value_colour']) # fx.normal levels = ['**', '* ', ' '] levels = map(lambda x: markup(x, fg.red), levels) from cStringIO import StringIO sio = StringIO() sio.write('%s' % markup(stripProxy(self).name, name_colour) + ' : ' + markup(stripProxy(self).docstring, docstring_colour) + '\n') opts = sorted(stripProxy(self).options.keys()) INDENT = ' ' * 2 for o in opts: sio.write(levels[stripProxy(self).getEffectiveLevel( o)] + ' ' + markup(o, name_colour) + ' = ' + markup(repr(stripProxy(self)[o]), value_colour) + '\n') sio.write(textwrap.fill(markup(stripProxy(self).options[o].docstring.strip( ), docstring_colour), width=80, initial_indent=INDENT, subsequent_indent=INDENT) + '\n') typelist = stripProxy(self).options[o].typelist if not typelist: typedesc = 'Type: ' + \ str(type(stripProxy(self).options[o].default_value)) else: typedesc = 'Allowed types: ' + \ str([t.split('.')[-1] for t in typelist]) sio.write(markup(INDENT + typedesc, docstring_colour) + '\n') filter = stripProxy(self).options[o].filter if filter: filter_doc = filter.__doc__ if not filter_doc: filter_doc = "undocumented" sio.write( markup(INDENT + "Filter: " + filter_doc, docstring_colour) + '\n') examples = stripProxy(self).options[o].examples if examples: sio.write( markup(INDENT + "Examples:", docstring_colour) + '\n') for e in examples.splitlines(): sio.write( markup(INDENT + e.strip(), docstring_colour) + '\n') return sio.getvalue()
def runClient(): end_trans = '###END-TRANS###' socket_addr = SocketAddress(address='localhost', port=5000) while 1: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(socket_addr) cmd = raw_input( getColour('fg.blue') + 'diracAPI_env > ' + getColour('fg.normal')) client_socket.sendall(cmd + end_trans) if (cmd == 'q' or cmd == 'Q' or cmd == 'exit' or cmd == 'exit()'): client_socket.close() break out = '' while end_trans not in out: data = client_socket.recv(1024) if not data: out = '###BROKEN###' break out += data if out != '###BROKEN###': out = out.replace(end_trans, '') if out == '': print('') else: try: print(getColour('fg.red') + 'diracAPI_env > ' + getColour('fg.normal') + pformat(eval(out)) + '\n') except: print( getColour('fg.red') + 'diracAPI_env > ' + getColour('fg.normal') + out) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close()
def runClient(): end_trans = '###END-TRANS###' socket_addr = SocketAddress(address='localhost', port=5000) while 1: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(socket_addr) cmd = raw_input( getColour('fg.blue') + 'diracAPI_env > ' + getColour('fg.normal')) client_socket.sendall(cmd + end_trans) if (cmd == 'q' or cmd == 'Q' or cmd == 'exit' or cmd == 'exit()'): client_socket.close() break out = '' while end_trans not in out: data = client_socket.recv(1024) if not data: out = '###BROKEN###' break out += data if out != '###BROKEN###': out = out.replace(end_trans, '') if out == '': print('') else: try: print( getColour('fg.red') + 'diracAPI_env > ' + getColour('fg.normal') + pformat(eval(out)) + '\n') except: print( getColour('fg.red') + 'diracAPI_env > ' + getColour('fg.normal') + out) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close()
def displayer(self, substitution, matches, longest_match_length): # try: max_line_width = self.shell_size()[1] # max(itertools.imap(lambda x: len(x), matches)) + 2 #two blank spaces column_width = longest_match_length + 2 num_per_line = max_line_width / column_width # need this as if partial auto-complete happens then get duplicate text # this is bcoz readline.get_line_buffer() already contains the partial # completed text user_input = readline.get_line_buffer()[:readline.get_endidx()] class_label, class_name, method_name, arg_num = current_class_method_and_arg( user_input, self.ns) if num_per_line == 0 or user_input.strip().endswith( '(') or user_input.strip().endswith(','): num_per_line = 1 # constructor if class_name is not None and method_name is None and user_input.strip( ).endswith(','): # if user_input.strip().endswith('(') or # user_input.strip().endswith(','): tmp = arg_splitter(user_input + ')', strip_args=False) new = tmp[:] # map(lambda x: x.strip(), tmp) wrong = False unrecognised = [] for i, arg in enumerate( itertools.ifilter(lambda x: x != '', itertools.imap(lambda x: x.strip(), new))): if '=' in arg: split_arg = arg.split('=') if split_arg[0].strip() not in eval( 'dict(%s._impl._schema.allItems())' % class_name, self.ns): unrecognised.append((split_arg[0].strip(), class_name)) # include = to avoid replacing letters e.g. a in the # RHS also to keep the users spaces intact new[i] = new[i].replace( split_arg[0] + '=', getColour('fg.red') + split_arg[0] + getColour('fg.normal') + '=') elif not eval('isinstance(%s, %s)' % (arg, class_name), self.ns): new[i] = new[i].replace( arg, getColour('fg.red') + arg + getColour('fg.normal')) wrong = True user_input = user_input.replace(','.join(tmp), ','.join(new)) if wrong: logger.warning( 'Only one positional arg allowed which must be an object of the same type to copy from' ) #user_input = user_input.replace(','.join(tmp), ','.join(new)) if unrecognised: for a, c in unrecognised: logger.warning( "Unrecognised keyword argument, '%s' is not a modifyable attribute of '%s'" % (a, c)) colour_green = getColour('fg.green') colour_red = getColour('fg.red') #colour_blue = getColour('fg.blue') colour_normal = getColour('fg.normal') display_str = '\n' for i, m in enumerate(itertools.ifilter(lambda x: x != '', matches)): coloured_text = self.colouriser(m, user_input) width = column_width if colour_green in coloured_text: width = column_width + len(colour_green) + len(colour_normal) elif colour_red in coloured_text: width = column_width + len(colour_red) + len(colour_normal) display_str += ('{0:<%d}' % width).format(coloured_text) if num_per_line and (i + 1) % num_per_line == 0: display_str += '\n' if num_per_line and (i + 1) % num_per_line == 0: display_str += '\n' else: display_str += '\n\n' display_str += getColour('fg.blue') display_str += 'In [%s]:' % len(self.ns['In']) display_str += getColour('fg.normal') display_str += user_input print(display_str, end='') readline.redisplay()
def colouriser(self, match, user_input): # try: if user_input.rstrip().endswith('(') or user_input.rstrip().endswith( ','): class_label, class_name, method_name, arg_num = current_class_method_and_arg( user_input, self.ns) args = arg_splitter(match) var_args = filter(lambda x: '*' in x, args) non_var_args = filter(lambda x: '*' not in x, args) if method_name is None: # constructor match = match.replace( class_name, getColour('fg.green') + class_name + getColour('fg.normal')) return match match = match.replace( method_name, getColour('fg.green') + method_name + getColour('fg.normal')) if args: match = match.replace(', '.join(args), '##ARGS##') if arg_num < len(non_var_args): non_var_args[arg_num] = getColour( 'fg.blue') + non_var_args[arg_num] + getColour('fg.normal') else: if var_args: var_args = map( lambda x: getColour('fg.blue') + x + getColour( 'fg.normal'), var_args) elif arg_num > 0: logger.warning('Too many arguments provided') match = match.replace( ')', getColour('fg.red') + ')' + getColour('fg.normal')) return match.replace( '##ARGS##', ', '.join(itertools.chain(non_var_args, var_args))) if user_input.endswith('.'): split_text = match.split('.') schema_items = {} if eval( 'hasattr(%s, "_schema") and hasattr(%s, "_readonly")' % (split_text[0], split_text[0]), self.ns): read_only = eval('%s._readonly()' % split_text[0], self.ns) schema_items = eval( 'dict(%s._schema.allItems())' % split_text[0], self.ns) elif eval( 'hasattr(%s,"_impl") and hasattr(%s._impl, "_schema") and hasattr(%s._impl, "_readonly")' % (split_text[0], split_text[0], split_text[0]), self.ns): read_only = eval('%s._impl._readonly()' % split_text[0], self.ns) schema_items = eval( 'dict(%s._impl._schema.allItems())' % split_text[0], self.ns) # if schema_items is not None and split_text[1] in schema_items: current_item = schema_items.get(split_text[1], None) if current_item is not None: if read_only: split_text[1] = getColour( 'fg.red') + split_text[1] + getColour('fg.normal') elif current_item['protected']: split_text[1] = getColour( 'fg.red') + split_text[1] + getColour('fg.normal') else: split_text[1] = getColour( 'fg.green') + split_text[1] + getColour('fg.normal') return '.'.join(split_text) return match
def _display(self, colour): from Ganga.Utility.ColourText import ANSIMarkup, NoMarkup, getColour, Foreground, Effects if colour: markup = ANSIMarkup() else: markup = NoMarkup() fg = Foreground() display_config = getConfig('Display') name_colour = getColour(display_config['config_name_colour']) docstring_colour = getColour( display_config['config_docstring_colour']) # fg.boldgrey value_colour = getColour( display_config['config_value_colour']) # fx.normal levels = ['**', '* ', ' '] levels = map(lambda x: markup(x, fg.red), levels) from cStringIO import StringIO sio = StringIO() sio.write('%s' % markup(stripProxy(self).name, name_colour) + ' : ' + markup(stripProxy(self).docstring, docstring_colour) + '\n') opts = sorted(stripProxy(self).options.keys()) INDENT = ' ' * 2 p = re.compile('[\.\w]*\.') for o in opts: sio.write( levels[stripProxy(self).getEffectiveLevel(o)] + ' ' + markup(o, name_colour) + ' = ' + markup(p.sub('', repr(stripProxy(self)[o])), value_colour) + '\n') sio.write( textwrap.fill(markup( stripProxy(self).options[o].docstring.strip(), docstring_colour), width=80, initial_indent=INDENT, subsequent_indent=INDENT) + '\n') typelist = stripProxy(self).options[o].typelist if not typelist: typedesc = 'Type: ' + \ p.sub('',str(type(stripProxy(self).options[o].default_value))) else: typedesc = 'Allowed types: ' + \ str([p.sub('',str(t)) for t in typelist]) sio.write(markup(INDENT + typedesc, docstring_colour) + '\n') filter = stripProxy(self).options[o].filter if filter: filter_doc = filter.__doc__ if not filter_doc: filter_doc = "undocumented" sio.write( markup(INDENT + "Filter: " + filter_doc, docstring_colour) + '\n') examples = stripProxy(self).options[o].examples if examples: sio.write( markup(INDENT + "Examples:", docstring_colour) + '\n') for e in examples.splitlines(): sio.write( markup(INDENT + e.strip(), docstring_colour) + '\n') return sio.getvalue()
def displayer(self, substitution, matches, longest_match_length): # try: max_line_width = self.shell_size()[1] # max(itertools.imap(lambda x: len(x), matches)) + 2 #two blank spaces column_width = longest_match_length + 2 num_per_line = max_line_width / column_width # need this as if partial auto-complete happens then get duplicate text # this is bcoz readline.get_line_buffer() already contains the partial # completed text user_input = readline.get_line_buffer()[: readline.get_endidx()] class_label, class_name, method_name, arg_num = current_class_method_and_arg(user_input, self.ns) if num_per_line == 0 or user_input.strip().endswith("(") or user_input.strip().endswith(","): num_per_line = 1 # constructor if class_name is not None and method_name is None and user_input.strip().endswith(","): # if user_input.strip().endswith('(') or # user_input.strip().endswith(','): tmp = arg_splitter(user_input + ")", strip_args=False) new = tmp[:] # map(lambda x: x.strip(), tmp) wrong = False unrecognised = [] for i, arg in enumerate(itertools.ifilter(lambda x: x != "", itertools.imap(lambda x: x.strip(), new))): if "=" in arg: split_arg = arg.split("=") if split_arg[0].strip() not in eval("dict(%s._impl._schema.allItems())" % class_name, self.ns): unrecognised.append((split_arg[0].strip(), class_name)) # include = to avoid replacing letters e.g. a in the # RHS also to keep the users spaces intact new[i] = new[i].replace( split_arg[0] + "=", getColour("fg.red") + split_arg[0] + getColour("fg.normal") + "=" ) elif not eval("isinstance(%s, %s)" % (arg, class_name), self.ns): new[i] = new[i].replace(arg, getColour("fg.red") + arg + getColour("fg.normal")) wrong = True user_input = user_input.replace(",".join(tmp), ",".join(new)) if wrong: logger.warning("Only one positional arg allowed which must be an object of the same type to copy from") # user_input = user_input.replace(','.join(tmp), ','.join(new)) if unrecognised: for a, c in unrecognised: logger.warning("Unrecognised keyword argument, '%s' is not a modifyable attribute of '%s'" % (a, c)) colour_green = getColour("fg.green") colour_red = getColour("fg.red") # colour_blue = getColour('fg.blue') colour_normal = getColour("fg.normal") display_str = "\n" for i, m in enumerate(itertools.ifilter(lambda x: x != "", matches)): coloured_text = self.colouriser(m, user_input) width = column_width if colour_green in coloured_text: width = column_width + len(colour_green) + len(colour_normal) elif colour_red in coloured_text: width = column_width + len(colour_red) + len(colour_normal) display_str += ("{0:<%d}" % width).format(coloured_text) if num_per_line and (i + 1) % num_per_line == 0: display_str += "\n" if num_per_line and (i + 1) % num_per_line == 0: display_str += "\n" else: display_str += "\n\n" display_str += getColour("fg.blue") display_str += "In [%s]:" % len(self.ns["In"]) display_str += getColour("fg.normal") display_str += user_input print(display_str, end="") readline.redisplay()
def colouriser(self, match, user_input): # try: if user_input.rstrip().endswith("(") or user_input.rstrip().endswith(","): class_label, class_name, method_name, arg_num = current_class_method_and_arg(user_input, self.ns) args = arg_splitter(match) var_args = filter(lambda x: "*" in x, args) non_var_args = filter(lambda x: "*" not in x, args) if method_name is None: # constructor match = match.replace(class_name, getColour("fg.green") + class_name + getColour("fg.normal")) return match match = match.replace(method_name, getColour("fg.green") + method_name + getColour("fg.normal")) if args: match = match.replace(", ".join(args), "##ARGS##") if arg_num < len(non_var_args): non_var_args[arg_num] = getColour("fg.blue") + non_var_args[arg_num] + getColour("fg.normal") else: if var_args: var_args = map(lambda x: getColour("fg.blue") + x + getColour("fg.normal"), var_args) elif arg_num > 0: logger.warning("Too many arguments provided") match = match.replace(")", getColour("fg.red") + ")" + getColour("fg.normal")) return match.replace("##ARGS##", ", ".join(itertools.chain(non_var_args, var_args))) if user_input.endswith("."): split_text = match.split(".") schema_items = {} if eval('hasattr(%s, "_schema") and hasattr(%s, "_readonly")' % (split_text[0], split_text[0]), self.ns): read_only = eval("%s._readonly()" % split_text[0], self.ns) schema_items = eval("dict(%s._schema.allItems())" % split_text[0], self.ns) elif eval( 'hasattr(%s,"_impl") and hasattr(%s._impl, "_schema") and hasattr(%s._impl, "_readonly")' % (split_text[0], split_text[0], split_text[0]), self.ns, ): read_only = eval("%s._impl._readonly()" % split_text[0], self.ns) schema_items = eval("dict(%s._impl._schema.allItems())" % split_text[0], self.ns) # if schema_items is not None and split_text[1] in schema_items: current_item = schema_items.get(split_text[1], None) if current_item is not None: if read_only: split_text[1] = getColour("fg.red") + split_text[1] + getColour("fg.normal") elif current_item["protected"]: split_text[1] = getColour("fg.red") + split_text[1] + getColour("fg.normal") else: split_text[1] = getColour("fg.green") + split_text[1] + getColour("fg.normal") return ".".join(split_text) return match
def colouriser(self, match, user_input): # try: if user_input.rstrip().endswith('(') or user_input.rstrip().endswith(','): class_label, class_name, method_name, arg_num = current_class_method_and_arg( user_input, self.ns) args = arg_splitter(match) var_args = filter(lambda x: '*' in x, args) non_var_args = filter(lambda x: '*' not in x, args) if method_name is None: # constructor match = match.replace( class_name, getColour('fg.green') + class_name + getColour('fg.normal')) return match match = match.replace( method_name, getColour('fg.green') + method_name + getColour('fg.normal')) if args: match = match.replace(', '.join(args), '##ARGS##') if arg_num < len(non_var_args): non_var_args[arg_num] = getColour( 'fg.blue') + non_var_args[arg_num] + getColour('fg.normal') else: if var_args: var_args = map( lambda x: getColour('fg.blue') + x + getColour('fg.normal'), var_args) elif arg_num > 0: logger.warning('Too many arguments provided') match = match.replace(')', getColour( 'fg.red') + ')' + getColour('fg.normal')) return match.replace('##ARGS##', ', '.join(itertools.chain(non_var_args, var_args))) if user_input.endswith('.'): split_text = match.split('.') schema_items = {} if eval('hasattr(%s, "_schema") and hasattr(%s, "_readonly")' % (split_text[0], split_text[0]), self.ns): read_only = eval('%s._readonly()' % split_text[0], self.ns) schema_items = eval('dict(%s._schema.allItems())' % split_text[0], self.ns) elif eval('hasattr(%s,"_impl") and hasattr(%s._impl, "_schema") and hasattr(%s._impl, "_readonly")' % (split_text[0], split_text[0], split_text[0]), self.ns): read_only = eval('%s._impl._readonly()' % split_text[0], self.ns) schema_items = eval('dict(%s._impl._schema.allItems())' % split_text[0], self.ns) # if schema_items is not None and split_text[1] in schema_items: current_item = schema_items.get(split_text[1], None) if current_item is not None: if read_only: split_text[1] = getColour( 'fg.red') + split_text[1] + getColour('fg.normal') elif current_item['protected']: split_text[1] = getColour( 'fg.red') + split_text[1] + getColour('fg.normal') else: split_text[1] = getColour( 'fg.green') + split_text[1] + getColour('fg.normal') return '.'.join(split_text) return match