Changeset 167

Show
Ignore:
Timestamp:
10/23/09 17:08:23 (9 months ago)
Author:
lrousseau
Message:

define a getinfo() function and call it if the script is called directly

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pykcs11/trunk/samples/getinfo.py

    r144 r167  
    11#!/usr/bin/env python 
    22 
    3 #   Copyright (C) 2006 Ludovic Rousseau (ludovic.rousseau@free.fr) 
     3#   Copyright (C) 2006-2009 Ludovic Rousseau (ludovic.rousseau@free.fr) 
    44# 
    55# This file is free software; you can redistribute it and/or modify it 
     
    3030    print "[-o][--opensession]" 
    3131 
    32 def colorize(text, arg): 
    33     print magenta + text + blue, arg, normal 
     32def getinfo(lib, pin = None, open_session = False, slot = None): 
     33    def colorize(text, arg): 
     34        print magenta + text + blue, arg, normal 
    3435 
    35 try: 
    36     opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho", ["pin=", "slot=", "lib=", "help", "opensession"]) 
    37 except getopt.GetoptError: 
    38     # print help information and exit: 
    39     usage() 
    40     sys.exit(2) 
     36    red = blue = magenta = normal = "" 
     37    if sys.stdout.isatty() and platform.system().lower() != 'windows': 
     38        red = "\x1b[01;31m" 
     39        blue = "\x1b[34m" 
     40        magenta = "\x1b[35m" 
     41        normal = "\x1b[0m" 
    4142 
    42 slot = None 
    43 lib = None 
    44 open_session = False 
    45 pin_available = False 
    46 for o, a in opts: 
    47     if o in ("-h", "--help"): 
    48         usage() 
    49         sys.exit() 
    50     if o in ("-p", "--pin"): 
    51         pin = a 
    52         pin_available = True 
    53         open_session = True 
    54     if o in ("-s", "--slot"): 
    55         slot = int(a) 
    56     if o in ("-c", "--lib"): 
    57         lib = a 
    58     if o in ("-o", "--opensession"): 
    59         open_session = True 
    60  
    61 red = blue = magenta = normal = "" 
    62 if sys.stdout.isatty() and platform.system().lower() != 'windows': 
    63     red = "\x1b[01;31m" 
    64     blue = "\x1b[34m" 
    65     magenta = "\x1b[35m" 
    66     normal = "\x1b[0m" 
    67  
    68 pkcs11 = PyKCS11.PyKCS11Lib() 
    69 try: 
     43    pkcs11 = PyKCS11.PyKCS11Lib() 
    7044    pkcs11.load(lib) 
    7145    info = pkcs11.getInfo() 
     
    9973 
    10074            if pin_available: 
     75                print " Using pin:", pin 
    10176                session.login(pin = pin) 
    10277 
     
    151126        session.closeSession() 
    152127 
    153 except PyKCS11.PyKCS11Error, e: 
    154     print "Error:", e 
     128if __name__ == '__main__': 
     129    try: 
     130        opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho", ["pin=", "slot=", "lib=", "help", "opensession"]) 
     131    except getopt.GetoptError: 
     132        # print help information and exit: 
     133        usage() 
     134        sys.exit(2) 
    155135 
     136    slot = None 
     137    lib = None 
     138    pin = None 
     139    open_session = False 
     140    pin_available = False 
     141    for o, a in opts: 
     142        if o in ("-h", "--help"): 
     143            usage() 
     144            sys.exit() 
     145        if o in ("-p", "--pin"): 
     146            pin = a 
     147            pin_available = True 
     148            open_session = True 
     149        if o in ("-s", "--slot"): 
     150            slot = int(a) 
     151        if o in ("-c", "--lib"): 
     152            lib = a 
     153        if o in ("-o", "--opensession"): 
     154            open_session = True 
     155 
     156    try: 
     157        getinfo(lib, pin = pin, open_session = open_session, slot = slot) 
     158    except PyKCS11.PyKCS11Error, e: 
     159        print "Error:", e 
     160 
     161 
(C) 2006 bit4id srl, for informations please contact info@bit4id.com
visitors since August 21, 2006