<commit_before>#! /usr/bin/env python
#===============================================================================
# Copyright 2001-2007 gemalto
# Author: <NAME>, mailto:<EMAIL>
#
# This file is part of scard-python.
#
# scard-python is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# scard-python is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with scard-python; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#===============================================================================
# Sample for python PCSC wrapper module
#===============================================================================
# Get card ATR in first pcsc reader
#===============================================================================

from smartcard.scard import *

try:
    hresult, hcontext = SCardEstablishContext( SCARD_SCOPE_USER )
    if hresult!=0:
        raise  'Failed to establish context: ' + SCardGetErrorMessage(hresult)
    print 'Context established!'

    try:
        hresult, readers = SCardListReaders( hcontext, [] )
        if hresult!=0:
            raise  'Failed to list readers: ' + SCardGetErrorMessage(hresult)
        if len(readers)<1:
            raise  'No smart card readers'
        print 'PCSC Readers: ', readers

        for reader in readers:
            print 'Trying to retreive ATR of card in ', reader

            hresult, hcard, dwActiveProtocol = SCardConnect(
                hcontext, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 )
            if hresult!=0:
                print 'Unable to connect: ' + SCardGetErrorMessage(hresult)
            else:

                print 'Connected with active protocol ', dwActiveProtocol

                try:
                    hresult, reader, state, protocol, atr = SCardStatus( hcard )
                    if hresult!=0:
                        print 'failed to get status: ' + SCardGetErrorMessage(hresult)
                    print 'Reader: ', reader
                    print 'State: ', state
                    print 'Protocol: ', protocol
                    print 'ATR: ',
                    for i in xrange(len(atr)):
                        print "0x%.2X" % atr[i],
                    print ""

                finally:
                    hresult = SCardDisconnect( hcard, SCARD_UNPOWER_CARD )
                    if hresult!=0:
                        print 'Failed to disconnect: ' + SCardGetErrorMessage(hresult)
                    print 'Disconnected'


    finally:
        hresult = SCardReleaseContext( hcontext )<commit_msg>support both T=0 and T=1<commit_after>#! /usr/bin/env python
#===============================================================================
# Copyright 2001-2007 gemalto
# Author: <NAME>, mailto:<EMAIL>
#
# This file is part of scard-python.
#
# scard-python is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# scard-python is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with scard-python; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#===============================================================================
# Sample for python PCSC wrapper module
#===============================================================================
# Get card ATR in first pcsc reader
#===============================================================================

from smartcard.scard import *

try:
    hresult, hcontext = SCardEstablishContext( SCARD_SCOPE_USER )
    if hresult!=0:
        raise  'Failed to establish context: ' + SCardGetErrorMessage(hresult)
    print 'Context established!'

    try:
        hresult, readers = SCardListReaders( hcontext, [] )
        if hresult!=0:
            raise  'Failed to list readers: ' + SCardGetErrorMessage(hresult)
        if len(readers)<1:
            raise  'No smart card readers'
        print 'PCSC Readers: ', readers

        for reader in readers:
            print 'Trying to retreive ATR of card in ', reader

            hresult, hcard, dwActiveProtocol = SCardConnect(
                hcontext, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 )
            if hresult!=0:
                print 'Unable to connect: ' + SCardGetErrorMessage(hresult)
            else:

                print 'Connected with active protocol ', dwActiveProtocol

                try:
                    hresult, reader, state, protocol, atr = SCardStatus( hcard )
                    if hresult!=0:
                        print 'failed to get status: ' + SCardGetErrorMessage(hresult)
                    print 'Reader: ', reader
                    print 'State: ', state
                    print 'Protocol: ', protocol
                    print 'ATR: ',
                    for i in xrange(len(atr)):
                        print "0x%.2X" % atr[i],
                    print ""

                finally:
                    hresult = SCardDisconnect( hcard, SCARD_UNPOWER_CARD )
                    if hresult!=0:
                        print 'Failed to disconnect: ' + SCardGetErrorMessage(hresult)
                    print 'Disconnected'


    finally:
        hresult = SCardReleaseContext( hcontext )
{'fraction_non_alphanumeric': 0.16934046345811052, 'fraction_numerical': 0.010209042294603793, 'mean_word_length': 2.485036702428007, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6098200', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1480, 'n_words': 549}