~mjorgensen/aw-watcher-sublimetext

ref: d78e0854591b628230fd521671f1bbd6c255f8a5 aw-watcher-sublimetext/main.py -rw-r--r-- 2.9 KiB
d78e0854Matthew Jorgensen Linted with flake8 2 months ago
                                                                                
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
import sublime
import sublime_plugin

from .activitywatch.api import ActivityWatchAPI
from .activitywatch import utils

# globals
CLIENT_ID = "aw-watcher-sublimetext"
SETTINGS_FILE = "aw-watcher.sublime-settings"
SETTINGS = {}
DEBUG = False
CONNECTED = False
api = ActivityWatchAPI()


def plugin_loaded():
	global SETTINGS
	SETTINGS = sublime.load_settings(SETTINGS_FILE)

	utils.log("Initializing ActivityWatch plugin.")

	after_loaded()


def after_loaded():
	global CONNECTED, DEBUG
	if DEBUG:
		utils.log("after_loaded() called")
	global api
	api.setup(
		CLIENT_ID, SETTINGS.get("hostname"),
		SETTINGS.get("port"), SETTINGS.get("heartbeat_frequency"))
	CONNECTED = api.check()
	DEBUG = SETTINGS.get("debug")
	if DEBUG:
		api.enable_debugging()

		utils.log("hostname: {}\n\tport: {}\n\theartbeat_freq: {}\n\tbucket_name: {}\n\tdebug: {}".format(
			SETTINGS.get("hostname"), SETTINGS.get("port"),
			SETTINGS.get("heartbeat_freq"), SETTINGS.get("bucket_name"),
			SETTINGS.get("debug")))
		utils.log("Connected? {}".format(CONNECTED))


def get_file_name(view):
	return view.file_name() or view.name() or "untitled"


def get_project_name(view):
	window = view.window()
	project = "unknown"

	if hasattr(window, "project_data"):
		project = window.project_data()
	if not project:
		project = "unknown"
	if "name" in project:
		project = project.get("name")
	elif "folders" in project:
		for folder in project.get("folders"):
			if get_file_name(view).startswith(folder.get("path")):
				project = folder.get("path")
				break
	if DEBUG:
		utils.log("project: {}".format(project))
	return project


def get_language(view):
	try:
		point = view.sel()[0].begin()
	except IndexError:
		return
	scopes = view.scope_name(point).strip().split(" ")
	if DEBUG:
		utils.log("scopes: {}".format(scopes))
	return scopes[0]


def correct_slashes(path):
	if DEBUG:
		utils.log("correct_slashes() called")
	return path.replace("\\", "/").replace("C:", "/C")


def handle_activity(view):
	if DEBUG:
		utils.log("handle_activity() fired")
	if CONNECTED:
		api.ensure_bucket(SETTINGS.get("bucket_name"))
	else:
		active_window = sublime.active_window()
		if active_window:
			for view in active_window.views():
				view.set_status(
					CLIENT_ID,
					"[aw-watcher-sublimetext] Could not connect "
					"to aw-server")
	event_data = {
		"file": correct_slashes(get_file_name(view)),
		"project": correct_slashes(get_project_name(view)),
		"language": get_language(view),
	}
	if DEBUG:
		utils.log("file: {}\n\tproject: {}\n\tlanguage: {}".format(
			event_data["file"],
			event_data["project"],
			event_data["language"]))
	api.heartbeat(SETTINGS.get("bucket_name"), event_data)


class ActivityWatchListener(sublime_plugin.EventListener):

	def on_selection_modified_async(self, view):
		if DEBUG:
			utils.log("on_selection_modified_async fired")
		if CONNECTED:
			handle_activity(view)

	def on_modified_async(self, view):
		if DEBUG:
			utils.log("on_modified_async fired")
		if CONNECTED:
			handle_activity(view)