Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from code import InteractiveConsole
  2. from os.path import getmtime, basename
  3. from datetime import datetime
  4. import sys
  5.  
  6. class AutoReloader(InteractiveConsole):
  7.     watch_pkg = None
  8.     last_load_time = None
  9.     def __init__(self, locals=None, filename="<console>", watch_pkg=None, watch_file=None):
  10.         super().__init__(locals=locals, filename=filename)
  11.         if watch_file is not None and watch_file.endswith('.py'):
  12.             watch_pkg = watch_file.split('.')[0]
  13.         if watch_pkg is not None:
  14.             self.watch_pkg = watch_pkg
  15.             self.runcode('import {0}'.format(self.watch_pkg)) # Necessary to be able to reload the funcs
  16.             self.runcode('from {0} import *'.format(self.watch_pkg))
  17.             self.last_load_time = datetime.utcnow()
  18.             self.runcode('from importlib import reload')
  19.  
  20.     def runsource(self, source, filename="<input>", symbol="single"):
  21.         if self.watch_pkg is not None and self.was_modified():
  22.             self.runcode('reload({0})'.format(self.watch_pkg)) # Reload function definitions...
  23.             self.runcode('from {0} import *'.format(self.watch_pkg)) # ...and pull them into interpreter namespace.
  24.         return super().runsource(source, filename=filename, symbol=symbol)
  25.  
  26.     def was_modified(self):
  27.         if self.last_load_time is None:
  28.             return False
  29.  
  30.         try:
  31.             modtime = datetime.utcfromtimestamp(getmtime('{0}.py'.format(self.watch_pkg)))
  32.             if modtime > self.last_load_time:
  33.                 self.last_load_time = datetime.utcnow()
  34.                 return True
  35.             else:
  36.                 return False
  37.         except:
  38.             print('Could not find file {0} to reload package {1}. Disabling reload.'.format('{0}.py'.format(self.watch_pkg), self.watch_pkg))
  39.             self.watch_pkg = None
  40.             self.last_load_time = None
  41.             return False
  42.  
  43.     def showtraceback(self):
  44.         type, value, tb = sys.exc_info()
  45.         if type == ImportError and value.name == self.watch_pkg:
  46.             print('Loading package {0} failed.'.format(self.watch_pkg))
  47.             self.watch_pkg = None
  48.             self.last_load_time = None
  49.         else:
  50.             return super().showtraceback()
  51.  
  52.     def interact(self):
  53.         banner = None
  54.         if self.watch_pkg is not None:
  55.             banner = 'AutoReloading interpreter loaded watching package: {0}'.format(self.watch_pkg)
  56.         else:
  57.             banner = 'AutoReloading interpreter loaded without a package to watch'
  58.         return super().interact(banner=banner)
  59.  
  60.     @staticmethod
  61.     def run_console():
  62.         from inspect import stack, getmodule
  63.         frame = stack()[1]
  64.         module = getmodule(frame[0])
  65.         AutoReloader(watch_file=basename(module.__file__)).interact()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement