Attachment 'migrate_attachments.py'

Download

   1 #!/usr/bin/env python
   2 """
   3 This is based on on the 12_to_13mig1.py script.
   4 
   5 Here we take our htdocs/files/<wikiname> directory and tweak all of the
   6 directorynames in it.
   7 
   8 The old directory will be put into <wikiname>-old.
   9 """
  10 
  11 from_encoding = 'iso8859-1'
  12 #from_encoding = 'utf-8'
  13 
  14 to_encoding = 'utf-8'
  15 
  16 import os.path, sys, shutil, urllib
  17 
  18 sys.path.insert(0, '../../..')
  19 from MoinMoin import wikiutil
  20 
  21 from migutil import opj, listdir, copy_file, copy_dir
  22 
  23 # this is a copy of the wikiutil.unquoteFilename of moin 1.2.1
  24 
  25 def unquoteFilename12(filename, encoding):
  26     """
  27     Return decoded original filename when given an encoded filename.
  28     
  29     @param filename: encoded filename
  30     @rtype: string
  31     @return: decoded, original filename
  32     """
  33     str = urllib.unquote(filename.replace('_', '%'))
  34     try:
  35         newstr = str.decode(encoding)
  36     except UnicodeDecodeError: # try again with iso
  37         newstr = str.decode('iso-8859-1')
  38     return newstr
  39 
  40 def convert_string(str, enc_from, enc_to):
  41     try:
  42         newstr = str.decode(enc_from)
  43     except UnicodeDecodeError: # try again with iso
  44         newstr = str.decode('iso-8859-1')
  45     return newstr.encode(enc_to)
  46     
  47 def qf_convert_string(str, enc_from, enc_to):
  48     str = unquoteFilename12(str, enc_from)
  49     str = wikiutil.quoteWikinameFS(str, enc_to)
  50     return str
  51 
  52 def convert_pagedir(dir_from, dir_to, enc_from, enc_to):
  53     os.mkdir(dir_to)
  54     for dname_from in listdir(dir_from):
  55         dname_to = qf_convert_string(dname_from, enc_from, enc_to)
  56         print "%s -> %s" % (dname_from, dname_to)
  57         shutil.copytree(opj(dir_from, dname_from), opj(dir_to, dname_to), 1)
  58 
  59 
  60 if __name__ == '__main__':
  61 
  62     if len( sys.argv ) != 2:
  63 	print 'must provide the name of a subdirectory to munge names of'
  64 	sys.exit( 1 )
  65 
  66     origdir = sys.argv[1] + '-old'
  67     targdir = sys.argv[1]
  68     try:
  69 	os.rename( targdir, origdir )
  70     except OSError:
  71         print "You need to be in the directory where the original data lies"
  72         sys.exit(1)
  73 
  74     convert_pagedir( origdir, targdir, from_encoding, to_encoding)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-08-12 14:41:50, 2.0 KB) [[attachment:migrate_attachments.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.