split locate_debugtalk_py to locate_file and convert_module_name

This commit is contained in:
debugtalk
2018-08-09 08:28:21 +08:00
parent 411c6b4428
commit 6c218a1c4f
2 changed files with 56 additions and 33 deletions

View File

@@ -166,22 +166,18 @@ def load_dot_env_file(path):
return env_variables_mapping return env_variables_mapping
############################################################################### def locate_file(start_path, file_name):
## debugtalk.py module loader """ locate filename and return file path.
###############################################################################
def locate_debugtalk_py(start_path):
""" locate debugtalk.py module and return module name.
searching will be recursive upward until current working directory. searching will be recursive upward until current working directory.
Args: Args:
start_path (str): start locating path, maybe file path or directory path start_path (str): start locating path, maybe file path or directory path
Returns: Returns:
str: located module name. None if module not found. str: located file path. None if file not found.
Raises: Raises:
exceptions.FileNotFound: If failed to locate debugtalk.py module. exceptions.FileNotFound: If failed to locate file.
""" """
if os.path.isfile(start_path): if os.path.isfile(start_path):
@@ -191,20 +187,47 @@ def locate_debugtalk_py(start_path):
else: else:
raise exceptions.FileNotFound("invalid path: {}".format(start_path)) raise exceptions.FileNotFound("invalid path: {}".format(start_path))
module_path = os.path.join(start_dir_path, "debugtalk.py") file_path = os.path.join(start_dir_path, file_name)
if os.path.isfile(module_path): if os.path.isfile(file_path):
if os.path.isabs(module_path): if os.path.isabs(file_path):
module_path = module_path[len(os.getcwd())+1:] file_path = file_path[len(os.getcwd())+1:]
module_name = module_path.replace("/", ".").rstrip(".py") return file_path
return module_name
# current working directory # current working directory
if os.path.abspath(start_dir_path) == os.getcwd(): if os.path.abspath(start_dir_path) == os.getcwd():
raise exceptions.FileNotFound("debugtalk.py module not found: {}".format(start_path)) raise exceptions.FileNotFound("{} not found in {}".format(file_name, start_path))
# locate recursive upward # locate recursive upward
return locate_debugtalk_py(os.path.dirname(start_dir_path)) return locate_file(os.path.dirname(start_dir_path), file_name)
###############################################################################
## debugtalk.py module loader
###############################################################################
def convert_module_name(python_file_path):
""" convert python file relative path to module name.
Args:
python_file_path (str): python file relative path
Returns:
str: module name
Examples:
>>> convert_module_name("debugtalk.py")
debugtalk
>>> convert_module_name("tests/debugtalk.py")
tests.debugtalk
>>> convert_module_name("tests/data/debugtalk.py")
tests.data.debugtalk
"""
module_name = python_file_path.replace("/", ".").rstrip(".py")
return module_name
def load_python_module(module): def load_python_module(module):
@@ -257,7 +280,8 @@ def load_debugtalk_module(start_path=None):
start_path = start_path or os.getcwd() start_path = start_path or os.getcwd()
try: try:
module_name = locate_debugtalk_py(start_path) module_path = locate_file(start_path, "debugtalk.py")
module_name = convert_module_name(module_path)
except exceptions.FileNotFound: except exceptions.FileNotFound:
return { return {
"variables": {}, "variables": {},

View File

@@ -140,38 +140,37 @@ class TestFileLoader(unittest.TestCase):
with self.assertRaises(exceptions.FileNotFound): with self.assertRaises(exceptions.FileNotFound):
loader.load_dot_env_file("not_exist.env") loader.load_dot_env_file("not_exist.env")
def test_locate_file(self):
class TestModuleLoader(unittest.TestCase):
def test_locate_debugtalk_py(self):
with self.assertRaises(exceptions.FileNotFound): with self.assertRaises(exceptions.FileNotFound):
loader.locate_debugtalk_py(os.getcwd()) loader.locate_file(os.getcwd(), "debugtalk.py")
with self.assertRaises(exceptions.FileNotFound): with self.assertRaises(exceptions.FileNotFound):
loader.locate_debugtalk_py("") loader.locate_file("", "debugtalk.py")
start_path = os.path.join(os.getcwd(), "tests") start_path = os.path.join(os.getcwd(), "tests")
self.assertEqual( self.assertEqual(
loader.locate_debugtalk_py(start_path), loader.locate_file(start_path, "debugtalk.py"),
"tests.debugtalk" "tests/debugtalk.py"
) )
self.assertEqual( self.assertEqual(
loader.locate_debugtalk_py("tests/"), loader.locate_file("tests/", "debugtalk.py"),
"tests.debugtalk" "tests/debugtalk.py"
) )
self.assertEqual( self.assertEqual(
loader.locate_debugtalk_py("tests"), loader.locate_file("tests", "debugtalk.py"),
"tests.debugtalk" "tests/debugtalk.py"
) )
self.assertEqual( self.assertEqual(
loader.locate_debugtalk_py("tests/base.py"), loader.locate_file("tests/base.py", "debugtalk.py"),
"tests.debugtalk" "tests/debugtalk.py"
) )
self.assertEqual( self.assertEqual(
loader.locate_debugtalk_py("tests/data/test.env"), loader.locate_file("tests/data/test.env", "debugtalk.py"),
"tests.debugtalk" "tests/debugtalk.py"
) )
class TestModuleLoader(unittest.TestCase):
def test_filter_module_functions(self): def test_filter_module_functions(self):
module_mapping = loader.load_python_module(loader) module_mapping = loader.load_python_module(loader)
functions_dict = module_mapping["functions"] functions_dict = module_mapping["functions"]