Matrix Science header

tools_tinycdb.py

Using the tinycdb index files.

#!/usr/bin/python
##############################################################################
# file: tools_tinycdb.py                                                     #
# Mascot Parser toolkit example code                                         #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2010 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: parser/examples/test_python/tools_tinycdb.py $
#     $Author: villek@matrixscience.com $
#       $Date: 2018-07-30 16:23:53 +0100 $
#   $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $
##############################################################################

import msparser
import sys
import re

def main() :
  # This next line just creates the object. It doesn't open a file
  # The 'source file' is (unusually!) this Python script (sys.argv[0]), 
  # meaning that if this file is changed, the index will be re-created.
  version = "1.1"
  sourceFile = sys.argv[0]
  cdbname = sys.argv[0] + '.cdb'
  cdb = msparser.ms_tinycdb(cdbname, version, sourceFile)

  if not cdb.isValid() :
    showErrors(cdb)
    return 1

  if not cdb.openIndexFile(1) :
    if cdb.isPossibleToCreate() :
      print("Creating file because: %s" % cdb.getLastErrorString())
      cdb.prepareToCreate()
      cdb.saveValueForKey("Hello", "world")
    
      # It is unusual to use the same key for 2 values, but it can be done:
      cdb.saveValueForKey("HELP", "me")
      cdb.saveValueForKey("HELP", "me too")
    
      # Example of saving a file offset. Can use a binary offset into the
      # file with the ms_tincycdb.saveFileOffsetForKey() function, but this
      # example uses saveIntForKey and a line offset. This facile example
      # uses this source file as the input!
      # and saves an offset to THIS line!
      try :
          fh = open(sys.argv[0], 'r')
      except IOError as err:
        errno, strerror = err.args
        print("Cannot open " + sys.argv[0] + " for reading: " + strerror)
        return 1
        sys.exit(1)
      line_nr = 1
      for line in fh :
        if re.search('\s*# and saves an.*', line) :
            break

        line_nr += 1

      fh.close();
      
      cdb.saveIntForKey("Line", line_nr)
      cdb.finishCreate()

      
  if cdb.isValid() :
    print("Retrieving value for Hello    : %s" % cdb.getValueFromKey("Hello"))
    print("Retrieving 1st value for HELP : %s" % cdb.getValueFromKey("HELP", 0))
    print("Retrieving 2nd value for HELP : %s" % cdb.getValueFromKey("HELP", 1))

    # Check saved line index  
    try :
        fh = open(sys.argv[0], 'r')
    except IOError as err:
        errno, strerror = err.args
        print("Cannot open " + sys.argv[0] + " for reading: " + strerror)
        return 1

    line_nr = cdb.getIntFromKey("Line")

    i = 1
    for line in fh :
        if i == line_nr : break
        i += 1

    print("Testing line index. Line " + str(line_nr + 1) + " : " + line)
    fh.close()
  else :
    showErrors(cdb)


def showErrors(obj) :
    err = obj.getErrorHandler()

    for i in range(1, 1 + err.getNumberOfErrors()) :
        print("Error number: %d (%d times): %s" % (err.getErrorNumber(i), err.getErrorRepeats(i) + 1, err.getErrorString(i))) 
            
    print(" ")
    obj.clearAllErrors()

if __name__ == "__main__" :
    sys.exit(main())


Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29