<commit_before>__author__ = 'gremorian'

import sys
import time
from datetime import date, datetime
from .types import OrientRecordLink, OrientRecord, OrientBinaryObject


class ORecordDecoder(object):

    def __init__(self, content):
        self.className = None
        self.data = {}

        if not isinstance(content, str):
            content = content.decode()
        self.__decode( content )

    def __decode( self, content ):
        """
         Deserialize a record.
           :param content str The input to un-serialize.
           :return: dict|None The un-serialized document, or None if the input is empty.
        """
        if not content:
            return

        content = content.strip()

        chunk = self.parse_first_key( content )

        if chunk[2]:
            # this is actually a class name.
            self.className = chunk[0]
            content = chunk[1]
            chunk = self.parse_key(content)
            key = chunk[0]
            content = chunk[1]
        else:
            key = chunk[0]
            content = chunk[1]

        if not key and not content:
            return

        chunk = self.parse_value(content)
        value = chunk[0]
        content = chunk[1]

        self.data[key] = value

        while len(content) != 0:
            if content[0] == ',':
                content = content[1:]
            else:
                break

            chunk = self.parse_key(content)
            key = chunk[0]
            content = chunk[1]
            if len(content) > 0:
                chunk = self.parse_value(content)
                value = chunk[0]
                content = chunk[1]
                self.data[key] = value
            else:
                self.data[key] = None

    def parse_first_key(self, content):
        """
         Consume the first field key, which could be a class name.
              :param content str The input to consume
              :return: list The collected string and any remaining content,
               followed by a boolean indicating whether this is a class name.
        """
        length = len(content)
        collected = ''
        is_class_name = False
        if content[0] == '"':
            result = self.parse_string(content[1:])
            return [result[0], result[1][1:]]

        i = 0
        for i in range(0, length):
            c = content[i]
            if c == '@':
                is_class_name = True
                break
            elif c == ':':
                break
            else:
                collected += c

        return [collected, content[( i + 1 ):], is_class_name]

    def parse_key( self, content ):
        """
          Consume a field key, which may or may not be quoted.
            :param content str The input to consume
            :return: dict The collected string and any remaining content.
        """
        length = len(content)
        collected = ''
        if content[ 0 ] == '"':
            result = self.parse_string( content[1:] )
            return [ result[ 0 ], result[1][1:] ]

        i = 0
        for i in range(0, length):
            c = content[i]
            if c == ':':
                break
            else:
                collected += c

        return [ collected, content[( i + 1 ):] ]

    def parse_value( self, content ):
        """
          Consume a field value.
            :param: content str The input to consume
            :return: list The collected value and any remaining content.
        """
        c = ''
        content = content.lstrip( " " )
        try:
            c = content[ 0 ]  # string index out of range 0
        except IndexError:
            pass
<commit_msg>This fixes selecting nodes without keys.<commit_after>__author__ = 'gremorian'

import sys
import time
from datetime import date, datetime
from .types import OrientRecordLink, OrientRecord, OrientBinaryObject


class ORecordDecoder(object):

    def __init__(self, content):
        self.className = None
        self.data = {}

        if not isinstance(content, str):
            content = content.decode()
        self.__decode( content )

    def __decode( self, content ):
        """
         Deserialize a record.
           :param content str The input to un-serialize.
           :return: dict|None The un-serialized document, or None if the input is empty.
        """
        if not content:
            return

        content = content.strip()

        chunk = self.parse_first_key( content )

        if chunk[2]:
            # this is actually a class name.
            self.className = chunk[0]
            content = chunk[1]
            chunk = self.parse_key(content)
            key = chunk[0]
            content = chunk[1]
        else:
            key = chunk[0]
            content = chunk[1]

        if not key and not content:
            return

        chunk = self.parse_value(content)
        value = chunk[0]
        content = chunk[1]

        self.data[key] = value

        while len(content) != 0:
            if content[0] == ',':
                content = content[1:]
            else:
                break

            chunk = self.parse_key(content)
            key = chunk[0]
            content = chunk[1]
            if len(content) > 0:
                chunk = self.parse_value(content)
                value = chunk[0]
                content = chunk[1]
                self.data[key] = value
            else:
                self.data[key] = None

    def parse_first_key(self, content):
        """
         Consume the first field key, which could be a class name.
              :param content str The input to consume
              :return: list The collected string and any remaining content,
               followed by a boolean indicating whether this is a class name.
        """
        length = len(content)
        collected = ''
        is_class_name = False
        if content[0] == '"':
            result = self.parse_string(content[1:])
            return [result[0], result[1][1:]]

        i = 0
        for i in range(0, length):
            c = content[i]
            if c == '@':
                is_class_name = True
                break
            elif c == ':':
                break
            else:
                collected += c

        return [collected, content[( i + 1 ):], is_class_name]

    def parse_key( self, content ):
        """
          Consume a field key, which may or may not be quoted.
            :param content str The input to consume
            :return: dict The collected string and any remaining content.
        """
        length = len(content)
        if length == 0:
            return [None, None]
        collected = ''
        if content[ 0 ] == '"':
            result = self.parse_string( content[1:] )
            return [ result[ 0 ], result[1][1:] ]

        i = 0
        for i in range(0, length):
            c = content[i]
            if c == ':':
                break
            else:
                collected += c

        return [ collected, content[( i + 1 ):] ]

    def parse_value( self, content ):
        """
          Consume a field value.
            :param: content str The input to consume
            :return: list The collected value and any remaining content.
        """
        c = ''
        content = content.lstrip( " " )
        try:
            c = content[ 0 ]  # string index out of range 0
        except IndexError:
            pass

{'fraction_non_alphanumeric': 0.09319427730980077, 'fraction_numerical': 0.009493247760395776, 'mean_word_length': 1.4144609425435766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5363668', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1902, 'n_words': 647}