/
pub2web.py
125 lines (99 loc) · 3.73 KB
/
pub2web.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
import sublime, sublime_plugin
class ShootCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
import sys
import os
import sublime
import sublime_plugin
import logging
import threading
import string
import random
import re
import json
sys.path.append(os.path.join(os.path.dirname(__file__), "socketIO"))
from socketIO_client import SocketIO, BaseNamespace
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
class BaseCommand(sublime_plugin.TextCommand):
def alertUser(msg):
sublime.message_dialog(msg)
class LogwebCommand(BaseCommand):
def run(self, edit):
print("Running LogwebCommand")
server = SocketServerThread()
server.start()
class GetWordsCommand(BaseCommand):
def run(self, edit):
view = self.view
r = sublime.Region(0, view.size())
text = view.substr(r)
words = text.split()
print(words)
view.run_command('find_logs', r)
class FindLogs(BaseCommand):
def run(self, edit):
view = self.view
pattern = r'(util\.logweb\((.*)\);)'
# These regions must be reversed so offsets dont get distorted.
for region in reversed(list(view.find_all(pattern))):
regString = view.substr(region)
if "uuid" not in regString:
matchString = re.sub(pattern, r'util.logweb(\g<2>, {uuid:"' + id_generator() +'"});', regString)
print("MATCH STRING: ", matchString)
if matchString != regString:
view.replace(edit,region,matchString)
class AnnotateLog(BaseCommand):
def run(self, edit, **args):
view = self.view
logVal = args["value"]
logId = args["uuid"]
pattern = r'(util\.logweb\((.*)\);)'
logRegions = view.find_all(pattern)
for region in reversed(list(view.find_all(pattern))):
regString = view.substr(region)
if logId in regString:
print("Found ID to annotate: ", regString)
stringWithAnnotation = regString + " /* Annotated value: " + str(logVal) + " */"
print("Replacing with: " + stringWithAnnotation)
view.replace(edit,region, stringWithAnnotation)
class registerRegions(sublime_plugin.EventListener):
# This method is called every time a file is saved (not only the first
# time is saved)
def on_pre_save(self, view):
print('Searching for logs in file: ' + view.file_name())
if view.window().extract_variables()['file_extension'] in ['html','pub', 'dict']:
view.run_command('find_logs')
pass
else:
view.erase_regions('logRegions')
# self.io.emit('message', "Received from client: ")
class SocketServerThread(threading.Thread):
"""A socket server to hold the connection."""
def __init__(self):
super(SocketServerThread, self).__init__()
threading.Thread.__init__(self)
def run(self):
try:
print("threading has begun")
self.io = SocketIO('http://10.224.59.158', 3000, verify=False)
print("Created new SocketServer")
self.io.on('connected', self.handleClientMessage)
self.io.on('reply', self.on_server_response)
self.io.wait()
return
except (e):
print("There was an error in the server thread.")
print("threading has completed")
# self.io.wait(seconds=10)
def on_connect(self):
print('[Connected]')
def on_server_response(self, responseObj):
print('Server response received', type(responseObj))
# responseDict = json.loads(responseObj)
sublime.active_window().active_view().run_command("annotate_log", responseObj)
def handleServerMessage(self, *args):
print("Server Message", args)
def handleClientMessage(self, *args):
print("Client Message", args)