Skip to content

Instantly share code, notes, and snippets.

@PartyWumpus
Last active June 13, 2024 19:24
Show Gist options
  • Save PartyWumpus/b1bc83b5b29b155e40742d0aa290f0db to your computer and use it in GitHub Desktop.
Save PartyWumpus/b1bc83b5b29b155e40742d0aa290f0db to your computer and use it in GitHub Desktop.
Diff valve's CSS
# get the old and new css folders from ~/.local/share/Steam/steamui/css/
# format them with cssunminifier or similar
# diff them with git diff --unified=0 --no-color --no-index old new > diff
# then
from collections import Counter
import cssselect
f = open("diff")
before = ""
name_map = {}
thevariable = None
def find_names(selector):
global thevariable
if type(selector) == list:
arr = []
for new in selector:
res = find_names(new)
if type(res) == list:
arr.extend(res)
else:
arr.append(res)
return arr
elif type(selector) == cssselect.Selector:
return find_names(selector.parsed_tree)
elif type(selector) == cssselect.parser.CombinedSelector or type(selector) == cssselect.parser.Negation:
arr = []
for new in [find_names(selector.selector), find_names(selector.subselector)]:
res = find_names(new)
if type(res) == list:
arr.extend(res)
else:
arr.append(res)
return arr
elif type(selector) == cssselect.parser.Class:
arr = []
res = find_names(selector.selector)
if type(res) == list:
arr.extend(res)
else:
arr.append(res)
arr.append(selector.class_name.strip())
return arr
elif type(selector) == cssselect.parser.Pseudo or type(selector) == cssselect.parser.Attrib or type(selector) == cssselect.parser.Function:
return find_names(selector.selector)
elif type(selector) == cssselect.parser.Hash:
return find_names(f"#{selector.id}")
elif type(selector) == cssselect.parser.Element:
if selector.element == "to" or selector.element == "from":
return None
res = selector.element
if res is not None:
return res.strip()
return None
elif type(selector) == str:
return selector.strip()
elif selector is None:
return None
else:
thevariable = selector
print("yet to be handled!", selector, type(selector))
import code
code.InteractiveConsole(locals=globals()).interact()
return [None]
def parserer(string):
string = string[1:-1]
if string.startswith("@keyframes"):
return [string[11:]]
if string.startswith("@-webkit-keyframes"):
return [string[19:]]
else:
try:
return [x for x in find_names(cssselect.parse(string)) if x is not None]
except cssselect.parser.SelectorSyntaxError as e:
return 'SKIP'
for line in f.readlines():
line = line.strip()
# skip line if it isn't a CSS class (ends in '{')
if line[-1] != "{":
continue
if line[0] == "-":
before = line
if line[0] == "+":
after = parserer(line)
beforee = parserer(before)
if before == 'SKIP' or after == 'SKIP':
continue
if len(beforee) != len(after):
continue
for index, class_before in enumerate(beforee):
if class_before not in name_map:
name_map[class_before.strip()] = []
name_map[class_before.strip()].append(after[index].strip())
def find_best_fit(arr) -> str:
if len(arr) == 1:
return arr[0]
count = Counter(name_map[x]).most_common()
if len(count) == 1 or count[0][1] > count[1][1]:
return count[0][0]
# there are only like 6 of these
print("failure for:", x, count)
return "FAIL"
final = {}
for x in name_map:
# there will be an array of several different classes.
# we want to pick the one that shows up the most, as it is probably the correct one
final[x] = find_best_fit(name_map[x])
final.pop(None, None)
for x in final:
y = final[x]
if not x[-5:] in y[:7] and x != y:
print(x, y, "is probably wrong")
f.close()
f = open("map.txt",'w')
f.write(str(final))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment