def update_family(families): """Update family files.""" ws = wikistats.WikiStats() for family in families or families_list: pywikibot.output('\nChecking family %s:' % family) original = Family.load(family).languages_by_size for code in exceptions: if code in original: original.remove(code) obsolete = Family.load(family).obsolete new = [] table = ws.languages_by_size(family) for code in table: if not (code in obsolete or code in exceptions): new.append(code) # put the missing languages to the right place missing = original != new and set(original) - set(new) if missing: pywikibot.warning("['%s'] not listed at wikistats." % "', '".join(missing)) index = {} for code in missing: index[original.index(code)] = code i = len(index) - 1 for key in sorted(index.keys(), reverse=True): new.insert(key - i, index[key]) i -= 1 if original == new: pywikibot.output(u'The lists match!') else: pywikibot.output(u"The lists don't match, the new list is:") text = ' self.languages_by_size = [\n' line = ' ' * 11 for code in new: if len(line) + len(code) < 76: line += u" '%s'," % code else: text += '%s\n' % line line = ' ' * 11 line += u" '%s'," % code text += '%s\n' % line text += u' ]' pywikibot.output(text) family_file_name = 'pywikibot/families/%s_family.py' % family family_file = codecs.open(family_file_name, 'r', 'utf8') family_text = family_file.read() old = re.findall(r'(?msu)^ {8}self.languages_by_size.+?\]', family_text)[0] family_text = family_text.replace(old, text) family_file = codecs.open(family_file_name, 'w', 'utf8') family_file.write(family_text) family_file.close()
def update_family(families): """Update family files.""" ws = wikistats.WikiStats() for family in families or families_list: pywikibot.output('\nChecking family {}:'.format(family)) original = Family.load(family).languages_by_size for code in exceptions.get(family, []): if code in original: original.remove(code) obsolete = Family.load(family).obsolete new = [] table = ws.languages_by_size(family) for code in table: if not (code in obsolete or code in exceptions.get(family, [])): new.append(code) # put the missing languages to the right place missing = original != new and set(original) - set(new) if missing: pywikibot.warning("['{}'] not listed at wikistats.".format( "', '".join(missing))) index = {} for code in missing: index[original.index(code)] = code i = len(index) - 1 for key in sorted(index.keys(), reverse=True): new.insert(key - i, index[key]) i -= 1 if original == new: pywikibot.output('The lists match!') continue pywikibot.output("The lists don't match, the new list is:") text = ' languages_by_size = [\n' line = ' ' * 7 for code in new: if len(line) + len(code) >= 76: text += line + '\n' line = ' ' * 7 line += " '{}',".format(code) text += line + '\n' text += ' ]' pywikibot.output(text) family_file_name = 'pywikibot/families/{}_family.py'.format(family) with codecs.open(family_file_name, 'r', 'utf8') as family_file: family_text = family_file.read() family_text = re.sub(r'(?ms)^ {4}languages_by_size.+?\]', text, family_text, 1) with codecs.open(family_file_name, 'w', 'utf8') as family_file: family_file.write(family_text)