Advertisement
Guest User

CintaNotes TxtDir2XML Tool

a guest
Apr 30th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import argparse as ap
  2. import os, os.path
  3. from xml.dom.minidom import Document as XmlDocument
  4. import time
  5.  
  6. VERSION  = "1.0"
  7. GREETING = "CintaNotes TXT folder importer V%s.\n" % VERSION
  8.  
  9. def main():
  10.    print(GREETING)
  11.    argsParser = createArgsParser()
  12.    argsParser.print_help()
  13.    args = argsParser.parse_args()
  14.  
  15.    print('Processing..')
  16.  
  17.    count = convert(args.inputFolder, args.outputXML, args.encoding, args.recurse)
  18.    print('\n-> Converted %d file(s).' % count)
  19.  
  20.  
  21. def createArgsParser():
  22.    parser = ap.ArgumentParser(description = "Converts folders with text files to importable XML. Only files with .txt extension are read.")
  23.  
  24.    parser.add_argument("inputFolder",
  25.                         help = 'Folder with TXT files to be converted.',
  26.                         type = str)
  27.  
  28.    parser.add_argument("outputXML",
  29.                         help = 'Resulting XML file name',
  30.                         type = str)
  31.  
  32.    parser.add_argument("-e", "--encoding", dest="encoding", help = 'Encoding of TXT files: utf-8 (default) or utf-16', type = str, default = 'utf-8')
  33.    parser.add_argument("-r", "--recurse", dest="recurse", help = 'Recurse into subdirectories', action = 'store_true', default = False)
  34.  
  35.    return parser
  36.  
  37.  
  38. def convert(inputFolder, outputXML, encoding, recurse):
  39.    xml = XmlDocument()
  40.    root = xml.createElement('notebook')
  41.    root.setAttribute('version', '1600')
  42.    xml.appendChild(root)
  43.  
  44.    count = convertFiles(inputFolder, xml, encoding, recurse)
  45.    output = open(outputXML, 'w', encoding = 'utf-16le')
  46.    output.write(xml.toprettyxml())
  47.    return count
  48.  
  49.  
  50. def convertFiles(inputFolder, xmlDocument, encoding, recurse):
  51.    count = 0
  52.    for root, dirs, files in os.walk(inputFolder):
  53.       for file in files:
  54.          (name, ext) = os.path.splitext(file)
  55.          if ext == '.txt':
  56.             addTextFileToXml(os.path.join(root, file), xmlDocument, encoding)
  57.             count = count + 1
  58.       if not recurse:
  59.          break
  60.  
  61.    return count
  62.  
  63.  
  64. def addTextFileToXml(filePath, xmlDocument, encoding):
  65.    filePath = os.path.abspath(filePath)
  66.    file = open(filePath, encoding = encoding)
  67.    (_, filename) = os.path.split(filePath)
  68.    (title, _) = os.path.splitext(filename)
  69.    body = removeBOM(file.read())
  70.  
  71.    note = xmlDocument.createElement('note')
  72.    note.setAttribute('title', title)
  73.    note.setAttribute('link', 'file://' + filePath)
  74.    note.setAttribute('tags', '')
  75.    note.setAttribute('source', filePath)
  76.    note.setAttribute('created', getCreatedTime(filePath))
  77.    note.setAttribute('modified', getModifiedTime(filePath))
  78.  
  79.    noteBody = xmlDocument.createCDATASection(body)
  80.    note.appendChild(noteBody)
  81.  
  82.    xmlDocument.documentElement.appendChild(note)
  83.  
  84.  
  85. def getCreatedTime(filePath):
  86.    return timeToStr(os.path.getctime(filePath))
  87.  
  88. def getModifiedTime(filePath):
  89.    return timeToStr(os.path.getmtime(filePath))
  90.  
  91. def timeToStr(tm):
  92.    return time.strftime('%Y%m%dT%H%M%S', time.gmtime(tm))
  93.  
  94. def removeBOM(s):
  95.    if s.startswith('\uFEFF') or s.startswith('\uFFFE') or s.startswith('\uEFBBBF'):
  96.       return s[1:]
  97.    return s
  98.  
  99. if __name__ == '__main__':
  100.    main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement