-
Notifications
You must be signed in to change notification settings - Fork 24
/
stringutilities.py
588 lines (474 loc) · 21.5 KB
/
stringutilities.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2016, Avtandil Kikabidze aka LONGMAN ([email protected])
# @link http://longman.me
# @license The MIT License (MIT)
import sublime
import sublime_plugin
import string
import re
import sys
import time
import base64
import html.entities as htmlentitydefs
from cgi import escape
from hashlib import md5, sha1, sha224, sha256, sha384, sha512
from datetime import datetime
from random import sample, choice, randrange
import os, socket, urllib
import binascii
import json
import pprint
if sys.hexversion >= 0x3000000:
def unichr(c):
return chr(c)
class StringUtilitiesExpandStringCommand(sublime_plugin.TextCommand):
"""If the region is contained in a string scope, expands the region to
the whole string. If the region is not contained in a string scope, this
command does nothing. It is applied to all regions in the current
selection."""
def run(self, edit):
for region in self.view.sel():
self._run(edit, region)
def _run(self, edit, region):
if (not self.view.match_selector(region.a, "string") or
not self.view.match_selector(region.b, "string")):
return
selector = "string punctuation.definition.string"
p = region.begin()
while not self.view.match_selector(p, selector):
p = self.view.find_by_class(p, False, sublime.CLASS_PUNCTUATION_START)
q = region.end()
while not self.view.match_selector(q, selector):
# sublime.CLASS_PUNCTUATION_END is broken
# this works too
q = self.view.find_by_class(q, True, sublime.CLASS_PUNCTUATION_START)
self.view.sel().add(sublime.Region(p, q + 1))
class ConvertSelection(sublime_plugin.TextCommand):
"""Abstract class to implement a command that modifies the current text
select. Subclasses must implement the convert method which accepts the
selected text and returns a replacement value."""
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region).encode(self.enc())
self.view.replace(edit, region, self.convert(text))
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
def convert(self, text):
raise NotImplementedError("Subclass must implement convert")
class ConvertTabsToSpacesCommand(sublime_plugin.TextCommand):
#Convert Tabs To Spaces
def run(self, edit):
sublime.status_message('Convert tabs to spaces.')
tab_size = int(self.view.settings().get('tab_size', 4))
for region in self.view.sel():
if not region.empty():
self.view.replace(edit, region, self.view.substr(region).expandtabs(tab_size))
else:
self.view.run_command('select_all')
self.view.replace(edit, self.view.sel()[0], self.view.substr(self.view.sel()[0]).expandtabs(tab_size))
self.view.sel().clear()
class ConvertSpacesToTabsCommand(sublime_plugin.TextCommand):
#Convert Spaces To Tabs
def run(self, edit):
sublime.status_message('Convert spaces to tabs.')
tab_size = str(self.view.settings().get('tab_size', 4))
for region in self.view.sel():
if not region.empty():
self.view.replace(edit, region, re.sub(r' {' + tab_size + r'}', r'\t', self.view.substr(region)))
else:
self.view.run_command('select_all')
self.view.replace(edit, self.view.sel()[0], re.sub(r' {' + tab_size + r'}', r'\t', self.view.substr(self.view.sel()[0])))
self.view.sel().clear()
class ConvertSpacesToNonBreaking(sublime_plugin.TextCommand):
#Convert Spaces into Non-breaking Spaces
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = text.replace(" ", " ")
self.view.replace(edit, region, text)
class ConvertCharsToHtmlCommand(sublime_plugin.TextCommand):
#Convert Chars into XML/HTML Entities
def run(self, edit):
for region in self.view.sel():
if not region.empty():
self.view.replace(edit, region, escape(self.view.substr(region), True))
class ConvertHtmlToCharsCommand(sublime_plugin.TextCommand):
#Convert XML/HTML Entities into Chars
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = re.sub('&(%s);' % '|'.join(htmlentitydefs.name2codepoint),
lambda m: unichr(htmlentitydefs.name2codepoint[m.group(1)]), self.view.substr(region))
self.view.replace(edit, region, text)
class ConvertCamelUnderscoresCommand(sublime_plugin.TextCommand):
#Convert camelCase to under_scores and vice versa
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = self.toCamelCase(text) if '_' in text and text[0].islower() else (text[0].islower() and self.toUnderscores(text))
self.view.replace(edit, region, text)
def toUnderscores(self, name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def toCamelCase(self, name):
return ''.join(ch.capitalize() if i > 0 else ch for i, ch in enumerate(name.split('_')))
class ConvertCamelDashCommand(sublime_plugin.TextCommand):
#Convert camelCase to dash and vice versa
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = self.toCamelCase(text) if '-' in text and text[0].islower() else (text[0].islower() and self.toDash(text))
self.view.replace(edit, region, text)
def toDash(self, name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
def toCamelCase(self, name):
return ''.join(ch.capitalize() if i > 0 else ch for i, ch in enumerate(name.split('-')))
class ConvertPascalUnderscoresCommand(sublime_plugin.TextCommand):
#Convert PascalCase to under_scores and vice versa
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = self.toPascalCase(text) if '_' in text and text[0].islower() else (text[0].isupper() and self.toUnderscores(text))
self.view.replace(edit, region, text)
def toUnderscores(self, name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def toPascalCase(self, name):
return ''.join(map(lambda x: x.capitalize(), name.split('_')))
class ConvertToUnicodeNotationCommand(sublime_plugin.TextCommand):
#Convert string to Unicode notation
def run(self, edit):
pattern = re.compile(r'\s+')
for region in self.view.sel():
if not region.empty():
text = ''
for c in self.view.substr(region):
if not re.match(pattern, c) and (ord(c) < 0x20 or ord(c) > 0x7e):
text += '\\u{0:04X}'.format(ord(c))
else:
text += c
self.view.replace(edit, region, text)
class ConvertFromUnicodeNotationCommand(sublime_plugin.TextCommand):
#Convert string from Unicode notation
def run(self, edit):
pattern = re.compile(r'(\\u)([0-9a-fA-F]{2,4})')
for region in self.view.sel():
if not region.empty():
text = re.sub(pattern, lambda m: unichr(int(m.group(2), 16)), self.view.substr(region))
self.view.replace(edit, region, text)
class ConvertToBase64Command(sublime_plugin.TextCommand):
#Encode string with base64
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region).encode(self.enc())
t = base64.b64encode(text)
txt = str(t, self.enc())
self.view.replace(edit, region, txt)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class ConvertFromBase64Command(sublime_plugin.TextCommand):
#Decode string with base64
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region).encode(self.enc())
t = base64.b64decode(text)
txt = str(t, self.enc())
self.view.replace(edit, region, txt)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class ConvertToHexCommand(sublime_plugin.TextCommand):
#Convert string to hex
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region).encode(self.enc())
t = binascii.hexlify(text)
txt = str(t,'ascii')
self.view.replace(edit, region, txt)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class ConvertFromHexCommand(sublime_plugin.TextCommand):
#Convert string from hex
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region).encode(self.enc())
t = binascii.unhexlify(text)
txt = str(t,'ascii')
self.view.replace(edit, region, txt)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class ConvertHexToRgbCommand(sublime_plugin.TextCommand):
#Convert hex to rgb color
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
self.view.replace(edit, region, self.hexToRgb(text))
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
def hexToRgb(self, value):
value = value.lstrip('#')
lv = len(value)
if lv == 6:
rgb = tuple(str(int(value[i:i+lv//3], 16)) for i in range(0, lv, lv//3))
if lv == 3:
rgb = tuple(str(int(value[i:i+1], 16)*17) for i in range(0, 3))
if lv == 1:
v = str(int(value, 16)*17)
rgb = v, v, v
return 'rgb(' + ','.join(rgb) + ')'
class ConvertRgbToHexCommand(sublime_plugin.TextCommand):
#Convert rgb to hex color
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
str_len = len(text)
reg_rgb = '^rgb[a]?\((\s*\d+\s*),(\s*\d+\s*),(\s*\d+\s*),?(\s*(0?.?\d)+\s*)?\)$'
rgb_match = re.match(reg_rgb, text)
if rgb_match is not None:
self.view.replace(edit, region, self.rgbToHex(rgb_match))
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
def rgbToHex(self, rgb_match):
"""Converts an rgb(a) value to a hex value.
Attributes:
self: The Regionset object.
rgb_match: The reg exp collection of matches.
"""
# Convert all values to 10-base integers, strip the leading characters,
# convert to hex and fill with leading zero's.
val_1 = hex(int(rgb_match.group(1), 10))[2:].zfill(2)
val_2 = hex(int(rgb_match.group(2), 10))[2:].zfill(2)
val_3 = hex(int(rgb_match.group(3), 10))[2:].zfill(2)
# Return the proformatted string with the new values.
return '#%s%s%s' % (val_1, val_2, val_3)
class ConvertSingleQuotesToDoubleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = text.replace("'", "\"")
self.view.replace(edit, region, text)
class ConvertDoubleQuotesToSingleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = text.replace("\"", "'")
self.view.replace(edit, region, text)
class UrlDecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = urllib.parse.unquote(text)
self.view.replace(edit, region, text)
class UrlEncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
text = urllib.parse.quote(text)
self.view.replace(edit, region, text)
class ConvertMd5Command(ConvertSelection):
"""Calculate the MD5 hash of the selected text"""
def convert(self, text):
return md5(text).hexdigest()
class ConvertSha1Command(ConvertSelection):
"""Calculate the SHA1 hash of the selected text"""
def convert(self, text):
return sha1(text).hexdigest()
class ConvertSha224Command(ConvertSelection):
"""Calculate the SHA224 hash of the selected text"""
def convert(self, text):
return sha224(text).hexdigest()
class ConvertSha256Command(ConvertSelection):
"""Calculate the SHA256 hash of the selected text"""
def convert(self, text):
return sha256(text).hexdigest()
class ConvertSha384Command(ConvertSelection):
"""Calculate the SHA384 hash of the selected text"""
def convert(self, text):
return sha384(text).hexdigest()
class ConvertSha512Command(ConvertSelection):
"""Calculate the SHA512 hash of the selected text"""
def convert(self, text):
return sha512(text).hexdigest()
class ConvertTimeFormatCommand(sublime_plugin.TextCommand):
#This will allow you to convert epoch to human readable date
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
if re.match('^([0-9]+)$', text):
result = self.fromUnix(text)
else:
result = self.toUnix(text)
if result:
self.view.replace(edit, region, result)
else:
sublime.status_message('Convert error.')
def fromUnix(self, timestamp):
sublime.status_message('Convert from epoch to human readable date.')
timestamp = float(timestamp)
stamp = datetime.fromtimestamp(timestamp)
return stamp.strftime("%Y-%m-%d %H:%M:%S")
def toUnix(self, timestr):
sublime.status_message('Convert from human readable date to epoch.')
try:
datetime_to_convert = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S")
return '%d' % (time.mktime(datetime_to_convert.timetuple()))
except:
return False
class InsertTimestampCommand(sublime_plugin.TextCommand):
#This will allow you to insert timestamp to current position
def run(self, edit):
for region in self.view.sel():
self.view.insert(edit, region.begin(), datetime.now().strftime("%Y-%m-%d %H:%M"))
class GeneratePasswordCommand(sublime_plugin.TextCommand):
chars = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ"
def run(self, edit, length=16):
length = int(length)
self.view.insert(edit, self.view.sel()[0].begin(), ''.join(sample(self.chars, length)))
class GeneratePasswordSpecSymbolsCommand(sublime_plugin.TextCommand):
chars = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ%*)?@#$~"
def run(self, edit, length=16):
length = int(length)
self.view.insert(edit, self.view.sel()[0].begin(), ''.join(sample(self.chars, length)))
class DecodeHeidiSqlCommand(sublime_plugin.TextCommand):
# Requires .strip('\x00') on output otherwise sublimetext adds a 'NUL' control chracter
def run(self, edit):
for region in self.view.sel():
if not region.empty():
text = self.view.substr(region)
if text[0].isdigit(): text = self.decodeHeidi(text)
self.view.replace(edit, region, text)
def decodeHeidi(self, hex_in):
shift = int(hex_in[-1])
shifted_list = [int(hex_in[i:i+2], 16) for i in range(0, len(hex_in), 2)]
return ''.join(chr(out_ch - shift) for out_ch in shifted_list).strip('\x00')
class StringUtilitiesExtIpCommand(sublime_plugin.TextCommand):
def run(self, edit):
url = "http://api.long.ge/sublimetext/ip.php"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
for region in self.view.sel():
self.view.insert(edit, region.begin(), response.read().decode(self.enc()))
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class StringUtilitiesIntIpCommand(sublime_plugin.TextCommand):
def run(self, edit):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
int_ip = s.getsockname()[0]
s.close()
for region in self.view.sel():
self.view.insert(edit, region.begin(), int_ip)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class StringUtilitiesDecodeJsonCommand(sublime_plugin.TextCommand):
output = ""
i = 0
def run(self, edit):
for region in self.view.sel():
self.output = ""
if not region.empty():
text = self.view.substr(region).encode(self.enc())
text = str(text, 'utf8')
data = json.loads(text, encoding='utf8')
output = json.dumps(data, indent=4, sort_keys=True)
self.view.replace(edit, region, output)
#self.recursivePrint(data)
#print(self.output)
#pp = pprint.PrettyPrinter(indent=4, width=1)
#data = pp.pformat(data)
#data = self.output
#data = data.replace('{ ', '{')
#data = data.replace('{', '\n {\n')
#self.view.replace(edit, region, self.output)
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
def recursivePrint(self, src, dpth = 0, key = ''):
""" Recursively prints nested elements."""
tabs = lambda n: '\t' * n * 1 # or 2 or 8 or...
brace = lambda s, n: '%s%s%s' % ('['*n, s, ']'*n)
if isinstance(src, dict):
for key, value in src.items():
if isinstance(value, dict) or (isinstance(value, list)):
self.output += tabs(dpth) + brace(key, dpth) + "\n"
self.recursivePrint(value, dpth + 1, key)
elif (isinstance(src, list)):
self.i = 0
for litem in src:
self.recursivePrint(litem, dpth + 1)
else:
if key:
self.output += tabs(dpth) + '[%s] => %s' % (key, src) + "\n"
else:
self.i = self.i + 1
self.output += tabs(dpth) + str(self.i) + ' => %s' % src + "\n"
class StringUtilitiesTestCommand(sublime_plugin.TextCommand):
def run(self, edit):
ext_ip = urllib2.urlopen('http://api.long.ge/sublimetext/ip.php').read()
for region in self.view.sel():
self.view.insert(edit, region.begin(), ext_ip.encode(self.enc()))
def enc(self):
if self.view.encoding() == 'Undefined':
return self.view.settings().get('default_encoding', 'UTF-8')
else:
return self.view.encoding()
class PhpObjectToArrayCommand(sublime_plugin.TextCommand):
"""
convertes PHP Object into PHP Array Access
from $obj->variable into $obj['variable']
"""
def run(self, edit):
for region in self.view.sel():
if not region.empty():
source_text = self.view.substr(region)
if "->" not in source_text:
# nothing to replace
pass
fragments = source_text.split("->")
result = "{}['{}']".format(fragments[0], fragments[1])
self.view.replace(edit, region, result)