def get_connection_file(app=None):
    """Return the path to the connection file of an app

    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    from traitlets.utils import filefind
    if app is None:
        from ipykernel.kernelapp import IPKernelApp
        if not IPKernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = IPKernelApp.instance()
    return filefind(app.connection_file, ['.', app.connection_dir])
 def init_connection_file(self):
     if not self.connection_file:
         self.connection_file = "kernel-%s.json"%os.getpid()
     try:
         self.connection_file = filefind(self.connection_file, ['.', self.connection_dir])
     except IOError:
         self.log.debug("Connection file not found: %s", self.connection_file)
         # This means I own it, and I'll create it in this directory:
         os.makedirs(os.path.dirname(self.abs_connection_file), mode=0o700, exist_ok=True)
         # Also, I will clean it up:
         atexit.register(self.cleanup_connection_file)
         return
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
         self.exit(1)