forked from SublimeText/PackageDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_conversion.py
333 lines (264 loc) · 13.6 KB
/
file_conversion.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os
import time
import sublime
import sublime_plugin
from sublime_lib import OutputPanel
from pathlib import Path
from .lib.view_utils import get_text
from .lib.fileconv import dumpers, loaders
__all__ = ('PackagedevConvertCommand',)
# build command
class PackagedevConvertCommand(sublime_plugin.WindowCommand):
"""Convert a file (view's buffer) of type ``source_format`` to type
``target_format``.
Supports the following parsers/loaders:
'json'
'plist'
'yaml'
Supports the following writers/dumpers:
'json'
'plist'
'yaml'
The different dumpers try to validate the data passed.
This works best for json -> anything because json only defines
strings, numbers, lists and objects (dicts, arrays, hash tables).
"""
# {name:, kwargs:}
# name is for the quick panel; others are arguments used when running the command again
target_list = [dict(name=fmt.name,
kwargs={"target_format": fmt.ext})
for fmt in dumpers.get.values()]
for i, itm in enumerate(target_list):
if itm['name'] == "YAML":
# Hardcode YAML block style, who knows if anyone can use this
target_list.insert(
i + 1,
dict(name="YAML (Block Style)",
kwargs={"target_format": "yaml", "default_flow_style": False})
)
def _auto_detect_file_type(self, source_format, target_format, output):
"""Available parameters:
source_format (str) = None
The source format. Any of "yaml", "plist" or "json".
If `None`, attempt to automatically detect the format by extension, used syntax
highlight or (with plist) the actual contents.
target_format (str) = None
The target format. Any of "yaml", "plist" or "json".
If `None`, attempt to find an option set in the file to parse.
If unable to find an option, ask the user directly with all available format options.
output (OutputPanel) = None
"""
type_handling = None
# Auto-detect the file type if it's not specified
if not source_format:
output.write("Input type not specified, auto-detecting...")
for Loader in loaders.get.values():
if Loader.file_is_valid(self.view):
source_format = Loader.ext
output.print(' %s\n' % Loader.name)
break
if not source_format:
type_handling = output.print("\nUnable to detect file type.")
elif target_format == source_format:
type_handling = output.print("File already is %s." % Loader.name)
return type_handling
def _validate_run(self, source_format=None, target_format=None):
"""Available parameters:
source_format (str) = None
The source format. Any of "yaml", "plist" or "json".
If `None`, attempt to automatically detect the format by extension, used syntax
highlight or (with plist) the actual contents.
target_format (str) = None
The target format. Any of "yaml", "plist" or "json".
If `None`, attempt to find an option set in the file to parse.
If unable to find an option, ask the user directly with all available format options.
"""
result = False
# Check the environment (view, args, ...)
if self.view.is_dirty():
# Save the file so that source and target file on the drive don't differ
self.view.run_command("save")
if self.view.is_dirty():
result = sublime.error_message("The file could not be saved correctly. "
"The build was aborted")
elif source_format and target_format == source_format:
result = True
self.status("Target and source file format are identical. (%s)" % target_format)
elif source_format and source_format not in loaders.get:
result = True
self.status("Loader for '%s' not supported/implemented." % source_format)
elif target_format and target_format not in dumpers.get:
result = True
self.status("Dumper for '%s' not supported/implemented." % target_format)
return result
def _revalidate_run(self, output, source_format=None, target_format=None,):
"""Available parameters:
source_format (str) = None
The source format. Any of "yaml", "plist" or "json".
If `None`, attempt to automatically detect the format by extension, used syntax
highlight or (with plist) the actual contents.
target_format (str) = None
The target format. Any of "yaml", "plist" or "json".
If `None`, attempt to find an option set in the file to parse.
If unable to find an option, ask the user directly with all available format options.
output (OutputPanel) = None
"""
result = None
# Validate the shit again, but this time print to output panel
if source_format is not None and target_format == source_format:
result = output.print("\nTarget and source file format are identical. (%s)"
% target_format)
if target_format not in dumpers.get:
result = output.print("\nDumper for '%s' not supported/implemented."
% target_format)
return result
def run(self, source_format=None, target_format=None, ext=None,
open_new_file=False, rearrange_yaml_syntax_def=False, _output=None, **kwargs):
"""Available parameters:
source_format (str) = None
The source format. Any of "yaml", "plist" or "json".
If `None`, attempt to automatically detect the format by extension, used syntax
highlight or (with plist) the actual contents.
target_format (str) = None
The target format. Any of "yaml", "plist" or "json".
If `None`, attempt to find an option set in the file to parse.
If unable to find an option, ask the user directly with all available format options.
ext (str) = None
The extension of the file to convert to, without leading dot. If `None`, the extension
will be automatically determined by a special algorythm using "appendixes".
Here are a few examples:
".YAML-ppplist" yaml -> plist ".ppplist"
".json" json -> yaml ".yaml"
".tmplist" plist -> json ".JSON-tmplist"
".yaml" json -> plist ".JSON-yaml" (yes, doesn't make much sense)
open_new_file (bool) = False
Open the (newly) created file in a new buffer.
rearrange_yaml_syntax_def (bool) = False
Interesting for language definitions, will automatically run
"rearrange_yaml_syntax_def" command on it, if the target format is "yaml".
Overrides "open_new_file" parameter.
_output (OutputPanel) = None
For internal use only.
**kwargs
Will be forwarded to both the loading function and the dumping function, after
stripping unsupported entries. Only do this if you know what you're doing.
Functions in question:
yaml.dump
json.dump
plist.writePlist (does not support any parameters)
A more detailed description of each supported parameter for the respective dumper can
be found in `fileconv/dumpers.py`.
"""
self.view = self.window.active_view()
result = self._validate_run(self, source_format, target_format)
if result:
return result
file_path = self.view.file_name()
if not file_path:
return self.status("File does not exist.", file_path)
file_path = Path(file_path)
# Now the actual "building" starts (collecting remaining parameters)
with OutputPanel.create(self.window, "package_dev",
read_only=True, force_writes=True) as output:
output.show()
type_handling = self._auto_detect_file_type(source_format, target_format, output)
if type_handling:
return type_handling
# Load inline options
Loader = loaders.get[source_format]
opts = Loader.load_options(self.view)
# Function to determine the new file extension depending on the target format
def get_new_ext(target_format):
if ext:
return '.' + ext
if opts and 'ext' in opts:
return '.' + opts['ext']
else:
new_ext, prepend_target_format = Loader.get_new_file_ext(self.view)
if prepend_target_format:
new_ext = ".%s-%s" % (target_format.upper(), new_ext[1:])
return (new_ext or '.' + target_format)
if not target_format:
output.write("No target format specified, searching in file...")
# No information about a target format; ask for it
if not opts or 'target_format' not in opts:
output.write(" Could not detect target format.\n"
"Please select or define a target format...")
# Show overlay with all dumping options except for the current type
# Save stripped-down `items` for later
options, items = [], []
for itm in self.target_list:
# To not clash with function-local "target_format"
target_format_ = itm['kwargs']['target_format']
if target_format_ != source_format:
options.append(["Convert to: %s" % itm['name'],
file_path.stem + get_new_ext(target_format_)])
items.append(itm)
def on_select(index):
if index < 0 or index >= len(items):
# canceled or other magic
output.print("\n\nBuild canceled.")
return
target = items[index]
output.print(' %s\n' % target['name'])
kwargs.update(target['kwargs'])
kwargs.update(dict(source_format=source_format, _output=output))
self.run(**kwargs)
# Forward all params to the new command call
self.window.show_quick_panel(options, on_select)
return
target_format = opts['target_format']
result = self._revalidate_run(self,
output,
source_format,
target_format)
if result:
return result
output.print(' %s\n' % dumpers.get[target_format].name)
start_time = time.time()
# Okay, THIS is where the building really starts
# Note: loader or dumper errors are not caught
# in order to receive a nice traceback in the console
loader_ = Loader(self.window, self.view, output=output)
try:
data = loader_.load(**kwargs)
except Exception:
output.print("Unexpected error occurred while parsing, "
"please see the console for details.")
raise
# Determine new file name
new_file_path = file_path.with_suffix(get_new_ext(target_format))
new_dir = new_file_path.parent
valid_path = True
try:
os.makedirs(str(new_dir), exist_ok=True)
except OSError:
output.print("Could not create folder '%s'" % new_dir)
valid_path = False
if not data or not valid_path:
return
# Now dump to new file
dumper = dumpers.get[target_format](self.window, self.view, str(new_file_path),
output=output)
try:
dumper.dump(data, **kwargs)
except Exception:
output.print("Unexpected error occurred while dumping, "
"please see the console for details.")
raise
self.status("File conversion successful. (%s -> %s)"
% (source_format, target_format))
# Finish
output.print("[Finished in %.3fs]" % (time.time() - start_time))
# We need to save the text if calling "rearrange_yaml_syntax_def"
# because `get_output_panel` resets its contents.
output_text = get_text(output.view)
# Continue with potential further steps
if open_new_file or rearrange_yaml_syntax_def:
new_view = self.window.open_file(str(new_file_path))
if rearrange_yaml_syntax_def:
new_view.run_command('packagedev_rearrange_yaml_syntax_def',
{'save': True, '_output_text': output_text})
def status(self, msg, file_path=None):
self.window.status_message(msg)
print("[PackageDev] " + msg + (" (%s)" % file_path if file_path is not None else ""))