Skip to content

Instantly share code, notes, and snippets.

@tabatkins
Created August 2, 2013 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabatkins/6136798 to your computer and use it in GitHub Desktop.
Save tabatkins/6136798 to your computer and use it in GitHub Desktop.
Small python script to find the relevant CSS spec folder from just a shortname and optionally a level, and a tiny shell script to actually change the directory accordingly.
function spec {
cd $(~/bin/findspec "$@")
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os.path import exists
from optparse import OptionParser, OptionValueError
def main():
optparser = OptionParser(usage="Usage: %prog [options] shortname [level]")
optparser.set_defaults(repo="either")
optparser.add_option("-g", "--git", dest="repo",
action="store_const", const="git",
help="Search the git spec repo.")
optparser.add_option("-c", "--csswg", dest="repo",
action="store_const", const="csswg",
help="Search the csswg spec repo.")
(options, posargs) = optparser.parse_args()
repo = options.repo
shortname = posargs[0] if len(posargs) >= 1 else None
level = posargs[1] if len(posargs) >= 2 else None
print findSpecPath(repo, shortname, level)
def findSpecPath(repo, shortname=None, level=None):
# The root directory the spec repos live under.
specDirectory = "/usr/local/google/home/tabatkins/specs"
# The largest-numbered spec in either repo.
# Used when no level is provided, and no unlevelled spec found.
largestSpecNumber = 4
if repo == "either":
subDirectories = ["csswg", "git"]
else:
subDirectories = [repo]
if shortname is None:
return specDirectory + "/" + subDirectories[0]
if level is not None:
# Look for levelled spec, falling back to unlevelled if not found.
specNamePatterns = [
"css-{0}-{1}",
"css-{0}",
"{0}{1}",
"{0}",
]
for subDirectory in subDirectories:
for pattern in specNamePatterns:
path = specDirectory + "/" + subDirectory + "/" + pattern.format(shortname, level)
if exists(path):
return path
else:
# Start by searching the unlevelled urls
specNamePatterns = [
"css-{0}",
"{0}",
]
for subDirectory in subDirectories:
for pattern in specNamePatterns:
path = specDirectory + "/" + subDirectory + "/" + pattern.format(shortname, level)
if exists(path):
return path
# Otherwise, search the leveled urls, finding the largest one.
specNamePatterns = [
"css{1}-{0}",
"css-{0}-{1}",
"{0}{1}",
]
for level in range(largestSpecNumber, 0, -1):
for subDirectory in subDirectories:
for pattern in specNamePatterns:
path = specDirectory + "/" + subDirectory + "/" + pattern.format(shortname, level)
if exists(path):
return path
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment