Project

Profile

Help

Download (4.17 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / notepad.py @ 96664dff

1 6d5cf9ef Vincent Le Goff
# 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
"""Module containing the Notepad class."""
30
31
import os
32
33
from log import main as logger
34
35
class Notepad:
36
37
    """Object representing a world or character notepad.
38

39
    A notepad is just what it sounds like:  a text file that can be
40
    edited through the interface.  It is specific to a world or
41
    character.
42

43
    When a user opens the notepad, the content of the file is retrieved
44
    and a dialog box is open with this content.  When the user closes
45
    this notepad, it is saved.
46

47
    """
48
49
    def __init__(self, owner):
50
        self.owner = owner
51
        self.content = ""
52
53
    def __repr__(self):
54
        return "<Notepad owner by {}>".format(self.owner)
55
56
    @property
57
    def path(self):
58
        """Return the path leading to the notepad file."""
59
        return os.path.join(self.owner.path, "notepad.txt")
60
61
    def open(self, empty_string):
62
        """Open the notepad file, read it and close it.
63

64
        If the file doesn't exist, or if it is empty, the 'empty_string'
65
        is placed in it.
66

67
        """
68
        parentdir = self.owner.path
69
        location = self.path
70
        logger.info("Opening the notepad file at {}".format(location))
71
72
        # Try to pen the file
73
        if not os.path.exists(location):
74
            # Try to create it
75
            if not os.access(parentdir, os.W_OK):
76
                logger.warning("CocoMUD doesn't have the right to " \
77
                        "write in {}".format(parentdir))
78
            else:
79
                if isinstance(empty_string, unicode):
80
                    content = empty_string.encode("latin-1", errors="replace")
81
82
                    with open(location, "w") as file:
83
                        file.write(content)
84
85
            self.content = empty_string
86
        else:
87
            if not os.access(location, os.R_OK):
88
                logger.warning("CocoMUD doesn't have the right to " \
89
                        "read {}".format(location))
90
            else:
91
                with open(location, "r") as file:
92
                    content = file.read()
93
94
                content = content.decode("latin-1", errors="replace")
95
                self.content = content
96
97
    def save(self):
98
        """Save the notepad in its file."""
99
        parentdir = self.owner.path
100
        location = self.path
101
        logger.info("Saving the notepad file in {}".format(location))
102
103
        if not os.access(location, os.W_OK):
104
            logger.warning("CocoMUD doesn't have the right to write " \
105
                    "in {}".format(location))
106
        else:
107
            content = self.content
108
            if isinstance(content, unicode):
109
                content = content.encode("latin-1", errors="replace")
110
111
            with open(location, "w") as file:
112
                file.write(content)