Skip to content

Commit

Permalink
Merge pull request #133 from gregoil/resources_py
Browse files Browse the repository at this point in the history
Resources py module for discovering
  • Loading branch information
UnDarkle committed Feb 25, 2019
2 parents 89b0870 + e629d47 commit 236ffab
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 64 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/complex_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ sub-resources.
For now, let's assume we already wrote the sub-resource under
:file:`resources/sub_process.py`.

Now, edit the file :file:`resources/calculator.py`:
Now, edit the file :file:`resources/resources.py`:

.. code-block:: python
Expand Down
16 changes: 0 additions & 16 deletions docs/configurations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,6 @@ in the following ways:
* Use the default, which is ``~/.rotest/artifacts``.

Shell Apps
----------

``rotest shell`` automatically attempts to load resources classes into
the environment to save the user the need to do so.
Define the default rotest applications to be loaded in the following ways:

* Define ``shell_apps`` in the configuration file:

.. code-block:: yaml
rotest:
shell_apps: ["resources", "tools"]
* Use the default, which is ``[]``.

Shell Startup Commands
----------------------

Expand Down
6 changes: 3 additions & 3 deletions docs/intro/more_on_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ following content:
from rotest.core import TestCase
from resources.calculator import Calculator
from resources.resources import Calculator
class AddTest(TestCase):
Expand Down Expand Up @@ -167,7 +167,7 @@ Test result events you can use in Rotest:
from rotest.core import TestCase
from resources.calculator import Calculator
from resources.resources import Calculator
class AddTest(TestCase):
Expand Down Expand Up @@ -195,7 +195,7 @@ Test result events you can use in Rotest:
from rotest.core import TestCase
from resources.calculator import Calculator
from resources.resources import Calculator
class AddTest(TestCase):
Expand Down
9 changes: 7 additions & 2 deletions docs/intro/using_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ content of the :file:`resources/admin.py` file:
register_resource_to_admin(models.CalculatorData, attr_list=['ip_address'])
Let's continue to write the Calculator resource, which exposes a simple
calculation action. Edit the file :file:`resources/calculator.py`:
calculation action. Edit the file :file:`resources/resources.py`:

.. code-block:: python
Expand Down Expand Up @@ -119,7 +119,12 @@ calculation action. Edit the file :file:`resources/calculator.py`:
Note the following:

* There is a use in the ``RPyC`` module, which can be installed using:
* `Rotest` expects a ``resources.py`` or ``resources/__init__.py`` file to be
present in your resources application, in which all your `BaseResource` classes
would be written or imported, much like how `Django` expects a ``models.py``
in for the models.

* This example uses the ``RPyC`` module, which can be installed using:

.. code-block:: console
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ ipdb==0.11
ipdbugger==2.0.3
ipython==5.8.0
ipython-genutils==0.2.0
isort==4.3.4
jsonschema==2.6.0
lazy-object-proxy==1.3.1
mccabe==0.6.1
mock==2.0.0
more-itertools==5.0.0
orderedset==2.0.1
pathlib2==2.3.3
pbr==5.1.1
pexpect==4.6.0
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages


__version__ = "6.2.0"
__version__ = "7.0.0"

result_handlers = [
"db = rotest.core.result.handlers.db_handler:DBHandler",
Expand Down Expand Up @@ -36,7 +36,6 @@
install_requires=[
'django>=1.8,<1.9',
'py',
'isort',
'ipdbugger>=2',
'xlwt',
'attrdict',
Expand All @@ -47,6 +46,7 @@
'jsonschema',
'basicstruct',
'future',
'orderedset',
'swaggapi>=0.6.5',
],
extras_require={
Expand Down
2 changes: 1 addition & 1 deletion src/rotest/cli/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fnmatch import fnmatch

import py
from isort.pie_slice import OrderedSet
from orderedset import OrderedSet

from rotest.common import core_log
from rotest.core import TestCase, TestFlow
Expand Down
4 changes: 0 additions & 4 deletions src/rotest/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ def get_configuration(configuration_schema,
"discoverer_blacklist": Option(
config_file_options=["discoverer_blacklist"],
default_value=[".tox", ".git", ".idea", "setup.py"]),
"shell_apps": Option(
config_file_options=["shell_apps"],
default_value=[]),
"shell_startup_commands": Option(
config_file_options=["shell_startup_commands"],
default_value=[]),
Expand Down Expand Up @@ -274,7 +271,6 @@ def get_configuration(configuration_schema,
DJANGO_SETTINGS_MODULE = CONFIGURATION.django_settings
ARTIFACTS_DIR = os.path.expanduser(CONFIGURATION.artifacts_dir)
DISCOVERER_BLACKLIST = CONFIGURATION.discoverer_blacklist
SHELL_APPS = CONFIGURATION.shell_apps
SHELL_STARTUP_COMMANDS = CONFIGURATION.shell_startup_commands

if DJANGO_SETTINGS_MODULE is None:
Expand Down
45 changes: 16 additions & 29 deletions src/rotest/management/utils/resources_discoverer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Auxiliary module for discovering Rotest resources within an app."""
from __future__ import absolute_import
import os
from fnmatch import fnmatch

import py
from importlib import import_module

import six
import django
from rotest.common.config import DISCOVERER_BLACKLIST
from django.conf import settings
from django.utils.module_loading import module_has_submodule

from rotest.management.base_resource import BaseResource


FILES_PATTERN = "*.py"
RESOURCES_MODULE_NAME = "resources"


def _is_resource_class(item):
Expand All @@ -35,40 +35,27 @@ def _import_resources_from_module(module_path):
Returns:
dict. all resource classes found in the module {name: class}.
"""
if not fnmatch(module_path, FILES_PATTERN):
return

module = py.path.local(module_path).pyimport()
module = import_module(module_path)

return {item.__name__: item for item in six.itervalues(module.__dict__)
if _is_resource_class(item)}


def get_resources(app_name, blacklist=DISCOVERER_BLACKLIST):
"""Get all the resource classes under a Django app.
Args:
app_name (str): application to search for resources inside.
blacklist (tuple): module patterns to ignore.
def get_resources():
"""Get all the resource classes from apps declared in settings.py.
Returns:
dict. all resource classes found in the application {name: class}.
"""
app_configs = django.apps.apps.app_configs
if app_name not in app_configs:
raise RuntimeError("Application %r was not found" % app_name)

app_path = app_configs[app_name].path
declared_apps = settings.INSTALLED_APPS
resources = {}

for sub_dir, _, modules in os.walk(app_path):
for module_name in modules:
module_path = os.path.join(sub_dir, module_name)
if any(fnmatch(module_path, pattern) for pattern in blacklist):
continue
for app_name in declared_apps:
app_module = import_module(app_name)

module_resources = _import_resources_from_module(module_path)
if module_resources is not None:
resources.update(module_resources)
if module_has_submodule(app_module, RESOURCES_MODULE_NAME):
resources_module = '%s.%s' % (app_name, RESOURCES_MODULE_NAME)
app_resources = _import_resources_from_module(resources_module)
resources.update(app_resources)

return resources
7 changes: 2 additions & 5 deletions src/rotest/management/utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from attrdict import AttrDict
from future.builtins import object
from rotest.core.result.result import Result
from rotest.common.config import SHELL_STARTUP_COMMANDS
from rotest.management.base_resource import BaseResource
from rotest.management.client.manager import ClientResourceManager
from rotest.common.config import SHELL_APPS, SHELL_STARTUP_COMMANDS
from rotest.core.runner import parse_config_file, DEFAULT_CONFIG_PATH
from rotest.core.result.handlers.stream.log_handler import LogDebugHandler

Expand Down Expand Up @@ -107,10 +107,7 @@ def main():
""")

startup_commands = [IMPORT_BLOCK_UTILS, IMPORT_RESOURCE_LOADER]
for app_name in SHELL_APPS:
startup_commands.append("globals().update(get_resources(%r))" %
app_name)

startup_commands.append("globals().update(get_resources())")
startup_commands.extend(SHELL_STARTUP_COMMANDS)
try:
IPython.start_ipython(["-i", "-c", ";".join(startup_commands)])
Expand Down

0 comments on commit 236ffab

Please sign in to comment.