| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import sys |
| 9 |
from array import * |
| 10 |
|
| 11 |
class CodemarkManager(): |
| 12 |
|
| 13 |
def __init__(self, parent=None): |
| 14 |
self.haystack = [] |
| 15 |
|
| 16 |
def check(self, filename, line): |
| 17 |
if self.haystack.count((filename, line)): |
| 18 |
return 1 |
| 19 |
return 0 |
| 20 |
|
| 21 |
def append(self, filename, line): |
| 22 |
if self.check(filename, line) == 0: |
| 23 |
self.haystack.append((filename, line)) |
| 24 |
return self.haystack.index((filename, line)) |
| 25 |
|
| 26 |
def delete(self, filename, line): |
| 27 |
if self.haystack.count((filename, line)) == 0: |
| 28 |
return -1 |
| 29 |
else: |
| 30 |
self.haystack.remove((filename, line)) |
| 31 |
return 0 |
| 32 |
|
| 33 |
def delete_index(self, index): |
| 34 |
if self.count() <= index: |
| 35 |
return -1 |
| 36 |
elif index < 0: |
| 37 |
return -1 |
| 38 |
else: |
| 39 |
self.haystack.pop(index) |
| 40 |
return 0 |
| 41 |
|
| 42 |
def count(self): |
| 43 |
return len(self.haystack) |
| 44 |
|
| 45 |
def get(self, index): |
| 46 |
if self.count() <= index: |
| 47 |
return (None, None) |
| 48 |
elif index < 0: |
| 49 |
return (None, None) |
| 50 |
else: |
| 51 |
(filename, line) = self.haystack.pop(index) |
| 52 |
self.haystack.insert(index, (filename, line)) |
| 53 |
return (filename, line) |
| 54 |
|
| 55 |
def clear(self): |
| 56 |
del self.haystack[:] |
| 57 |
|
| 58 |
def codemarks(self): |
| 59 |
return self.haystack |
| 60 |
|
| 61 |
def dump(self): |
| 62 |
for i in range(len(self.haystack)): |
| 63 |
print("cm index ", i, " ", self.haystack[i], file=sys.stderr) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if __name__ == '__main__': |
| 72 |
|
| 73 |
cm = CodemarkManager() |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
print("Initial empty", file=sys.stderr) |
| 80 |
|
| 81 |
print("cm has ", cm.count(), "items (0)", file=sys.stderr) |
| 82 |
|
| 83 |
cm.append("file1", 100) |
| 84 |
cm.append("file2", 200) |
| 85 |
cm.append("file3", 300) |
| 86 |
|
| 87 |
print("cm has " , cm.count(), "items (3) ", file=sys.stderr) |
| 88 |
|
| 89 |
cm.delete("file1", 100) |
| 90 |
|
| 91 |
print("cm has " , cm.count(), "items (2)", file=sys.stderr) |
| 92 |
|
| 93 |
cm.delete("file2", 100) |
| 94 |
|
| 95 |
print("cm has " , cm.count(), "items (2)", file=sys.stderr) |
| 96 |
|
| 97 |
cm.delete_index(1) |
| 98 |
|
| 99 |
print("cm has " , cm.count(), "items (1)", file=sys.stderr) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
for i in range(cm.count()): |
| 106 |
(f, l) = cm.get(i) |
| 107 |
print(f, " (#", l, ")", file=sys.stderr) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
index = cm.append("file4", 400) |
| 114 |
print("index for file4 #400 is " , index, file=sys.stderr) |
| 115 |
|
| 116 |
index = cm.append("file4", 400) |
| 117 |
print("index for file4 #400 is " , index, " (again)", file=sys.stderr) |
| 118 |
|
| 119 |
print("cm has " , cm.count(), "items (2) ", file=sys.stderr) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
print("cm dump for final check", file=sys.stderr) |
| 126 |
|
| 127 |
cm.dump() |