#NEWLINE# def custom( srcpaths , event_cb = None , poll_interval = 1 , recurse = True , restart_cb = None , restart_func = None , close_fds = True ) : #NEWLINE# #INDENT# "Sets up lazarus in custom mode.\n\n    See the :py:func:`default` function for a simpler mode of use.\n\n    The custom mode of lazarus is to watch all modules rooted at any of the\n    source paths provided for changes and restart when they take place.\n\n    Keyword arguments:\n\n        event_cb -- Callback invoked when a file rooted at a source path\n        changes. Without specifying an event callback, changes to any module\n        rooted at a source path will trigger a restart.\n\n        poll_interval -- Rate at which changes will be detected. The default\n        value of ``1`` means it may take up to one second to detect changes.\n        Decreasing this value may lead to unnecessary overhead.\n\n        recurse -- Whether to watch all subdirectories of every source path for\n        changes or only the paths provided.\n\n        restart_cb -- Callback invoked prior to restarting the process; allows\n        for any cleanup to occur prior to restarting. Returning anything other\n        than *None* in the callback will cancel the restart.\n\n        restart_func -- Function invoked to restart the process. This supplants\n        the default behavior of using *sys.executable* and *sys.argv*.\n\n        close_fds -- Whether all file descriptors other than *stdin*, *stdout*,\n        and *stderr* should be closed\n\n    An example of using a cleanup function prior to restarting:\n\n        >>> def cleanup():\n        ...     pass\n        >>> import lazarus\n        >>> lazarus.custom(os.curdir, restart_cb=cleanup)\n        >>> lazarus.stop()\n\n    An example of avoiding restarts when any ``__main__.py`` changes:\n\n        >>> def skip_main(event):\n        ...     if event.src_path == '__main__.py':\n        ...         return False\n        ...     return True\n        >>> import lazarus\n        >>> lazarus.custom(os.curdir, event_cb=skip_main)\n        >>> lazarus.stop()\n    " #NEWLINE# if _active : #NEWLINE# #INDENT# msg = 'lazarus is already active' #NEWLINE# raise RuntimeWarning ( msg ) #NEWLINE# #UNINDENT# if ( restart_cb and ( not callable ( restart_cb ) ) ) : #NEWLINE# #INDENT# msg = 'restart_cb keyword argument is not callable' #NEWLINE# raise TypeError ( msg ) #NEWLINE# #UNINDENT# if ( restart_func and ( not callable ( restart_func ) ) ) : #NEWLINE# #INDENT# msg = 'restart_func keyword argument is not callable' #NEWLINE# raise TypeError ( msg ) #NEWLINE# #UNINDENT# global _close_fds #NEWLINE# _close_fds = close_fds #NEWLINE# try : #NEWLINE# #INDENT# from watchdog.observers import Observer #NEWLINE# from watchdog.events import FileSystemEventHandler #NEWLINE# #UNINDENT# except ImportError as ie : #NEWLINE# #INDENT# msg = ( 'no watchdog support (%s)' % str ( ie ) ) #NEWLINE# raise RuntimeError ( kwargs ) #NEWLINE# #UNINDENT# class _Handler ( FileSystemEventHandler ) : #NEWLINE# #INDENT# def __init__( self ) : #NEWLINE# #INDENT# self . active = True #NEWLINE# #UNINDENT# def dispatch( self , event ) : #NEWLINE# #INDENT# if ( not self . active ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# super ( _Handler , self ) . dispatch ( event ) #NEWLINE# #UNINDENT# def all_events( self , event ) : #NEWLINE# #INDENT# if event_cb : #NEWLINE# #INDENT# if event_cb ( event ) : #NEWLINE# #INDENT# cancelled = _restart ( ) #NEWLINE# if ( not cancelled ) : #NEWLINE# #INDENT# self . active = False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if is_restart_event ( event ) : #NEWLINE# #INDENT# cancelled = _restart ( ) #NEWLINE# if ( not cancelled ) : #NEWLINE# #INDENT# self . active = False #NEWLINE# #UNINDENT# self . active = False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def on_created( self , event ) : #NEWLINE# #INDENT# self . all_events ( event ) #NEWLINE# #UNINDENT# def on_deleted( self , event ) : #NEWLINE# #INDENT# self . all_events ( event ) #NEWLINE# #UNINDENT# def on_modified( self , event ) : #NEWLINE# #INDENT# self . all_events ( event ) #NEWLINE# #UNINDENT# def on_moved( self , event ) : #NEWLINE# #INDENT# self . all_events ( event ) #NEWLINE# #UNINDENT# #UNINDENT# global _observer #NEWLINE# kwargs = { #NEWLINE# #INDENT# 'timeout' : poll_interval , #NEWLINE# #UNINDENT# } #NEWLINE# _observer = Observer ( ** kwargs ) #NEWLINE# global _restart_cb #NEWLINE# _restart_cb = restart_cb #NEWLINE# handler = _Handler ( ) #NEWLINE# srcpaths = _as_list ( srcpaths ) #NEWLINE# kwargs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if recurse : #NEWLINE# #INDENT# kwargs [ 'recursive' ] = True #NEWLINE# #UNINDENT# for srcpath in srcpaths : #NEWLINE# #INDENT# _observer . schedule ( handler , srcpath , ** kwargs ) #NEWLINE# #UNINDENT# _activate ( ) #NEWLINE# _observer . start ( )