Skip to content

Commit

Permalink
Added fullpath to jupyter_server_extension (#3270)
Browse files Browse the repository at this point in the history
* Added fullpath to jupyter_server_extension

* Added expanduser where needed elsewhere

* Moved fullpath to util

* Added a context manager
  • Loading branch information
hoxbro committed Mar 28, 2022
1 parent 0e61f37 commit 7878f78
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/generate_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def recurse_tree(path, excludes, opts):
"""
# use absolute path for root, as relative paths like '../../foo' cause
# 'if "/." in root ...' to filter out *all* modules otherwise
path = os.path.abspath(path)
path = os.path.abspath(os.path.expanduser(path))
# check if the base directory is a package and get is name
if INIT in os.listdir(path):
package_name = path.split(os.path.sep)[-1]
Expand Down
3 changes: 2 additions & 1 deletion panel/command/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from ..auth import OAuthProvider
from ..config import config
from ..util import fullpath
from ..io.rest import REST_PROVIDERS
from ..io.reload import record_modules, watch
from ..io.server import INDEX_HTML, get_static_routes, set_curdoc
Expand Down Expand Up @@ -267,7 +268,7 @@ def customize_kwargs(self, args, server_kwargs):
code = compile(nodes, filename=setup_path, mode='exec', dont_inherit=True)
module_name = 'panel_setup_module'
module = ModuleType(module_name)
module.__dict__['__file__'] = os.path.abspath(setup_path)
module.__dict__['__file__'] = fullpath(setup_path)
exec(code, module.__dict__)
state._setup_module = module

Expand Down
9 changes: 5 additions & 4 deletions panel/io/jupyter_server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from tornado.web import StaticFileHandler

from ..config import config
from ..util import edit_readonly
from ..util import edit_readonly, fullpath
from .state import state
from .resources import DIST_DIR, Resources

Expand Down Expand Up @@ -75,12 +75,13 @@ def root_dir(self):
may be different from the root dir.
Reference: https://github.com/holoviz/panel/issues/3170
"""
return self._app.settings['server_root_dir']
return fullpath(self._app.settings['server_root_dir'])

def __getattr__(self, key):
return getattr(self._app, key)



class PanelHandler(DocHandler):

def __init__(self, app, request, *args, **kw):
Expand All @@ -93,7 +94,7 @@ def initialize(self, *args, **kws):
pass

async def get(self, path, *args, **kwargs):
path = os.path.join(self.application.root_dir, path)
path = os.path.join(self.application.root_dir, fullpath(path))
if path in _APPS:
app, context = _APPS[path]
else:
Expand Down Expand Up @@ -135,7 +136,7 @@ def initialize(self, *args, **kwargs):
pass

async def open(self, path, *args, **kwargs):
path = os.path.join(self.application.root_dir, path)
path = os.path.join(self.application.root_dir, fullpath(path))
_, context = _APPS[path]

token = self._token
Expand Down
3 changes: 2 additions & 1 deletion panel/io/reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .callbacks import PeriodicCallback
from .state import state
from ..util import fullpath

_watched_files = set()
_modules = set()
Expand Down Expand Up @@ -103,7 +104,7 @@ def record_modules():
else:
filepath = spec.origin

filepath = os.path.abspath(filepath)
filepath = fullpath(filepath)

if filepath is None or in_blacklist(filepath):
continue
Expand Down
4 changes: 2 additions & 2 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from tornado.wsgi import WSGIContainer

# Internal imports
from ..util import edit_readonly
from ..util import edit_readonly, fullpath
from .document import init_doc, with_lock, unlocked # noqa
from .logging import LOG_SESSION_CREATED, LOG_SESSION_DESTROYED, LOG_SESSION_LAUNCHING
from .profile import profile_ctx
Expand Down Expand Up @@ -499,7 +499,7 @@ def get_static_routes(static_dirs):
if slug == '/static':
raise ValueError("Static file route may not use /static "
"this is reserved for internal use.")
path = os.path.abspath(path)
path = fullpath(path)
if not os.path.isdir(path):
raise ValueError("Cannot serve non-existent path %s" % path)
patterns.append(
Expand Down
5 changes: 3 additions & 2 deletions panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .layout import Column, Panel, Row, Spacer, Tabs
from .pane.base import PaneBase, ReplacementPane
from .util import (
abbreviated_repr, classproperty, full_groupby, get_method_owner,
abbreviated_repr, classproperty, full_groupby, fullpath, get_method_owner,
is_parameterized, param_name, recursive_parameterized
)
from .reactive import Reactive
Expand Down Expand Up @@ -943,7 +943,8 @@ def __call__(self, parameterized):
if self.json_file or env_var.endswith('.json'):
try:
fname = self.json_file if self.json_file else env_var
spec = json.load(open(os.path.abspath(fname), 'r'))
with open(fullpath(fname), 'r') as f:
spec = json.load(f)
except Exception:
warnobj.warning('Could not load JSON file %r' % spec)
else:
Expand Down
6 changes: 6 additions & 0 deletions panel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,9 @@ def parse_timedelta(time_str):
if p:
time_params[name] = float(p)
return dt.timedelta(**time_params)


def fullpath(path):
"""Expanduser and then abspath for a given path
"""
return os.path.abspath(os.path.expanduser(path))
11 changes: 6 additions & 5 deletions panel/widgets/file_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..io import PeriodicCallback
from ..layout import Column, Divider, Row
from ..viewable import Layoutable
from ..util import fullpath
from .base import CompositeWidget
from .button import Button
from .input import TextInput
Expand Down Expand Up @@ -92,10 +93,10 @@ class FileSelector(CompositeWidget):
def __init__(self, directory=None, **params):
from ..pane import Markdown
if directory is not None:
params['directory'] = os.path.abspath(os.path.expanduser(directory))
params['directory'] = fullpath(directory)
if 'root_directory' in params:
root = params['root_directory']
params['root_directory'] = os.path.abspath(os.path.expanduser(root))
params['root_directory'] = fullpath(root)
if params.get('width') and params.get('height') and 'sizing_mode' not in params:
params['sizing_mode'] = None

Expand Down Expand Up @@ -162,7 +163,7 @@ def _update_value(self, event):
self.value = value

def _dir_change(self, event):
path = os.path.abspath(os.path.expanduser(self._directory.value))
path = fullpath(self._directory.value)
if not path.startswith(self._root_directory):
self._directory.value = self._root_directory
return
Expand All @@ -174,7 +175,7 @@ def _refresh(self):
self._update_files(refresh=True)

def _update_files(self, event=None, refresh=False):
path = os.path.abspath(self._directory.value)
path = fullpath(self._directory.value)
refresh = refresh or (event and getattr(event, 'obj', None) is self._reload)
if refresh:
path = self._cwd
Expand Down Expand Up @@ -233,7 +234,7 @@ def _select(self, event):
return

relpath = event.new[0].replace('📁', '')
sel = os.path.abspath(os.path.join(self._cwd, relpath))
sel = fullpath(os.path.join(self._cwd, relpath))
if os.path.isdir(sel):
self._directory.value = sel
else:
Expand Down

0 comments on commit 7878f78

Please sign in to comment.