8
Generate directory structure
11
... {'type': 'dir', 'name': 'test',
13
... {'type': 'file', 'name': 'test.txt', 'content': "sample content"},
14
... {'type': 'dir', 'name': 'test11',
15
... 'items': [{'type': 'dir', 'name': 'test111'}]
19
... {'type': 'dir', 'name': 'test2'}
23
>>> fg = FS_Generator(dirStruct, '/home/sal/workspace/snappy/')
25
# Test directory creation
26
>>> os.path.exists("/home/sal/workspace/snappy/test")
28
>>> os.path.exists("/home/sal/workspace/snappy/test/test11")
30
>>> os.path.exists("/home/sal/workspace/snappy/test/test11/test111")
34
>>> os.path.exists("/home/sal/workspace/snappy/test/test.txt")
36
>>> handle = open("/home/sal/workspace/snappy/test/test.txt")
37
>>> data = handle.read()
46
def __init__(self, struct, home):
53
self.__generate_items_recursive(self.struct, self.home)
55
def __generate_items_recursive(self, items, path):
57
if ('dir' == e['type'] and not os.path.exists(path + e['name'])):
58
os.mkdir(path + e['name'] + '/')
60
elif ('file' == e['type'] and not os.path.exists(path + e['name'])):
61
self.__create_file(path + e['name'], e['content'])
64
self.__generate_items_recursive(e['items'], path + e['name'] + '/')
67
def __create_file(self, filename, content):
68
handle = open(filename, 'w')
75
if ('dir' == e['type'] and os.path.exists(self.home + e['name'])):
76
shutil.rmtree(self.home + e['name'])
85
Search files in directory and make simple operation other it
88
... {'type': 'dir', 'name': 'test',
90
... {'type': 'file', 'name': 'test.txt', 'content': "aaa"},
91
... {'type': 'dir', 'name': 'test11',
93
... {'type': 'dir', 'name': 'test111'},
94
... {'type': 'file', 'name': 'test11.txt', 'content': "bbb"}
97
... {'type': 'dir', 'name': 'test2', 'items':[{'type': 'file', 'name': 'test2.txt', 'content': "ccc"}]}
101
>>> fg = FS_Generator(dirStruct, '/home/sal/workspace/snappy/')
104
>>> fs = FS_Scanner('/home/sal/workspace/snappy/test')
105
>>> list = fs.files.sort()
107
['/home/sal/workspace/snappy/test/test.txt', '/home/sal/workspace/snappy/test/test11/test11.txt', '/home/sal/workspace/snappy/test/test2/test2.txt']
116
def __init__(self, path):
123
for root, subFolders, files in os.walk(self.home):
125
self.files.append(os.path.join(root, file))
130
for filename in self.files:
131
handle = open(filename, 'r')
b'\\ No newline at end of file'