~light.coders/snappysnoop/pysnappy

« back to all changes in this revision

Viewing changes to utils/models.py

  • Committer: Evgeny Sergeev
  • Date: 2010-10-10 12:38:36 UTC
  • Revision ID: sal@sal-20101010123836-vd714gmxre2hducn
Проект SnappySnoop на питоне

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os.path
 
2
import shutil
 
3
 
 
4
 
 
5
 
 
6
class FS_Generator():
 
7
    """
 
8
    Generate directory structure
 
9
 
 
10
    >>> dirStruct = [
 
11
    ... {'type': 'dir', 'name': 'test',
 
12
    ...    'items': [
 
13
    ...        {'type': 'file', 'name': 'test.txt', 'content': "sample content"},
 
14
    ...        {'type': 'dir', 'name': 'test11',
 
15
    ...          'items': [{'type': 'dir', 'name': 'test111'}]
 
16
    ...        }
 
17
    ...    ]
 
18
    ... },
 
19
    ... {'type': 'dir', 'name': 'test2'}
 
20
    ... ]
 
21
 
 
22
 
 
23
    >>> fg = FS_Generator(dirStruct, '/home/sal/workspace/snappy/')
 
24
 
 
25
    # Test directory creation
 
26
    >>> os.path.exists("/home/sal/workspace/snappy/test")
 
27
    True
 
28
    >>> os.path.exists("/home/sal/workspace/snappy/test/test11")
 
29
    True
 
30
    >>> os.path.exists("/home/sal/workspace/snappy/test/test11/test111")
 
31
    True
 
32
 
 
33
    # Test file content
 
34
    >>> os.path.exists("/home/sal/workspace/snappy/test/test.txt")
 
35
    True
 
36
    >>> handle = open("/home/sal/workspace/snappy/test/test.txt")
 
37
    >>> data = handle.read()
 
38
    >>> handle.close()
 
39
    >>> data
 
40
    "sample content"
 
41
    
 
42
    # Clear all
 
43
    >>> fg.clean()
 
44
    """
 
45
 
 
46
    def __init__(self, struct, home):
 
47
        self.struct = struct
 
48
        self.home = home
 
49
        self.generate()
 
50
 
 
51
 
 
52
    def generate(self):
 
53
        self.__generate_items_recursive(self.struct, self.home)
 
54
 
 
55
    def __generate_items_recursive(self, items, path):
 
56
        for e in items:
 
57
            if ('dir' == e['type'] and not os.path.exists(path + e['name'])):
 
58
                os.mkdir(path + e['name'] + '/')
 
59
                
 
60
            elif ('file' == e['type'] and not os.path.exists(path + e['name'])):
 
61
                self.__create_file(path + e['name'], e['content'])
 
62
                
 
63
            if 'items' in e:
 
64
                self.__generate_items_recursive(e['items'], path + e['name'] + '/')
 
65
 
 
66
 
 
67
    def __create_file(self, filename, content):
 
68
        handle = open(filename, 'w')
 
69
        handle.write(content)
 
70
        handle.close()
 
71
 
 
72
        
 
73
    def clean(self):
 
74
        for e in self.struct:
 
75
            if ('dir' == e['type'] and os.path.exists(self.home + e['name'])):
 
76
                shutil.rmtree(self.home + e['name'])
 
77
 
 
78
 
 
79
 
 
80
 
 
81
 
 
82
 
 
83
class FS_Scanner():
 
84
    """
 
85
    Search files in directory and make simple operation other it
 
86
 
 
87
    >>> dirStruct = [
 
88
    ...  {'type': 'dir', 'name': 'test',
 
89
    ...    'items': [
 
90
    ...        {'type': 'file', 'name': 'test.txt', 'content': "aaa"},
 
91
    ...        {'type': 'dir', 'name': 'test11',
 
92
    ...          'items': [
 
93
    ...                      {'type': 'dir', 'name': 'test111'}, 
 
94
    ...                      {'type': 'file', 'name': 'test11.txt', 'content': "bbb"}
 
95
    ...                   ]
 
96
    ...        },
 
97
    ...        {'type': 'dir', 'name': 'test2', 'items':[{'type': 'file', 'name': 'test2.txt', 'content': "ccc"}]}
 
98
    ...     ]
 
99
    ...  }
 
100
    ... ]
 
101
    >>> fg = FS_Generator(dirStruct, '/home/sal/workspace/snappy/')
 
102
 
 
103
    
 
104
    >>> fs = FS_Scanner('/home/sal/workspace/snappy/test')
 
105
    >>> list = fs.files.sort()
 
106
    >>> fs.files
 
107
    ['/home/sal/workspace/snappy/test/test.txt', '/home/sal/workspace/snappy/test/test11/test11.txt', '/home/sal/workspace/snappy/test/test2/test2.txt']
 
108
    >>> fs.concat()
 
109
    "aaabbbccc"
 
110
 
 
111
    # Clear all
 
112
    >>> fg.clean()
 
113
    """
 
114
 
 
115
 
 
116
    def __init__(self, path):
 
117
        self.home = path
 
118
        self.files = []
 
119
        self.scan()
 
120
        pass
 
121
 
 
122
    def scan(self):
 
123
        for root, subFolders, files in os.walk(self.home):
 
124
            for file in files:
 
125
                self.files.append(os.path.join(root, file))
 
126
        
 
127
 
 
128
    def concat(self):
 
129
        result = ""
 
130
        for filename in self.files:
 
131
            handle = open(filename, 'r')
 
132
            data = handle.read()
 
133
            handle.close()
 
134
            result += data
 
135
            
 
136
        return result
 
 
b'\\ No newline at end of file'