<commit_before># -*- coding: utf-8 -*-
#
# Manages user accounts stored in MoinMoin user directory.
# Author: <EMAIL>
#
from os import listdir, stat
from os.path import join, exists
import re

from passlib.context import CryptContext

from trac.core import Component, implements
from trac.config import Option, BoolOption

from acct_mgr.api import IPasswordStore


class MoinMoinPasswordStore(Component):
    implements(IPasswordStore)

    USER_FILE_RE = re.compile(r'^[0-9\.]+$')

    mm_user_dir = Option('moinmoinauth', 'mm_user_dir', doc='The MoinMoin users directory')
    disable_cache = BoolOption('moinmoinauth', 'disable_cache', default=False,
                               doc='Caches (as default) the user data from the MoinMoin user directory.')

    def __init__(self):
        if self.mm_user_dir is None:
            raise ValueError('No "mm_user_dir" configuration.')
        if not exists(self.mm_user_dir):
            raise ValueError('mm_user_dir "%s" doesn`t exist!' % self.mm_user_dir)

        self._crypt_context = CryptContext(
            # is the default value in the MoinMoin wiki
            schemes=['sha512_crypt', ]
        )

        self._user_cache = None
        self._user_cache_check = None

    def has_user(self, user):
        return self._read_users().has_key(user)

    def get_users(self):
        self.log.debug('getting user list...')
        usernames = []
        for name in self._read_users():
            usernames.append(name)

        return usernames

    def check_password(self, user, password):
        self.log.info('acct_mgr: checking password for %s ' % user)
        if self._is_user_ignored(user):
            return None

        users = self._read_users()
        for name in users:
            if name == user:
                pw_correct = self._crypt_context.verify(password, users[name])
                self.log.info('User %s found, pw check success: %s' % (name, pw_correct))
                return pw_correct

        return None

    def set_password(self, user, password, old_password=None):
        raise NotImplementedError

    def delete_user(self, user):
        raise NotImplementedError

    def _is_user_ignored(self, username):
        if username is None:
            return True
        # ignore the default build-in groups
        if username == 'authenticated' or username == 'anonymous':
            return True
        # our groups start with '__group__'
        return username.startswith('__group__')

    def _must_read_again(self):
        if self.disable_cache:
            return True

        if self._user_cache is None or self._user_cache_check is None:
            self._user_cache_check = self._get_dir_check_value()
            return True

        new_check = self._get_dir_check_value()
        if new_check == self._user_cache_check:
            return False
        self._user_cache_check = new_check
        return True

    def _get_dir_check_value(self):
        (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = stat(self.mm_user_dir)
        return '%s-%s-%s-%s' % (size, atime, mtime, ctime)

    def _read_users(self):
        if not self._must_read_again():
            return self._user_cache

        self.log.debug('read user data again')
        users = {}
        for file in listdir(self.mm_user_dir):
            if self.USER_FILE_RE.match(file) is None:
                continue
            (name, password) = self._get_name_and_password(file)
            if name is None:
                continue
            if self._is_user_ignored(name):
                continue
            users[name] = password
<commit_msg>handling usernames with unicode<commit_after># -*- coding: utf-8 -*-
#
# Manages user accounts stored in MoinMoin user directory.
# Author: <EMAIL>
#
from os import listdir, stat
from os.path import join, exists
import re

from passlib.context import CryptContext

from trac.core import Component, implements
from trac.config import Option, BoolOption

from acct_mgr.api import IPasswordStore


class MoinMoinPasswordStore(Component):
    implements(IPasswordStore)

    USER_FILE_RE = re.compile(r'^[0-9\.]+$')

    mm_user_dir = Option('moinmoinauth', 'mm_user_dir', doc='The MoinMoin users directory')
    disable_cache = BoolOption('moinmoinauth', 'disable_cache', default=False,
                               doc='Caches (as default) the user data from the MoinMoin user directory.')

    def __init__(self):
        if self.mm_user_dir is None:
            raise ValueError('No "mm_user_dir" configuration.')
        if not exists(self.mm_user_dir):
            raise ValueError('mm_user_dir "%s" doesn`t exist!' % self.mm_user_dir)

        self._crypt_context = CryptContext(
            # is the default value in the MoinMoin wiki
            schemes=['sha512_crypt', ]
        )

        self._user_cache = None
        self._user_cache_check = None

    def has_user(self, user):
        return self._read_users().has_key(user)

    def get_users(self):
        self.log.debug('getting user list...')
        usernames = []
        for name in self._read_users():
            usernames.append(name)

        return usernames

    def check_password(self, user, password):
        self.log.info('acct_mgr: checking password for %s ' % user)
        if self._is_user_ignored(user):
            return None

        users = self._read_users()
        for name in users:
            if name == user:
                pw_correct = self._crypt_context.verify(password, users[name])
                self.log.info('User %s found, pw check success: %s' % (name, pw_correct))
                return pw_correct

        return None

    def set_password(self, user, password, old_password=None):
        raise NotImplementedError

    def delete_user(self, user):
        raise NotImplementedError

    def _is_user_ignored(self, username):
        if username is None:
            return True
        # ignore the default build-in groups
        if username == 'authenticated' or username == 'anonymous':
            return True
        # our groups start with '__group__'
        return username.startswith('__group__')

    def _must_read_again(self):
        if self.disable_cache:
            return True

        if self._user_cache is None or self._user_cache_check is None:
            self._user_cache_check = self._get_dir_check_value()
            return True

        new_check = self._get_dir_check_value()
        if new_check == self._user_cache_check:
            return False
        self._user_cache_check = new_check
        return True

    def _get_dir_check_value(self):
        (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = stat(self.mm_user_dir)
        return '%s-%s-%s-%s' % (size, atime, mtime, ctime)

    def _read_users(self):
        if not self._must_read_again():
            return self._user_cache

        self.log.debug('read user data again')
        users = {}
        for file in listdir(self.mm_user_dir):
            if self.USER_FILE_RE.match(file) is None:
                continue
            (name, password) = self._get_name_and_password(file)
            if name is None:
                continue
            if self._is_user_ignored(name):
                continue
            name = name.decode('utf8')
            users[name] = password

{'fraction_non_alphanumeric': 0.09084727322181423, 'fraction_numerical': 0.001767985856113151, 'mean_word_length': 2.3096309630963097, 'pattern_counts': {'":': 0, '<': 5, '<?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': '344806', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2138, 'n_words': 573}