Menu

[r1]: / hexExtractor_old.py  Maximize  Restore  History

Download this file

133 lines (81 with data), 3.7 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
import os, pdb
css_lines = []
var_lines = []
constant_name = ''
var_list = []
var_lookup = []
def extract_hex(css_list):
counter = 1
in_rule = False;
in_hex = False;
hex_val = ''
hex_list = []
for css_text in css_list:
usable_css = css_text
for i in xrange(len(css_text)):
if css_text[i] == '{':
in_rule = True
if css_text[i] == '}':
in_rule = False
if css_text[i] == '#' and in_rule:
in_hex = True
hex_begin = i
if in_hex:
hex_val += css_text[i]
if (css_text[i] == ';' or css_text[i] == ' ') and in_hex:
in_hex = False
hex_end = i
if hex_val[-1] == ';': hex_val = hex_val[0:-1]
if hex_val.lower() not in hex_list:
hex_list.append(hex_val.lower())
print 'Found hex value in the following line:'
print '----->\t' + css_text
var_name = raw_input('What variable shall I replace the hex code with? ')
if var_name[0] is not '$':
var_name = '$' + var_name
css_temp = []
for j in usable_css:
css_temp.append(j)
css_temp_after = css_temp[hex_end:]
css_temp = css_temp[0:hex_begin]
css_temp.extend(var_name)
css_temp.extend(css_temp_after)
usable_css = ''.join(css_temp)
var_lookup.append((var_name, hex_val))
hex_val = ''
css_lines.append(usable_css)
return hex_list
if __name__ == '__main__':
path_to_core = raw_input('What is the path to core? ')
#path_to_core = 'C:\Documents and Settings\dworley\Desktop'
file_to_parse = raw_input('What is the path to the file you\'d like to parse? ')
#file_to_parse = 'C:\Documents and Settings\dworley\Desktop\TEST.css'
sss_save_dir = raw_input('Where shall we save the site-specific SSS output?')
#sss_save_dir = 'C:\Documents and Settings\dworley\Desktop'
constant_name = raw_input('What would you like to name the constants we\'ll be creating? ')
#constant_name = 'TEST'
constant_list = []
constant_dict = {}
if os.path.isdir(sss_save_dir) and os.path.isfile(file_to_parse):
filename = os.path.splitext(os.path.split(file_to_parse)[1])[0]
sss_output = open(os.path.join(sss_save_dir, filename + '.sss'), 'w')
sss = open(file_to_parse, 'r')
sss_text = sss.readlines()
sss.close()
hexcodes = extract_hex(sss_text)
hexcodes.sort()
for i in xrange(len(hexcodes)):
constant_declaration = '@constant ' + constant_name + '_' + str(i + 1) + ':\t' + hexcodes[i] + ';\n'
constant_list.append(constant_declaration)
constant_dict[hexcodes[i]] = constant_name + '_' + str(i + 1)
print constant_declaration[:-1]
for i in var_lookup:
print i[0] + ':\t' + constant_dict[i[1].lower()] + ';'
var_list.append(i[0] + ':\t' + constant_dict[i[1].lower()] + ';\n')
sss_output.writelines(constant_list)
sss_output.write('\n\n')
sss_output.writelines(var_list)
sss_output.close()
core_output = open(os.path.join(path_to_core, '_' + filename + '.sss'), 'w')
core_output.writelines(css_lines)
core_output.close()
MongoDB Logo MongoDB