root/trunk/pyramid/utils.py

Revision 163, 3.5 kB (checked in by tim, 3 years ago)

ran reindent and raised file exceptions in flatteners

Line 
1 import os
2 import shutil
3
4 class Stack:
5
6     def __init__(self, indent, pointer):
7         self.indent = indent
8         self.pointer = pointer
9         self.indentStack = []
10         self.pointerStack = []
11
12     def push(self,indent):
13         self.indentStack.append(self.indent)
14         self.pointerStack.append(self.pointer)
15         self.indent = indent
16         self.pointer = self.pointer[-1]['children']
17
18     def pop(self):
19         self.indent = self.indentStack.pop()
20         self.pointer = self.pointerStack.pop()
21
22     def prevIndent(self):
23         return self.indentStack[-1]
24
25 def parseIndentedList(text, factory):
26
27     lines = text.split('\n')
28     data = {'children':[]}
29
30     # skip any empty lines
31     while lines[0].strip() == '':
32         lines = lines[1:]
33
34     # this should be the first non-empty line
35     trimline, newindent = stripAndCountIndent(lines[0])
36     stack = Stack(newindent,data['children'])
37
38     for line in lines:
39         if line.strip() == '':
40             continue
41         trimline, newindent = stripAndCountIndent(line)
42         if newindent > stack.indent:
43             stack.push(newindent)
44
45         if newindent < stack.indent:
46             while newindent < stack.indent:
47                 stack.pop()
48             if newindent != stack.indent:
49                 raise KeyError
50
51         stack.pointer.append( {'data':factory(trimline), 'children':[] } )
52
53     return data['children']
54
55 def stripAndCountIndent(line):
56     lstripline = line.lstrip()
57     return lstripline, len(line)-len(lstripline)
58
59 def recursiveRemoveDirectoryContents(dir):
60     for root, dirs, files in os.walk(dir, topdown=False):
61         for name in files:
62             os.remove(os.path.join(root, name))
63         for name in dirs:
64             os.rmdir(os.path.join(root, name))
65
66 def pathsplitall(path):
67     segments = []
68     head = path
69     while 1:
70         head, tail = os.path.split(head)
71         segments.append(tail)
72         if head == '':
73             break
74     segments.reverse()
75     return tuple(segments)
76
77 # Test code:
78 if __name__ == "__main__":
79     text = '''
80     documentation /docs
81         language /docs/language
82         libraries /docs/libraries
83         more... /docs
84     downloads /downloads
85         python-2.3.tar.bz2 /downloads/unix
86         python-2.3.exe /downloads/libraries
87         more... /downloads
88 '''
89
90
91     def f(text):
92         t = text.split(' ')
93         return {'href': t[-1], 'label': t[:-1]}
94
95     print 'text'
96     print text
97     tree = parseIndentedList(text, f)
98
99     print "indented list"
100     from pprint import pprint
101     pprint(tree)
102
103     # recreate indented list from data
104     def printnode(item, indent=0):
105         if indent >= 0:
106             print '%s%s' % (' '*indent, item['data'])
107         for child in item['children']:
108             printnode(child, indent+4)
109
110     printnode(tree, indent=-4)
111
112
113
114 def copytree(src, dst, symlinks=0, nocopylist=[]):
115     names = os.listdir(src)
116     if not os.path.isdir(dst):
117         os.mkdir(dst)
118     for name in names:
119         srcname = os.path.join(src, name)
120         dstname = os.path.join(dst, name)
121         try:
122             if symlinks and os.path.islink(srcname):
123                 linkto = os.readlink(srcname)
124                 os.symlink(linkto, dstname)
125             elif os.path.isdir(srcname):
126                 if name not in nocopylist:
127                     copytree(srcname, dstname, symlinks, nocopylist)
128             else:
129                 shutil.copy2(srcname, dstname)
130         except (IOError, os.error), why:
131             print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
Note: See TracBrowser for help on using the browser.