示例#1
0
def parse_gamelog(path):
    """
    parse gamelog and return list of users

    :param problemId: id (as string) of problem to process
    :param path: path to directory with logs
    :return: list of users
    """

    users = []
    firstLine = True
    currentUser = None

    with open(path, "r") as f:
        for line in f:
            if firstLine:
                # First line contains the name of the problem, which can be arbitrary and can therefore interfere
                # with conditions below. For example the problem "960" interferes with if re.match("\d",line)
                firstLine = False
                continue
            if re.search("User", line):
                uid = int(line.rstrip().split()[-1])
                currentUser = User(uid)
                users.append(currentUser)

            if re.search("Game", line):
                currentUser.games.append(Game())

            if re.match("\d", line):
                currentUser.games[-1].submits.append(
                    Submit.parsedFromStr(line))

    return users
示例#2
0
class Serialise(object):
    """ convert a line into a fielded object """
    def __init__(self, data, (connection, eventhandler)):
        self._raw = data
        self.connection = connection
        self.eventhandler = eventhandler
        # this regular expression splits an IRC line up into four parts:
        # ORIGIN, TYPE, TARGET, MESSAGE
        regex = "^(?:\:([^\s]+)\s)?([A-Za-z0-9]+)\s(?:([^\s\:]+)\s)?(?:\:?(.*))?$"
        # a regular expression to match and dissect IRC protocol messages
        # this is around 60% faster than not using a RE
        p = re.compile(regex, re.VERBOSE)
        try:
            self.origin, self.type, self.target, self.message = (None, None,
                                                                 None, None)
            self._origin, self._type, self._target, self._message = p.match(
                data).groups()
            # turn each serialisable field into an object
            self.origin = User.User(
                self.connection,
                self._origin) if self._origin is not None else None
            self.type = Type.Type(
                self._type) if self._type is not None else None
            if self._target.startswith('#', 0, 1) is True:
                self.target = Channel.Channel(
                    self.connection,
                    self._target) if self._target is not None else None
            else:
                self.target = User.User(
                    self.connection,
                    self._target) if self._target is not None else None
            self.message = Message.Message(self._message)
        except (AttributeError):
            pass
        try:
            if logging.getLogger('ashiema').getEffectiveLevel(
            ) is logging.DEBUG and self.connection.debug is True:
                if self.type and self.message:
                    logging.getLogger('ashiema').debug(
                        "%s %s %s %s" % (str(self.origin), str(
                            self.type), str(self.target), str(self.message)))
        except:
            [
                logging.getLogger('ashiema').error(trace)
                for trace in traceback.format_exc(5).split('\n')
            ]
            pass
示例#3
0
    def add_user(self, username: str, password: str, role: str,
                 realname: str = None) -> dict:
        """This function adds a user to the system, using their username, password, role, and real name, and throwing that all into a dictionary.

        Keyword arguments:
        username -- unique identifier for a user
        password -- users password
        role -- users initial role
        """
        if self._persist.retrieve(User, username):
            return {"success": False, "message": "User already exists."}

        hashed_password = self._hash_password(password)
        self._persist.store(User(username, hashed_password, role, realname))
        return {"success": True}
    def retrieve(self, structure: T, structure_id: str) -> T:
        """Retrieve a structure.

        Keyword arguments:
        structure -- class retrieve stored
        structure_id -- the structure unique id
        """
        structure_name = structure.__name__
        self._ensure_structure_dict(structure_name)
        if structure_id in self._shelf[structure_name]:
            return self._shelf[structure_name][structure_id]


atexit.register(Persistence._cleanup)

if __name__ == "__main__":
    p = Persistence("persist_test")
    p._wipe()

    from structures import User

    user = User("test", "foo", "student")

    user_id = user.id

    p.store(user)

    same_user = p.retrieve(User, user_id)

    print(same_user is user)
        structure -- class being stored
        """
        structure_name = structure.__class__.__name__
        self._ensure_structure_dict(structure_name)
        self._shelf[structure_name][structure.id] = structure

    def retrieve(self, structure: T, structure_id: str) -> T:
        """This method is used to retrieve structures from the shelf. It takes in the structure itself, as well as the 
        name of the structure so that it can be determined from multiple structures in a module.

        Keyword arguments:
        structure -- class retrieve stored
        structure_id -- the structure unique id
        """
        structure_name = structure.__name__
        self._ensure_structure_dict(structure_name)
        if structure_id in self._shelf[structure_name]:
            return self._shelf[structure_name][structure_id]


atexit.register(Persistence._cleanup)

if __name__ == "__main__":
    p = Persistence("persist_test")
    p._wipe()

    from structures import User
    p.store(User("jackharrhy", "nice meme", "student"))

    print(p._shelf["User"].keys())
示例#6
0
 def test_persistence_store_user(self):
     new_user = User('foobar', 'baz', 'student')
     storage.store(new_user)
     same_user = storage.retrieve(User, new_user.id)
     self.assertIsNotNone(same_user)
     self.assertIsInstance(same_user, User)
        structure -- class being stored
        '''
        structure_name = structure.__class__.__name__
        self._ensure_structure_dict(structure_name)
        self._shelf[structure_name][structure.id] = structure

    def retrieve(self, structure: T, structure_id: str) -> T:
        '''This method is used to retrieve structures from the shelf. It takes in the structure itself, as well as the 
        name of the structure so that it can be determined from multiple structures in a module.

        Keyword arguments:
        structure -- class retrieve stored
        structure_id -- the structure unique id
        '''
        structure_name = structure.__name__
        self._ensure_structure_dict(structure_name)
        if structure_id in self._shelf[structure_name]:
            return self._shelf[structure_name][structure_id]


atexit.register(Persistence._cleanup)

if __name__ == '__main__':
    p = Persistence('persist_test')
    p._wipe()

    from structures import User
    p.store(User('jackharrhy', 'nice meme', 'student'))

    print(p._shelf['User'].keys())
    def do_POST(self):
        try:
            if not parser.validatePostRequest(self.path):
                return self.http_invalid_request()

            queryType = self.path.strip('/').split('/')[0]
            if queryType == 'RESET':
                self.server.sqldb.reset_tables()
                return self.http_ok()

            elif queryType == 'D':
                houseID = parser.getHouseID(self.path)
                roomID = parser.getRoomID(self.path)
                deviceType = parser.getDeviceType(self.path)
                if not sql.are_ints([houseID, roomID, deviceType]):
                    return self.http_invalid_request()
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)
                newDevice = Device(houseID, None, deviceType, data, roomID)
                deviceID = ''
                if roomID == 0:
                    deviceID = self.server.sqldb.insert_house_device(newDevice)
                else:
                    deviceID = self.server.sqldb.insert_room_device(newDevice)
                return self.http_ok(deviceID, 'Content-Type', 'text')

            elif queryType == 'R':
                houseID = parser.getHouseID(self.path)
                if not sql.are_ints([houseID]):
                    return self.http_invalid_request()
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)
                newRoom = Room(houseID, None, data, None)
                roomID = self.server.sqldb.insert_room(newRoom)
                return self.http_ok(roomID, 'Content-Type', 'text')

            elif queryType == 'H':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)
                newHouse = House(None, data, None, None)
                try:
                    houseID = self.server.sqldb.insert_house(newHouse)
                except:
                    self.send_response(333)
                    self.end_headers()
                return self.http_ok(houseID, 'Content-Type', 'text')

            elif queryType == 'U':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)
                username = parser.getUserName(self.path)
                password = parser.getUserPass(self.path)
                if username is None or password is None:
                    return self.http_invalid_request()

                userID = self.server.sqldb.get_user_id(username, password)
                if not userID is None:
                    return self.http_resource_not_found()

                newUser = User(None, username, password, None, data)
                userID = self.server.sqldb.insert_user(newUser)
                return self.http_ok(userID, 'Content-Type', 'text')

            elif queryType == 'UU':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)
                userID = parser.getUserID(self.path)
                if not sql.are_ints([userID]):
                    return self.http_invalid_request()
                stored = self.server.sqldb.get_user_data(userID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()
                self.server.sqldb.update_user(userID, data)
                return self.http_ok()

            elif queryType == 'UPU':
                userID = parser.getUserID(self.path)
                userpass = parser.getUserPass(self.path)
                if not sql.are_ints([userID]) or userpass is None:
                    return self.http_invalid_request()
                stored = self.server.sqldb.get_user_data(userID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()
                self.server.sqldb.update_user_pass(userID, userpass)
                return self.http_ok()

            elif queryType == 'UTU':
                userID = parser.getUserID(self.path)
                token = parser.getUserToken(self.path)
                if not sql.are_ints([userID]):
                    return self.http_invalid_request()
                stored = self.server.sqldb.get_user_data(userID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()
                self.server.sqldb.update_user_token(userID, usertoken)
                return self.http_ok()

            elif queryType == 'UH':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)

                houseID = parser.getHouseID(self.path)
                if not sql.are_ints([houseID]):
                    return self.http_invalid_request()

                # Ensure data is already there.
                stored = self.server.sqldb.get_house_data(houseID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()

                # Update the house data and send a 200.
                self.server.sqldb.update_house(houseID, data)
                return self.http_ok()

            elif queryType == 'UR':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)

                houseID = parser.getHouseID(self.path)
                roomID = parser.getRoomID(self.path)
                if not sql.are_ints([houseID, roomID]):
                    return self.http_invalid_request()

                # Ensure data is already there.
                stored = self.server.sqldb.get_room_data(houseID, roomID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()

                # Update the room data and send a 200.
                self.server.sqldb.update_room(houseID, roomID, data)
                return self.http_ok()

            elif queryType == 'UD':
                length = int(self.headers.getheader('content-length', 0))
                data = self.rfile.read(length)

                houseID = parser.getHouseID(self.path)
                roomID = parser.getRoomID(self.path)
                deviceID = parser.getDeviceID(self.path)
                if not sql.are_ints([houseID, roomID, deviceID]):
                    return self.http_invalid_request()

                # Ensure data is already there.
                stored = self.server.sqldb.get_device_data(
                    houseID, deviceID, roomID)
                if stored is None or stored == '':
                    return self.http_resource_not_found()

                # Update the device data and send a 200.
                self.server.sqldb.update_device(houseID, deviceID, data,
                                                roomID)
                return self.http_ok()
        except Exception, e:
            sys.stdout.write(str(e) + '\n')
            self.http_internal_error()