Created
January 6, 2010 18:57
-
-
Save bebraw/270525 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import inspect | |
| import imp | |
| import os | |
| import tempfile | |
| from node import TreeNode | |
| class File(TreeNode): | |
| def __init__(self, path=None): | |
| super(File, self).__init__() | |
| self.name = None | |
| self.classes = {} | |
| self.__init_classes(path) | |
| self.__init_structure(path) | |
| def __init_classes(self, path): | |
| if path is None: | |
| return | |
| if os.path.isdir(path): | |
| for child in os.listdir(path): | |
| child_path = os.path.join(path, child) | |
| self.children.append(File(child_path)) | |
| else: | |
| with open(path, 'r') as f: | |
| file_content = f.read() | |
| # http://docs.python.org/library/tempfile.html#tempfile.mktemp | |
| temp_file = tempfile.NamedTemporaryFile(delete=False, | |
| suffix='.py') | |
| temp_file.write(file_content) | |
| temp_file.close() | |
| try: | |
| module = imp.load_source('', temp_file.name) | |
| except Exception, e: | |
| print e | |
| return | |
| module_classes = inspect.getmembers(module, inspect.isclass) | |
| for name, klass in module_classes: | |
| self.classes[name.lower()] = klass | |
| os.unlink(temp_file.name) | |
| os.unlink(temp_file.name + 'c') | |
| def __init_structure(self, path): | |
| if not isinstance(path, str): | |
| return | |
| current_node = self | |
| parts = path.split('/') | |
| if len(parts) == 1: | |
| parts = path.split('\\') | |
| current_node.name = parts[-1] | |
| for part in reversed(parts): | |
| current_node.name = part | |
| current_node.parent = File() | |
| current_node = current_node.parent | |
| class PluginDirectory(File): | |
| def __init__(self): | |
| super(PluginDirectory, self).__init__(self.plugin_path) | |
| @property | |
| def plugin_path(self): | |
| return os.path.join(self.current_directory, 'commands') | |
| @property | |
| def current_directory(self): | |
| # http://code.activestate.com/recipes/474083/#c8 | |
| return os.path.dirname(os.path.realpath(__file__)) |
This file contains hidden or 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
| import os | |
| from mock import Mock, patch, sentinel | |
| from placidity.file import File | |
| def mock_with(mock, file_mock): | |
| mock.return_value = Mock() | |
| mock.return_value.__enter__ = Mock() | |
| mock.return_value.__enter__.return_value = file_mock | |
| mock.return_value.__exit__ = Mock() | |
| def mock_with_read(mock, read_return=sentinel.file_contents): | |
| file_mock = Mock() | |
| file_mock.read.return_value = read_return | |
| mock_with(mock, file_mock) | |
| class TestFile: | |
| @patch('__builtin__.open') | |
| @patch('os.path.isdir') | |
| def test_get_file_name(self, isdir_mock, open_mock): | |
| mock_with_read(open_mock) | |
| isdir_mock.return_value = False | |
| def test_func(file): | |
| assert file.name == 'file' | |
| self.separators_test(test_func) | |
| @patch('__builtin__.open') | |
| @patch('os.path.isdir') | |
| def test_get_file_parent(self, isdir_mock, open_mock): | |
| mock_with_read(open_mock) | |
| isdir_mock.return_value = False | |
| def test_func(file): | |
| assert file.parent.name == 'to' | |
| assert file.parent.parent.name == 'path' | |
| self.separators_test(test_func) | |
| @patch('__builtin__.open') | |
| @patch('os.path.isdir') | |
| def test_get_directory_children(self, isdir_mock, open_mock): | |
| mock_with_read(open_mock) | |
| isdir_mock.return_value = False | |
| def test_func(file): | |
| directory = file.parent | |
| assert directory.children == [file, ] | |
| self.separators_test(test_func) | |
| @patch('__builtin__.open') | |
| @patch('os.path.isdir') | |
| def test_find_by_name(self, isdir_mock, open_mock): | |
| mock_with_read(open_mock) | |
| isdir_mock.return_value = False | |
| def test_func(file): | |
| directory = file.parent | |
| assert directory.find(name='file') == file | |
| self.separators_test(test_func) | |
| @patch('__builtin__.open') | |
| @patch('os.path.isdir') | |
| def test_load_python_file(self, isdir_mock, open_mock): | |
| read_return_value = ''' | |
| class Bar: flag = True | |
| class Foo: flag = False | |
| ''' | |
| mock_with_read(open_mock, read_return_value) | |
| isdir_mock.return_value = False | |
| file = File(sentinel.filepath) | |
| assert 'bar' in file.classes | |
| assert file.classes['bar'].flag == True | |
| assert 'foo' in file.classes | |
| assert file.classes['foo'].flag == False | |
| @patch('__builtin__.open') | |
| def test_python_files_in_folder(self, open_mock): | |
| mock_with_read(open_mock) | |
| file_path = 'path' | |
| def isdir(path): | |
| if path is file_path: | |
| return True | |
| return False | |
| os.path.isdir = isdir | |
| def listdir(path): | |
| if path is file_path: | |
| return ['bar', 'baz', 'foo', ] | |
| os.listdir = listdir | |
| file = File(file_path) | |
| assert file.find(name='bar') | |
| assert file.find(name='baz') | |
| assert file.find(name='foo') | |
| def test_no_path(self): | |
| file = File() | |
| assert file.name == None | |
| def separators_test(self, test_func): | |
| for sep in ('/', '\\'): | |
| path = sep + 'path' + sep + 'to' + sep + 'file' | |
| file = File(path) | |
| test_func(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment