Repository navigation
Expand file tree
/
Copy pathlist-operation.py
More file actions
142 lines (124 loc) · 3.97 KB
/
Copy pathlist-operation.py
File metadata and controls
142 lines (124 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__description__ = "Notepad++ PythonScript plugin Python program to perform operations on lists"
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2024/09/07'
"""
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2024/09/07: start
Todo:
"""
import random
def GetCurentAndNextBufferID():
currentBufferID = notepad.getCurrentBufferID()
nextBufferID = None
previousBufferId = None
for item in notepad.getFiles():
if previousBufferId == None and currentBufferID == item[1]:
previousBufferId = currentBufferID
elif previousBufferId != None:
nextBufferID = item[1]
break
if nextBufferID != None:
notepad.activateBufferID(nextBufferID)
return currentBufferID, nextBufferID
def BufferToList(bufferID=None):
if bufferID != None:
currentBufferID = notepad.getCurrentBufferID()
notepad.activateBufferID(bufferID)
counter = 0
items = []
while True:
try:
line = editor.getLine(counter)
except IndexError:
break
items.append(line.rstrip('\r\n'))
counter += 1
if bufferID != None:
notepad.activateBufferID(currentBufferID)
if len(items) > 1 and items[-1] == '':
items = items[:-1]
return items
def GetEOL(bufferID=None):
if bufferID != None:
currentBufferID = notepad.getCurrentBufferID()
notepad.activateBufferID(bufferID)
line = editor.getLine(0)
if bufferID != None:
notepad.activateBufferID(currentBufferID)
if line.endswith('\r\n'):
return '\r\n'
elif line.endswith('\r'):
return '\r'
elif line.endswith('\n'):
return '\n'
else:
raise Exception('Unknown EOL marker')
def CheckIfMarked():
try:
markBufferID
except NameError:
return False
return True
def GetOperation():
dOperations = {
's': 'Subtract',
'i': 'Intersect',
'u': 'Union',
'l': 'Listify',
'r': 'Randomize',
}
operation = notepad.prompt(' '.join(['%s: %s' % item for item in dOperations.items()]) + '\nAdd suffix n to create a new document', 'Select the desired operation', '')
if operation == None:
return None, None
operation = operation.lower()
if operation.endswith('n'):
newDocument = True
operation = operation[:-1]
else:
newDocument = False
if operation not in dOperations:
notepad.messageBox('Error: unknown operation "%s"!' % operation, 'Error')
return None, None
return operation, newDocument
def Operation():
operation, newDocument = GetOperation()
if operation == None:
return
if not operation in ['l', 'r'] and not CheckIfMarked():
notepad.messageBox('Error: mark a document first!', 'Error')
return
currentList = BufferToList()
currentEOL = GetEOL()
if not operation in ['l', 'r']:
markedList = BufferToList(markBufferID)
newList = []
if operation == 's':
for item in currentList:
if not item in markedList:
newList.append(item)
elif operation == 'i':
for item in currentList:
if item in markedList:
newList.append(item)
elif operation == 'u':
newList.extend(currentList)
for item in markedList:
if not item in currentList:
newList.append(item)
elif operation == 'r':
newList.extend(currentList)
random.shuffle(newList)
elif operation == 'l':
listifyFormat = notepad.prompt('<OPENLIST><SEPARATOR><CLOSELIST>', 'Provide the list format', '[, ]')
if listifyFormat == None:
return
listify = listifyFormat[0] + listifyFormat[1:-1].join(currentList) + listifyFormat[-1]
newList.append(listify)
if newDocument:
notepad.new()
editor.setText(currentEOL.join(newList + ['']))
Operation()