forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asettings.py
133 lines (110 loc) · 3.14 KB
/
asettings.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
import sublime
import sublime_plugin
import datetime
import os
import logging
import shutil
import OrgExtended.orgutil.template as temp
log = logging.getLogger(__name__)
defaultTodoStates = ["TODO(!)", "NEXT", "BLOCKED","WAITING","|", "CANCELLED", "DONE","MEETING","PHONE","NOTE"]
daysOfWeek = ["sun","mon","tue","wed","th","fri","sat"]
class ASettings:
def __init__(self, settingsName):
self.settings = sublime.load_settings(settingsName + '.sublime-settings')
def Get(self, name, defaultVal):
val = self.settings.get(name)
if(val == None):
val = defaultVal
return val
configFilename = "OrgExtended"
def setup_user_settings():
# This is old and I should remove it!
# We don't need this mechanism anymore!
filename = configFilename + ".sublime-settings"
user_settings_path = os.path.join(
sublime.packages_path(),
"User",
filename)
if not os.path.exists(user_settings_path):
default_settings_path = os.path.join(
sublime.packages_path(),
"OrgExtended",
filename)
if os.path.exists(default_settings_path):
shutil.copyfile(default_settings_path, user_settings_path)
def setup_mousemap():
filename = "Default.sublime-mousemap"
user_settings_path = os.path.join(
sublime.packages_path(),
"User",
filename)
mousemap = """
[
{
"button": "button1",
"count": 1,
"modifiers": ["alt"],
"press_command": "drag_select",
// In the future we may vector this to a more generic handler
"command": "org_mouse_handler",
}
]
"""
if not os.path.exists(user_settings_path):
with open(user_settings_path,"w") as o:
o.write(mousemap)
o.write("\n")
class OrgSetupMouseMapCommand(sublime_plugin.TextCommand):
def run(self, edit, event=None):
setup_mousemap()
# Singleton access
_sets = None
def Load():
global configFilename
global _sets
_sets = ASettings(configFilename)
def Get(name, defaultValue, formatDictionary = None):
global _sets
if(_sets == None):
log.warning("SETTINGS IS NULL? IS THIS BEING CALLED BEFORE PLUGIN START?")
Load()
rv = _sets.Get(name, defaultValue)
formatDict = {
"date": str(datetime.date.today()),
"time": datetime.datetime.now().strftime("%H:%M:%S"),
"datetime": str(datetime.datetime.now().strftime("%Y-%m-%d %a %H:%M")),
}
if(formatDictionary != None):
formatDict.update(formatDictionary)
if(str == type(rv)):
formatter = temp.TemplateFormatter()
rv = formatter.format(rv, **formatDict)
if(list == type(rv)):
formatter = temp.TemplateFormatter()
rv = [ (formatter.format(r, **formatDict) if str == type(r) else r) for r in rv ]
return rv
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
def GetInt(name, defaultValue):
v = Get(name, defaultValue)
try:
i = int(v)
return i
except:
return defaultValue
# Will return a date or an index as an integer where 0 means Sunday
# and so on in the week.
def GetDateAsIndex(name, defaultValue):
global daysOfWeek
val = Get(name, defaultValue)
if(RepresentsInt(val)):
return int(val) % 7
val = val.lower()
for i in range(0,len(daysOfWeek)):
if daysOfWeek[i] in val:
return i
return 0