Merge with main

This commit is contained in:
Griatch 2023-03-24 15:23:51 +01:00
commit db6d2bf4df
22 changed files with 489 additions and 217 deletions

View file

@ -4,6 +4,10 @@
- Feature: Add support for saving `deque` with `maxlen` to Attributes (before
`maxlen` was ignored).
- Fix: More unit tests for scripts (Storsorken)
- Docs: Made separate doc pages for Exits, Characters and Rooms. Expanded on how
to change the description of an in-game object with templating.
- Docs: Fixed a multitude of doc issues.
## Evennia 1.2.1

View file

@ -4,6 +4,9 @@
- Feature: Add support for saving `deque` with `maxlen` to Attributes (before
`maxlen` was ignored).
- Fix: More unit tests for scripts (Storsorken)
- Docs: Made separate doc pages for Exits, Characters and Rooms. Expanded on how
to change the description of an in-game object with templating.
## Evennia 1.2.1

View file

@ -0,0 +1,29 @@
# Characters
**Inheritance Tree:
```
┌─────────────┐
│DefaultObject│
└─────▲───────┘
┌─────┴──────────┐
│DefaultCharacter│
└─────▲──────────┘
│ ┌────────────┐
│ ┌─────────►ObjectParent│
│ │ └────────────┘
┌───┴─┴───┐
│Character│
└─────────┘
```
_Characters_ is an in-game [Object](./Objects.md) commonly used to represent the player's in-game avatar. The empty `Character` class is found in `mygame/typeclasses/characters.py`. It inherits from [DefaultCharacter](evennia.objects.objects.DefaultCharacter) and the (by default empty) `ObjectParent` class (used if wanting to add share properties between all in-game Objects).
When a new [Account](./Accounts.md) logs in to Evennia for the first time, a new `Character` object is created and the [Account](./Accounts.md) will be set to _puppet_ it. By default this first Character will get the same name as the Account (but Evennia supports [alternative connection-styles](../Concepts/Connection-Styles.md) if so desired).
A `Character` object will usually have a [Default Commandset](./Command-Sets.md) set on itself at creation, or the account will not be able to issue any in-game commands!
If you want to change the default character created by the default commands, you can change it in settings:
BASE_CHARACTER_TYPECLASS = "typeclasses.characters.Character"
This deafult points at the empty class in `mygame/typeclasses/characters.py` , ready for you to modify as you please.

View file

@ -13,6 +13,9 @@ Sessions.md
Typeclasses.md
Accounts.md
Objects.md
Characters.md
Rooms.md
Exits.md
Scripts.md
Channels.md
Msg.md

View file

@ -645,9 +645,6 @@ def _gamble(caller, raw_string, **kwargs):
else:
return "win"
def _try_again(caller, raw_string, **kwargs):
return None # reruns the same node
template_string = """
## node start
@ -660,8 +657,8 @@ he says.
## options
1. Roll the dice -> gamble()
2. Try to talk yourself out of rolling -> ask_again()
1: Roll the dice -> gamble()
2: Try to talk yourself out of rolling -> start
## node win
@ -687,7 +684,9 @@ says Death.
"""
goto_callables = {"gamble": _gamble, "ask_again": _ask_again}
# map the in-template callable-name to real python code
goto_callables = {"gamble": _gamble}
# this starts the evmenu for the caller
evmenu.template2menu(caller, template_string, goto_callables)
```

View file

@ -0,0 +1,55 @@
# Exits
**Inheritance Tree:**
```
┌─────────────┐
│DefaultObject│
└─────▲───────┘
┌─────┴─────┐
│DefaultExit│
└─────▲─────┘
│ ┌────────────┐
│ ┌─────►ObjectParent│
│ │ └────────────┘
┌─┴─┴┐
│Exit│
└────┘
```
*Exits* are in-game [Objects](./Objects.md) connecting other objects (usually [Rooms](./Rooms.md)) together.
An object named `north` or `in` might be exits, as well as `door`, `portal` or `jump out the window`.
An exit has two things that separate them from other objects.
1. Their `.destination` property is set and points to a valid target location. This fact makes it easy and fast to locate exits in the database.
2. Exits define a special [Transit Command](./Commands.md) on themselves when they are created. This command is named the same as the exit object and will, when called, handle the practicalities of moving the character to the Exits's `.destination` - this allows you to just enter the name of the exit on its own to move around, just as you would expect.
The default exit functionality is all defined on the [DefaultExit](DefaultExit) typeclass. You could in principle completely change how exits work in your game by overriding this - it's not recommended though, unless you really know what you are doing).
Exits are [locked](./Locks.md) using an `access_type` called *traverse* and also make use of a few hook methods for giving feedback if the traversal fails. See `evennia.DefaultExit` for more info.
Exits are normally overridden on a case-by-case basis, but if you want to change the default exit createad by rooms like `dig` , `tunnel` or `open` you can change it in settings:
BASE_EXIT_TYPECLASS = "typeclasses.exits.Exit"
In `mygame/typeclasses/exits.py` there is an empty `Exit` class for you to modify.
### Exit details
The process of traversing an exit is as follows:
1. The traversing `obj` sends a command that matches the Exit-command name on the Exit object. The [cmdhandler](./Commands.md) detects this and triggers the command defined on the Exit. Traversal always involves the "source" (the current location) and the `destination` (this is stored on the Exit object).
1. The Exit command checks the `traverse` lock on the Exit object
1. The Exit command triggers `at_traverse(obj, destination)` on the Exit object.
1. In `at_traverse`, `object.move_to(destination)` is triggered. This triggers the following hooks, in order:
1. `obj.at_pre_move(destination)` - if this returns False, move is aborted.
1. `origin.at_pre_leave(obj, destination)`
1. `obj.announce_move_from(destination)`
1. Move is performed by changing `obj.location` from source location to `destination`.
1. `obj.announce_move_to(source)`
1. `destination.at_object_receive(obj, source)`
1. `obj.at_post_move(source)`
1. On the Exit object, `at_post_traverse(obj, source)` is triggered.
If the move fails for whatever reason, the Exit will look for an Attribute `err_traverse` on itself and display this as an error message. If this is not found, the Exit will instead call `at_failed_traverse(obj)` on itself.

View file

@ -1,5 +1,6 @@
# Objects
**Message-path:**
```
┌──────┐ │ ┌───────┐ ┌───────┐ ┌──────┐
│Client├─┼──►│Session├───►│Account├──►│Object│
@ -7,51 +8,63 @@
^
```
All in-game objects in Evennia, be it characters, chairs, monsters, rooms or hand grenades are
represented by an Evennia *Object*. Objects form the core of Evennia and is probably what you'll
spend most time working with. Objects are [Typeclassed](./Typeclasses.md) entities.
All in-game objects in Evennia, be it characters, chairs, monsters, rooms or hand grenades are jointly referred to as an Evennia *Object*. An Object is generally something you can look and interact with in the game world. When a message travels from the client, the Object-level is the last stop.
An Evennia Object is, by definition, a Python class that includes [evennia.objects.objects.DefaultObject](evennia.objects.objects.DefaultObject) among its parents. Evennia defines several subclasses of `DefaultObject` in the following inheritance tree:
Objects form the core of Evennia and is probably what you'll spend most time working with. Objects are [Typeclassed](./Typeclasses.md) entities.
An Evennia Object is, by definition, a Python class that includes [evennia.objects.objects.DefaultObject](evennia.objects.objects.DefaultObject) among its parents. Evennia defines several subclasses of `DefaultObject`:
- `Object` - the base in-game entity. Found in `mygame/typeclasses/objects.py`. Inherits directly from `DefaultObject`.
- [Characters](./Characters.md) - the normal in-game Character, controlled by a player. Found in `mygame/typeclasses/characters.py`. Inherits from `DefaultCharacter`, which is turn a child of `DefaultObject`.
- [Rooms](./Rooms.md) - a location in the game world. Found in `mygame/typeclasses/rooms.py`. Inherits from `DefaultRoom`, which is in turn a child of `DefaultObject`).
- [Exits](./Exits.md) - represents a one-way connection to another location. Found in `mygame/typeclasses/exits.py` (inherits from `DefaultExit`, which is in turn a child of `DefaultObject`).
## Object
**Inheritance Tree:**
```
┌────────────┐
Evennia│ │ObjectParent│
library│ └──────▲─────┘
┌─────────────┐ │ ┌──────┐ │
│DefaultObject◄────────────────────┼────┤Object├──────┤
└──────▲──────┘ │ └──────┘ │
│ ┌────────────────┐ │ ┌─────────┐ │
├────────┤DefaultCharacter◄─┼────┤Character├───┤
│ └────────────────┘ │ └─────────┘ │
│ ┌────────────────┐ │ ┌────┐ │
├────────┤DefaultRoom ◄─┼────┤Room├────────┤
│ └────────────────┘ │ └────┘ │
│ ┌────────────────┐ │ ┌────┐ │
└────────┤DefaultExit ◄─┼────┤Exit├────────┘
└────────────────┘ │ └────┘
│Game-dir
┌─────────────┐
│DefaultObject│
└──────▲──────┘
│ ┌────────────┐
│ ┌─────►ObjectParent│
│ │ └────────────┘
┌─┴─┴──┐
│Object│
└──────┘
```
Here, arrows indicate inheritance and point from-child-to-parent.
> For an explanation of `ObjectParent`, see next section.
So, for example `DefaultObject` is a child of `DefaultCharacter` (in the Evennia library), which is a parent of `Character` (in the game dir). The class in the game-dir is the one you should modify for your game.
The `Object` class is meant to be used as the basis for creating things that are neither characters, rooms or exits - anything from weapons and armour, equipment and houses can be represented by extending the Object class. Depending on your game, this also goes for NPCs and monsters (in some games you may want to treat NPCs as just an un-puppeted [Character](./Characters.md) instead).
> Note the `ObjectParent` class. This is an empty mix-in that all classes in the game-dir inherits from. It's where you put things you want _all_ these classes to have.
You should not use Objects for game _systems_. Don't use an 'invisible' Object for tracking weather, combat, economy or guild memberships - that's what [Scripts](./Scripts.md) are for.
- [evennia.objects.objects.DefaultCharacter](evennia.objects.objects.DefaultCharacter) - the normal in-game Character, controlled by a player.
- [evennia.objects.objects.DefaultRoom](evennia.objects.objects.DefaultRoom) - a location in the game world.
- [evennia.objects.objects.DefaultExit](evennia.objects.objects.DefaultExit) - an entity that in a location (usually a Room). It represents a one-way connection to another location.
## ObjectParent - Adding common functionality
Here are the import paths for the relevant child classes in the game dir
`Object`, as well as `Character`, `Room` and `Exit` classes all additionally inherit from `mygame.typeclasses.objects.ObjectParent`.
- `mygame.typeclasses.objects.Object` (inherits from `DefaultObject`)
- `mygame.typeclasses.characters.Character` (inherits from `DefaultCharacter`)
- `mygame.typeclasses.rooms.Room` (inherits from `DefaultRoom`)
- `mygame.typeclasses.exits.Exit` (inherits from `DefaultExit`)
`ObjectParent` is an empty 'mixin' class. You can add stuff to this class that you want _all_ in-game entities to have.
## Working with Objects
Here is an example:
You can easily add your own in-game behavior by either modifying one of the typeclasses in your game dir or by inheriting from them.
```python
# in mygame/typeclasses/objects.py
# ...
from evennia.objects.objects import DefaultObject
class ObjectParent:
def at_pre_get(self, getter, **kwargs):
# make all entities by default un-pickable
return False
```
Now all of `Object`, `Exit`. `Room` and `Character` default to not being able to be picked up using the `get` command.
## Working with children of DefaultObject
This functionality is shared by all sub-classes of `DefaultObject`. You can easily add your own in-game behavior by either modifying one of the typeclasses in your game dir or by inheriting further from them.
You can put your new typeclass directly in the relevant module, or you could organize your code in some other way. Here we assume we make a new module `mygame/typeclasses/flowers.py`:
@ -82,15 +95,10 @@ What the `create` command actually *does* is to use the [evennia.create_object](
new_rose = create_object("typeclasses.flowers.Rose", key="MyRose")
```
(The `create` command will auto-append the most likely path to your typeclass, if you enter the
call manually you have to give the full path to the class. The `create.create_object` function is
powerful and should be used for all coded object creating (so this is what you use when defining
your own building commands).
This particular Rose class doesn't really do much, all it does it make sure the attribute
`desc`(which is what the `look` command looks for) is pre-set, which is pretty pointless since you
will usually want to change this at build time (using the `desc` command or using the [Spawner](./Prototypes.md)).
(The `create` command will auto-append the most likely path to your typeclass, if you enter the call manually you have to give the full path to the class. The `create.create_object` function is powerful and should be used for all coded object creating (so this is what you use when defining your own building commands).
This particular Rose class doesn't really do much, all it does it make sure the attribute `desc`(which is what the `look` command looks for) is pre-set, which is pretty pointless since you will usually want to change this at build time (using the `desc` command or using the [Spawner](./Prototypes.md)).
### Properties and functions on Objects
Beyond the properties assigned to all [typeclassed](./Typeclasses.md) objects (see that page for a list
@ -99,16 +107,14 @@ of those), the Object also has the following custom properties:
- `aliases` - a handler that allows you to add and remove aliases from this object. Use `aliases.add()` to add a new alias and `aliases.remove()` to remove one.
- `location` - a reference to the object currently containing this object.
- `home` is a backup location. The main motivation is to have a safe place to move the object to if its `location` is destroyed. All objects should usually have a home location for safety.
- `destination` - this holds a reference to another object this object links to in some way. Its main use is for [Exits](./Objects.md#exits), it's otherwise usually unset.
- `destination` - this holds a reference to another object this object links to in some way. Its main use is for [Exits](./Exits.md), it's otherwise usually unset.
- `nicks` - as opposed to aliases, a [Nick](./Nicks.md) holds a convenient nickname replacement for a real name, word or sequence, only valid for this object. This mainly makes sense if the Object is used as a game character - it can then store briefer shorts, example so as to quickly reference game commands or other characters. Use nicks.add(alias, realname) to add a new one.
- `account` - this holds a reference to a connected [Account](./Accounts.md) controlling this object (if any). Note that this is set also if the controlling account is *not* currently online - to test if an account is online, use the `has_account` property instead.
- `sessions` - if `account` field is set *and the account is online*, this is a list of all active sessions (server connections) to contact them through (it may be more than one if multiple connections are allowed in settings).
- `has_account` - a shorthand for checking if an *online* account is currently connected to this object.
- `contents` - this returns a list referencing all objects 'inside' this object (i,e. which has this object set as their `location`).
- `exits` - this returns all objects inside this object that are *Exits*, that is, has the `destination` property set.
The last two properties are special:
- `appearance_template` - this helps formatting the look of the Object when someone looks at it (see next section).l
- `cmdset` - this is a handler that stores all [command sets](./Command-Sets.md) defined on the object (if any).
- `scripts` - this is a handler that manages [Scripts](./Scripts.md) attached to the object (if any).
@ -119,87 +125,40 @@ The Object also has a host of useful utility functions. See the function headers
- `search()` - this is a convenient shorthand to search for a specific object, at a given location or globally. It's mainly useful when defining commands (in which case the object executing the command is named `caller` and one can do `caller.search()` to find objects in the room to operate on).
- `execute_cmd()` - Lets the object execute the given string as if it was given on the command line.
- `move_to` - perform a full move of this object to a new location. This is the main move method and will call all relevant hooks, do all checks etc.
- `clear_exits()` - will delete all [Exits](./Objects.md#exits) to *and* from this object.
- `clear_exits()` - will delete all [Exits](./Exits.md) to *and* from this object.
- `clear_contents()` - this will not delete anything, but rather move all contents (except Exits) to their designated `Home` locations.
- `delete()` - deletes this object, first calling `clear_exits()` and `clear_contents()`.
- `return_appearance` is the main hook letting the object visually describe itself.
The Object Typeclass defines many more *hook methods* beyond `at_object_creation`. Evennia calls these hooks at various points. When implementing your custom objects, you will inherit from the base parent and overload these hooks with your own custom code. See `evennia.objects.objects` for an updated list of all the available hooks or the [API for DefaultObject here](evennia.objects.objects.DefaultObject).
## Characters
## Changing an Object's appearance
The [DefaultCharacters](evennia.objects.objects.DefaultCharacter) is the root class for player in-game entities. They are usually _puppeted_ by [Accounts](./Accounts.md).
When you type `look <obj>`, this is the sequence of events that happen:
When a new Account logs in to Evennia for the first time, a new `Character` object is created and the Account object is assigned to the `account` attribute (but Evennia supports [alternative connection-styles](../Concepts/Connection-Styles.md) if so desired).
1. The command checks if the `caller` of the command (the 'looker') passes the `view` [lock](./Locks.md) of the target `obj`. If not, they will not find anything to look at (this is how you make objects invisible).
1. The `look` command calls `caller.at_look(obj)` - that is, the `at_look` hook on the 'looker' (the caller of the command) is called to perform the look on the target object. The command will echo whatever this hook returns.
2. `caller.at_look` calls and returns the outcome of `obj.return_apperance(looker, **kwargs)`. Here `looker` is the `caller` of the command. In other words, we ask the `obj` to descibe itself to `looker`.
3. `obj.return_appearance` makes use of its `.appearance_template` property and calls a slew of helper-hooks to populate this template. This is how the template looks by default:
A `Character` object must have a [Default Commandset](./Command-Sets.md) set on itself at creation, or the account will not be able to issue any commands!
```python
appearance_template = """
{header}
|c{name}|n
{desc}
{exits}{characters}{things}
{footer}
"""```
If you want to change the default character created by the default commands, you can change it in settings:
4. Each field of the template is populated by a matching helper method (and their default returns):
- `name` -> `obj.get_display_name(looker, **kwargs)` - returns `obj.name`.
- `desc` -> `obj.get_display_desc(looker, **kwargs)` - returns `obj.db.desc`.
- `header` -> `obj.get_display_header(looker, **kwargs)` - empty by default.
- `footer` -> `obj.get_display_footer(looker, **kwargs)` - empty by default.
- `exits` -> `obj.get_display_exits(looker, **kwargs)` - a list of `DefaultExit`-inheriting objects found inside this object (usually only present if `obj` is a `Room`).
- `characters` -> `obj.get_display_characters(looker, **kwargs)` - a list of `DefaultCharacter`-inheriting entities inside this object.
- `things` -> `obj.get_display_things(looker, **kwargs)` - a list of all other Objects inside `obj`.
5. `obj.format_appearance(string, looker, **kwargs)` is the last step the populated template string goes through. This can be used for final adjustments, such as stripping whitespace. The return from this method is what the user will see.
BASE_CHARACTER_TYPECLASS = "typeclasses.characters.Character"
This deafult points at the empty class in `mygame/typeclasses/characters.py` , ready for you to modify as you please.
## Rooms
[Rooms](evennia.objects.objects.DefaultRoom) are the root containers of all other objects.
The only thing really separating a room from any other object is that they have no `location` of their own and that default commands like `dig` creates objects of this class - so if you want to expand your rooms with more functionality, just inherit from `evennia.DefaultRoom`.
To change the default room created by `dig`, `tunnel` and other commands, change it in settings:
BASE_ROOM_TYPECLASS = "typeclases.rooms.Room"
The empty class in `mygame/typeclasses/rooms.py` is a good place to start!
## Exits
*Exits* are objects connecting other objects (usually *Rooms*) together. An object named *North* or *in* might be an exit, as well as *door*, *portal* or *jump out the window*. An exit has two things that separate them from other objects. Firstly, their *destination* property is set and points to a valid object. This fact makes it easy and fast to locate exits in the database. Secondly, exits define a special [Transit Command](./Commands.md) on themselves when they are created. This command is named the same as the exit object and will, when called, handle the practicalities of moving the character to the Exits's *destination* - this allows you to just enter the name of the exit on its own to move around, just as you would expect.
The exit functionality is all defined on the Exit typeclass, so you could in principle completely change how exits work in your game (it's not recommended though, unless you really know what you are doing). Exits are [locked](./Locks.md) using an access_type called *traverse* and also make use of a few hook methods for giving feedback if the traversal fails. See `evennia.DefaultExit` for more info.
Exits are normally overridden on a case-by-case basis, but if you want to change the default exit createad by rooms like `dig` , `tunnel` or `open` you can change it in settings:
BASE_EXIT_TYPECLASS = "typeclasses.exits.Exit"
In `mygame/typeclasses/exits.py` there is an empty `Exit` class for you to modify.
### Exit details
The process of traversing an exit is as follows:
1. The traversing `obj` sends a command that matches the Exit-command name on the Exit object. The [cmdhandler](./Commands.md) detects this and triggers the command defined on the Exit. Traversal always involves the "source" (the current location) and the `destination` (this is stored on the Exit object).
1. The Exit command checks the `traverse` lock on the Exit object
1. The Exit command triggers `at_traverse(obj, destination)` on the Exit object.
1. In `at_traverse`, `object.move_to(destination)` is triggered. This triggers the following hooks, in order:
1. `obj.at_pre_move(destination)` - if this returns False, move is aborted.
1. `origin.at_pre_leave(obj, destination)`
1. `obj.announce_move_from(destination)`
1. Move is performed by changing `obj.location` from source location to `destination`.
1. `obj.announce_move_to(source)`
1. `destination.at_object_receive(obj, source)`
1. `obj.at_post_move(source)`
1. On the Exit object, `at_post_traverse(obj, source)` is triggered.
If the move fails for whatever reason, the Exit will look for an Attribute `err_traverse` on itself and display this as an error message. If this is not found, the Exit will instead call `at_failed_traverse(obj)` on itself.
## Adding common functionality
`Object`, `Character`, `Room` and `Exit` also inherit from `mygame.typeclasses.objects.ObjectParent`.
This is an empty 'mixin' class. Optionally, you can modify this class if you want to easily add some _common_ functionality to all your Objects, Characters, Rooms and Exits at once. You can still customize each subclass separately (see the Python docs on [multiple inheritance](https://docs.python.org/3/tutorial/classes.html#multiple-inheritance) for details).
Here is an example:
```python
# in mygame/typeclasses/objects.py
# ...
from evennia.objects.objects import DefaultObject
class ObjectParent:
def at_pre_get(self, getter, **kwargs):
# make all entities by default un-pickable
return False
```
Now all of `Object`, `Exit`. `Room` and `Character` default to not being able to be picked up using the `get` command.
As each of these hooks (and the template itself) can be overridden in your child class, you can customize your look extensively. You can also have objects look different depending on who is looking at them. The extra `**kwargs` are not used by default, but are there to allow you to pass extra data into the system if you need it (like light conditions etc.)

View file

@ -55,7 +55,8 @@ Selected permission strings can be organized in a *permission hierarchy* by edit
`settings.PERMISSION_HIERARCHY`. Evennia's default permission hierarchy is as follows
(in increasing order of power):
Player # can chat and send tells (default level) (lowest)
Guest # temporary account, only used if GUEST_ENABLED=True (lowest)
Player # can chat and send tells (default level)
Helper # can edit help files
Builder # can edit the world
Admin # can administrate accounts
@ -63,9 +64,9 @@ Selected permission strings can be organized in a *permission hierarchy* by edit
(Besides being case-insensitive, hierarchical permissions also understand the plural form, so you could use `Developers` and `Developer` interchangeably).
> There is also a `Guest` level below `Player` that is only active if `settings.GUEST_ENABLED` is set. The Guest is is never part of `settings.PERMISSION_HIERARCHY`.
When checking a hierarchical permission (using one of the methods to follow), you will pass checks for your level *and below*. That is, if you have the "Admin" hierarchical permission, you will also pass checks asking for "Builder", "Helper" and so on.
When checking a hierarchical permission (using one of the methods to follow), you will pass checks for your level and all *below* you. That is, even if the check explicitly checks for "Builder" level access, you will actually pass if you have one of "Builder", "Admin" or "Developer". By contrast, if you check for a non-hierarchical permission, like "Blacksmith" you *must* have exactly that permission to pass.
By contrast, if you check for a non-hierarchical permission, like "Blacksmith" you must have *exactly* that permission to pass.
### Checking permissions

View file

@ -0,0 +1,31 @@
# Rooms
**Inheritance Tree:**
```
┌─────────────┐
│DefaultObject│
└─────▲───────┘
┌─────┴─────┐
│DefaultRoom│
└─────▲─────┘
│ ┌────────────┐
│ ┌─────►ObjectParent│
│ │ └────────────┘
┌─┴─┴┐
│Room│
└────┘
```
[Rooms](evennia.objects.objects.DefaultRoom) are in-game [Objects](./Objects.md) representing the root containers of all other objects.
The only thing technically separating a room from any other object is that they have no `location` of their own and that default commands like `dig` creates objects of this class - so if you want to expand your rooms with more functionality, just inherit from `evennia.DefaultRoom`.
To change the default room created by `dig`, `tunnel` and other default commands, change it in settings:
BASE_ROOM_TYPECLASS = "typeclases.rooms.Room"
The empty class in `mygame/typeclasses/rooms.py` is a good place to start!
While the default Room is very simple, there are several Evennia [contribs](../Contribs/Contribs-Overview.md) customizing and extending rooms with more functionality.

View file

@ -1,28 +1,20 @@
# TickerHandler
One way to implement a dynamic MUD is by using "tickers", also known as "heartbeats". A ticker is a
timer that fires ("ticks") at a given interval. The tick triggers updates in various game systems.
One way to implement a dynamic MUD is by using "tickers", also known as "heartbeats". A ticker is a timer that fires ("ticks") at a given interval. The tick triggers updates in various game systems.
## About Tickers
Tickers are very common or even unavoidable in other mud code bases. Certain code bases are even hard-coded to rely on the concept of the global 'tick'. Evennia has no such notion - the decision to use tickers is very much up to the need of your game and which requirements you have. The "ticker recipe" is just one way of cranking the wheels.
Tickers are very common or even unavoidable in other mud code bases. Certain code bases are even
hard-coded to rely on the concept of the global 'tick'. Evennia has no such notion - the decision to
use tickers is very much up to the need of your game and which requirements you have. The "ticker
recipe" is just one way of cranking the wheels.
The most fine-grained way to manage the flow of time is to use [utils.delay](evennia.utils.utils.delay) (using the [TaskHandler](evennia.scripts.taskhandler.TaskHandler)). Another is to use the time-repeat capability of [Scripts](./Scripts.md). These tools operate on individual objects.
Many types of operations (weather being the classic example) are however done on multiple objects in the same way at regular intervals, and for this, it's inefficient to set up separate delays/scripts for every such object.
The most fine-grained way to manage the flow of time is of course to use [Scripts](./Scripts.md). Many
types of operations (weather being the classic example) are however done on multiple objects in the
same way at regular intervals, and for this, storing separate Scripts on each object is inefficient.
The way to do this is to use a ticker with a "subscription model" - let objects sign up to be
triggered at the same interval, unsubscribing when the updating is no longer desired.
triggered at the same interval, unsubscribing when the updating is no longer desired. This means that the time-keeping mechanism is only set up once for all objects, making subscribing/unsubscribing faster.
Evennia offers an optimized implementation of the subscription model - the *TickerHandler*. This is
a singleton global handler reachable from `evennia.TICKER_HANDLER`. You can assign any *callable* (a
function or, more commonly, a method on a database object) to this handler. The TickerHandler will
then call this callable at an interval you specify, and with the arguments you supply when adding
it. This continues until the callable un-subscribes from the ticker. The handler survives a reboot
and is highly optimized in resource usage.
Evennia offers an optimized implementation of the subscription model - the *TickerHandler*. This is a singleton global handler reachable from [evennia.TICKER_HANDLER](evennia.utils.tickerhandler.TickerHandler). You can assign any *callable* (a function or, more commonly, a method on a database object) to this handler. The TickerHandler will then call this callable at an interval you specify, and with the arguments you supply when adding it. This continues until the callable un-subscribes from the ticker. The handler survives a reboot and is highly optimized in resource usage.
## Usage
Here is an example of importing `TICKER_HANDLER` and using it:
@ -32,10 +24,13 @@ Here is an example of importing `TICKER_HANDLER` and using it:
tickerhandler.add(20, obj.at_tick)
```
That's it - from now on, `obj.at_tick()` will be called every 20 seconds.
You can also import function and tick that:
```{important}
Everything you supply to `TickerHandler.add` will need to be pickled at some point to be saved into the database - also if you use `persistent=False`. Most of the time the handler will correctly store things like database objects, but the same restrictions as for [Attributes](./Attributes.md) apply to what the TickerHandler may store.
```
You can also import a function and tick that:
```python
from evennia import TICKER_HANDLER as tickerhandler
@ -51,9 +46,7 @@ Removing (stopping) the ticker works as expected:
tickerhandler.remove(30, myfunc)
```
Note that you have to also supply `interval` to identify which subscription to remove. This is
because the TickerHandler maintains a pool of tickers and a given callable can subscribe to be
ticked at any number of different intervals.
Note that you have to also supply `interval` to identify which subscription to remove. This is because the TickerHandler maintains a pool of tickers and a given callable can subscribe to be ticked at any number of different intervals.
The full definition of the `tickerhandler.add` method is
@ -63,74 +56,47 @@ The full definition of the `tickerhandler.add` method is
```
Here `*args` and `**kwargs` will be passed to `callback` every `interval` seconds. If `persistent`
is `False`, this subscription will not survive a server reload.
is `False`, this subscription will be wiped by a _server shutdown_ (it will still survive a normal reload).
Tickers are identified and stored by making a key of the callable itself, the ticker-interval, the
`persistent` flag and the `idstring` (the latter being an empty string when not given explicitly).
Tickers are identified and stored by making a key of the callable itself, the ticker-interval, the `persistent` flag and the `idstring` (the latter being an empty string when not given explicitly).
Since the arguments are not included in the ticker's identification, the `idstring` must be used to
have a specific callback triggered multiple times on the same interval but with different arguments:
Since the arguments are not included in the ticker's identification, the `idstring` must be used to have a specific callback triggered multiple times on the same interval but with different arguments:
```python
tickerhandler.add(10, obj.update, "ticker1", True, 1, 2, 3)
tickerhandler.add(10, obj.update, "ticker2", True, 4, 5)
```
> Note that, when we want to send arguments to our callback within a ticker handler, we need to
specify `idstring` and `persistent` before, unless we call our arguments as keywords, which would
often be more readable:
> Note that, when we want to send arguments to our callback within a ticker handler, we need to specify `idstring` and `persistent` before, unless we call our arguments as keywords, which would often be more readable:
```python
tickerhandler.add(10, obj.update, caller=self, value=118)
```
If you add a ticker with exactly the same combination of callback, interval and idstring, it will
overload the existing ticker. This identification is also crucial for later removing (stopping) the
subscription:
overload the existing ticker. This identification is also crucial for later removing (stopping) the subscription:
```python
tickerhandler.remove(10, obj.update, idstring="ticker1")
tickerhandler.remove(10, obj.update, idstring="ticker2")
```
The `callable` can be on any form as long as it accepts the arguments you give to send to it in
`TickerHandler.add`.
The `callable` can be on any form as long as it accepts the arguments you give to send to it in `TickerHandler.add`.
> Note that everything you supply to the TickerHandler will need to be pickled at some point to be
saved into the database. Most of the time the handler will correctly store things like database
objects, but the same restrictions as for [Attributes](./Attributes.md) apply to what the TickerHandler
may store.
When testing, you can stop all tickers in the entire game with `tickerhandler.clear()`. You can also
view the currently subscribed objects with `tickerhandler.all()`.
When testing, you can stop all tickers in the entire game with `tickerhandler.clear()`. You can also view the currently subscribed objects with `tickerhandler.all()`.
See the [Weather Tutorial](../Howtos/Tutorial-Weather-Effects.md) for an example of using the TickerHandler.
### When *not* to use TickerHandler
Using the TickerHandler may sound very useful but it is important to consider when not to use it.
Even if you are used to habitually relying on tickers for everything in other code bases, stop and
think about what you really need it for. This is the main point:
Using the TickerHandler may sound very useful but it is important to consider when not to use it. Even if you are used to habitually relying on tickers for everything in other code bases, stop and think about what you really need it for. This is the main point:
> You should *never* use a ticker to catch *changes*.
Think about it - you might have to run the ticker every second to react to the change fast enough.
Most likely nothing will have changed at a given moment. So you are doing pointless calls (since
skipping the call gives the same result as doing it). Making sure nothing's changed might even be
computationally expensive depending on the complexity of your system. Not to mention that you might
need to run the check *on every object in the database*. Every second. Just to maintain status quo
...
Think about it - you might have to run the ticker every second to react to the change fast enough. Most likely nothing will have changed at a given moment. So you are doing pointless calls (since skipping the call gives the same result as doing it). Making sure nothing's changed might even be computationally expensive depending on the complexity of your system. Not to mention that you might need to run the check *on every object in the database*. Every second. Just to maintain status quo ...
Rather than checking over and over on the off-chance that something changed, consider a more
proactive approach. Could you implement your rarely changing system to *itself* report when its
status changes? It's almost always much cheaper/efficient if you can do things "on demand". Evennia
itself uses hook methods for this very reason.
Rather than checking over and over on the off-chance that something changed, consider a more proactive approach. Could you implement your rarely changing system to *itself* report when its status changes? It's almost always much cheaper/efficient if you can do things "on demand". Evennia itself uses hook methods for this very reason.
So, if you consider a ticker that will fire very often but which you expect to have no effect 99% of
the time, consider handling things things some other way. A self-reporting on-demand solution is
usually cheaper also for fast-updating properties. Also remember that some things may not need to be
updated until someone actually is examining or using them - any interim changes happening up to that
moment are pointless waste of computing time.
So, if you consider a ticker that will fire very often but which you expect to have no effect 99% of the time, consider handling things things some other way. A self-reporting on-demand solution is usually cheaper also for fast-updating properties. Also remember that some things may not need to be updated until someone actually is examining or using them - any interim changes happening up to that moment are pointless waste of computing time.
The main reason for needing a ticker is when you want things to happen to multiple objects at the
same time without input from something else.
The main reason for needing a ticker is when you want things to happen to multiple objects at the same time without input from something else.

View file

@ -39,7 +39,7 @@ using ingame-python events.
defines the context in which we would like to call some arbitrary code. For
instance, one event is defined on exits and will fire every time a character
traverses through this exit. Events are described on a [typeclass](../Components/Typeclasses.md)
([exits](../Components/Objects.md#exits) in our example). All objects inheriting from this
([exits](../Components/Exits.md) in our example). All objects inheriting from this
typeclass will have access to this event.
- **Callbacks** can be set on individual objects, on events defined in code.
These **callbacks** can contain arbitrary code and describe a specific

View file

@ -3,7 +3,7 @@
Contribution by titeuf87, 2017
This contrib provides a wilderness map without actually creating a large number
of rooms - as you move, you instead end up back in the same room but its description
of rooms - as you move, you instead end up back in the same room but its description
changes. This means you can make huge areas with little database use as
long as the rooms are relatively similar (e.g. only the names/descs changing).
@ -19,11 +19,11 @@ with their own name. If no name is provided, then a default one is used. Interna
the wilderness is stored as a Script with the name you specify. If you don't
specify the name, a script named "default" will be created and used.
@py from evennia.contrib.grid import wilderness; wilderness.create_wilderness()
py from evennia.contrib.grid import wilderness; wilderness.create_wilderness()
Once created, it is possible to move into that wilderness map:
@py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me)
py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me)
All coordinates used by the wilderness map are in the format of `(x, y)`
tuples. x goes from left to right and y goes from bottom to top. So `(0, 0)`
@ -102,14 +102,21 @@ class PyramidMapProvider(wilderness.WildernessMapProvider):
desc = "This is a room in the pyramid."
if y == 3 :
desc = "You can see far and wide from the top of the pyramid."
room.ndb.desc = desc
room.ndb.active_desc = desc
```
Note that the currently active description is stored as `.ndb.active_desc`. When
looking at the room, this is what will be pulled and shown.
> Exits on a room are always present, but locks hide those not used for a
> location. So make sure to `quell` if you are a superuser (since the superuser ignores
> locks, those exits will otherwise not be hidden)
Now we can use our new pyramid-shaped wilderness map. From inside Evennia we
create a new wilderness (with the name "default") but using our new map provider:
py from world import pyramid as p; p.wilderness.create_wilderness(mapprovider=p.PyramidMapProvider())
py from evennia.contrib import wilderness; wilderness.enter_wilderness(me, coordinates=(4, 1))
py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me, coordinates=(4, 1))
## Implementation details

View file

@ -494,7 +494,7 @@ and abort an ongoing traversal, respectively.
_Contribution by titeuf87, 2017_
This contrib provides a wilderness map without actually creating a large number
of rooms - as you move, you instead end up back in the same room but its description
of rooms - as you move, you instead end up back in the same room but its description
changes. This means you can make huge areas with little database use as
long as the rooms are relatively similar (e.g. only the names/descs changing).

View file

@ -50,9 +50,9 @@ The flat API is defined in `__init__.py` [viewable here](github:evennia/__init__
- [evennia.DefaultAccount](evennia.accounts.accounts.DefaultAccount) - player account class ([docs](Components/Accounts.md))
- [evennia.DefaultGuest](evennia.accounts.accounts.DefaultGuest) - base guest account class
- [evennia.DefaultObject](evennia.objects.objects.DefaultObject) - base class for all objects ([docs](Components/Objects.md))
- [evennia.DefaultCharacter](evennia.objects.objects.DefaultCharacter) - base class for in-game characters ([docs](Components/Objects.md#characters))
- [evennia.DefaultRoom](evennia.objects.objects.DefaultRoom) - base class for rooms ([docs](Components/Objects.md#rooms))
- [evennia.DefaultExit](evennia.objects.objects.DefaultExit) - base class for exits ([docs](Components/Objects.md#exits))
- [evennia.DefaultCharacter](evennia.objects.objects.DefaultCharacter) - base class for in-game characters ([docs](Components/Characters.md))
- [evennia.DefaultRoom](evennia.objects.objects.DefaultRoom) - base class for rooms ([docs](Components/Rooms.md))
- [evennia.DefaultExit](evennia.objects.objects.DefaultExit) - base class for exits ([docs](Components/Exits.md))
- [evennia.DefaultScript](evennia.scripts.scripts.DefaultScript) - base class for OOC-objects ([docs](Components/Scripts.md))
- [evennia.DefaultChannel](evennia.comms.comms.DefaultChannel) - base class for in-game channels ([docs](Components/Channels.md))

View file

@ -39,7 +39,7 @@ using ingame-python events.
defines the context in which we would like to call some arbitrary code. For
instance, one event is defined on exits and will fire every time a character
traverses through this exit. Events are described on a [typeclass](Typeclasses)
([exits](Objects#exits) in our example). All objects inheriting from this
([exits](Exits) in our example). All objects inheriting from this
typeclass will have access to this event.
- **Callbacks** can be set on individual objects, on events defined in code.
These **callbacks** can contain arbitrary code and describe a specific

View file

@ -277,7 +277,7 @@ class ContribClothing(DefaultObject):
else:
message = f"$You() $conj(put) on {self.name}"
if to_cover:
message += ", covering {iter_to_str(to_cover)}"
message += f", covering {iter_to_str(to_cover)}"
wearer.location.msg_contents(message + ".", from_obj=wearer)
def remove(self, wearer, quiet=False):

View file

@ -323,7 +323,7 @@ class TBBasicCharacter(DefaultCharacter):
can be changed at creation and factor into combat calculations.
"""
def at_pre_move(self, destination):
def at_pre_move(self, destination, move_type='move', **kwargs):
"""
Called just before starting to move this object to
destination.

View file

@ -3,7 +3,7 @@
Contribution by titeuf87, 2017
This contrib provides a wilderness map without actually creating a large number
of rooms - as you move, you instead end up back in the same room but its description
of rooms - as you move, you instead end up back in the same room but its description
changes. This means you can make huge areas with little database use as
long as the rooms are relatively similar (e.g. only the names/descs changing).
@ -19,11 +19,11 @@ with their own name. If no name is provided, then a default one is used. Interna
the wilderness is stored as a Script with the name you specify. If you don't
specify the name, a script named "default" will be created and used.
@py from evennia.contrib.grid import wilderness; wilderness.create_wilderness()
py from evennia.contrib.grid import wilderness; wilderness.create_wilderness()
Once created, it is possible to move into that wilderness map:
@py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me)
py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me)
All coordinates used by the wilderness map are in the format of `(x, y)`
tuples. x goes from left to right and y goes from bottom to top. So `(0, 0)`
@ -102,14 +102,21 @@ class PyramidMapProvider(wilderness.WildernessMapProvider):
desc = "This is a room in the pyramid."
if y == 3 :
desc = "You can see far and wide from the top of the pyramid."
room.ndb.desc = desc
room.ndb.active_desc = desc
```
Note that the currently active description is stored as `.ndb.active_desc`. When
looking at the room, this is what will be pulled and shown.
> Exits on a room are always present, but locks hide those not used for a
> location. So make sure to `quell` if you are a superuser (since the superuser ignores
> locks, those exits will otherwise not be hidden)
Now we can use our new pyramid-shaped wilderness map. From inside Evennia we
create a new wilderness (with the name "default") but using our new map provider:
py from world import pyramid as p; p.wilderness.create_wilderness(mapprovider=p.PyramidMapProvider())
py from evennia.contrib import wilderness; wilderness.enter_wilderness(me, coordinates=(4, 1))
py from evennia.contrib.grid import wilderness; wilderness.enter_wilderness(me, coordinates=(4, 1))
## Implementation details

View file

@ -92,9 +92,16 @@ class PyramidMapProvider(wilderness.WildernessMapProvider):
desc = "This is a room in the pyramid."
if y == 3 :
desc = "You can see far and wide from the top of the pyramid."
room.ndb.desc = desc
room.ndb.active_desc = desc
```
Note that the currently active description is stored as `.ndb.active_desc`. When
looking at the room, this is what will be pulled and shown.
> Exits on a room are always present, but locks hide those not used for a
> location. So make sure to `quell` if you are a superuser (since the superuser ignores
> locks, those exits will otherwise not be hidden)
Now we can use our new pyramid-shaped wilderness map. From inside Evennia we
create a new wilderness (with the name "default") but using our new map provider:
@ -116,13 +123,7 @@ create a new wilderness (with the name "default") but using our new map provider
"""
from evennia import (
DefaultExit,
DefaultRoom,
DefaultScript,
create_object,
create_script,
)
from evennia import DefaultExit, DefaultRoom, DefaultScript, create_object, create_script
from evennia.typeclasses.attributes import AttributeProperty
from evennia.utils import inherits_from

View file

@ -511,7 +511,8 @@ def is_ooc(accessing_obj, accessed_obj, *args, **kwargs):
is_ooc()
This is normally used to lock a Command, so it can be used
only when out of character.
only when out of character. When not logged in at all, this
function will still return True.
"""
obj = accessed_obj.obj if hasattr(accessed_obj, "obj") else accessed_obj
account = obj.account if hasattr(obj, "account") else obj
@ -520,9 +521,14 @@ def is_ooc(accessing_obj, accessed_obj, *args, **kwargs):
try:
session = accessed_obj.session
except AttributeError:
session = account.sessions.get()[0] # note-this doesn't work well
# note-this doesn't work well
# for high multisession mode. We may need
# to change to sessiondb to resolve this
sessions = session = account.sessions.get()
session = sessions[0] if sessions else None
if not session:
# this suggests we are not even logged in; treat as ooc.
return True
try:
return not account.get_puppet(session)
except TypeError:

View file

@ -1,12 +1,23 @@
"""
Unit tests for the scripts package
"""
from unittest import TestCase, mock
from collections import defaultdict
from parameterized import parameterized
from evennia import DefaultScript
from evennia.objects.objects import DefaultObject
from evennia.scripts.models import ObjectDoesNotExist, ScriptDB
from evennia.scripts.scripts import DoNothing, ExtendedLoopingCall
from evennia.utils.create import create_script
from evennia.utils.test_resources import BaseEvenniaTest
from evennia.scripts.tickerhandler import TickerHandler
from evennia.scripts.monitorhandler import MonitorHandler
from evennia.scripts.manager import ScriptDBManager
from evennia.utils.dbserialize import dbserialize
class TestScript(BaseEvenniaTest):
@ -18,6 +29,66 @@ class TestScript(BaseEvenniaTest):
self.assertFalse(errors, errors)
mockinit.assert_called()
class TestTickerHandler(TestCase):
""" Test the TickerHandler class """
def test_store_key_raises_RunTimeError(self):
""" Test _store_key method raises RuntimeError for interval < 1 """
with self.assertRaises(RuntimeError):
th=TickerHandler()
th._store_key(None, None, 0, None)
def test_remove_raises_RunTimeError(self):
""" Test remove method raises RuntimeError for catching old ordering of arguments """
with self.assertRaises(RuntimeError):
th=TickerHandler()
th.remove(callback=1)
class TestScriptDBManager(TestCase):
""" Test the ScriptDBManger class """
def test_not_obj_return_empty_list(self):
""" Test get_all_scripts_on_obj returns empty list for falsy object """
manager_obj = ScriptDBManager()
returned_list = manager_obj.get_all_scripts_on_obj(False)
self.assertEqual(returned_list, [])
class TestingListIntervalScript(DefaultScript):
"""
A script that does nothing. Used to test listing of script with nonzero intervals.
"""
def at_script_creation(self):
"""
Setup the script
"""
self.key = "interval_test"
self.desc = "This is an empty placeholder script."
self.interval = 1
self.repeats = 1
class TestScriptHandler(BaseEvenniaTest):
"""
Test the ScriptHandler class.
"""
def setUp(self):
self.obj, self.errors = DefaultObject.create("test_object")
def tearDown(self):
self.obj.delete()
def test_start(self):
"Check that ScriptHandler start function works correctly"
self.obj.scripts.add(TestingListIntervalScript)
self.num = self.obj.scripts.start(self.obj.scripts.all()[0].key)
self.assertTrue(self.num == 1)
def test_list_script_intervals(self):
"Checks that Scripthandler __str__ function lists script intervals correctly"
self.obj.scripts.add(TestingListIntervalScript)
self.str = str(self.obj.scripts)
self.assertTrue("None/1" in self.str)
self.assertTrue("1 repeats" in self.str)
class TestScriptDB(TestCase):
"Check the singleton/static ScriptDB object works correctly"
@ -88,3 +159,133 @@ class TestExtendedLoopingCall(TestCase):
loopcall.__call__.assert_not_called()
self.assertEqual(loopcall.interval, 20)
loopcall._scheduleFrom.assert_called_with(121)
def test_start_invalid_interval(self):
""" Test the .start method with interval less than zero """
with self.assertRaises(ValueError):
callback = mock.MagicMock()
loopcall = ExtendedLoopingCall(callback)
loopcall.start(-1, now=True, start_delay=None, count_start=1)
def test__call__when_delay(self):
""" Test __call__ modifies start_delay and starttime if start_delay was previously set """
callback = mock.MagicMock()
loopcall = ExtendedLoopingCall(callback)
loopcall.clock.seconds = mock.MagicMock(return_value=1)
loopcall.start_delay = 2
loopcall.starttime = 0
loopcall()
self.assertEqual(loopcall.start_delay, None)
self.assertEqual(loopcall.starttime, 1)
def test_force_repeat(self):
""" Test forcing script to run that is scheduled to run in the future """
callback = mock.MagicMock()
loopcall = ExtendedLoopingCall(callback)
loopcall.clock.seconds = mock.MagicMock(return_value=0)
loopcall.start(20, now=False, start_delay=5, count_start=0)
loopcall.force_repeat()
loopcall.stop()
callback.assert_called_once()
def dummy_func():
""" Dummy function used as callback parameter """
return 0
class TestMonitorHandler(TestCase):
"""
Test the MonitorHandler class.
"""
def setUp(self):
self.handler = MonitorHandler()
def test_add(self):
"""Tests that adding an object to the monitor handler works correctly"""
obj = mock.Mock()
fieldname = "db_add"
callback = dummy_func
idstring = "test"
self.handler.add(obj, fieldname, callback, idstring=idstring)
self.assertIn(fieldname, self.handler.monitors[obj])
self.assertIn(idstring, self.handler.monitors[obj][fieldname])
self.assertEqual(self.handler.monitors[obj][fieldname][idstring], (callback, False, {}))
def test_remove(self):
"""Tests that removing an object from the monitor handler works correctly"""
obj = mock.Mock()
fieldname = 'db_remove'
callback = dummy_func
idstring = 'test_remove'
"""Add an object to the monitor handler and then remove it"""
self.handler.add(obj,fieldname,callback,idstring=idstring)
self.handler.remove(obj,fieldname,idstring=idstring)
self.assertEquals(self.handler.monitors[obj][fieldname], {})
def test_add_with_invalid_function(self):
obj = mock.Mock()
"""Tests that add method rejects objects where callback is not a function"""
fieldname = "db_key"
callback = "not_a_function"
self.handler.add(obj, fieldname, callback)
self.assertNotIn(fieldname, self.handler.monitors[obj])
def test_all(self):
"""Tests that all method correctly returns information about added objects"""
obj = [mock.Mock(),mock.Mock()]
fieldname = ["db_all1","db_all2"]
callback = dummy_func
idstring = ["test_all1","test_all2"]
self.handler.add(obj[0], fieldname[0], callback, idstring=idstring[0])
self.handler.add(obj[1], fieldname[1], callback, idstring=idstring[1],persistent=True)
output = self.handler.all()
self.assertEquals(output,
[(obj[0], fieldname[0], idstring[0], False, {}),
(obj[1], fieldname[1], idstring[1], True, {})])
def test_clear(self):
"""Tests that the clear function correctly clears the monitor handler"""
obj = mock.Mock()
fieldname = "db_add"
callback = dummy_func
idstring = "test"
self.handler.add(obj, fieldname, callback, idstring=idstring)
self.assertIn(obj, self.handler.monitors)
self.handler.clear()
self.assertNotIn(obj, self.handler.monitors)
self.assertEquals(defaultdict(lambda: defaultdict(dict)), self.handler.monitors)
def test_add_remove_attribute(self):
"""Tests that adding and removing an object attribute to the monitor handler works correctly"""
obj = mock.Mock()
obj.name = "testaddattribute"
fieldname = "name"
callback = dummy_func
idstring = "test"
category = "testattribute"
"""Add attribute to handler and assert that it has been added"""
self.handler.add(obj, fieldname, callback, idstring=idstring,category=category)
index = obj.attributes.get(fieldname, return_obj=True)
name = "db_value[testattribute]"
self.assertIn(name, self.handler.monitors[index])
self.assertIn(idstring, self.handler.monitors[index][name])
self.assertEqual(self.handler.monitors[index][name][idstring], (callback, False, {}))
"""Remove attribute from the handler and assert that it is gone"""
self.handler.remove(obj,fieldname,idstring=idstring,category=category)
self.assertEquals(self.handler.monitors[index][name], {})

View file

@ -38,7 +38,7 @@ def _log(msg, logfunc, prefix="", **kwargs):
try:
msg = str(msg)
except Exception as err:
msg = str(e)
msg = str(err)
if kwargs:
logfunc(msg, **kwargs)
else: