-
Notifications
You must be signed in to change notification settings - Fork 0
/
molecular.py
219 lines (175 loc) · 7.03 KB
/
molecular.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Copyright (c) 2001, Chris Gonnerman
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of the author nor the names of any contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""molecular.py -- molecular (ngenoid) name generator
This module knows how to generate "random" names for RPG characters.
It uses the same method as the "ngen" name generator by Kimmo Kulovesi,
and in fact it can use the same name files. molecular.py knows how
to merge multiple tables also, which can be handy...
If run as a command-line program, use the following options:
-r namefile -- read the given name file and add to the
current name table.
nnn -- generate nnn (a number) names and print
on standard output.
To generate names from a name file:
python molecular.py -r file 10
As a module (to be imported) you get the following classes and functions:
NameFile (class) -- a file wrapper with a disabled close() method,
used internally and probably not useful otherwise.
nameopen (function) -- opens a file; takes filename and mode options,
searches the default name file directory if not
found in current directory, handles "-" filenames,
and uses NameFile to disable closing of sys.stdin/
sys.stdout.
Molecule (class) -- the meat of the matter. A Molecule instance has
the following methods:
.load(file) -- loads a name file,
which may be a file-like
object with a .readline()
method or a filename as a
string.
.name() -- generate one name and
return it.
"""
__version__ = "1.0"
import string, sys, random
NAMEDIR = "namefiles"
NAMESECTIONS = ["inf", "first", "mid", "final", "notes", "end"]
class NameFile:
__file_attributes = ("closed", "mode", "name", "softspace")
def __init__(self, file):
self.fd = file
def close(self):
pass
def flush(self):
return self.fd.flush()
def isatty(self):
return self.fd.isatty()
def fileno(self):
return self.fd.fileno()
def read(self, *args):
return self.fd.read(*args)
def readline(self, *args):
return self.fd.readline(*args)
def readlines(self, *args):
return self.fd.readlines(*args)
def seek(self, *args):
return self.fd.seek(*args)
def tell(self):
return self.fd.tell()
def write(self, str):
return self.fd.write(str)
def writelines(self, list):
return self.fd.writelines(list)
def __repr__(self):
return repr(self.fd)
def __getattr__(self, name):
if name in self.__file_attributes:
return getattr(self.fd, name)
else:
return self.__dict__[name]
def __setattr__(self, name, value):
if name in self.__file_attributes:
setattr(self.fd, name, value)
else:
self.__dict__[name] = value
def __cmp__(self, file):
"""I'm not sure what the correct behavior is, and therefore
this implementation is just a guess."""
if type(file) == type(self.fd):
return cmp(self.fd, file)
else:
return cmp(self.fd, file.fd)
class NameReader:
def __init__(self, file):
self.file = file
self.line = ""
def __next__(self):
self.line = self.file.readline()
return self.line
def close(self):
return self.file.close()
def safeopen(filename, mode):
try:
return open(filename, mode)
except IOError:
return None
def nameopen(filename, mode):
if filename == "-":
if "r" in mode:
return NameFile(sys.stdin)
else:
return NameFile(sys.stdout)
fp = safeopen(filename, mode)
if fp is None:
fp = safeopen(filename + ".nam", mode)
if "r" in mode and fp is None:
fp = safeopen(NAMEDIR + "/" + filename, mode)
# last call is open() instead of safeopen() to finally raise
# the exception if we just can't find the file.
if fp is None:
fp = open(NAMEDIR + "/" + filename + ".nam", mode)
return fp
class Molecule:
def __init__(self):
self.nametbl = {}
for i in NAMESECTIONS:
self.nametbl[i] = []
self.nametbl[""] = []
self.cursection = self.nametbl[""]
def load(self, fp):
if type(fp) is type(""):
fp = nameopen(fp, "r")
else:
fp = NameFile(fp)
rdr = NameReader(fp)
while next(rdr):
line = rdr.line[:-1]
if len(line) > 0 and line[0] == "[" and line[-1] == "]":
line = line.lstrip("[")
line = line.rstrip("]")
if line not in self.nametbl:
self.nametbl[line] = []
self.cursection = self.nametbl[line]
else:
self.cursection.append(line)
fp.close()
def name(self):
n = []
if len(self.nametbl["first"]) > 0:
n.append(random.choice(self.nametbl["first"]))
if len(self.nametbl["mid"]) > 0:
n.append(random.choice(self.nametbl["mid"]))
if len(self.nametbl["final"]) > 0:
n.append(random.choice(self.nametbl["final"]))
return "".join(n)
if __name__ == "__main__":
name = Molecule()
name.load("namefiles/orcs_wh.nam")
print(name.name())