User:Inductiveload/Paginate list.py

From Wikisource
Jump to navigation Jump to search
#!/usr/bin/env python
#-*- coding:utf-8 -*-

# Takes a list of pages on a wiki, and creates a set of pages to list them
#  neatly, ordered by the first letter.

import pw_script_header
import wikipedia

def chunks(inList, n):
    # Yield successive n-sized chunks from inList.
    #(list(chunks(range(75), 10)))

    for i in range(0, len(inList), n):
        yield inList[i:i+n]


class PaginateList:

    def upload_page(self, pageName, contents):
        page = wikipedia.Page(site = self.site, title = pageName);
        page.put(contents, comment = self.comment, minorEdit = False)


    def generate_basepage(self):
        string = '{| class="wikitable" style="text-align:center; margin:auto;" width="50%"\n\
|-\n\
| [[BASE/A|A]] || [[BASE/B|B]] || [[BASE/C|C]] || [[BASE/D|D]]\n\
|-\n\
| [[BASE/E|E]] || [[BASE/F|F]] || [[BASE/G|G]] || [[BASE/H|H]]\n\
|-\n\
| [[BASE/I|I]] || [[BASE/J|J]] || [[BASE/K|K]] || [[BASE/L|L]]\n\
|-\n\
| [[BASE/M|M]] || [[BASE/N|N]] || [[BASE/O|O]] || [[BASE/P|P]]\n\
|-\n\
| [[BASE/Q|Q]] || [[BASE/R|R]] || [[BASE/S|S]] || [[BASE/T|T]]\n\
|-\n\
| [[BASE/U|U]] || [[BASE/V|V]] || [[BASE/W|W]] || [[BASE/X|X]]\n\
|-\n\
| [[BASE/Y|Y]] || [[BASE/Z|Z]] || colspan="2" | [[BASE/Other|Other]]\n\
|}\n'
        string.replace('BASE', self.opts.baseName)
        self.upload_page(self.opts.basename, string)
        
    def process_list(self):
        
        self.itemList = [ x.capitalize() for x in self.itemList ] #capitalise the first letter
        
        self.itemDict = {}
        
        for char in range(ord('A'), ord('Z')+1):
            self.itemDict[chr(char)] = [x for x in self.itemList if ord(x[0]) == char]
        
        self.itemDict['Other'] = [x for x in self.itemList if ord(x[0]) not in range(ord('A'), ord('Z')+1)]
        
        #if self.debug:
        #    print 'Generated dictionary:\n'
        #    print self.itemDict
            
            
            
    def create_pages(self):
        
        for char in self.itemDict:
            pageName = '%s/%s'%(self.baseName, char)
            
            if self.debug:
                print pageName
            
            #break list into chunks
            chunkList = list(chunks(self.itemDict[char], self.chunkSize))
            contents = ''
            
            #for each chunk, add a section
            for index, chunk in enumerate(chunkList):

                contents += '===%d-%d===\n'%(index*self.chunkSize + 1, (index+1)*self.chunkSize)
                
                for item in chunk:
                    contents += self.process_item(item)
                
                #if self.debug:
                #    print contents
            
            if not self.upload:
                self.upload_page(pageName, contents):
    
    
    #Create a string of the item
    # You may want to modify this function to do what you want, rather than just printing it.
    def process_item(self, item):
    
        string = '# %s\n' % item
        return string

    def __init__(self, site, itemList, baseName, comment, upload=True, debug=False):
        self.site = site
        self.comment = comment
        self.itemList = itemList
        self.baseName = baseName
        self.debug = debug
        self.upload = upload
        
        self.chunkSize = 50
        
        self.process_list()
        self.create_pages()