Skip to content

Instantly share code, notes, and snippets.

@ciembor
Created April 14, 2012 18:52
Show Gist options
  • Save ciembor/2386910 to your computer and use it in GitHub Desktop.
Save ciembor/2386910 to your computer and use it in GitHub Desktop.
Count scopes defined in Textmate color themes, than sort them and print
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import operator
try:
from collections import defaultdict
except Exception:
print("Failed to import defaultdict")
try:
from lxml import etree
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
themes_path = "./TextMate-Themes/"
textmate_extension = ".tmTheme"
occurrences = defaultdict(int)
filenames = []
# get filenames of Textmate themes
for oDirPaths, oDirNames, oFiles in os.walk(themes_path, True, None):
filenames.append(oFiles)
filenames = filenames[0]
# remove all not Textmate themes
for filename in filenames:
if (not filename.endswith(textmate_extension)):
filenames.remove(filename)
# check each Textmate theme for list of declared scopes
for filename in filenames:
try:
tree = etree.parse(themes_path + filename)
strings = tree.findall(".//string")
for element in strings:
if ("scope" == str(element.getprevious().text)):
for scope in str(element.text).split(","):
occurrences[scope.strip()] += 1
except Exception:
print("INVALID: " + filename)
sorted_occurrences = sorted(occurrences.items(), key=lambda x: x[0])
sorted_occurrences = sorted(sorted_occurrences, key=lambda x: x[1], reverse=True)
print("\noccurrences\tscopes\n")
for entry in sorted_occurrences:
print("\t" + str(entry[1]) + ":\t" + entry[0])
print("\n" + str(len(sorted_occurrences)) + " scopes in " + str(len(filenames)) + " Textmate themes.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment