<commit_before>import json

import Config
import User
from Util import Util
from BoardManager import BoardManager
from UserManager import UserManager
from BCache import BCache
from errors import *

TYPE_BOARD = 1
TYPE_DIR = 2

DEFAULT_LIST_FAVBOARD_COUNT = 20

class FavBoard:
    def __init__(self, index = -1, name = '', father = -1):
        self._index = index
        self._type = type
        self._name = name
        self._father = father
        if (index == -1):
            self._type = TYPE_DIR
        else:
            self._type = TYPE_BOARD

    def IsBoard(self):
        return (self._type == TYPE_BOARD)

    def IsDir(self):
        return (self._type == TYPE_DIR)

    @staticmethod
    def GET(svc, session, params, action):
        if (session == None):
            raise Unauthorized("login first")
        if (action == "list"):
            FavBoardMgr.ListFavBoards(svc, session, params)
        else:
            raise WrongArgs("unknown action")

    @staticmethod
    def POST(svc, session, params, action):
        if (session == None):
            raise NoPerm("login first")
        raise WrongArgs("unknown action")

    def GetInfo(self, index, user):
        rfb = {}
        rfb['index'] = index
        rfb['father'] = self._father
        if (self.IsBoard()):
            rfb['type'] = 'board'
            board = BoardManager.GetBoardByIndex(self._index + 1)
            if (board != None):
                rfb['binfo'] = board.GetInfoWithUser(user)
        else:
            rfb['type'] = 'dir'
            rfb['name'] = self._name
        return rfb

    def GetInfoJSON(self, index, user):
        return json.dumps(self.GetInfo(index, user))
    
class FavBoardMgr:
    _favboards_set = {}
    @staticmethod
    def LoadFavBoards(userid):
        if (userid not in FavBoardMgr._favboards_set):
            fboards = FavBoardMgr.LoadNewFavBoards(userid)
            if (fboards == None):
                return None
            FavBoardMgr._favboards_set[userid] = fboards
        return FavBoardMgr._favboards_set[userid]

    @staticmethod
    def LoadNewFavBoards(userid):
        fboards = FavBoards(userid)
        fboards.LoadFavBoards()
        return fboards

    @staticmethod
    def ListFavBoards(svc, session, params):
        start = Util.GetInt(params, 'start')
        end = Util.GetInt(params, 'end')
        count = Util.GetInt(params, 'count')
        father = Util.GetInt(params, 'father', -1)

        fboards = FavBoardMgr.LoadFavBoards(session.GetUser().name)
        if (fboards == None):
            raise ServerError("failed to load fav boards")

        fboards.LoadFavBoards()

        start, end = Util.CheckRange(start, end, count, DEFAULT_LIST_FAVBOARD_COUNT, fboards._count)
        if ((start <= end) and (start >= 1) and (end <= fboards._count)):
            first = True
            result = '[\n'
            for index in range(0, fboards._count):
                fboard = fboards._favboards[index]
                if (fboard._father == father):
                    if (not first):
                        result += ',\n'
                    first = False
                    result += fboard.GetInfoJSON(index, session.GetUser())
            result += '\n]'
            svc.writedata(result)
            return
        else:
            raise WrongArgs('invalid arguments')

class FavBoards:
    def __init__(self, userid):
        self._userid = userid
        self._favboards = {}

    def DelFavBoard(self, index):
        if (index >= self._count):
            return self._count
        if (index < 0):
            return self._count

        fboard = self._favboards[index]
        if (fboard.IsDir()):
            j = 0
            while (j < self._count):
                if (self._favboards[j]._father == index):
                    DelFavBoard(j)
                    if (j < index):<commit_msg>minor fix<commit_after>import json

import Config
import User
from Util import Util
from BoardManager import BoardManager
from UserManager import UserManager
from BCache import BCache
from errors import *

TYPE_BOARD = 1
TYPE_DIR = 2

DEFAULT_LIST_FAVBOARD_COUNT = 20

class FavBoard:
    def __init__(self, index = -1, name = '', father = -1):
        self._index = index
        self._type = type
        self._name = name
        self._father = father
        if (index == -1):
            self._type = TYPE_DIR
        else:
            self._type = TYPE_BOARD

    def IsBoard(self):
        return (self._type == TYPE_BOARD)

    def IsDir(self):
        return (self._type == TYPE_DIR)

    @staticmethod
    def GET(svc, session, params, action):
        if (session == None):
            raise Unauthorized("login first")
        if (action == "list"):
            FavBoardMgr.ListFavBoards(svc, session, params)
        else:
            raise WrongArgs("unknown action")

    @staticmethod
    def POST(svc, session, params, action):
        if (session == None):
            raise NoPerm("login first")
        raise WrongArgs("unknown action")

    def GetInfo(self, index, user):
        rfb = {}
        rfb['index'] = index
        rfb['father'] = self._father
        if (self.IsBoard()):
            rfb['type'] = 'board'
            board = BoardManager.GetBoardByIndex(self._index + 1)
            if (board != None):
                rfb['binfo'] = board.GetInfoWithUser(user)
        else:
            rfb['type'] = 'dir'
            rfb['name'] = self._name
        return rfb

    def GetInfoJSON(self, index, user):
        return json.dumps(self.GetInfo(index, user))
    
class FavBoardMgr:
    _favboards_set = {}
    @staticmethod
    def LoadFavBoards(userid):
        if (userid not in FavBoardMgr._favboards_set):
            fboards = FavBoardMgr.LoadNewFavBoards(userid)
            if (fboards == None):
                return None
            FavBoardMgr._favboards_set[userid] = fboards
        return FavBoardMgr._favboards_set[userid]

    @staticmethod
    def LoadNewFavBoards(userid):
        fboards = FavBoards(userid)
        fboards.LoadFavBoards()
        return fboards

    @staticmethod
    def ListFavBoards(svc, session, params):
        start = Util.GetInt(params, 'start')
        end = Util.GetInt(params, 'end')
        count = Util.GetInt(params, 'count')
        father = Util.GetInt(params, 'father', -1)

        fboards = FavBoardMgr.LoadFavBoards(session.GetUser().name)
        if (fboards == None):
            raise ServerError("failed to load fav boards")

        fboards.LoadFavBoards()

        start, end = Util.CheckRange(start, end, count, DEFAULT_LIST_FAVBOARD_COUNT, fboards._count)
        if ((start <= end) and (start >= 1) and (end <= fboards._count)):
            first = True
            result = '[\n'
            for index in range(0, fboards._count):
                fboard = fboards._favboards[index]
                if (fboard._father == father):
                    if (not first):
                        result += ',\n'
                    first = False
                    result += fboard.GetInfoJSON(index, session.GetUser())
            result += '\n]'
            svc.writedata(result)
            return
        else:
            raise WrongArgs('invalid arguments')

class FavBoards:
    def __init__(self, userid):
        self._userid = userid
        self._favboards = {}

    def DelFavBoard(self, index):
        if (index >= self._count):
            return self._count
        if (index < 0):
            return self._count

        fboard = self._favboards[index]
        if (fboard.IsDir()):
            j = 0
            while (j < self._count):
                if (self._favboards[j]._father == index):
                    self.DelFavBoard(j)
                    if (j < index):
{'fraction_non_alphanumeric': 0.10779137153895686, 'fraction_numerical': 0.00334835801674179, 'mean_word_length': 1.8902121324897656, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '2163366', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2263, 'n_words': 566}