Skip to content

Commit

Permalink
_load_data
Browse files Browse the repository at this point in the history
  • Loading branch information
hainm committed Nov 28, 2024
1 parent ac6fbae commit 219e49d
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions nglview/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,13 +1108,12 @@ def add_component(self, filename, **kwargs):
return self[-1]

def _load_data(self, obj, **kwargs):
'''
"""
Parameters
----------
obj : nglview.Structure or any object having 'get_structure_string' method or
string buffer (open(fn).read())
'''
"""
kwargs2 = _camelize_dict(kwargs)

try:
Expand All @@ -1125,42 +1124,46 @@ def _load_data(self, obj, **kwargs):
if 'defaultRepresentation' not in kwargs2:
kwargs2['defaultRepresentation'] = True

if not is_url:
if hasattr(obj, 'get_structure_string'):
blob = obj.get_structure_string()
kwargs2['ext'] = obj.ext
passing_buffer = True
binary = False
else:
fh = FileManager(obj,
ext=kwargs.get('ext'),
compressed=kwargs.get('compressed'))
# assume passing string
blob = fh.read(force_buffer=True)
passing_buffer = True

if fh.ext is None and passing_buffer:
raise ValueError('must provide extension')

kwargs2['ext'] = fh.ext
binary = fh.is_binary
use_filename = fh.use_filename

if binary and not use_filename:
# send base64
blob = base64.b64encode(blob).decode('utf8')
blob_type = 'blob' if passing_buffer else 'path'
args = [{'type': blob_type, 'data': blob, 'binary': binary}]
if is_url:
self._load_data_from_url(obj, kwargs2)
else:
# is_url
blob_type = 'url'
url = obj
args = [{'type': blob_type, 'data': url, 'binary': False}]
self._load_data_from_object(obj, kwargs2, kwargs)

def _load_data_from_url(self, url, kwargs2):
args = [{'type': 'url', 'data': url, 'binary': False}]
self._remote_call("loadFile", target='Stage', args=args, kwargs=kwargs2)

def _load_data_from_object(self, obj, kwargs2, kwargs):
if hasattr(obj, 'get_structure_string'):
blob, ext, binary = self._get_structure_string_data(obj)
else:
blob, ext, binary, use_filename = self._get_file_manager_data(obj, kwargs)

if binary and not use_filename:
blob = base64.b64encode(blob).decode('utf8')

args = [{'type': 'blob', 'data': blob, 'binary': binary}]
kwargs2['ext'] = ext
name = py_utils.get_name(obj, **kwargs2)
self._ngl_component_names.append(name)
self._remote_call("loadFile", target='Stage', args=args, kwargs=kwargs2)

def _get_structure_string_data(self, obj):
blob = obj.get_structure_string()
ext = obj.ext
binary = False
return blob, ext, binary

def _get_file_manager_data(self, obj, kwargs):
fh = FileManager(obj, ext=kwargs.get('ext'), compressed=kwargs.get('compressed'))
blob = fh.read(force_buffer=True)
if fh.ext is None:
raise ValueError('must provide extension')
ext = fh.ext
binary = fh.is_binary
use_filename = fh.use_filename
return blob, ext, binary, use_filename

def remove_component(self, c):
"""remove component by its uuid.
If isinstance(c, ComponentViewer), `c` won't be associated with `self`
Expand Down

0 comments on commit 219e49d

Please sign in to comment.