forked from petervaro/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
147 lines (128 loc) · 4.94 KB
/
Copy pathconvert.py
File metadata and controls
147 lines (128 loc) · 4.94 KB
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
#!/usr/bin/python3
# -*- coding: utf8 -*-
import os
import plistlib
# Generate and print separator comments
def str_to_separator(string: str) -> None:
print('#{:-<78}#'.format('-- {} '.format(string.upper())))
# Convert integer dictionary keys into string literals
def int_to_str(obj: object) -> object:
try:
for key, value in obj.items():
obj[key] = int_to_str(value)
if isinstance(key, int):
obj[str(key)] = obj.pop(key)
except AttributeError:
if isinstance(obj, list):
for i, item in enumerate(obj):
obj[i] = int_to_str(item)
return obj
# Convert dictionary into property list file
def dict_to_plist(dictionary, name, path):
d = int_to_str(dictionary)
d['name'] = name
with open(path, 'w+b') as f:
plistlib.writePlist(d, f)
# Convert dictionary to TextMate Theme file
def dict_to_theme(dictionary: dict, name: str, path: str = None, local: bool = False) -> None:
dict_to_plist(dictionary, name, 'tmTheme', path, local)
print(name, 'style dictionary has been converted and placed.')
# Convert dictionary to TextMate Language file
def dict_to_lang(dictionary,
repo_fname='',
repo_dname='',
test_fname='',
test_dname='',
test_fpath=''):
"""
dictionary: a dict object, contains the syntax definition
repo_fname: if provided:
name of the file that will be placed insed CWD
repo_dname: if repo_fname provided:
definition name of syntax (appears in Sublime Text)
test_fname: if provided:
name of the file that will be placed into test_fpath
test_dname: if test_fname provided:
definition name of syntax (appears in Sublime Text)
test_fpath: if test_fname provided:
location where the test file will be placed
"""
# Place local copy
if repo_fname:
repo_dname = repo_dname if repo_dname else repo_fname
dict_to_plist(dictionary,
repo_dname,
os.path.join(os.pardir,
'{}.{}'.format(repo_fname, 'tmLanguage')))
print(repo_dname, 'syntax dictionary has been converted and placed.')
# Place Sublime User copy
if test_fname:
test_dname = test_dname if test_dname else test_fname
dict_to_plist(dictionary,
test_dname,
os.path.join(os.path.expanduser(test_fpath),
'{}.{}'.format(test_fname, 'tmLanguage')))
print(test_dname, 'syntax dictionary has been converted and placed.')
# Convert hexadecimal values to rgba
def hex_to_rgba(hexa: str) -> str:
return 'rgba({}, {}, {}, {:.2f})'.format(
int(hexa[1:3], 16),
int(hexa[3:5], 16),
int(hexa[5:7], 16),
int(hexa[7:], 16)/255
)
# Convert dictionary to css
def dict_to_css(dictionary, name, local):
# todo: decide if we need `word-wrap: break-word;` or not?
KEYS = {
'fontStyle' : 'font-style',
'foreground': 'color',
'background': 'background-color'
}
output = []
output.append('/*\n*{auth}\n*{name} syntax highlight theme\n*\n*{comm}\n*/\n'.format(
auth = dictionary['author'],
name = dictionary['name'],
comm = dictionary['comment']
)
)
output.append('body\n{\n\tbackground: #282828;\n}\n')
pre = dictionary['settings'][0]['settings']
output.append('pre, code\n{{\n{default}{dynamic}\n}}\n'.format(
default = (
'\tmargin: 0px;\n'
'\tpadding-left: 20px;\n'
'\tfont-size: 12.5px;\n'
"\tfont-family: 'Menlo', monospace;\n"
),
dynamic = '\n'.join(
[
'\tbackground: {};'.format(pre['background']),
'\tcolor: {};'.format(pre['foreground']),
]
)
)
)
output.append('::selection\n{{\n\tbackground: {};\n}}\n'.format(
hex_to_rgba(pre['selection']))
)
for item in dictionary['settings'][1:]:
try:
_name = item['scope']
prefs = []
for key, value in item['settings'].items():
try:
k = KEYS[key]
except KeyError:
k = key
if value.startswith('#') and len(value) == 9:
v = hex_to_rgba(value)
else:
v = value
prefs.append('\t{}: {};'.format(k, v))
output.append('pre .{}\n{{\n{}\n}}\n'.format(_name, '\n'.join(prefs)))
except KeyError:
pass
with open(os.path.join(os.pardir, local, '{}.css'.format(name)), 'w') as f:
f.write('\n'.join(output))
print(name, 'style dictionary has been converted and placed.')