<commit_before>#!/usr/bin/python

from color import Color
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError

DEFAULT_FILENAME = None
DEFAULT_FILTER_LIST = []
DEFAULT_IGNORE_LIST = []
DEFAULT_NEW_LOG_ENTRY_REGEX = None
DEFAULT_COLOR = Color.WHITE

SEPARATOR_TOKEN = ','
SEPARATOR_TAG_COLOR = '='

COLOR_MAP = {
   'CRITICAL' : Color.RED,
   'ERROR'    : Color.RED,
   'INFO'     : Color.WHITE,
   'WARN'     : Color.YELLOW,
   'DEBUG'    : Color.PURPLE
}

class ConfigurationProperty(object):
   FILENAME = 'filename'
   FILTER_LIST = 'filter_list'
   IGNORE_LIST = 'ignore_list'
   NEW_LOG_ENTRY_REGEX = 'new_log_entry_regex'
   COLOR = 'color'
   DEFAULT_COLOR = 'default_color'

class Configuration(object):
   def __init__(self):
      self.filename = None
      self.filter_list = []
      self.ignore_list = []
      self.new_log_entry_regex = None
      self.color_map = {}
      self.default_color = None

      # TODO Remove when file configuration is implemented
      self.color_map = COLOR_MAP.copy()
      self.default_color = DEFAULT_COLOR

   def set(self, prop, value):
      if prop == ConfigurationProperty.FILENAME:
         self.filename = value
      elif prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list = self._tokenize(value)
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list = self._tokenize(value)
      elif prop == ConfigurationProperty.NEW_LOG_ENTRY_REGEX:
         self.new_log_entry_regex = value
      elif prop == ConfigurationProperty.DEFAULT_COLOR:
         self.default_color = Color.parse(value)

   def add(self, prop, value):
      if prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list.extend(self._tokenize(value))
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list.extend(self._tokenize(value))
      elif prop == ConfigurationProperty.COLOR:
         tag, color = self._parse_tag_color_pair(value)
         self.color_map[tag] = color

   def reset(self, prop):
      if prop == ConfigurationProperty.FILENAME:
         self.filename = ''
      elif prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list = []
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list = []
      elif prop == ConfigurationProperty.NEW_LOG_ENTRY_REGEX:
         self.new_log_entry_regex = None
      elif prop == ConfigurationProperty.COLOR:
         self.color_map.clear()
      elif prop == ConfigurationProperty.DEFAULT_COLOR:
         self.default_color = None

   @staticmethod
   def _parse_tag_color_pair(value):
      tokens = value.split(SEPARATOR_TAG_COLOR)

      if len(tokens) != 2:
         raise Exception('Wrong format of COLOR configuration!')

      tag = tokens[0].strip()
      color = Color.parse(tokens[1].strip())

      return (tag, color)

   @staticmethod
   def _tokenize(value):
      return [token.strip() for token in value.split(SEPARATOR_TOKEN)]

class ConfigurationManager(object):
   CONFIG_FILE = '.loggy.cfg'

   def __init__(self):
      self._configuration = None

   def load(self):
      fileConfigurator = FileConfigurator(self.CONFIG_FILE)
      self._configuration = fileConfigurator.get()<commit_msg>Added logic for loading the configuration file from the home directory.<commit_after>#!/usr/bin/python

from color import Color
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
from os.path import expanduser

DEFAULT_FILENAME = None
DEFAULT_FILTER_LIST = []
DEFAULT_IGNORE_LIST = []
DEFAULT_NEW_LOG_ENTRY_REGEX = None
DEFAULT_COLOR = Color.WHITE

SEPARATOR_TOKEN = ','
SEPARATOR_TAG_COLOR = '='

COLOR_MAP = {
   'CRITICAL' : Color.RED,
   'ERROR'    : Color.RED,
   'INFO'     : Color.WHITE,
   'WARN'     : Color.YELLOW,
   'DEBUG'    : Color.PURPLE
}

class ConfigurationProperty(object):
   FILENAME = 'filename'
   FILTER_LIST = 'filter_list'
   IGNORE_LIST = 'ignore_list'
   NEW_LOG_ENTRY_REGEX = 'new_log_entry_regex'
   COLOR = 'color'
   DEFAULT_COLOR = 'default_color'

class Configuration(object):
   def __init__(self):
      self.filename = None
      self.filter_list = []
      self.ignore_list = []
      self.new_log_entry_regex = None
      self.color_map = {}
      self.default_color = None

      # TODO Remove when file configuration is implemented
      self.color_map = COLOR_MAP.copy()
      self.default_color = DEFAULT_COLOR

   def set(self, prop, value):
      if prop == ConfigurationProperty.FILENAME:
         self.filename = value
      elif prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list = self._tokenize(value)
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list = self._tokenize(value)
      elif prop == ConfigurationProperty.NEW_LOG_ENTRY_REGEX:
         self.new_log_entry_regex = value
      elif prop == ConfigurationProperty.DEFAULT_COLOR:
         self.default_color = Color.parse(value)

   def add(self, prop, value):
      if prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list.extend(self._tokenize(value))
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list.extend(self._tokenize(value))
      elif prop == ConfigurationProperty.COLOR:
         tag, color = self._parse_tag_color_pair(value)
         self.color_map[tag] = color

   def reset(self, prop):
      if prop == ConfigurationProperty.FILENAME:
         self.filename = ''
      elif prop == ConfigurationProperty.FILTER_LIST:
         self.filter_list = []
      elif prop == ConfigurationProperty.IGNORE_LIST:
         self.ignore_list = []
      elif prop == ConfigurationProperty.NEW_LOG_ENTRY_REGEX:
         self.new_log_entry_regex = None
      elif prop == ConfigurationProperty.COLOR:
         self.color_map.clear()
      elif prop == ConfigurationProperty.DEFAULT_COLOR:
         self.default_color = None

   @staticmethod
   def _parse_tag_color_pair(value):
      tokens = value.split(SEPARATOR_TAG_COLOR)

      if len(tokens) != 2:
         raise Exception('Wrong format of COLOR configuration!')

      tag = tokens[0].strip()
      color = Color.parse(tokens[1].strip())

      return (tag, color)

   @staticmethod
   def _tokenize(value):
      return [token.strip() for token in value.split(SEPARATOR_TOKEN)]

class ConfigurationManager(object):
   CONFIG_FILE = '.loggy.cfg'

   def __init__(self):
      self._configuration = None

   def load(self):
      file_path = expanduser('~') + '/' + self.CONFIG_FILE
      fileConfigurator = FileConfigurator(file_path)
      self._configuration = fileConfigurator.get()
{'fraction_non_alphanumeric': 0.09700926066494611, 'fraction_numerical': 0.0009108850766661606, 'mean_word_length': 3.214971209213052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4767219', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1934, 'n_words': 400}