Project

Profile

Help

How to connect?
Download (4.69 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / character.py @ 05b173a7

1
# Copyright (c) 2016, LE GOFF Vincent
2
# All rights reserved.
3

    
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
6

    
7
# * Redistributions of source code must retain the above copyright notice, this
8
#   list of conditions and the following disclaimer.
9

    
10
# * Redistributions in binary form must reproduce the above copyright notice,
11
#   this list of conditions and the following disclaimer in the documentation
12
#   and/or other materials provided with the distribution.
13

    
14
# * Neither the name of ytranslate nor the names of its
15
#   contributors may be used to endorse or promote products derived from
16
#   this software without specific prior written permission.
17

    
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

    
29
"""This file contains the Character class."""
30

    
31
import os
32

    
33
from ytranslate import t
34

    
35
from log import character as logger
36
from notepad import Notepad
37
from safe import Safe
38

    
39
class Character:
40

    
41
    """An object to represent a character from a world.
42

43
    A world represents a game, with the information to reach the
44
    server.  A character, on the other hand, represents specific
45
    information to one player in this world.  Worlds may have no
46
    character (the default).  Characters can be set to keep login
47
    information safely, like the username and password and additional
48
    commands.
49

50
    Characters may have their specific configuration, like their
51
    specific set of aliases, macros or triggers.
52

53
    """
54

    
55
    def __init__(self, world, location):
56
        self.world = world
57
        self.location = location
58
        self.name = "unknown"
59
        self.username = ""
60
        self.password = ""
61
        self.other_commands = ""
62
        self.default = False
63

    
64
        # Character's configuration
65
        self.aliases = []
66
        self.macros = []
67
        self.triggers = []
68
        self.notepad = None
69

    
70
    def __repr__(self):
71
        return "<Character {} (world={}, location={})>".format(self.name,
72
                self.world and self.world.name or "unknown", self.location)
73

    
74
    def __str__(self):
75
        return self.name
76

    
77
    @property
78
    def path(self):
79
        return os.path.join(self.world.path, self.location)
80

    
81
    def create_safe(self):
82
        """Create a safe for this character."""
83
        location = os.path.join(self.world.path, self.location)
84
        if not os.path.exists(location):
85
            logger.info("Try to create the {} location for a character".format(
86
                    repr(location)))
87
            os.makedirs(location)
88

    
89
        # Create a safe for this character
90
        safe = Safe(file=os.path.join(location, ".passphrase"),
91
                secret=os.path.join(location, "login"))
92

    
93
        return safe
94

    
95
    def load(self):
96
        """Load the encrypted configuration.
97

98
        If present, it will be in {world}/{location}/{login}.
99
        The passphrase will be in {world}/{location}/.passphrase .
100

101
        """
102
        location = os.path.join(self.world.path, self.location)
103

    
104
        # Retrieve encrypted information
105
        safe = self.create_safe()
106
        self.name = safe.retrieve("name", "")
107
        self.username = safe.retrieve("username", "")
108
        self.password = safe.retrieve("password", "")
109
        self.other_commands = safe.retrieve("other_commands", "")
110
        self.default = safe.retrieve("default", False)
111

    
112
    def save(self):
113
        """Save the character."""
114
        safe = self.create_safe()
115
        safe.store("name", self.name)
116
        safe.store("username", self.username)
117
        safe.store("password", self.password)
118
        safe.store("other_commands", self.other_commands)
119
        safe.store("default", self.default)
120

    
121
    def open_notepad(self):
122
        """Open and return the notepad associated to this character."""
123
        if self.notepad:
124
            return self.notepad
125

    
126
        self.notepad = Notepad(self)
127
        empty_string = t("ui.message.notepad.character_empty",
128
                character=self.name, world=self.world.name)
129
        self.notepad.open(empty_string)
130
        return self.notepad
(4-4/20)