Skip to content

Instantly share code, notes, and snippets.

@halfak
Created January 4, 2017 16:00
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 halfak/57de7bc6709de574a6bfe7051370fea9 to your computer and use it in GitHub Desktop.
Save halfak/57de7bc6709de574a6bfe7051370fea9 to your computer and use it in GitHub Desktop.
import re
HOST_EXCEPTIONS = {
'mediawikiwiki': "mediawiki.org",
'wikidatawiki': "wikidata.org",
'commonswiki': "commons.wikimedia.org",
'metawiki': "meta.wikimedia.org",
'specieswiki': "species.wikimedia.org",
'wikisourcewiki': "wikisource.org",
'incubatorwiki': "incubator.wikimedia.org",
'labswiki': "wikitech.wikimedia.org",
'foundationwiki': "wikimediafoundation.org"
}
HOST_RULES = (
(re.compile(r'(.*)wiki$'),
lambda match: match.group(1) + ".wikipedia.org"),
(re.compile(r'(.*)wiktionary$'),
lambda match: match.group(1) + ".wiktionary.org"),
(re.compile(r'(.*)wikinews$'),
lambda match: match.group(1) + ".wikinews.org"),
(re.compile(r'(.*)wikiquote$'),
lambda match: match.group(1) + ".wikiquote.org"),
(re.compile(r'(.*)wikibooks$'),
lambda match: match.group(1) + ".wikibooks.org"),
(re.compile(r'(.*)wikiversity$'),
lambda match: match.group(1) + ".wikiversity.org"),
(re.compile(r'(.*)wikivoyage$'),
lambda match: match.group(1) + ".wikivoyage.org")
)
def host_from_dbname(dbname):
if dbname in HOST_EXCEPTIONS:
return HOST_EXCEPTIONS[dbname]
else:
for regex, apply_rule in HOST_RULES:
match = regex.match(dbname)
if match is not None:
return apply_rule(match)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment