/
pandownTouchProjectConfigCommand.py
46 lines (40 loc) · 1.95 KB
/
pandownTouchProjectConfigCommand.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
from __future__ import print_function
import sublime
import sublime_plugin
import os
import shutil
import codecs
class PandownTouchProjectConfigCommand(sublime_plugin.WindowCommand):
def run(self):
if self.window.active_view().file_name():
configFile = os.path.join(os.path.dirname(self.window.active_view().file_name()), 'pandoc-config.json')
else:
sublime.status_message("Cannot create project configuration for unsaved files.")
return
if os.path.exists(configFile):
self.window.open_file(configFile)
return
defaultConfigFile = os.path.join(sublime.packages_path(), 'Pandown', 'default-pandoc-config.json')
userConfigFile = os.path.join(sublime.packages_path(), 'User', 'pandoc-config.json')
if not os.path.exists(defaultConfigFile) and not os.path.exists(userConfigFile):
try:
s = sublime.load_resource("Packages/Pandown/default-pandoc-config.json")
except OSError as e:
sublime.status_message("Could not load default Pandoc configuration.")
print("[Pandown could not find a default configuration file in Packages/Pandown/default-pandoc-config.json]")
print("[Loading from the binary package resource file also failed.]")
print("[e: {0}]".format(e))
return
with codecs.open(configFile, "w", "utf-8") as f:
f.write(s)
self.window.open_file(configFile)
else:
try:
toCopy = defaultConfigFile if not os.path.exists(userConfigFile) else userConfigFile
shutil.copy(toCopy, configFile)
except Exception as e:
sublime.status_message("Could not write {0}".format(configFile))
print("[Pandown encountered an exception:]")
print("[e: {0}]".format(e))
else:
self.window.open_file(configFile)