def test_default_configuration(self): """Check we can load the default configuration""" conf = Conf() loaded = conf.load() self.assertEqual(conf, loaded) self.assertEqual(conf.mibs, []) self.assertEqual(conf.ipython, True) self.assertEqual(conf.prompt, "\033[1m[snimpy]>\033[0m ")
def test_loading_custom_configuration(self): conffile = tempfile.NamedTemporaryFile(delete=False) try: conffile.write(""" mibs = [ "IF-MIB", "LLDP-MIB" ] ipython = False unknown = "hey!" """.encode("ascii")) conffile.close() conf = Conf().load(conffile.name) self.assertEqual(conf.mibs, ["IF-MIB", "LLDP-MIB"]) self.assertEqual(conf.unknown, "hey!") self.assertEqual(conf.ipython, False) self.assertEqual(conf.ipythonprofile, None) finally: os.unlink(conffile.name)
def interact(argv=sys.argv): # pragma: no cover conf = Conf().load() banner = "\033[1mSnimpy\033[0m ({0}) -- {1}.\n".format( snimpy.__version__, snimpy.__doc__) banner += " load -> load an additional MIB\n" banner += " M -> manager object" local = { "conf": conf, "M": manager.Manager, "load": manager.load, "timedelta": timedelta, "snmp": manager.snmp } if len(argv) <= 1: manager.Manager._complete = True for ms in conf.mibs: manager.load(ms) globals().update(local) if len(argv) > 1: argv = argv[1:] exec(compile(open(argv[0]).read(), argv[0], 'exec')) in local return try: try: try: # ipython >= 1.0 from IPython.terminal.embed import \ InteractiveShellEmbed except ImportError: # ipython >= 0.11 from IPython.frontend.terminal.embed import \ InteractiveShellEmbed try: # ipython >= 4 from traitlets.config.loader import Config except ImportError: # ipython >= 0.11 from IPython.config.loader import Config cfg = Config() try: # >= 0.12 cfg.PromptManager.in_template = "Snimpy [\\#]> " cfg.PromptManager.out_template = "Snimpy [\\#]: " except ImportError: # 0.11 cfg.InteractiveShellEmbed.prompt_in1 = "Snimpy [\\#]> " cfg.InteractiveShellEmbed.prompt_out = "Snimpy [\\#]: " if conf.ipythonprofile: cfg.InteractiveShellEmbed.profile = conf.ipythonprofile shell = InteractiveShellEmbed(config=cfg, banner1=banner, user_ns=local) # Not interested by traceback in this module shell.InteractiveTB.tb_offset += 1 except ImportError: # ipython < 0.11 from IPython.Shell import IPShellEmbed argv = [ "-prompt_in1", "Snimpy [\\#]> ", "-prompt_out", "Snimpy [\\#]: " ] if conf.ipythonprofile: argv += ["-profile", conf.ipythonprofile] shell = IPShellEmbed(argv=argv, banner=banner, user_ns=local) # Not interested by traceback in this module shell.IP.InteractiveTB.tb_offset += 1 except ImportError: shell = None if shell and conf.ipython: shell() else: try: import rlcompleter import readline except ImportError: readline = None if readline: if conf.histfile: try: readline.read_history_file( os.path.expanduser(conf.histfile)) except IOError: pass atexit.register(lambda: readline.write_history_file( os.path.expanduser(conf.histfile))) readline.set_completer(rlcompleter.Completer(local).complete) readline.parse_and_bind("tab: menu-complete") sys.ps1 = conf.prompt code.interact(banner=banner, local=local)
def test_inexistent_configuration(self): conf = Conf().load("dontexist") self.assertEqual(conf.mibs, []) self.assertEqual(conf.ipython, True)