<commit_before># -*- coding: utf8 -*-

from constants import PREFIX_DECLARATION_TYPE_INTEGER
from constants import PREFIX_DECLARATION_TYPE_REAL
from constants import PREFIX_DECLARATION_TYPE_CHARACTER
from constants import PREFIX_DECLARATION_TYPE_LOGICAL
from constants import PREFIX_CONTEXT_TYPE_LOCAL
from constants import PREFIX_CONTEXT_TYPE_ARGUMENT
from constants import PREFIX_CONTEXT_TYPE_MODULE
from constants import PREFIX_CONTEXT_TYPE_OBJECT
from constants import PREFIX_CONTEXT_TYPE_GLOBAL
from extractors import *
from graph import Digraph
import re
import os

# ...
class Block(object):
    """

    """
    def __init__(self, keyword, \
                 TAG="", source=None, \
                 verbose=0, \
                 ancestor=None, \
                 color=None, \
                 prefix=None, \
                 prefix_lib=None, \
                 filename=None):

        self._keyword     = keyword
        self._TAG         = TAG
        self._text        = None
        self._source      = source
        self._variables   = None
        self._name        = TAG
        self._label       = None
        self._description = None
        self._verbose     = verbose
        self._dict_call   = {}
        self._dict_decl  = {}
        self._ancestor    = ancestor
        self._color       = color
        self._prefix      = prefix
        self._prefix_lib  = prefix_lib
        self._contains    = None
        self._is_valid    = False
        self._filename    = filename

        if color is None:
            self.set_color()

        if prefix is None:
            self._prefix = ""

        if prefix_lib is None:
            self._prefix_lib = ""

        self._re_block     = extract_blocks(self._keyword, TAG)
        self._re_signature = extract_signature()
        self._re_arguments = extract_arguments()

    @property
    def keyword(self):
        return self._keyword

    @property
    def filename(self):
        return self._filename

    @property
    def label(self):
        if self._label is None:
            self._label = self.name
        return self._label

    @property
    def prefix(self):
        return self._prefix

    @property
    def prefix_lib(self):
        return self._prefix_lib

    @property
    def name(self):
        return self._name

    @property
    def description(self):
        return self._description

    @property
    def text(self):
        return self._text

    @property
    def source(self):
        return self._source

    @property
    def variables(self):
        if self._variables is None:
            self.parse_variables()

        return self._variables

    @property
    def variables_arguments(self):
        return [var for var in self.variables if var.name in self._arguments]

    @property
    def variables_local(self):
        return [var for var in self.variables if var.name not in self._arguments]

    @property
    def verbose(self):
        return self._verbose

    @property
    def dict_call(self):
        return self._dict_call

    @property
    def dict_decl(self):
        return self._dict_decl

    @property
    def ancestor(self):
        return self._ancestor

    @property
    def color(self):
        if self._color is None:
            self.set_color()
        return self._color

    @property
    def contains(self):
        if self._contains is None:
            _re = extract_contains()
            self._contains = (len(_re.findall(self.text, re.I)) > 0)
        return self._contains

    @property
    def is_valid(self):
        return self._is_valid

    def set_source(self, source):
        self._source = source

    def get_code(self):
        if self.text is None:
            if self.source is not None:
                n = len(self._re_block.split(self.source))
                if n > 1:
                    self._is_valid = True

#                print "==========================="
#                for i in range(0,n):
#                    print ">>>>>", self._re_block.split(self.source)[i]
#                print n
#                print "==========================="

                if self.is_valid:
                    try:
                        self._text = self._re_block.findall(self.source, re.I)[0]
                    except:
                        print ("Cannot parse the source code")
    #                    print (self.source)
                        raise()
#                print ">>> text:", self.text
            else:
                print ("you must provide the source code before parsing")
                raise()

        return self.text

    def get_signature(self):
        if self.text is None:
            self.get_code()
        return self._re_signature.findall(self.text, re.I)[0]

    def get_arguments(self):
        if self.text is None:
            self.get_code()
        _text = self.get_signature()
        if self.verbose > 0:
            print (">>> signature:", _text)
        data = self._re_arguments.findall(_text, re.I)
        self._arguments = [b.rstrip() for b in data if len(b) > 0]
        return self._arguments

    def get_decl_call(self):
        """
        """
        self._dict_decl, self._dict_call = get_declarations_calls(self.text)
#        if self.keyword == "module":
#            print ">>>>>>>>>>> MODULE :", self.name
#        if self.keyword == "subroutine":<commit_msg>fix bugs when regex result is None. now Jorek is parsed with no errors<commit_after># -*- coding: utf8 -*-

from constants import PREFIX_DECLARATION_TYPE_INTEGER
from constants import PREFIX_DECLARATION_TYPE_REAL
from constants import PREFIX_DECLARATION_TYPE_CHARACTER
from constants import PREFIX_DECLARATION_TYPE_LOGICAL
from constants import PREFIX_CONTEXT_TYPE_LOCAL
from constants import PREFIX_CONTEXT_TYPE_ARGUMENT
from constants import PREFIX_CONTEXT_TYPE_MODULE
from constants import PREFIX_CONTEXT_TYPE_OBJECT
from constants import PREFIX_CONTEXT_TYPE_GLOBAL
from extractors import *
from graph import Digraph
import re
import os

# ...
class Block(object):
    """

    """
    def __init__(self, keyword, \
                 TAG="", source=None, \
                 verbose=0, \
                 ancestor=None, \
                 color=None, \
                 prefix=None, \
                 prefix_lib=None, \
                 filename=None):

        self._keyword     = keyword
        self._TAG         = TAG
        self._text        = None
        self._source      = source
        self._variables   = None
        self._name        = TAG
        self._label       = None
        self._description = None
        self._verbose     = verbose
        self._dict_call   = {}
        self._dict_decl  = {}
        self._ancestor    = ancestor
        self._color       = color
        self._prefix      = prefix
        self._prefix_lib  = prefix_lib
        self._contains    = None
        self._is_valid    = False
        self._filename    = filename

        if color is None:
            self.set_color()

        if prefix is None:
            self._prefix = ""

        if prefix_lib is None:
            self._prefix_lib = ""

        self._re_block     = extract_blocks(self._keyword, TAG)
        self._re_signature = extract_signature()
        self._re_arguments = extract_arguments()

    @property
    def keyword(self):
        return self._keyword

    @property
    def filename(self):
        return self._filename

    @property
    def label(self):
        if self._label is None:
            self._label = self.name
        return self._label

    @property
    def prefix(self):
        return self._prefix

    @property
    def prefix_lib(self):
        return self._prefix_lib

    @property
    def name(self):
        return self._name

    @property
    def description(self):
        return self._description

    @property
    def text(self):
        return self._text

    @property
    def source(self):
        return self._source

    @property
    def variables(self):
        if self._variables is None:
            self.parse_variables()

        return self._variables

    @property
    def variables_arguments(self):
        return [var for var in self.variables if var.name in self._arguments]

    @property
    def variables_local(self):
        return [var for var in self.variables if var.name not in self._arguments]

    @property
    def verbose(self):
        return self._verbose

    @property
    def dict_call(self):
        return self._dict_call

    @property
    def dict_decl(self):
        return self._dict_decl

    @property
    def ancestor(self):
        return self._ancestor

    @property
    def color(self):
        if self._color is None:
            self.set_color()
        return self._color

    @property
    def contains(self):
        if self._contains is None:
            _re = extract_contains()
            self._contains = (len(_re.findall(self.text, re.I)) > 0)
        return self._contains

    @property
    def is_valid(self):
        return self._is_valid

    def set_source(self, source):
        self._source = source

    def get_code(self):
        if self.text is None:
            if self.source is not None:
                n = len(self._re_block.split(self.source))
                if n > 1:
                    self._is_valid = True

#                print "==========================="
#                for i in range(0,n):
#                    print ">>>>>", self._re_block.split(self.source)[i]
#                print n
#                print "==========================="

                if self.is_valid:
                    try:
                        self._text = self._re_block.findall(self.source, re.I)[0]
                    except:
                        print ("Cannot parse the source code")
    #                    print (self.source)
                        raise()
#                print ">>> text:", self.text
            else:
                print ("you must provide the source code before parsing")
                raise()

        return self.text

    def get_signature(self):
        if self.text is None:
            self.get_code()

        try:
            t = self._re_signature.findall(self.text, re.I)[0]
        except:
            t = None

        return t

    def get_arguments(self):
        if self.text is None:
            self.get_code()
        _text = self.get_signature()
        if self.verbose > 0:
            print (">>> signature:", _text)
        try:
            data = self._re_arguments.findall(_text, re.I)
            self._arguments = [b.rstrip() for b in data if len(b) > 0]
        except:
            self._arguments = []
        return self._arguments

    def get_decl_call(self):
        """
        """
        self._dict_decl, self._dict_call = get_declarations_calls(self.text)
#        if self.keyword == "module":
#            print ">>>>>>>>>>> MODULE :", self.name
#        if self.keyword == "subroutine":
{'fraction_non_alphanumeric': 0.09440145653163405, 'fraction_numerical': 0.0016385980883022303, 'mean_word_length': 1.7052450135434623, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 55, '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': '3992228', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2915, 'n_words': 773}