| 1 |
|
|---|
| 2 |
import logging; |
|---|
| 3 |
|
|---|
| 4 |
class OperationBase: |
|---|
| 5 |
""" Base class of the operations. |
|---|
| 6 |
""" |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
def __init__(self, opName): |
|---|
| 12 |
self.opName = opName; |
|---|
| 13 |
|
|---|
| 14 |
def getName(self): |
|---|
| 15 |
return self.opName; |
|---|
| 16 |
|
|---|
| 17 |
def applyOp(self, driver): |
|---|
| 18 |
""" Perform operation - intended for override in subclasses. |
|---|
| 19 |
""" |
|---|
| 20 |
logging.getLogger("OperationBase").error( |
|---|
| 21 |
"OperationBase.applyOp called"); |
|---|
| 22 |
|
|---|
| 23 |
def addSelf(self, opBundle): |
|---|
| 24 |
""" Helper callback for adding to a bundle. |
|---|
| 25 |
""" |
|---|
| 26 |
opBundle.internal_addOp(self); |
|---|
| 27 |
|
|---|
| 28 |
def subSelf(self, opBundle): |
|---|
| 29 |
""" Helper callback for removing from a bundle. |
|---|
| 30 |
""" |
|---|
| 31 |
opBundle.internal_subOp(self); |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
class GroupingOperation(OperationBase): |
|---|
| 38 |
""" Group of operations. |
|---|
| 39 |
|
|---|
| 40 |
If a group is added to a bundle all the operations in that group |
|---|
| 41 |
will be added and likewise for removal. |
|---|
| 42 |
Watch out when using multiple groups that contain the same operation, |
|---|
| 43 |
there will only be one instance of the operation in the bundle and it |
|---|
| 44 |
is removed when the first group that contains it is removed. |
|---|
| 45 |
""" |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
def __init__(self, opName): |
|---|
| 51 |
OperationBase.__init__(self, opName); |
|---|
| 52 |
self.opNameList = []; |
|---|
| 53 |
|
|---|
| 54 |
def applyOp(self, driver): |
|---|
| 55 |
""" Do nothing -- all operations in this group were added individually. |
|---|
| 56 |
""" |
|---|
| 57 |
logging.getLogger("GroupingOperation").error( |
|---|
| 58 |
"GroupingOperation.applyOp called."); |
|---|
| 59 |
|
|---|
| 60 |
def addOp(self, opName): |
|---|
| 61 |
""" Add an operation to this group |
|---|
| 62 |
""" |
|---|
| 63 |
if opName not in self.opNameList: |
|---|
| 64 |
self.opNameList.append(opName); |
|---|
| 65 |
|
|---|
| 66 |
def subOp(self, opName): |
|---|
| 67 |
""" Remove an operation from this group |
|---|
| 68 |
""" |
|---|
| 69 |
if opName in self.opNameList: |
|---|
| 70 |
self.opNameList.remove(opName); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
def getOpNameList(self): |
|---|
| 74 |
""" Get a list of operations in this group - names only |
|---|
| 75 |
""" |
|---|
| 76 |
return self.opNameList; |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
def addSelf(self, opBundle): |
|---|
| 80 |
""" Helper callback for adding to a bundle - adds all op in this group. |
|---|
| 81 |
""" |
|---|
| 82 |
for opName in self.opNameList: |
|---|
| 83 |
opBundle.addOp(opName); |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
def subSelf(self, opBundle): |
|---|
| 87 |
""" Helper callback for removing from a bundle - removes all op |
|---|
| 88 |
in this group. |
|---|
| 89 |
""" |
|---|
| 90 |
for opName in self.opNameList: |
|---|
| 91 |
opBundle.subOp(opName); |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
class SimpleReplaceOperation(OperationBase): |
|---|
| 99 |
""" Perform a simple replace of one string with another. |
|---|
| 100 |
""" |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
def __init__(self, opName, replace, replacement): |
|---|
| 107 |
OperationBase.__init__(self, opName); |
|---|
| 108 |
self.replaceString = replace; |
|---|
| 109 |
self.replacementString = replacement; |
|---|
| 110 |
|
|---|
| 111 |
def applyOp(self, driver): |
|---|
| 112 |
""" perform the replacement on the file currently loaded by the driver. |
|---|
| 113 |
""" |
|---|
| 114 |
log = logging.getLogger("SimpleReplaceOperation"); |
|---|
| 115 |
log.debug(">> apply"); |
|---|
| 116 |
log.info("Replace \"%s\" with \"%s\"." % (self.replaceString, self.replacementString)); |
|---|
| 117 |
fileContent = driver.getCurrFileContent(); |
|---|
| 118 |
fileContent = fileContent.replace(self.replaceString, self.replacementString); |
|---|
| 119 |
driver.setCurrFileContent(fileContent); |
|---|
| 120 |
log.debug("<< apply"); |
|---|
| 121 |
|
|---|
| 122 |
def getReplace(self): |
|---|
| 123 |
return self.replaceString; |
|---|
| 124 |
|
|---|
| 125 |
def getReplacement(self): |
|---|
| 126 |
return replacementString; |
|---|
| 127 |
|
|---|