Orb/python/orb/filerenamer.py
changeset 4 468f4c8d3d5b
parent 2 932c358ece3e
equal deleted inserted replaced
3:d8fccb2cd802 4:468f4c8d3d5b
    11 # Description:
    11 # Description:
    12 #
    12 #
    13 import os.path
    13 import os.path
    14 import sys
    14 import sys
    15 import unittest
    15 import unittest
    16 import xml
       
    17 import stat
    16 import stat
    18 import logging
    17 import logging
    19 from cStringIO import StringIO
       
    20 from xml.etree import ElementTree as etree
       
    21 from lib import scan, main, XmlParser, StubXmlParser
    18 from lib import scan, main, XmlParser, StubXmlParser
    22 from optparse import OptionParser
    19 from optparse import OptionParser
    23 
    20 
    24 __version__ = '0.1'
    21 __version__ = '0.1'
    25 
    22 
    26 
    23 
    27 UNSAFE_CHARS = ("\n", "\t", ":", "?", ",", "=", ".", "\\", "/", "[", "]", "|", "<", ">", "+", ";", '"', "-")
    24 UNSAFE_CHARS = ("\n", "\t", ":", "?", ",", "=", ".", "\\", "/", "[", "]", "|", "<", ">", "+", ";", '"', "-")
       
    25 
       
    26 
       
    27 logger = logging.getLogger('orb.filerenamer')
    28 
    28 
    29 
    29 
    30 class FileRenamer(object):
    30 class FileRenamer(object):
    31     """
    31     """
    32     Given an xml file this class returns a MODE compatable filename
    32     Given an xml file this class returns a MODE compatable filename
    72     for filepath in scan(indir):
    72     for filepath in scan(indir):
    73         newfilename = os.path.join(os.path.dirname(filepath), fr.rename(filepath))
    73         newfilename = os.path.join(os.path.dirname(filepath), fr.rename(filepath))
    74         try:
    74         try:
    75             os.chmod(filepath, stat.S_IWRITE)
    75             os.chmod(filepath, stat.S_IWRITE)
    76         except Exception, e:
    76         except Exception, e:
    77             logging.error('Unable to make file \"%s\" writable, error was: %s' % (filepath, e))
    77             logger.error('Unable to make file \"%s\" writable, error was: %s' % (filepath, e))
    78             continue
    78             continue
    79         else:
    79         else:
    80             logging.debug("Renaming %s to %s" % (filepath, newfilename))
    80             logger.debug("Renaming %s to %s" % (filepath, newfilename))
    81             try:
    81             try:
    82                 os.rename(filepath, newfilename)
    82                 os.rename(filepath, newfilename)
    83             except Exception, e:
    83             except Exception, e:
    84                 logging.error('Unable to rename file \"%s\" to \"%s\", error was: %s' % (filepath, newfilename, e))
    84                 logger.error('Unable to rename file \"%s\" to \"%s\", error was: %s' % (filepath, newfilename, e))
    85 
    85 
    86 def main():        
    86 def main():        
    87     usage = "usage: %prog <Path to the XML content> <publishing_target>\n"
    87     usage = "usage: %prog <Path to the XML content> <publishing_target>\n"
    88     parser = OptionParser(usage, version='%prog ' + __version__)
    88     parser = OptionParser(usage, version='%prog ' + __version__)
    89     parser.add_option("-p", dest="publishing_target", type="choice", choices=["mode", "ditaot"], default="mode", 
    89     parser.add_option("-p", dest="publishing_target", type="choice", choices=["mode", "ditaot"], default="mode", 
    93     if len(args) < 1:
    93     if len(args) < 1:
    94         parser.print_help()
    94         parser.print_help()
    95         parser.error("Please supply the path to the XML content")
    95         parser.error("Please supply the path to the XML content")
    96         
    96         
    97     if options.loglevel:
    97     if options.loglevel:
    98         logging.basicConfig(level=options.loglevel)
    98         logger.basicConfig(level=options.loglevel)
    99     
    99     
   100     rename(args[0],options.publishing_target)
   100     rename(args[0],options.publishing_target)
   101 
   101 
   102 if __name__ == '__main__':
   102 if __name__ == '__main__':
   103     sys.exit(main())
   103     sys.exit(main())