# -*- coding: utf-8 -*-
import re
from copy import deepcopy

import jsonpickle


class BaseReader(object):
    """
    Base class for dynamic readers
    """
    _default_conf = {}

    def __init__(self, conf):
        self.conf = deepcopy(self._default_conf)
        self.conf.update(conf)
        self.key_pattern = self.conf.get('pattern')
        self.auto_casting = self.conf.get('auto_casting')
        self.key_prefix = self.conf.get('prefix')

    def get(self, key):
        if not self._is_valid_key(key):
            return
        result = self._get(self._qualified_key(key))
        if self.auto_casting and (result is not None):
            result = jsonpickle.decode(result)
        return result

    def set(self, key, value):
        if not self._is_valid_key(key):
            return
        if self.auto_casting:
            value = jsonpickle.encode(value)
        self._set(self._qualified_key(key), value)

    def _is_valid_key(self, key):
        if not self.key_pattern:
            return True
        return bool(re.match(self.key_pattern, key))

    def _qualified_key(self, key):
        """
        Prepends the configured prefix to the key (if applicable).

        :param key: The unprefixed key.
        :return: The key with any configured prefix prepended.
        """
        pfx = self.key_prefix if self.key_prefix is not None else ''
        return '{}{}'.format(pfx, key)

[((15, 20, 15, 48), 'copy.deepcopy', 'deepcopy', ({(15, 29, 15, 47): 'self._default_conf'}, {}), '(self._default_conf)', False, 'from copy import deepcopy\n'), ((26, 21, 26, 46), 'jsonpickle.decode', 'jsonpickle.decode', ({(26, 39, 26, 45): 'result'}, {}), '(result)', False, 'import jsonpickle\n'), ((33, 20, 33, 44), 'jsonpickle.encode', 'jsonpickle.encode', ({(33, 38, 33, 43): 'value'}, {}), '(value)', False, 'import jsonpickle\n'), ((39, 20, 39, 51), 're.match', 're.match', ({(39, 29, 39, 45): 'self.key_pattern', (39, 47, 39, 50): 'key'}, {}), '(self.key_pattern, key)', False, 'import re\n')]