-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathload_resource.py
85 lines (63 loc) · 2.13 KB
/
load_resource.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
"""A module with tools loading ST resources."""
import codecs
import os
try:
from .st_helper import running_in_st
from . import st_helper
from . import path
except ValueError:
from st_helper import running_in_st
import st_helper
import path
if running_in_st():
import sublime # pylint: disable=import-error
else:
from . import sublime
def load_resource(file_path):
"""
Polyfill for ST3 sublime.load_resource function.
Arguments:
- file_path - a resource path.
Returns a string with the resource's content.
"""
if st_helper.is_st3():
return sublime.load_resource(file_path)
file_path = os.path.join(os.path.dirname(path.packages_path(path.ABSOLUTE)), file_path)
return _read_file(file_path)
def load_binary_resource(file_path):
"""
Polyfill for ST3 sublime.load_binary_resource function.
Arguments:
- file_path - a resource path.
Returns a byte string with the resource's content.
"""
if st_helper.is_st3():
return sublime.load_binary_resource(file_path)
file_path = os.path.join(os.path.dirname(path.packages_path(path.ABSOLUTE)), file_path)
return _read_binary_file(file_path)
def get_binary_resource_size(file_path):
"""
Polyfill for ST3 sublime.load_binary_resource function.
Arguments:
- file_path - a resource path.
Returns a byte string with the resource's content.
"""
if st_helper.is_st3():
return len(sublime.load_binary_resource(file_path))
file_path = os.path.join(os.path.dirname(path.packages_path(path.ABSOLUTE)), file_path)
return os.path.getsize(file_path)
def copy_resource(resource, destination_path):
"""
Copy resource to a file.
Arguments:
- resource - the resource to copy.
- destination_path - the path where to copy the resource.
"""
with open(destination_path, "wb") as file:
file.write(load_binary_resource(resource))
def _read_file(file_path):
with codecs.open(file_path, "r", "utf-8") as file:
return file.read()
def _read_binary_file(file_path):
with open(file_path, "rb") as file:
return file.read()