<commit_before># parse.py - module for reading PSKC files
# coding: utf-8
#
# Copyright (C) 2014 <NAME>
#
# This library 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.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

from xml.etree import ElementTree
import base64


# the relevant XML namespaces for PSKC
namespaces = dict(
    # the XML namespace URI for version 1.0 of PSKC
    pskc='urn:ietf:params:xml:ns:keyprov:pskc',
    # the XML Signature namespace
    ds='http://www.w3.org/2000/09/xmldsig#',
    # the XML Encryption namespace
    xenc='http://www.w3.org/2001/04/xmlenc#',
    # the XML Encryption version 1.1 namespace
    xenc11='http://www.w3.org/2009/xmlenc11#',
    # the PKCS #5 namespace
    pkcs5='http://www.rsasecurity.com/rsalabs/pkcs/schemas/pkcs-5v2-0#',
)


def g_e_v(element, match):
    value = element.find(match, namespaces=namespaces)
    if value is not None:
        return value.text.strip()


class DataType(object):

    def __init__(self, element, t=str):
        self.element = element
        self.t = t

    @property
    def plain_value(self):
        if self.element is None:
            return
        plain_value = self.element.find('pskc:PlainValue', namespaces=namespaces)
        if plain_value is not None:
            return plain_value.text.strip()


class BinaryDataType(DataType):

    @property
    def value(self):
        plain_value = self.plain_value
        if plain_value:
            return base64.b64decode(plain_value)
        # TODO: else: see if EncryptedValue is present and decode


class IntegerDataType(DataType):

    @property
    def value(self):
        plain_value = self.plain_value
        if plain_value:
            return int(plain_value)
        # TODO: else: see if EncryptedValue is present and decode


class Key(object):

    def __init__(self, key_package):

        self.manufacturer = g_e_v(key_package, 'pskc:DeviceInfo/pskc:Manufacturer')
        self.serial = g_e_v(key_package, 'pskc:DeviceInfo/pskc:SerialNo')
        self.model = g_e_v(key_package, 'pskc:DeviceInfo/pskc:Model')
        self.issue_no = g_e_v(key_package, 'pskc:DeviceInfo/pskc:IssueNo')
        self.device_binding = g_e_v(key_package, 'pskc:DeviceInfo/pskc:DeviceBinding')
        self.start_date = g_e_v(key_package, 'pskc:DeviceInfo/pskc:StartDate')
        # TODO: handle <StartDate> as datetime
        self.expiry_date = g_e_v(key_package, 'pskc:DeviceInfo/pskc:ExpiryDate')
        # TODO: handle <ExpiryDate> as datetime
        self.device_userid = g_e_v(key_package, 'pskc:DeviceInfo/pskc:UserId')
<commit_msg>Support parsing date and integer values<commit_after># parse.py - module for reading PSKC files
# coding: utf-8
#
# Copyright (C) 2014 <NAME>
#
# This library 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.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

from xml.etree import ElementTree
import base64

import dateutil.parser


# the relevant XML namespaces for PSKC
namespaces = dict(
    # the XML namespace URI for version 1.0 of PSKC
    pskc='urn:ietf:params:xml:ns:keyprov:pskc',
    # the XML Signature namespace
    ds='http://www.w3.org/2000/09/xmldsig#',
    # the XML Encryption namespace
    xenc='http://www.w3.org/2001/04/xmlenc#',
    # the XML Encryption version 1.1 namespace
    xenc11='http://www.w3.org/2009/xmlenc11#',
    # the PKCS #5 namespace
    pkcs5='http://www.rsasecurity.com/rsalabs/pkcs/schemas/pkcs-5v2-0#',
)


def g_e_v(tree, match):
    """Get the text value of an element (or None)."""
    element = tree.find(match, namespaces=namespaces)
    if element is not None:
        return element.text.strip()


def g_e_i(tree, match):
    """Return an element value as an int (or None)."""
    element = tree.find(match, namespaces=namespaces)
    if element is not None:
        return int(element.text.strip())


def g_e_d(tree, match):
    """Return an element value as a datetime (or None)."""
    element = tree.find(match, namespaces=namespaces)
    if element is not None:
        return dateutil.parser.parse(element.text.strip())


class DataType(object):

    def __init__(self, element, t=str):
        self.element = element
        self.t = t

    @property
    def plain_value(self):
        if self.element is None:
            return
        plain_value = self.element.find('pskc:PlainValue', namespaces=namespaces)
        if plain_value is not None:
            return plain_value.text.strip()


class BinaryDataType(DataType):

    @property
    def value(self):
        plain_value = self.plain_value
        if plain_value:
            return base64.b64decode(plain_value)
        # TODO: else: see if EncryptedValue is present and decode


class IntegerDataType(DataType):

    @property
    def value(self):
        plain_value = self.plain_value
        if plain_value:
            return int(plain_value)
        # TODO: else: see if EncryptedValue is present and decode


class Key(object):

    def __init__(self, key_package):

        self.manufacturer = g_e_v(key_package, 'pskc:DeviceInfo/pskc:Manufacturer')
        self.serial = g_e_v(key_package, 'pskc:DeviceInfo/pskc:SerialNo')
        self.model = g_e_v(key_package, 'pskc:DeviceInfo/pskc:Model')
        self.issue_no = g_e_v(key_package, 'pskc:DeviceInfo/pskc:IssueNo')
        self.device_binding = g_e_v(key_package, 'pskc:DeviceInfo/pskc:DeviceBinding')
        self.start_date = g_e_d(key_package, 'pskc:DeviceInfo/pskc:StartDate')
        self.expiry_date = g_e_d(key_package, 'pskc:DeviceInfo/pskc:ExpiryDate')
        self.device_userid = g_e_v(key_package, 'pskc:DeviceInfo/pskc:UserId')

{'fraction_non_alphanumeric': 0.09688277476950095, 'fraction_numerical': 0.016391043465534903, 'mean_word_length': 3.358418367346939, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 10}, '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': '1012391', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2126, 'n_words': 640}