forked from jgirardet/sublack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (60 loc) · 1.79 KB
/
main.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
"""
Sublack
Order of imports should not be changed
"""
import logging
import sublime
import os
from .sublack import (
PACKAGE_NAME,
SETTINGS_FILE_NAME,
get_settings,
cache_path,
clear_cache,
Path,
BlackFileCommand,
BlackDiffCommand,
BlackToggleBlackOnSaveCommand,
BlackEventListener,
BlackdStartCommand,
BlackdStopCommand,
BlackFormatAllCommand,
) # flake8: noqa
LOG = logging.getLogger(PACKAGE_NAME)
if not os.environ.get("CI", None):
LOG.propagate = False
def plugin_loaded():
# load config
current_view = sublime.active_window().active_view()
config = get_settings(current_view)
if config["black_log"] == None:
config["black_log"] = "info"
# Setup logging
if not LOG.handlers:
debug_formatter = logging.Formatter(
"[{}:%(filename)s](%(levelname)s) %(message)s".format(PACKAGE_NAME)
)
dh = logging.StreamHandler()
dh.setLevel(logging.DEBUG)
dh.setFormatter(debug_formatter)
LOG.addHandler(dh)
try:
LOG.setLevel(config.get("black_log").upper())
except ValueError as err: # https://forum.sublimetext.com/t/an-odd-problem-about-sublime-load-settings/30335/6
LOG.error(err)
LOG.setLevel("ERROR")
LOG.error("fallback to loglevel ERROR")
LOG.info("Loglevel set to %s", config["black_log"].upper())
# check cache_path
cp = cache_path()
if not cp.exists():
cp.mkdir()
# clear cache
clear_cache()
# # check blackd autostart
if config["black_blackd_autostart"]:
sublime.set_timeout_async(lambda: current_view.run_command("blackd_start"), 0)
# watch for loglevel change
sublime.load_settings(SETTINGS_FILE_NAME).add_on_change(
"black_log", lambda: Path(__file__).touch()
)