Repository: jupyterhub/jupyterhub

Issue:
Jupyterhub prespawn hook to modify spawner args
Hi,

I'm trying to use the prespawn hook function modify the spawner notebook directory but it seems that its not getting picked up. 

I'm using Jupyterhub 0.8.1 and LocalProcessSpawner. I've modified the jupyterhub_config.py and added the following:

```
def notebook_dir_hook(spawner):
    import sqlite3
    conn = sqlite3.connect("/src/jupyterhub-service/jupyterdata.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM notebook_dir WHERE name=?", (spawner.user.name,))
    row = cur.fetchone()
    cur.close()
    if row:
        spawner.notebook_dir = row[1]
        spawner.log.info("Spawner notebook_dir  %s", spawner.notebook_dir)
    #spawner.log.info("Spawner args  %s", spawner.get_args())

c.Spawner.pre_spawn_hook = notebook_dir_hook
```

The last line of the pre_spawn_hook function which is commented out, gives an error if run.

```
Traceback (most recent call last):
      File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 1543, in _execute
        result = yield result
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/usr/lib/python3.5/types.py", line 179, in throw
        return self.__wrapped.throw(tp, *rest)
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/apihandlers/users.py", line 209, in post
        yield self.spawn_single_user(user, server_name, options=options)
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/handlers/base.py", line 475, in spawn_single_user
        yield gen.with_timeout(timedelta(seconds=self.slow_spawn_timeout), finish_spawn_future)
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/handlers/base.py", line 445, in finish_user_spawn
        yield spawn_future
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/user.py", line 439, in spawn
        raise e
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/user.py", line 374, in spawn
        yield gen.maybe_future(self.spawner.run_pre_spawn_hook())
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/spawner.py", line 649, in run_pre_spawn_hook
        return self.pre_spawn_hook(self)
      File "/src/jupyterhub-service/jupyterhub_config.py", line 584, in notebook_dir_hook
        spawner.log.info("username_solution  %s", spawner.get_args())
      File "/usr/local/lib/python3.5/dist-packages/jupyterhub/spawner.py", line 628, in get_args
        elif self.server.port:
    AttributeError: 'NoneType' object has no attribute 'port'
```

Any ideas how to do this? 


Discussion:

Resolving patch:
diff --git a/jupyterhub/spawner.py b/jupyterhub/spawner.py
--- a/jupyterhub/spawner.py
+++ b/jupyterhub/spawner.py
@@ -667,7 +667,7 @@ def get_args(self):
 
         if self.port:
             args.append('--port=%i' % self.port)
-        elif self.server.port:
+        elif self.server and self.server.port:
             self.log.warning("Setting port from user.server is deprecated as of JupyterHub 0.7.")
             args.append('--port=%i' % self.server.port)
 


Test patch: