Coverage for mtools.util.cmdlinetool : 64%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
except ImportError: from pymongo import Connection from pymongo.errors import ConnectionFailure, AutoReconnect, OperationFailure, ConfigurationError
""" This class extends the FileType class from the argparse module. It will try to open the file and pass the handle to a new LogFile object, but if that's not possible it will catch the exception and interpret the string as a MongoDB URI and try to connect to the database. In that case, it will return a ProfileCollection object.
Both derive from the same base class InputSource and support iteration over LogEvents. """ # catch filetype and return LogFile object
except argparse.ArgumentTypeError as e: # not a file, try open as MongoDB database m = re.match('^(\w+)(?::(\d+))?(?:/([a-zA-Z0-9._-]+))?$', string) if m: host, port, namespace = m.groups() port = int(port) if port else 27017 namespace = namespace or 'test.system.profile' if '.' in namespace: database, collection = namespace.split('.', 1) else: database = namespace collection = 'system.profile'
if host == 'localhost' or re.match('\d+\.\d+\.\d+\.\d+', host): return ProfileCollection(host, port, database, collection)
raise argparse.ArgumentTypeError("can't parse %s as file or MongoDB connection string." % string)
except ImportError:
class InputSourceAction(argparse.FileType): pass
""" Base class for any mtools command line tool. Adds --version flag and basic control flow. """
""" Constructor. Any inheriting class should add a description to the argparser and extend it with additional arguments as needed. """ # define argument parser and add version argument
""" Init point to execute the script. If `arguments` string is given, will evaluate the arguments, else evaluates sys.argv. Any inheriting class should extend the run method (but first calling BaseCmdLineTool.run(self)). """ # redirect PIPE signal to quiet kill script, if not on Windows
else: else: else: self.args = vars(self.argparser.parse_args())
""" converts the datetime to unix epoch (properly). """ else: return 0
""" use this helper function to print a progress bar for longer-running scripts. The progress value is a value between 0.0 and 1.0. If a prefix is present, it will be printed before the progress bar. """
else:
""" Base class for any mtools tool that acts on logfile(s). """
""" Constructor. Adds logfile(s) and stdin option to the argument parser. """
else:
if not self.stdin_allowed: raise SystemExit("this tool can't parse input from stdin.")
arg_opts['const'] = LogFile(sys.stdin) arg_opts['action'] = 'store_const' if 'type' in arg_opts: del arg_opts['type'] if 'nargs' in arg_opts: del arg_opts['nargs']
tool = LogFileTool(multiple_logfiles=True, stdin_allowed=True) tool.run() print tool.args # for line in tool.args['logfile']: # print line
|