Revision: 6324 Author: russblau Date: 2009-02-04 17:20:55 +0000 (Wed, 04 Feb 2009)
Log Message: ----------- fix mangled comments from last commit
Modified Paths: -------------- branches/rewrite/pywikibot/family.py
Modified: branches/rewrite/pywikibot/family.py =================================================================== --- branches/rewrite/pywikibot/family.py 2009-02-04 16:30:26 UTC (rev 6323) +++ branches/rewrite/pywikibot/family.py 2009-02-04 17:20:55 UTC (rev 6324) @@ -55,7 +55,9 @@ self.servergmtoffset = timedelta()
# letters that can follow a wikilink and are regarded as part of - # this link # This depends on the linktrail setting in LanguageXx.php and on # [[MediaWiki:Linktrail]]. + # this link + # This depends on the linktrail setting in LanguageXx.php and on + # [[MediaWiki:Linktrail]]. # Note: this is a regular expression. self.linktrails = { '_default': u'[a-z]*', @@ -546,13 +548,13 @@ 'arab' : [ 'ar', 'arz', 'ps', 'sd', 'ur', # languages using multiple scripts, including arabic - 'kk', 'ku', 'tt', 'ug' + 'kk', 'ku', 'tt', 'ug' ], # languages that use chinese symbols 'chinese': [ 'wuu', 'zh', 'zh-classical', 'zh-yue', 'gan', 'ii', # languages using multiple/mixed scripts, including chinese - 'ja', 'za' + 'ja', 'za' ], # languages that use the cyrillic alphabet 'cyril': [ @@ -607,7 +609,8 @@ # keys are the languages that can be linked to from the lang+ns, or # '_default'; values are a list of namespace numbers self.crossnamespace = {} - #### Examples : + ## + ## Examples : ## Allowing linking to pt' 102 NS from any other lang' 0 NS is # self.crossnamespace[0] = { # '_default': { 'pt': [102]} @@ -638,7 +641,122 @@ "ERROR: linktrail in language %(language_code)s unknown" % {'language_code': code})
-## def namespace(self, code, ns_number, fallback='_default', all=False):## if not self.isDefinedNS(ns_number):## raise KeyError(##'ERROR: Unknown namespace %(ns_number)d for %(language_code)s:%(ns_name)s'## % {'ns_number': ns_number,## 'language_code': code,## 'ns_name': self.name})## elif self.isNsI18N(ns_number, code):## v = self.namespaces[ns_number][code]## if type(v) is not list:## v = [v,]## if all and self.isNsI18N(ns_number, fallback):## v2 = self.namespaces[ns_number][fallback]## if type(v2) is list:## v.extend(v2)## else:## v.append(v2)## elif fallback and self.isNsI18N(ns_number, fallback):## v = self.namespaces[ns_number][fallback]## if type(v) is not list:## v = [v,]## else:## raise KeyError(##'ERROR: title for namespace %(ns_number)d in language %(language_code)s unknown'## % {'ns_number': ns_number,## 'language_code': code})## if all:## namespaces = list(set(v))## # Lowercase versions of namespaces## if code not in self.nocapitalize:## namespaces.extend([ns[0].lower() + ns[1:]## for ns in namespaces## if ns and ns[0].lower() != ns[0].upper()])## # Underscore versions of namespaces## namespaces.extend([ns.replace(' ', '_')## for ns in namespaces if ns and ' ' in ns])## return tuple(namespaces)## else:## return v[0]#### def isDefinedNS(self, ns_number):## """Return True if the namespace has been defined in this family."""## ## return self.namespaces.has_key(ns_number)#### def isNsI18N(self, ns_number, code):## """Return True if the namespace has been internationalized.#### (it has a custom entry for a given language)#### """## return self.namespaces[ns_number].has_key(code)#### def isDefinedNSLanguage(self, ns_number, code, fallback='_default'):## """Return True if the namespace has been defined in this family## for this language or its fallback.## """## if not self.isDefinedNS(ns_number):## return False## elif self.isNsI18N(ns_number, code):## return True## elif fallback and self.isNsI18N(ns_number, fallback):## return True## else:## return False#### def normalizeNamespace(self, code, value):## """Given a value, attempt to match it with all available namespaces,## with default and localized versions. Sites may have more than one## way to write the same namespace - choose the first one in the list.## If nothing can be normalized, return the original value.## """## for ns, items in self.namespaces.iteritems():## if items.has_key(code):## v = items[code]## elif items.has_key('_default'):## v = items['_default']## else:## continue## if type(v) is list:## if value in v: return v[0]## else:## if value == v: return v## try:## if value == self.namespace('_default', ns):## return self.namespace(code, ns)## except KeyError:## pass## return value#### def getNamespaceIndex(self, lang, namespace):## """Given a namespace, attempt to match it with all available## namespaces. Sites may have more than one way to write the same## namespace - choose the first one in the list. Returns namespace## index or None.## """## namespace = namespace.lower()## for n in self.namespaces.keys():## try:## nslist = self.namespaces[n][lang]## if type(nslist) != type([]):## nslist = [nslist]## for ns in nslist:## if ns.lower() == namespace:## return n## except (KeyError,AttributeError):## # The namespace has no localized name defined## pass## if lang != '_default':## # This is not a localized namespace. Try if it## # is a default (English) namespace.## return self.getNamespaceIndex('_default', namespace)## else:## # give up## return None## def category_redirects(self, code, fallback="_default"): +## def namespace(self, code, ns_number, fallback='_default', all=False): +## if not self.isDefinedNS(ns_number): +## raise KeyError( +##'ERROR: Unknown namespace %(ns_number)d for %(language_code)s:%(ns_name)s' +## % {'ns_number': ns_number, +## 'language_code': code, +## 'ns_name': self.name}) +## elif self.isNsI18N(ns_number, code): +## v = self.namespaces[ns_number][code] +## if type(v) is not list: +## v = [v,] +## if all and self.isNsI18N(ns_number, fallback): +## v2 = self.namespaces[ns_number][fallback] +## if type(v2) is list: +## v.extend(v2) +## else: +## v.append(v2) +## elif fallback and self.isNsI18N(ns_number, fallback): +## v = self.namespaces[ns_number][fallback] +## if type(v) is not list: +## v = [v,] +## else: +## raise KeyError( +##'ERROR: title for namespace %(ns_number)d in language %(language_code)s unknown' +## % {'ns_number': ns_number, +## 'language_code': code}) +## if all: +## namespaces = list(set(v)) +## # Lowercase versions of namespaces +## if code not in self.nocapitalize: +## namespaces.extend([ns[0].lower() + ns[1:] +## for ns in namespaces +## if ns and ns[0].lower() != ns[0].upper()]) +## # Underscore versions of namespaces +## namespaces.extend([ns.replace(' ', '_') +## for ns in namespaces if ns and ' ' in ns]) +## return tuple(namespaces) +## else: +## return v[0] +## +## def isDefinedNS(self, ns_number): +## """Return True if the namespace has been defined in this family.""" +## +## return self.namespaces.has_key(ns_number) +## +## def isNsI18N(self, ns_number, code): +## """Return True if the namespace has been internationalized. +## +## (it has a custom entry for a given language) +## +## """ +## return self.namespaces[ns_number].has_key(code) +## +## def isDefinedNSLanguage(self, ns_number, code, fallback='_default'): +## """Return True if the namespace has been defined in this family +## for this language or its fallback. +## """ +## if not self.isDefinedNS(ns_number): +## return False +## elif self.isNsI18N(ns_number, code): +## return True +## elif fallback and self.isNsI18N(ns_number, fallback): +## return True +## else: +## return False +## +## def normalizeNamespace(self, code, value): +## """Given a value, attempt to match it with all available namespaces, +## with default and localized versions. Sites may have more than one +## way to write the same namespace - choose the first one in the list. +## If nothing can be normalized, return the original value. +## """ +## for ns, items in self.namespaces.iteritems(): +## if items.has_key(code): +## v = items[code] +## elif items.has_key('_default'): +## v = items['_default'] +## else: +## continue +## if type(v) is list: +## if value in v: return v[0] +## else: +## if value == v: return v +## try: +## if value == self.namespace('_default', ns): +## return self.namespace(code, ns) +## except KeyError: +## pass +## return value +## +## def getNamespaceIndex(self, lang, namespace): +## """Given a namespace, attempt to match it with all available +## namespaces. Sites may have more than one way to write the same +## namespace - choose the first one in the list. Returns namespace +## index or None.## """ +## namespace = namespace.lower() +## for n in self.namespaces.keys(): +## try: +## nslist = self.namespaces[n][lang] +## if type(nslist) != type([]): +## nslist = [nslist] +## for ns in nslist: +## if ns.lower() == namespace: +## return n +## except (KeyError,AttributeError): +## # The namespace has no localized name defined +## pass +## if lang != '_default': +## # This is not a localized namespace. Try if it +## # is a default (English) namespace. +## return self.getNamespaceIndex('_default', namespace) +## else: +## # give up +## return None + + def category_redirects(self, code, fallback="_default"): if code in self.category_redirect_templates: return self.category_redirect_templates[code] elif fallback: