def __init__(self, fn, In = None, Out = None): if In is None: In = sys.stdin if Out is None: Out = sys.stdout self.fn = fn self.chan = Chan() self.chan.set_channels(In, Out) self.filewatch = FileWatcher(fn) self.code_locals = {} try: self.load_code(self.code_locals) self.filewatch.note_new_files() self.chan.send_cmd('ready') except: self.filewatch.note_new_files() self.chan.send_cmd('exception', string = traceback.format_exc())
class Reloader(object): @save_args def __init__(self, get_app, watch, skip=SKIP, exclude='*.pyc'): self.app = self.get_app() self.watcher = FileWatcher(watch, exclude=exclude) def __call__(self, environ, start_response): if self.watcher.changed(): del self.app for i in set(sys.modules.keys()).difference(self.skip): del sys.modules[i] self.app = self.get_app() return self.app(environ, start_response)
def synchronize_drive(self): shared_folder = os.path.join(self.root_folder, "SharedWithMe") # synchronize drive files self._synchronize_files("root", self.root_folder) # synchronize shared files # self._synchronize_files_by_type(shared_folder, 'sharedWithMe') # start watching files for changes local_watcher = FileWatcher(self, self.root_folder) local_watcher.start() drive_watcher = DriveChanges(self) drive_watcher.start() try: while True: time.sleep(1) except (KeyboardInterrupt, SystemExit): local_watcher.stop() drive_watcher.stop() local_watcher.join() drive_watcher.join()
# Starting point of MatroxBot import re import time import sys from messageparse import MessageParser from matroxcommand import MatroxCommandManager from permissionsmanager import PermissionsManager from announcementmanager import AnnouncementManager from chatmanager import ChatManager from filewatcher import FileWatcher # Make sure you prefix the quotes with an 'r'! CHAT_MSG=re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") # Start File Monitoring FileWatcher.Instance().startFileWatcherThread() # Open connection to the target chat ChatManager.Instance().openChatConnection() messageParser = MessageParser() commandManager = MatroxCommandManager() permissionsManager = PermissionsManager() # Start auto announcements AnnouncementManager.Instance().startAnnouncementThread() while True: # Blocking socket receive call response = ChatManager.Instance().receiveMessage() if response == "ping":
def __init__(self, get_app, watch, skip=SKIP, exclude='*.pyc'): self.app = self.get_app() self.watcher = FileWatcher(watch, exclude=exclude)
class Runner: STOP = 0 CONTINUE = 1 def __init__(self, fn, In = None, Out = None): if In is None: In = sys.stdin if Out is None: Out = sys.stdout self.fn = fn self.chan = Chan() self.chan.set_channels(In, Out) self.filewatch = FileWatcher(fn) self.code_locals = {} try: self.load_code(self.code_locals) self.filewatch.note_new_files() self.chan.send_cmd('ready') except: self.filewatch.note_new_files() self.chan.send_cmd('exception', string = traceback.format_exc()) def run_cmd(self, cmd): if cmd['action'] == 'shutdown': return Runner.STOP elif cmd['action'] == 'get_locals': value = self.code_locals.get(cmd['var'], '') self.chan.send_cmd('locals', val = value) elif cmd['action'] == 'set_reload_interval': self.filewatch.interval = cmd['value'] elif cmd['action'] == 'run_func': self.run_func(cmd) elif cmd['action'] == 'crash': sys.exit() elif cmd['action'] == 'abort': # client disconnected, but script was finished by that time pass elif cmd['action'] == 'source_changed': value = 1 if self.filewatch.has_changed() else 0 self.chan.send_cmd('', value = value) else: log('runner.py has idea was this incoming is:\n', repr(cmd)) raise NotImplementedError return Runner.CONTINUE def loop(self): import time while 1: cmd = self.chan.recv_cmd() #log("Test %d %s\n" % (time.time(), repr(cmd))) if self.run_cmd(cmd) == Runner.STOP: return def load_code(self, my_locals): with open(self.fn) as f: code_src = f.read() try: self.code = compile(code_src, os.path.abspath(self.fn), 'exec') except Exception, e: hosts_line = re.findall('^(hosts\s*=[^\n]+)', code_src) if hosts_line: c = compile(hosts_line[0], '', 'exec') exec c in my_locals raise e exec self.code in my_locals
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with pysched. If not, see <http://www.gnu.org/licenses/>. from pysched import Scheduler from filewatcher import FileWatcher print """ This script creates two FileWatcher processes, each watching for one file. The files watched for are called "file1" and "file2". The scheduler will execute the appropriate process if one of the 2 files are created. After both processes have excuted the scheduler will exit. """ sched = Scheduler() proc1 = FileWatcher("proc1", "file1") proc2 = FileWatcher("proc2", "file2") sched.add_process(proc1) sched.add_process(proc2) while not sched.finished(): sched.check_events() sched.run_next()