def ready(self): """ Startup method for Tethys Apps django app. """ # Perform App Harvesting harvester = SingletonHarvester() harvester.harvest()
def test_harvest_extension_instances_TypeError(self, mock_subclass, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_extension_instances With a TypeError exception mocked up :param mock_subclass: mock for the TypeError exception :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() dict_ext = {'test_extension': 'tethysext.test_extension'} mock_args = dict_ext mock_subclass.side_effect = TypeError shv = SingletonHarvester() shv._harvest_extension_instances(mock_args) valid_ext_instances = [] valid_extension_modules = {} self.assertEqual(valid_ext_instances, shv.extensions) self.assertEqual(valid_extension_modules, shv.extension_modules) mock_logexception.assert_not_called() mock_subclass.assert_called() self.assertIn('Tethys Extensions Loaded:', mock_stdout.getvalue())
def get_current_use(self): """ calculates/retrieves the current use of the resource Returns: Int: current use of resource """ current_use = 0.0 if isinstance(self.entity, User): harvester = SingletonHarvester() installed_apps = harvester.apps for app in installed_apps: workspace = _get_user_workspace(app, self.entity) current_use += float(workspace.get_size(self.units)) elif isinstance(self.entity, TethysApp): harvester = SingletonHarvester() installed_apps = harvester.apps tethys_app = next( (x for x in installed_apps if x.name == self.entity.name), None) if tethys_app is not None: current_use = float( _get_app_workspace(tethys_app).get_size(self.units)) return current_use
def test_harvest_app_instances_TypeError(self, mock_subclass, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances :param mock_subclass: mock for the TypeError exception :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() list_apps = [ u'.gitignore', u'test_app', u'__init__.py', u'__init__.pyc' ] mock_args = list_apps mock_subclass.side_effect = TypeError shv = SingletonHarvester() shv._harvest_app_instances(mock_args) valid_app_instance_list = [] self.assertEqual(valid_app_instance_list, shv.apps) mock_logexception.assert_not_called() mock_subclass.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue())
def setUp(self): # Resets the apps database and app permissions (workaround since Django's testing framework refreshes the # core db after each individual test) from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() harvester.harvest() logging.disable(logging.CRITICAL) self.set_up()
def test_harvest_validate_extension(self): """ Test for SingletonHarvester._validate_extension :return: """ mock_args = mock.MagicMock() shv = SingletonHarvester() extension = shv._validate_extension(mock_args) self.assertEqual(mock_args, extension)
def test_harvest_get_url_patterns(self): """ Test for SingletonHarvester.get_url_patterns :return: """ shv = SingletonHarvester() app_url_patterns, extension_url_patterns = shv.get_url_patterns() self.assertGreaterEqual(len(app_url_patterns), 1) self.assertIn('test_app', app_url_patterns) self.assertGreaterEqual(len(extension_url_patterns), 1) self.assertIn('test_extension', extension_url_patterns)
def sync_tethys_apps_db(**kwargs): """Sync the Tethys database with the installed apps and extensions Args: **kwargs: processed key word arguments from commandline """ write_info( 'Syncing the Tethys database with installed apps and extensions...') load_apps() from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() harvester.harvest()
def test_harvest_extensions_apps(self, mock_stdout, _): """ Test for SingletonHarvester.harvest. Checks for expected text output :param mock_stdout: mock for text output :return: """ shv = SingletonHarvester() shv.harvest() self.assertIn('Loading Tethys Extensions...', mock_stdout.getvalue()) self.assertIn('Tethys Extensions Loaded:', mock_stdout.getvalue()) self.assertIn('test_extension', mock_stdout.getvalue())
def test_harvest_validate_app(self): """ Test for SingletonHarvester._validate_app Gives invalid icon and color information which is altered by the function :return: """ mock_args = mock.MagicMock() mock_args.icon = '/foo_icon' # prepended slash mock_args.color = 'foo_color' # missing prepended #, not 6 or 3 digit hex color shv = SingletonHarvester() validate = shv._validate_app(mock_args) self.assertEqual('foo_icon', validate.icon) self.assertEqual('', validate.color)
def test_harvest_extensions_exception(self, mock_pkgutil, mock_stdout): """ Test for SingletonHarvester.harvest. With an exception thrown, when harvesting the extensions :param mock_pkgutil: mock for the exception :param mock_stdout: mock for the text output :return: """ mock_pkgutil.side_effect = Exception shv = SingletonHarvester() shv.harvest_extensions() self.assertIn('Loading Tethys Extensions...', mock_stdout.getvalue()) self.assertNotIn('Tethys Extensions Loaded:', mock_stdout.getvalue()) self.assertNotIn('test_extension', mock_stdout.getvalue())
def manage_storage(request, username): """ Handle clear workspace requests. """ # Users are not allowed to make changes to other users settings if request.user.username != username: messages.warning(request, "You are not allowed to change other users' settings.") return redirect('user:profile', username=request.user.username) apps = SingletonHarvester().apps user = request.user for app in apps: workspace = _get_user_workspace(app, user) app.current_use = _convert_storage_units('gb', workspace.get_size('gb')) codename = 'user_workspace_quota' rqh = WorkspaceQuotaHandler(user) current_use = _convert_storage_units(rqh.units, rqh.get_current_use()) quota = get_quota(user, codename) quota = _check_quota_helper(quota) context = {'apps': apps, 'context_user': request.user, 'current_use': current_use, 'quota': quota, } return render(request, 'tethys_portal/user/manage_storage.html', context)
def test_harvest_app_instances_ImportError(self, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances With an ImportError exception thrown due to invalid argument information passed :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() list_apps = ['foo'] mock_args = list_apps shv = SingletonHarvester() shv._harvest_app_instances(mock_args) valid_app_instance_list = [] self.assertEqual(valid_app_instance_list, shv.apps) mock_logexception.assert_called_once() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue())
def test_harvest_app_instances_Exceptions2(self, mock_permissions, mock_logwarning, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances For the exception on lines 239-240 With an exception mocked up for register_app_permissions :param mock_permissions: mock for throwing a ProgrammingError exception :param mock_logerror: mock for the tethys_log error :param mock_stdout: mock for the text output :return: """ list_apps = [u'.gitignore', u'test_app', u'__init__.py', u'__init__.pyc'] mock_permissions.side_effect = ProgrammingError shv = SingletonHarvester() shv._harvest_app_instances(list_apps) mock_logwarning.assert_called() mock_permissions.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue()) self.assertIn('test_app', mock_stdout.getvalue())
def test_harvest_app_instances_Exceptions1(self, mock_url_maps, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances For the exception on lines 230-234 With an exception mocked up for the url_patterns :param mock_url_maps: mock for url_patterns to thrown an Exception :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() list_apps = [u'.gitignore', u'test_app', u'__init__.py', u'__init__.pyc'] mock_args = list_apps mock_url_maps.side_effect = ImportError shv = SingletonHarvester() shv._harvest_app_instances(mock_args) mock_logexception.assert_called() mock_url_maps.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue())
def test_harvest_app_instances_load_handler_patterns_exception( self, mock_url_maps, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances For the app url patterns exception With an exception mocked up for the url_patterns :param mock_url_maps: mock for url_patterns to throw an Exception :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ list_apps = {'test_app': 'tethysapp.test_app'} mock_args = list_apps mock_url_maps.side_effect = ['', ImportError] shv = SingletonHarvester() shv._harvest_app_instances(mock_args) mock_logexception.assert_called() mock_url_maps.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue())
def test_harvest_extension_instances_ImportError(self, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_extension_instances With an ImportError exception thrown due to invalid argument information passed :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() dict_ext = {'foo': 'foo_ext'} mock_args = dict_ext shv = SingletonHarvester() shv._harvest_extension_instances(mock_args) valid_ext_instances = [] valid_extension_modules = {} self.assertEqual(valid_ext_instances, shv.extensions) self.assertEqual(valid_extension_modules, shv.extension_modules) mock_logexception.assert_called_once() self.assertIn('Tethys Extensions Loaded:', mock_stdout.getvalue())
def test_harvest_app_instances_object_does_not_exist( self, mock_permissions, mock_logwarning, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances For the app permissions exception (ObjectDoesNotExist) With an exception mocked up for register_app_permissions :param mock_permissions: mock for throwing a ObjectDoesNotExist exception :param mock_logerror: mock for the tethys_log error :param mock_stdout: mock for the text output :return: """ list_apps = {'test_app': 'tethysapp.test_app'} mock_permissions.side_effect = ObjectDoesNotExist shv = SingletonHarvester() shv._harvest_app_instances(list_apps) mock_logwarning.assert_called() mock_permissions.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue()) self.assertIn('test_app', mock_stdout.getvalue())
def test_harvest_app_instances_TypeError(self, mock_subclass, mock_logexception, mock_stdout): """ Test for SingletonHarvester._harvest_app_instances :param mock_subclass: mock for the TypeError exception :param mock_logexception: mock for the tethys_log exception :param mock_stdout: mock for the text output :return: """ mock_args = mock.MagicMock() list_apps = [u'.gitignore', u'test_app', u'__init__.py', u'__init__.pyc'] mock_args = list_apps mock_subclass.side_effect = TypeError shv = SingletonHarvester() shv._harvest_app_instances(mock_args) valid_app_instance_list = [] self.assertEqual(valid_app_instance_list, shv.apps) mock_logexception.assert_not_called() mock_subclass.assert_called() self.assertIn('Tethys Apps Loaded:', mock_stdout.getvalue())
def get_installed_tethys_extensions(): """ Get a list of installed extensions """ harvester = SingletonHarvester() install_extensions = harvester.extension_modules extension_paths = {} for extension_name, extension_module in install_extensions.items(): try: extension = __import__(extension_module, fromlist=['']) extension_paths[extension_name] = extension.__path__[0] except (IndexError, ImportError): '''DO NOTHING''' return extension_paths
def get_installed_tethys_apps(): """ Returns a list apps installed in the tethysapp directory. """ harvester = SingletonHarvester() installed_apps = harvester.app_modules tethys_apps = {} for app_name, app_module in installed_apps.items(): try: app = __import__(app_module, fromlist=['']) tethys_apps[app_name] = app.__path__[0] except (IndexError, ImportError): '''DO NOTHING''' return tethys_apps
def get_directories_in_tethys(directory_names, with_app_name=False): """ # Locate given directories in tethys apps and extensions. Args: directory_names: directory to get path to. with_app_name: include the app name if True. Returns: list: list of paths to directories in apps and extensions. """ potential_dirs = [] # Determine the directories of tethys extensions harvester = SingletonHarvester() for _, app_module in harvester.app_modules.items(): try: app_module = __import__(app_module, fromlist=['']) potential_dirs.append(app_module.__path__[0]) except (ImportError, AttributeError, IndexError): pass for _, extension_module in harvester.extension_modules.items(): try: extension_module = __import__(extension_module, fromlist=['']) potential_dirs.append(extension_module.__path__[0]) except (ImportError, AttributeError, IndexError): pass # Check each directory combination match_dirs = [] for potential_dir in potential_dirs: for directory_name in directory_names: # Only check directories if os.path.isdir(potential_dir): match_dir = safe_join(potential_dir, directory_name) if match_dir not in match_dirs and os.path.isdir(match_dir): if not with_app_name: match_dirs.append(match_dir) else: match_dirs.append( (os.path.basename(potential_dir), match_dir)) return match_dirs
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() app_ws_patterns = harvester.get_url_patterns()['ws_url_patterns'] ws_routing_patterns = [] for namespace, urls in app_ws_patterns.items(): for url in urls: ws_routing_patterns.append(url) application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AuthMiddlewareStack(URLRouter(ws_routing_patterns)) })
* Created On: 2014 * Copyright: (c) Brigham Young University 2014 * License: BSD 2-Clause ******************************************************************************** """ from django.conf.urls import url, include from tethys_apps.harvester import SingletonHarvester from tethys_apps.views import library, send_beta_feedback_email import logging tethys_log = logging.getLogger('tethys.' + __name__) urlpatterns = [ url(r'^$', library, name='app_library'), url(r'^send-beta-feedback/$', send_beta_feedback_email, name='send_beta_feedback'), ] # Append the app urls urlpatterns harvester = SingletonHarvester() app_url_patterns, extension_url_patterns = harvester.get_url_patterns() for namespace, urls in app_url_patterns.items(): root_pattern = r'^{0}/'.format(namespace.replace('_', '-')) urlpatterns.append(url(root_pattern, include(urls, namespace=namespace))) extension_urls = [] for namespace, urls in extension_url_patterns.items(): root_pattern = r'^{0}/'.format(namespace.replace('_', '-')) extension_urls.append(url(root_pattern, include(urls, namespace=namespace)))
def get_app_class(app): apps_s = SingletonHarvester().apps for app_s in apps_s: if app_s.name == app.name: return app_s
def manage_command(args): """ Management commands. """ # Get the path to manage.py manage_path = get_manage_path(args) # Define the process to be run primary_process = None if args.command == MANAGE_START: if args.port: primary_process = ['python', manage_path, 'runserver', args.port] else: primary_process = ['python', manage_path, 'runserver'] elif args.command == MANAGE_SYNCDB: intermediate_process = ['python', manage_path, 'makemigrations'] run_process(intermediate_process) primary_process = ['python', manage_path, 'migrate'] elif args.command == MANAGE_COLLECTSTATIC: # Run pre_collectstatic intermediate_process = ['python', manage_path, 'pre_collectstatic'] run_process(intermediate_process) # Setup for main collectstatic primary_process = ['python', manage_path, 'collectstatic'] if args.noinput: primary_process.append('--noinput') elif args.command == MANAGE_COLLECTWORKSPACES: # Run collectworkspaces command if args.force: primary_process = ['python', manage_path, 'collectworkspaces', '--force'] else: primary_process = ['python', manage_path, 'collectworkspaces'] elif args.command == MANAGE_COLLECT: # Convenience command to run collectstatic and collectworkspaces # Run pre_collectstatic intermediate_process = ['python', manage_path, 'pre_collectstatic'] run_process(intermediate_process) # Setup for main collectstatic intermediate_process = ['python', manage_path, 'collectstatic'] if args.noinput: intermediate_process.append('--noinput') run_process(intermediate_process) # Run collectworkspaces command primary_process = ['python', manage_path, 'collectworkspaces'] elif args.command == MANAGE_CREATESUPERUSER: primary_process = ['python', manage_path, 'createsuperuser'] elif args.command == MANAGE_SYNC: from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() harvester.harvest() if primary_process: run_process(primary_process)
******************************************************************************** """ from django.conf.urls import url, include from tethys_apps.harvester import SingletonHarvester from tethys_apps.views import library, send_beta_feedback_email import logging tethys_log = logging.getLogger('tethys.' + __name__) urlpatterns = [ url(r'^$', library, name='app_library'), url(r'^send-beta-feedback/$', send_beta_feedback_email, name='send_beta_feedback'), ] # Append the app urls urlpatterns harvester = SingletonHarvester() app_url_patterns, extension_url_patterns = harvester.get_url_patterns() for namespace, urls in app_url_patterns.items(): root_pattern = r'^{0}/'.format(namespace.replace('_', '-')) urlpatterns.append(url(root_pattern, include(urls, namespace=namespace))) extension_urls = [] for namespace, urls in extension_url_patterns.items(): root_pattern = r'^{0}/'.format(namespace.replace('_', '-')) extension_urls.append(url(root_pattern, include(urls, namespace=namespace)))
def manage_command(args): """ Management commands. """ # Get the path to manage.py manage_path = get_manage_path(args) # Define the process to be run primary_process = None if args.command == MANAGE_START: if args.port: primary_process = ['python', manage_path, 'runserver', args.port] else: primary_process = ['python', manage_path, 'runserver'] elif args.command == MANAGE_SYNCDB: intermediate_process = ['python', manage_path, 'makemigrations'] run_process(intermediate_process) primary_process = ['python', manage_path, 'migrate'] elif args.command == MANAGE_COLLECTSTATIC: # Run pre_collectstatic intermediate_process = ['python', manage_path, 'pre_collectstatic'] run_process(intermediate_process) # Setup for main collectstatic primary_process = ['python', manage_path, 'collectstatic'] if args.noinput: primary_process.append('--noinput') elif args.command == MANAGE_COLLECTWORKSPACES: # Run collectworkspaces command if args.force: primary_process = [ 'python', manage_path, 'collectworkspaces', '--force' ] else: primary_process = ['python', manage_path, 'collectworkspaces'] elif args.command == MANAGE_COLLECT: # Convenience command to run collectstatic and collectworkspaces # Run pre_collectstatic intermediate_process = ['python', manage_path, 'pre_collectstatic'] run_process(intermediate_process) # Setup for main collectstatic intermediate_process = ['python', manage_path, 'collectstatic'] if args.noinput: intermediate_process.append('--noinput') run_process(intermediate_process) # Run collectworkspaces command primary_process = ['python', manage_path, 'collectworkspaces'] elif args.command == MANAGE_CREATESUPERUSER: primary_process = ['python', manage_path, 'createsuperuser'] elif args.command == MANAGE_SYNC: from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() harvester.harvest() if primary_process: run_process(primary_process)
def sync_tethys_apps_db(**kwargs): write_info('Syncing the Tethys database with installed apps and extensions...') load_apps() from tethys_apps.harvester import SingletonHarvester harvester = SingletonHarvester() harvester.harvest()
from ..gizmo_options.base import TethysGizmoOptions import tethys_sdk.gizmos GIZMO_NAME_PROPERTY = 'gizmo_name' GIZMO_NAME_MAP = {} EXTENSION_PATH_MAP = {} # Add gizmos to GIZMO_NAME_MAP for name, cls in tethys_sdk.gizmos.__dict__.items(): if inspect.isclass(cls) and issubclass( cls, TethysGizmoOptions) and hasattr(cls, GIZMO_NAME_PROPERTY): GIZMO_NAME_MAP[cls.gizmo_name] = cls # Add extension gizmos to the GIZMO_NAME_MAP harvester = SingletonHarvester() extension_modules = harvester.extension_modules for module_name, extension_module in extension_modules.items(): try: gizmo_module = __import__('{}.gizmos'.format(extension_module), fromlist=['']) for name, cls in gizmo_module.__dict__.items(): if inspect.isclass(cls) and issubclass( cls, TethysGizmoOptions) and hasattr( cls, GIZMO_NAME_PROPERTY): GIZMO_NAME_MAP[cls.gizmo_name] = cls gizmo_module_path = gizmo_module.__path__[0] EXTENSION_PATH_MAP[cls.gizmo_name] = os.path.abspath( os.path.dirname(gizmo_module_path)) except ImportError: