github / src / notepad.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 |
"""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 |
content = empty_string |
80 |
|
81 |
with open(location, "w", encoding="utf-8") as file: |
82 |
file.write(content)
|
83 |
|
84 |
self.content = empty_string
|
85 |
else:
|
86 |
if not os.access(location, os.R_OK): |
87 |
logger.warning("CocoMUD doesn't have the right to " \
|
88 |
"read {}".format(location))
|
89 |
else:
|
90 |
with open(location, "r", encoding="utf-8") as file: |
91 |
content = file.read()
|
92 |
|
93 |
self.content = content
|
94 |
|
95 |
def save(self): |
96 |
"""Save the notepad in its file."""
|
97 |
parentdir = self.owner.path
|
98 |
location = self.path
|
99 |
logger.info("Saving the notepad file in {}".format(location))
|
100 |
|
101 |
if not os.access(location, os.W_OK): |
102 |
logger.warning("CocoMUD doesn't have the right to write " \
|
103 |
"in {}".format(location))
|
104 |
else:
|
105 |
content = self.content
|
106 |
with open(location, "w", encoding="utf-8") as file: |
107 |
file.write(content)
|