Skip to content

Instantly share code, notes, and snippets.

@bebraw bebraw/
Created Jan 7, 2010

What would you like to do?
import os.path
import inspect
import imp
import os
from node import TreeNode
class File(TreeNode):
def __init__(self, path=None, name=None):
super(File, self).__init__()
parts = self.__init_attributes(path, name)
def __init_attributes(self, path, name=None):
parts = [] = name
self.type = None
self.classes = {}
if isinstance(path, str):
parts = path.split('/')
if len(parts) == 1:
parts = path.split('\\')
last_part_split = parts[-1].split('.') = last_part_split[0]
if len(last_part_split) > 1:
self.type = last_part_split[1]
return parts
def __init_classes(self, path):
if path is None:
if os.path.isdir(path):
for child in os.listdir(path):
child_path = os.path.join(path, child)
elif self.type == 'py':
module = imp.load_source('', path)
except Exception, e:
print e
module_classes = inspect.getmembers(module, inspect.isclass)
for name, klass in module_classes:
self.classes[name.lower()] = klass
def __init_structure(self, parts):
prev_node = self
for part in reversed(parts[:-1]):
prev_node.parent = File(name=part)
prev_node = prev_node.parent
class PluginDirectory(File):
def __init__(self):
super(PluginDirectory, self).__init__(self.plugin_path)
def plugin_path(self):
return os.path.join(self.current_directory, 'commands')
def current_directory(self):
return os.path.dirname(os.path.realpath(__file__))
import os
import tempfile
from mock import patch
from placidity.file import File
class TestFile:
def test_get_file_name(self, isdir_mock):
isdir_mock.return_value = False
def test_func(file):
assert == 'file'
def test_get_file_parent(self, isdir_mock):
isdir_mock.return_value = False
def test_func(file):
assert == 'to'
assert == 'path'
def test_get_directory_children(self, isdir_mock):
isdir_mock.return_value = False
def test_func(file):
directory = file.parent
assert directory.children == [file, ]
def test_find_by_name(self, isdir_mock,):
isdir_mock.return_value = False
def test_func(file):
directory = file.parent
assert directory.find(name='file') == file
def test_find_by_name_and_type(self, isdir_mock,):
isdir_mock.return_value = False
def test_func(file):
directory = file.parent
assert == 'file'
assert file.type == 'py'
assert directory.find(name='file', type='py') == file
self.separators_test(test_func, extension='py')
def test_load_python_file(self, isdir_mock):
def create_temp_file(code):
temp_file = tempfile.NamedTemporaryFile(delete=False,
return temp_file
def remove_temp_files(temp_file):
python_code = '''
class Bar: flag = True
class Foo: flag = False
isdir_mock.return_value = False
temp_file = create_temp_file(python_code)
file = File(
assert 'bar' in file.classes
assert file.classes['bar'].flag == True
assert 'foo' in file.classes
assert file.classes['foo'].flag == False
def test_python_files_in_folder(self):
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 == None
def separators_test(self, test_func, extension=None):
extension = '.' + extension if extension else ''
for sep in ('/', '\\'):
path = sep + 'path' + sep + 'to' + sep + 'file' + extension
file = File(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.