def get_img_info(toyz_settings, tid, params): """ Map a large image into a set of tiles that make up the larger image """ import toyz.web.viewer as viewer print('************************************************') core.check4keys(params, ['img_info', 'file_info']) if tid['user_id']!='admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.') shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) save_path = os.path.join(shortcuts['temp'], tid['session_id'], 'images') params['img_info']['save_path'] = save_path img_info = viewer.get_img_info(params['file_info'], params['img_info']) result = get_tile_info(toyz_settings, tid, { 'file_info': params['file_info'], 'img_info': img_info }) img_info['tiles'] = result['new_tiles'] response = { 'id': 'img info', 'img_info': img_info, 'new_tiles': result['new_tiles'] } print('************************************************') return response
def save_workspace(toyz_settings, tid, params): """ Save a workspace for later use """ core.check4keys(params, ['workspaces', 'overwrite']) workspaces = db_utils.get_param(toyz_settings.db, 'workspaces', user_id=tid['user_id']) work_id = params['workspaces'].keys()[0] if work_id in workspaces and params['overwrite'] is False: response = { 'id': 'verify', 'func': 'save_workspace' } else: user_id = tid['user_id'] if 'user_id' in params and params['user_id']!=tid['user_id']: params['work_id'] = work_id permissions = core.get_workspace_permissions(toyz_settings, tid, params) if permissions['modify']: user_id = params['user_id'] else: raise ToyzJobError( "You do not have permission to save {0}".format(params['work_id'])) db_utils.update_param(toyz_settings.db, 'workspaces', workspaces=params['workspaces'], user_id=user_id) response = { 'id': 'notification', 'msg': 'Workspace saved successfully', 'func': 'save_workspace' } return response
def run_sextractor(toyz_settings, tid, params): """ Run sextractor using parameters defined in the client """ import astrotoyz.astromatic.sex as sex import toyz.utils.db import shutil core.check4keys(params, ['config', 'params', 'filename']) shortcuts = toyz.utils.db.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) # Just in case the user did something dumb, like remove his/her temp path if 'temp' not in shortcuts: raise ToyJobError("You removed your 'temp' shortcut, DON'T DO THAT!" + "Refresh your browser and it will be restored") # Create a path for temporary files that will be erased once this has been completed temp_path = os.path.join(shortcuts['temp'], tid['session_id'], str(tid['request_id'])) core.create_paths(temp_path) # Run SExtractor params['temp_path'] = temp_path result = sex.run_sextractor(**params) # Remove the temporary path form the server #shutil.rmtree(temp_path) if result != 'success': response = {'id': 'ERROR', 'error': result} else: response = {'id': 'run_sextractor', 'result': result} return response
def get_img_data(toyz_settings, tid, params): """ Get data from an image or FITS file """ core.check4keys(params, ['data_type', 'file_info', 'img_info']) response = astro.viewer.get_img_data(**params) return response
def wcs2px(toyz_Settings, tid, params): """ Align all images in the viewer with the world coordinates of the current image """ import toyz.web from astrotoyz.viewer import get_wcs import numpy as np print('filepath:', params['file_info']['filepath'], '\n\n') core.check4keys(params, ['file_info', 'img_info', 'ra', 'dec']) hdulist = toyz.web.viewer.get_file(params['file_info']) wcs = get_wcs(params['file_info'], hdulist) if wcs is None: raise astrotoyz.core.AstroToyzError( 'Unable to load WCS for the current image') data = hdulist[int(params['file_info']['frame'])].data ra = params['ra'] dec = params['dec'] wcs_array = wcs.wcs_world2pix(np.array([[ra, dec]]), 1) response = { 'id': 'wcs2px', 'x': int(round(wcs_array[0][0])), 'y': int(round(wcs_array[0][1])), # next two lines for testing 'ra': ra, 'dec': dec } print('px coords response', response) return response
def create_paths(toyz_settings, tid, params): """ Creates a new path on the server (if it does not already exist). Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): path to create on the server Response id: 'create_folder', status (*string* ): 'success', path (*string* ): path created on the server """ core.check4keys(params, ['path', 'new_folder']) path = os.path.join(params['path'], params['new_folder']) permissions = file_access.get_parent_permissions(toyz_settings.db, path, user_id=tid['user_id']) if 'w' not in permissions and 'x' not in permissions: raise ToyzJobError( "You do not have permission to create path {0}".format(path)) core.create_paths(path) response = { 'id': 'notification', 'msg': 'Created path'.format(path), 'func': 'create_dir' } return response
def get_file_info(toyz_settings, tid, params): """ Get information about an image file """ import toyz.web.viewer as viewer core.check4keys(params, ['file_info', 'img_info']) if tid['user_id'] != 'admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if permissions is None or 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.' ) file_info = viewer.get_file_info(params['file_info']) img_info = file_info['images'][file_info['frame']] img_info.update(params['img_info']) # Get the tile map for the first image result = get_img_info(toyz_settings, tid, { 'file_info': file_info, 'img_info': img_info }) file_info['images'][file_info['frame']] = result['img_info'] response = { 'id': 'file info', 'file_info': file_info, 'new_tiles': result['new_tiles'] } return response
def get_img_tile(toyz_settings, tid, params): """ Load a tile from a larger image and notify the client it has been created """ import toyz.web.viewer as viewer core.check4keys(params, ['img_info', 'file_info', 'tile_info']) if tid['user_id'] != 'admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.' ) created, tile_info = viewer.create_tile(params['file_info'], params['img_info'], params['tile_info']) response = { 'id': 'tile created', 'success': created, 'tile_info': tile_info } return response
def get_file_info(toyz_settings, tid, params): """ Get information about an image file """ import toyz.web.viewer as viewer core.check4keys(params, ['file_info', 'img_info']) if tid['user_id']!='admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if permissions is None or 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.') file_info = viewer.get_file_info(params['file_info']) img_info = file_info['images'][file_info['frame']] img_info.update(params['img_info']) # Get the tile map for the first image result = get_img_info(toyz_settings, tid, { 'file_info': file_info, 'img_info': img_info }) file_info['images'][file_info['frame']] = result['img_info'] response = { 'id': 'file info', 'file_info': file_info, 'new_tiles': result['new_tiles'] } return response
def get_tile_info(toyz_settings, tid, params): """ Get new tiles that need to be loaded """ import toyz.web.viewer as viewer core.check4keys(params, ['img_info', 'file_info']) if tid['user_id'] != 'admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.' ) all_tiles, new_tiles = viewer.get_tile_info(params['file_info'], params['img_info']) #print('all tile:', all_tiles) response = { 'id': 'tile info', 'all_tiles': all_tiles, 'new_tiles': new_tiles } return response
def create_catalog(toyz_settings, tid, params): """ Save a catalog """ core.check4keys(params, ['settings', 'cid']) catalog = astro.catalog.get_catalog(create='yes', **params) # Store the catalog in the session for future use session_vars.catalogs[catalog.cid] = catalog # Translate the dataframe to a format that can be JSON encoded # (this includes translating NaN values to strings that will be # translated back to NaN in the client) index = list(catalog.index.names) dataframe = { 'columns': index + catalog.columns.values.tolist(), 'index': index, 'data': catalog.astype(object).fillna('NaN').values.tolist() } cat_info = { 'cid': catalog.cid, 'name': catalog.name, 'settings': catalog.settings, 'dataframe': dataframe } response = {'id': 'create_catalog', 'cat_info': cat_info} return response
def run_sextractor(toyz_settings, tid, params): """ Run sextractor using parameters defined in the client """ import astrotoyz.astromatic.sex as sex import toyz.utils.db import shutil core.check4keys(params, ['config', 'params', 'filename']) shortcuts = toyz.utils.db.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) # Just in case the user did something dumb, like remove his/her temp path if 'temp' not in shortcuts: raise ToyJobError("You removed your 'temp' shortcut, DON'T DO THAT!" + "Refresh your browser and it will be restored") # Create a path for temporary files that will be erased once this has been completed temp_path = os.path.join(shortcuts['temp'], tid['session_id'], str(tid['request_id'])) core.create_paths(temp_path) # Run SExtractor params['temp_path'] = temp_path result = sex.run_sextractor(**params) # Remove the temporary path form the server #shutil.rmtree(temp_path) if result!='success': response = { 'id': 'ERROR', 'error': result } else: response = { 'id': 'run_sextractor', 'result': result } return response
def save_workspace(toyz_settings, tid, params): """ Save a workspace for later use """ core.check4keys(params, ['workspaces', 'overwrite']) workspaces = db_utils.get_param(toyz_settings.db, 'workspaces', user_id=tid['user_id']) work_id = params['workspaces'].keys()[0] if work_id in workspaces and params['overwrite'] is False: response = {'id': 'verify', 'func': 'save_workspace'} else: user_id = tid['user_id'] if 'user_id' in params and params['user_id'] != tid['user_id']: params['work_id'] = work_id permissions = core.get_workspace_permissions( toyz_settings, tid, params) if permissions['modify']: user_id = params['user_id'] else: raise ToyzJobError( "You do not have permission to save {0}".format( params['work_id'])) db_utils.update_param(toyz_settings.db, 'workspaces', workspaces=params['workspaces'], user_id=user_id) response = { 'id': 'notification', 'msg': 'Workspace saved successfully', 'func': 'save_workspace' } return response
def load_workspace(toyz_settings, tid, params): """ Load a workspace """ core.check4keys(params, ['work_id']) user_id = tid['user_id'] if 'user_id' in params and params['user_id'] != tid['user_id']: permissions = core.get_workspace_permissions(toyz_settings, tid, params) print('permissions', permissions) if permissions['view']: user_id = params['user_id'] else: raise ToyzJobError("You do not have permission to load {0}".format( params['work_id'])) workspaces = db_utils.get_param(toyz_settings.db, 'workspaces', user_id=user_id) if params['work_id'] not in workspaces: raise ToyzJobError("{0} not found in your workspaces".format( params['work_id'])) response = { 'id': 'workspace', 'work_id': params['work_id'], 'settings': workspaces[params['work_id']] } return response
def get_tile_info(toyz_settings, tid, params): """ Get new tiles that need to be loaded """ import toyz.web.viewer as viewer core.check4keys(params, ['img_info', 'file_info']) if tid['user_id']!='admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.') all_tiles, new_tiles = viewer.get_tile_info(params['file_info'], params['img_info']) #print('all tile:', all_tiles) response = { 'id': 'tile info', 'all_tiles': all_tiles, 'new_tiles': new_tiles } return response
def wcs2px(toyz_Settings, tid, params): """ Align all images in the viewer with the world coordinates of the current image """ import toyz.web from astrotoyz.viewer import get_wcs import numpy as np print('filepath:', params['file_info']['filepath'],'\n\n') core.check4keys(params, ['file_info', 'img_info', 'ra', 'dec']) hdulist = toyz.web.viewer.get_file(params['file_info']) wcs = get_wcs(params['file_info'], hdulist) if wcs is None: raise astrotoyz.core.AstroToyzError('Unable to load WCS for the current image') data = hdulist[int(params['file_info']['frame'])].data ra = params['ra'] dec = params['dec'] wcs_array = wcs.wcs_world2pix(np.array([[ra, dec]]), 1) response = { 'id': 'wcs2px', 'x': int(round(wcs_array[0][0])), 'y': int(round(wcs_array[0][1])), # next two lines for testing 'ra': ra, 'dec': dec } print('px coords response', response) return response
def get_img_tile(toyz_settings, tid, params): """ Load a tile from a larger image and notify the client it has been created """ import toyz.web.viewer as viewer core.check4keys(params, ['img_info', 'file_info', 'tile_info']) if tid['user_id']!='admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.') created, tile_info = viewer.create_tile( params['file_info'], params['img_info'], params['tile_info']) response = { 'id': 'tile created', 'success': created, 'tile_info': tile_info } return response
def create_catalog(toyz_settings, tid, params): """ Save a catalog """ core.check4keys(params, ['settings', 'cid']) catalog = astro.catalog.get_catalog(create='yes', **params) # Store the catalog in the session for future use session_vars.catalogs[catalog.cid] = catalog # Translate the dataframe to a format that can be JSON encoded # (this includes translating NaN values to strings that will be # translated back to NaN in the client) index = list(catalog.index.names) dataframe = { 'columns': index+catalog.columns.values.tolist(), 'index': index, 'data': catalog.astype(object).fillna('NaN').values.tolist() } cat_info = { 'cid': catalog.cid, 'name': catalog.name, 'settings': catalog.settings, 'dataframe': dataframe } response = { 'id': 'create_catalog', 'cat_info': cat_info } return response
def create_paths(toyz_settings, tid, params): """ Creates a new path on the server (if it does not already exist). Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): path to create on the server Response id: 'create_folder', status (*string* ): 'success', path (*string* ): path created on the server """ core.check4keys(params, ['path', 'new_folder']) path = os.path.join(params['path'], params['new_folder']) permissions = file_access.get_parent_permissions(toyz_settings.db, path, user_id=tid['user_id']) if 'w' not in permissions and 'x' not in permissions: raise ToyzJobError("You do not have permission to create path {0}".format(path)) core.create_paths(path) response = { 'id': 'notification', 'msg': 'Created path'.format(path), 'func': 'create_dir' } return response
def get_2d_fit(toyz_settings, tid, params): """ Get desired fit for 2d data array """ import numpy as np core.check4keys(params, ['file_info', 'fit_type', 'x', 'y', 'width', 'height']) response = astro.viewer.get_2d_fit(**params) return response
def get_img_info(toyz_settings, tid, params): """ Get image info specific to astronomy FITS images (like physical coords, wcs, etc.) """ core.check4keys(params, ['file_info', 'img_info']) response = { 'id': 'get_img_info', 'img_info': astro.viewer.get_img_info(**params) } return response
def get_img_data(toyz_settings, tid, params): """ Get data from an image or FITS file """ import toyz.web.viewer as viewer core.check4keys(params, ['data_type', 'file_info', 'img_info']) response = viewer.get_img_data(**params) #print('response:', response) return response
def save_catalog(toyz_settings, tid, params): """ Save a catalog """ core.check4keys(params, ['cid']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') if 'filepath' in params: catalog.save(params['filepath']) else: catalog.save() response = {'id': 'save_catalog', 'status': 'success'} return response
def select_src(toyz_settings, tid, params): """ Get the nearest source to a given point """ core.check4keys(params, ['cid']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') src = catalog.select_src(**params) if len(src) > 0: status = 'success' else: status = 'failed: no source found' response = {'id': 'select_src', 'status': status, 'src': src} return response
def delete_src(toyz_settings, tid, params): """ Delete a source from the catalog """ core.check4keys(params, ['cid', 'src_info']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') status = catalog.delete_src(params['src_info']) response = {'id': 'delete_src'} if status is True: response['status'] = 'success' else: response['status'] = 'failed' return response
def save_catalog(toyz_settings, tid, params): """ Save a catalog """ core.check4keys(params, ['cid']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') if 'filepath' in params: catalog.save(params['filepath']) else: catalog.save() response = { 'id': 'save_catalog', 'status': 'success' } return response
def detect_sources(toyz_settings, tid, params): """ Detect sources and create an object catalog in the current session """ core.check4keys(params, ['file_info', 'cid', 'settings']) catalog = astro.catalog.detect_sources(**params) #print('catalog', catalog) #print('catlog shape', catalog.shape) print('generating response') response = { 'id': 'detect_sources', 'sources': catalog.get_markers(), 'settings': catalog.settings } print('response', response) return response
def detect_sources(toyz_settings, tid, params): """ Detect sources and create an object catalog in the current session """ core.check4keys(params,['file_info', 'cid', 'settings']) catalog = astro.catalog.detect_sources(**params) #print('catalog', catalog) #print('catlog shape', catalog.shape) print('generating response') response = { 'id': 'detect_sources', 'sources': catalog.get_markers(), 'settings': catalog.settings } print('response', response) return response
def add_src(toyz_settings, tid, params): """ Add a source to the catalog """ core.check4keys(params, ['cid', 'file_info', 'src_info']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') del params['cid'] src = catalog.add_src(**params) response = {'id': 'add_src', 'src': src} if len(src) > 0: response['status'] = 'success' else: response['status'] = 'failed: source already exists' #print('catalog after added source') #print(catalog) return response
def select_src(toyz_settings, tid, params): """ Get the nearest source to a given point """ core.check4keys(params, ['cid']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') src = catalog.select_src(**params) if len(src)>0: status = 'success' else: status = 'failed: no source found' response = { 'id': 'select_src', 'status': status, 'src': src } return response
def add_src(toyz_settings, tid, params): """ Add a source to the catalog """ core.check4keys(params, ['cid', 'file_info', 'src_info']) catalog = astro.catalog.get_catalog(params['cid'], create='fail') del params['cid'] src = catalog.add_src(**params) response = { 'id': 'add_src', 'src': src } if len(src)>0: response['status'] = 'success' else: response['status'] = 'failed: source already exists' #print('catalog after added source') #print(catalog) return response
def get_workspace_sharing(toyz_settings, tid, params): """ Get shared workspace settings for users and groups Params - work_id ( *string* ): name of the workspace """ core.check4keys(params, ['work_id']) shared_workspaces = db_utils.get_workspace_sharing( toyz_settings.db, user_id=tid['user_id'], work_id=params['work_id']) ws_users = [row for row in shared_workspaces if row['share_id_type']=='user_id'] ws_groups = [row for row in shared_workspaces if row['share_id_type']=='group_id'] response = { 'id': 'get_workspace_sharing', 'ws_users': ws_users, 'ws_groups': ws_groups } return response
def change_pwd(toyz_settings, tid, params): """ Change a users password. Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - current_pwd (*string* ): Users current password. Must match the password on file or an exception is raised - new_pwd (*string* ): New password - confirm_pwd (*string* ): Confirmation of the the new password. If new_pwd and confirm_pwd do not match, an exception is raised Response - id: 'notification' - func: 'change_pwd' - msg: 'Password changed successfully' """ core.check4keys(params, ['current_pwd', 'new_pwd', 'confirm_pwd']) if core.check_pwd(toyz_settings, tid['user_id'], params['current_pwd']): if params['new_pwd'] == params['confirm_pwd']: pwd_hash = core.encrypt_pwd(toyz_settings, params['new_pwd']) else: raise ToyzJobError("New passwords did not match") else: raise ToyzJobError("Invalid user_id or password") db_utils.update_param(toyz_settings.db, 'pwd', user_id=tid['user_id'], pwd=pwd_hash) response = { 'id': 'notification', 'func': 'change_pwd', 'msg': 'Password changed successfully', } return response
def get_workspace_sharing(toyz_settings, tid, params): """ Get shared workspace settings for users and groups Params - work_id ( *string* ): name of the workspace """ core.check4keys(params, ['work_id']) shared_workspaces = db_utils.get_workspace_sharing( toyz_settings.db, user_id=tid['user_id'], work_id=params['work_id']) ws_users = [ row for row in shared_workspaces if row['share_id_type'] == 'user_id' ] ws_groups = [ row for row in shared_workspaces if row['share_id_type'] == 'group_id' ] response = { 'id': 'get_workspace_sharing', 'ws_users': ws_users, 'ws_groups': ws_groups } return response
def load_workspace(toyz_settings, tid, params): """ Load a workspace """ core.check4keys(params, ['work_id']) user_id = tid['user_id'] if 'user_id' in params and params['user_id']!=tid['user_id']: permissions = core.get_workspace_permissions(toyz_settings, tid, params) print('permissions', permissions) if permissions['view']: user_id = params['user_id'] else: raise ToyzJobError("You do not have permission to load {0}".format(params['work_id'])) workspaces = db_utils.get_param(toyz_settings.db, 'workspaces', user_id=user_id) if params['work_id'] not in workspaces: raise ToyzJobError("{0} not found in your workspaces".format(params['work_id'])) response = { 'id': 'workspace', 'work_id': params['work_id'], 'settings': workspaces[params['work_id']] } return response
def get_img_info(toyz_settings, tid, params): """ Map a large image into a set of tiles that make up the larger image """ import toyz.web.viewer as viewer print('************************************************') core.check4keys(params, ['img_info', 'file_info']) if tid['user_id'] != 'admin': permissions = file_access.get_parent_permissions( toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id']) if 'r' not in permissions: raise ToyzJobError( 'You do not have permission to view the requested file.' 'Please contact your network administrator if you believe this is an error.' ) shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) save_path = os.path.join(shortcuts['temp'], tid['session_id'], 'images') params['img_info']['save_path'] = save_path img_info = viewer.get_img_info(params['file_info'], params['img_info']) result = get_tile_info(toyz_settings, tid, { 'file_info': params['file_info'], 'img_info': img_info }) img_info['tiles'] = result['new_tiles'] response = { 'id': 'img info', 'img_info': img_info, 'new_tiles': result['new_tiles'] } print('************************************************') return response
def load_directory(toyz_settings, tid, params): """ Used by the file browser to load the folders and files in a given path. Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): Path to search Response - id: 'directory' - path (*string* ): path passed to the function - shortcuts (*dict* ): Dictionary of ``shortcut_name: shortcut_path`` 's for the user - folders (*list* of strings): folders contained in the path - files (*list* of strings): files contained in the path - parent (*string* ): parent directory of current path """ core.check4keys(params, ['path']) show_hidden = False # If the path is contained in a set of dollar signs (for example `$images$`) then # search in the users shortcuts for the given path shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) if params['path'][0] == '$' and params['path'][-1] == '$': shortcut = params['path'][1:-1] if shortcut not in shortcuts: raise ToyzJobError("Shortcut '{0}' not found for user {1}".format( shortcut, tid['user_id'])) params['path'] = shortcuts[shortcut] if not os.path.isdir(params['path']): parent = os.path.dirname(params['path']) if not os.path.isdir(parent): raise ToyzJobError("Path '{0}' not found".format(params['path'])) params['path'] = parent if 'show_hidden' in params and params['show_hidden']: show_hidden = True # Keep separate lists of the files and directories for the current path. # Only include the files and directories the user has permissions to view files = [] folders = [] groups = db_utils.get_param(toyz_settings.db, 'groups', user_id=tid['user_id']) if 'admin' in groups or tid['user_id'] == 'admin': admin = True else: admin = False for f in os.listdir(params['path']): if (f[0] != '.' or show_hidden): f_path = os.path.join(params['path'], f) if admin: permission = True else: permissions = file_access.get_parent_permissions( toyz_settings.db, f_path, user_id=tid['user_id']) if permissions is None: permissions = '' permission = 'f' in permissions if permission: if os.path.isfile(f_path): files.append(str(f)) elif os.path.isdir(f_path): folders.append(str(f)) else: print("no access to", f) files.sort(key=lambda v: v.lower()) folders.sort(key=lambda v: v.lower()) response = { 'id': 'directory', 'path': os.path.join(params['path'], ''), 'shortcuts': shortcuts.keys(), 'folders': folders, 'files': files, 'parent': os.path.abspath(os.path.join(params['path'], os.pardir)) } #print('path info:', response) return response
def load_directory(toyz_settings, tid, params): """ Used by the file browser to load the folders and files in a given path. Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): Path to search Response - id: 'directory' - path (*string* ): path passed to the function - shortcuts (*dict* ): Dictionary of ``shortcut_name: shortcut_path`` 's for the user - folders (*list* of strings): folders contained in the path - files (*list* of strings): files contained in the path - parent (*string* ): parent directory of current path """ core.check4keys(params,['path']) show_hidden=False # If the path is contained in a set of dollar signs (for example `$images$`) then # search in the users shortcuts for the given path shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) if params['path'][0]=='$' and params['path'][-1]=='$': shortcut = params['path'][1:-1] if shortcut not in shortcuts: raise ToyzJobError("Shortcut '{0}' not found for user {1}".format(shortcut, tid['user_id'])) params['path'] = shortcuts[shortcut] if not os.path.isdir(params['path']): parent = os.path.dirname(params['path']) if not os.path.isdir(parent): raise ToyzJobError("Path '{0}' not found".format(params['path'])) params['path'] = parent if 'show_hidden' in params and params['show_hidden']: show_hidden=True # Keep separate lists of the files and directories for the current path. # Only include the files and directories the user has permissions to view files = [] folders = [] groups = db_utils.get_param(toyz_settings.db, 'groups', user_id=tid['user_id']) if 'admin' in groups or tid['user_id'] == 'admin': admin=True else: admin=False for f in os.listdir(params['path']): if(f[0]!='.' or show_hidden): f_path =os.path.join(params['path'],f) if admin: permission = True else: permissions = file_access.get_parent_permissions(toyz_settings.db, f_path, user_id=tid['user_id']) if permissions is None: permissions = '' permission = 'f' in permissions if permission: if os.path.isfile(f_path): files.append(str(f)) elif os.path.isdir(f_path): folders.append(str(f)) else: print("no access to", f) files.sort(key=lambda v: v.lower()) folders.sort(key=lambda v: v.lower()) response={ 'id': 'directory', 'path': os.path.join(params['path'],''), 'shortcuts': shortcuts.keys(), 'folders': folders, 'files': files, 'parent': os.path.abspath(os.path.join(params['path'],os.pardir)) } #print('path info:', response) return response