-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
presents initial instructions for importing modules
- Loading branch information
1 parent
18f3487
commit 3d9ede2
Showing
7 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
six==1.16.0 | ||
click==8.1.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import os | ||
from pathlib import Path | ||
import click | ||
|
||
from sure.version import version | ||
from sure.runner import Runner | ||
from sure.importer import resolve_path | ||
|
||
|
||
def entrypoint(): | ||
@click.command() | ||
@click.argument("paths", nargs=-1) | ||
@click.option("-r", "--reporter", default="spec") | ||
def entrypoint(paths, reporter): | ||
runner = Runner(resolve_path(os.getcwd()), reporter) | ||
runner.run(paths) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import sys | ||
import importlib | ||
import importlib.util | ||
from importlib.machinery import PathFinder | ||
from pathlib import Path | ||
|
||
|
||
__MODULES__ = {} | ||
__ROOTS__ = {} | ||
|
||
|
||
def resolve_path(path, relative_to="~") -> Path: | ||
return Path(path).absolute().relative_to(Path(relative_to).expanduser()) | ||
|
||
|
||
def get_root_python_module(path) -> Path: | ||
if not isinstance(path, Path): | ||
path = Path(path) | ||
|
||
if not path.is_dir(): | ||
path = path.parent | ||
|
||
counter = 0 | ||
found = None | ||
while not found: | ||
counter += 1 | ||
if not path.parent.joinpath('__init__.py').exists(): | ||
found = path | ||
path = path.parent | ||
|
||
return found | ||
|
||
|
||
class importer(object): | ||
@classmethod | ||
def load_recursive(cls, path, ignore_errors=True, glob_pattern='*.py'): | ||
base_path = Path(path).expanduser().absolute() | ||
targets = list(base_path.glob(glob_pattern)) | ||
for file in targets: | ||
if file.is_dir(): | ||
logger.debug(f'ignoring directory {file}') | ||
continue | ||
|
||
if file.name.startswith('_') or file.name.endswith('_'): | ||
continue | ||
|
||
module, root = cls.dig_to_root(file) | ||
__ROOTS__[str(root)] = root | ||
|
||
return list(__MODULES__.values()) | ||
|
||
@classmethod | ||
def dig_to_root(cls, file): | ||
root = get_root_python_module(file) | ||
module_is_not_artificial = file.name != '__init__.py' and file.parent.joinpath('__init__.py').exists() | ||
if module_is_not_artificial: | ||
module_name = file.parent.name | ||
import ipdb;ipdb.set_trace() | ||
else: | ||
module_name = file.parent.name | ||
|
||
spec = importlib.util.spec_from_file_location(module_name, file) | ||
module = importlib.util.module_from_spec(spec) | ||
__MODULES__[module_name] = module | ||
sys.modules[module_name] = module | ||
spec.loader.exec_module(module) | ||
return module, root.absolute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# <sure - utility belt for automated testing in python> | ||
# Copyright (C) <2010-2023> Gabriel Falcão <gabriel@nacaolivre.org> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from __future__ import unicode_literals | ||
|
||
from pathlib import Path | ||
from sure.importer import importer | ||
|
||
__path__ = Path(__file__).parent.absolute() | ||
|
||
REPORTERS = {} | ||
|
||
|
||
def add_reporter(reporter: type) -> type: | ||
REPORTERS[reporter.name] = reporter | ||
return reporter | ||
|
||
|
||
def get_reporter(name: str) -> type: | ||
return REPORTERS.get(name) | ||
|
||
|
||
class MetaReporter(type): | ||
def __init__(cls, name, bases, attrs): | ||
if cls.__module__ != __name__: | ||
cls = add_reporter(cls) | ||
attrs['importer'] = cls.importer = importer | ||
|
||
super(MetaReporter, cls).__init__(name, bases, attrs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters