Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeStats.py
More file actions
Latest commit
232 lines (174 loc) · 6.39 KB
/
Copy pathCodeStats.py
File metadata and controls
232 lines (174 loc) · 6.39 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
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
importsublime
importsublime_plugin
importos
importshutil
importdatetime
importjson
importrequests
# Pulses will be sent after intervals of this many seconds
PULSE_TIMEOUT=10
# Default URL for the API
DEFAULT_URL='https://codestats.net/api/my/pulses/'
deflog(*msg):
print('code-stats-sublime:', *msg)
defsend_pulses():
window=sublime.active_window()
# If required settings are not defined, don't act but complain to user
ifnotConfig.has_required_settings():
window.status_message('C::S missing API key! Please add API key in settings.')
log('Missing API key, cannot send data! Please add API key in settings.')
return
pulses=Pulse.pulses_to_send
ifPulse.current_pulseisnotNone:
pulses+= [str(Pulse.current_pulse)]
failed_pulses= []
window.status_message('C::S submitting…')
forpulseinpulses:
failed=False
r=None
try:
r=requests.post(
Config.api_url,
headers={
'content-type': 'application/json',
'x-api-token': Config.api_key,
'user-agent': 'code-stats-sublime/1.1.0',
},
data=pulse
)
ifr.status_code!=201:
failed=True
log('Pulse failed with status', r.status_code, 'and content:', r.text)
window.status_message('C::S submit failed (check API key): {} {}'.format(r.status_code, r.text))
exceptrequests.exceptions.RequestExceptionase:
failed=True
log('Pulse failed with exception', str(e))
window.status_message('C::S error: '+str(e))
iffailed:
failed_pulses+= [pulse]
Pulse.current_pulse=None
Pulse.pulses_to_send=failed_pulses
iflen(failed_pulses) ==0:
window.status_message('')
classConfig:
"""
Configuration handler. Listens to changes in plugin configuration.
"""
api_key=None
api_url=None
initted=False
@classmethod
definit(cls):
cls.load_settings()
cls.settings.add_on_change('API_URL', cls.url_changed)
cls.settings.add_on_change('API_KEY', cls.key_changed)
cls.initted=True
ifnotcls.__is_undefined__(cls.api_key):
log('Initialised with key {}.'.format(cls.api_key))
else:
log('Initialised with no key.')
@classmethod
defload_settings(cls):
cls.settings=sublime.load_settings('CodeStats.sublime-settings')
cls.url_changed()
cls.key_changed()
@classmethod
defurl_changed(cls):
cls.api_url=cls.settings.get('API_URL', DEFAULT_URL)
log('URL changed to {}.'.format(cls.api_url))
@classmethod
defkey_changed(cls):
cls.api_key=cls.settings.get('API_KEY', None)
log('Key changed to {}.'.format(cls.api_key))
@classmethod
defhas_required_settings(cls):
returnnotcls.__is_undefined__(cls.api_url) andnotcls.__is_undefined__(cls.api_key)
@classmethod
defhas_init(cls):
returncls.initted
@staticmethod
def__is_undefined__(value):
returnvalueisNoneorvalue==''
classTimer:
"""
Timer that runs given function after given time.
"""
def__init__(self, fun):
self.fun=fun
self.set_timeout()
defrun(self):
self.fun()
defset_timeout(self):
sublime.set_timeout_async(self.run, PULSE_TIMEOUT*1000)
classPulse:
"""
Represents one Pulse to be sent to the API.
"""
# Current active pulse
current_pulse=None
# JSONified pulses waiting for sending because of network problems
pulses_to_send= []
def__init__(self):
self.xps= {}
defadd_xp(self, language, amount):
"""
Add XP with the given language and given amount into the pulse.
"""
xp=self.xps.get(language, 0) +amount
self.xps[language] =xp
def__str__(self):
# Convert pulse into JSON string that can be sent to API
ret= {'coded_at': self.__timestamp__()}
ret['xps'] = [{'language': l, 'xp': x} forl, xinself.xps.items()]
returnjson.dumps(ret)
@classmethod
defget_pulse(cls):
"""
Get or create currently active Pulse.
"""
ifcls.current_pulseisNone:
cls.current_pulse=Pulse()
returncls.current_pulse
def__timestamp__(self):
# Get ISO timestamp with local time and offset
utc_dt=datetime.datetime.now(datetime.timezone.utc)
loc_dt=utc_dt.astimezone()
returnloc_dt.replace(microsecond=0).isoformat()
classChangeListener(sublime_plugin.EventListener):
"""
Event listener that listens to changes in any editors and counts them.
Changes seem to be a good approximation of characters typed in Sublime Text.
"""
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.timer=None
deftimer_run(self):
send_pulses()
self.timer=None
defon_modified_async(self, view):
# If plugin isn't fully loaded yet, don't do anything
ifnotConfig.has_init():
return
# Prevent XP from other views than editor view
#
# * If the view is read only, the user is obviously not supposed to type there so any events are non-user
# * If the view is a scratch view, err on the side of no XP as these can be stuff like output panels and
# similar too
# * If the view is a widget it's like a panel or the console or some other builtin thing where we don't
# want to capture typing
# * If the view is not the currently active view of the window, it's not the user that is typing, because
# the user can only type in the active view.
if (view.is_read_only() or
view.is_scratch() or
view.settings().get('is_widget') or
view.id() !=view.window().active_view().id()):
return
# Start timer if not already started
ifself.timerisNone:
self.timer=Timer(self.timer_run)
pulse=Pulse.get_pulse()
syntax_file=os.path.basename(view.settings().get('syntax'))
language=os.path.splitext(syntax_file)[0]
pulse.add_xp(language, 1)
defplugin_loaded():
Config.init()