pageit.tools

Tools for changing, serving, and watching paths.

class pageit.tools.Watcher(path=None, callback=None, log=None)

Bases: watchdog.events.FileSystemEventHandler

Handler for file changes.

Parameters:
  • path (str) – path to watch
  • callback (callable) – function to run when files change
  • log (logging.Logger, optional) – logger to use
__enter__()

Enter a context.

Returns:Watcher – this watcher
__exit__(exc_type, exc_value, traceback)

Exit a context.

loop()

Run until CTRL+C is pressed.

Returns:Watcher – for method chaining
on_modified(event=None)

Handle a file modification.

Parameters:event (object) – watchdog event object
Returns:Watcher – for method chaining
start()

Start the underlying observer.

Returns:Watcher – for method chaining
stop()

Stop the underlying observer.

Returns:Watcher – for method chaining
pageit.tools.pushd(*args, **kwds)

Change the current working directory for a context.

Parameters:path (str) – temporary path to change to
Yields:str – absolute path to the new path

Example

>>> cwd = os.getcwd()
>>> with pushd('..') as newpath:
...     os.getcwd() != cwd
True
pageit.tools.serve(path, port=80, log=None)

Serve a path on a given port.

This function will change the working directory to the path and host it on the port specified. If path is not supplied, it returns immediately.

Parameters:
  • path (str) – path to host
  • port (int, optional) – port on which to host; default is 80.
  • log (logging.Logger, optional) – logger to use
pageit.tools.watch(*args, **kwds)

Watch a directory and call the given callback when it changes.

This is a convenience method for Watcher.

Parameters:
  • path (str) – path to watch
  • callback (callable) – callable to call when path changes; will be passed the path to the file that changed
  • log (logging.Logger, optional) – logger to use
Yields:

Watcher – file system event handler for this watch