/usr/share/pyshared/pygopherd/gopherentry.py is in pygopherd 2.0.18.3+nmu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | # pygopherd -- Gopher-based protocol server in Python
# module: Generic gopher entry object
# Copyright (C) 2002 John Goerzen
# <jgoerzen@complete.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import SocketServer
import re
import os, stat, os.path, mimetypes, urllib
mapping = None
eaexts = None
class GopherEntry:
"""The entry object for Gopher. It holds information about each
Gopher object."""
def __init__(self, selector, config):
"""Initialize object based on a selector and config."""
self.selector = selector # Gopher path to file
self.config = config # Our config object
self.fspath = None # Path to the obj in filesystem
self.type = None # Gopher0 type char
self.name = None # Menu name
self.host = None # Hostname
self.port = None # Port number (an int)
self.mimetype = None # MIME type
self.encodedmimetype = None # Real MIME type if encoded.
self.size = None # Size
self.encoding = None # Encoding type
self.populated = 0 # Whether or not it's been populated
self.language = None # Language
self.ctime = None # Creation date
self.mtime = None # Modification date
self.num = 0 # Number in menu
self.gopherpsupport = 0 # Supports gopher+
self.ea = {} # Extended attributes -- Gopher+
# Abstract, etc.
def populatefromvfs(self, vfs, selector):
self.populatefromfs(selector, statval = vfs.stat(selector),
vfs = vfs)
def populatefromfs(self, fspath, statval = None, vfs = None):
"""Fills in self with data gleaned from the filesystem.
The argument fspath specifies where in the filesystem it will search.
statval, if present, will be used instead of calling stat() itself
so as to cut down on the number of system calls.
The overall idea of this function is to set only those values that
are not already set and to do so only if a definitive answer for
them can be obtained from the operating system. The rest of this
information describes the details of how it does this.
If populatefromfs has already been called or self.populated is true,
this function will note the fspath and the return without modifying
anything else.
If either gethost() or getport() return anything other than None,
the same thing will happen.
If there is no statval and os.stat returns an error, again,
the same thing will happen.
Assuming these tests pass, then:
self.gopherpsupport will be set to true for any file or directory.
The remaining values will be set only if they are not set already:
For both files and directories, the creation and modification times
will be noted, the name will be set to the filename (as returned
by os.path.basename on the selector).
For directories only, the type will be set to 1 and the mimetype to
application/gopher-menu. Gopher+ protocols may wish to
indicate application/gopher+-menu is available as well, but that
is outside the scope of this function.
For files only, the size will be noted. An attempt will be made
to ascertain a mimetype and an encoding. If only a mimetype is
found, it will be noted in self.mimetype. If both a mimetype
and an encoding is found, self.mimetype will be
application/octet-stream, self.encoding will note the encoding,
and self.encodedmimetype will note the type of the encoded data.
If no mimetype can be found, it will be set to the default
from the config file. If no gopher0 type character is already present,
self.guesstype() will be called to set it."""
self.fspath = fspath
if vfs == None:
from pygopherd.handlers.base import VFS_Real
vfs = VFS_Real(self.config)
if self.populated:
return
# Just let the stat catch the OSError rather than testing
# for existance here. Help cut down on the number of syscalls.
if not (self.gethost() == None and self.getport() == None):
return
if not statval:
try:
statval = vfs.stat(self.fspath)
except OSError:
return
self.populated = 1
self.gopherpsupport = 1 # Indicate gopher+ support for locals.
# All this "or" stuff means that we only modify it if it's not already
# set.
self.ctime = self.ctime or statval[9]
self.mtime = self.mtime or statval[8]
self.name = self.name or os.path.basename(self.selector)
if stat.S_ISDIR(statval[0]):
self.type = self.type or '1'
self.mimetype = self.mimetype or 'application/gopher-menu'
self.handleeaext(self.fspath + '/', vfs) # Add the / so we get /.abs
return
self.handleeaext(self.fspath, vfs)
self.size = self.size or statval[6]
mimetype, encoding = mimetypes.guess_type(self.selector, strict = 0)
if encoding:
self.mimetype = self.mimetype or 'application/octet-stream'
self.encoding = self.encoding or encoding
self.encodedmimetype = self.encodedmimetype or mimetype
else:
self.mimetype = self.mimetype or mimetype
# Did we get no mime type at all? Fall back to a default.
if not self.mimetype:
self.mimetype = self.config.get("GopherEntry", "defaultmimetype")
self.type = self.type or self.guesstype()
def guesstype(self):
global mapping
if not mapping:
mapping = eval(self.config.get("GopherEntry", "mapping"))
for maprule in mapping:
if re.match(maprule[0], self.mimetype):
return maprule[1]
return '0'
def handleeaext(self, selector, vfs):
"""Handle getting extended attributes from the filesystem."""
global eaexts
if eaexts == None:
eaexts = eval(self.config.get("GopherEntry", "eaexts"))
if vfs == None:
from pygopherd.handlers.base import VFS_Real
vfs = VFS_Real(self.config)
for extension, blockname in eaexts.items():
if self.ea.has_key(blockname):
continue
try:
rfile = vfs.open(selector + extension, "rt")
self.setea(blockname, "\n".join(
[x.rstrip() for x in rfile.readlines(20480)]))
except IOError:
pass
def getselector(self, default = None):
if self.selector == None:
return default
return self.selector
def setselector(self, arg):
self.selector = arg
def getconfig(self, default = None):
return self.config or default
def setconfig(self, arg):
self.config = arg
def getfspath(self, default = None):
if self.fspath == None:
return default
return self.fspath
def setfspath(self, arg):
self.fspath = arg
def gettype(self, default = None):
if self.type == None:
return default
return self.type
def settype(self, arg):
self.type = arg
def getname(self, default = None):
if self.name == None:
return default
return self.name
def setname(self, arg):
self.name = arg
def gethost(self, default = None):
if self.host == None:
return default
return self.host
def sethost(self, arg):
self.host = arg
def getport(self, default = None):
if self.port == None:
return default
return self.port
def setport(self, arg):
self.port = arg
def getmimetype(self, default = None):
if self.mimetype == None:
return default
return self.mimetype
def getencodedmimetype(self, default = None):
if self.encodedmimetype == None:
return default
return self.encodedmimetype
def setencodedmimetype(self, arg):
self.encodedmimetype = arg
def setmimetype(self, arg):
self.mimetype = arg
def getsize(self, default = None):
if self.size == None:
return default
return self.size
def setsize(self, arg):
self.size = arg
def getencoding(self, default = None):
if self.encoding == None:
return default
return self.encoding
def setencoding(self, arg):
self.encoding = arg
def getlanguage(self, default = None):
if self.language == None:
return default
return self.language
def setlanguage(self, arg):
self.language = arg
def getctime(self, default = None):
if self.ctime == None:
return default
return self.ctime
def setctime(self, arg):
self.ctime = arg
def getmtime(self, default = None):
if self.mtime == None:
return default
return self.mtime
def setmtime(self, arg):
self.mtime = arg
def getpopulated(self, default = None):
if self.populated != None:
return self.populated
return default
def setpopulated(self, arg):
self.populated = arg
def geturl(self, defaulthost = 'MISSINGHOST', defaultport = 70):
"""If this selector is a URL: one, then we just return the rest of
it. Otherwise, generate a gopher:// URL and quote it."""
if re.search("^(/|)URL:.+://", self.selector):
if self.selector[0] == '/':
return self.selector[5:]
else:
return self.selector[4:]
retval = 'gopher://%s:%d/' % (self.gethost(defaulthost),
self.getport(defaultport))
retval += urllib.quote('%s%s' % (self.gettype(), self.getselector()))
return retval
def getnum(self, default = None):
if self.num != None:
return self.num
return default
def setnum(self, arg):
self.num = arg
def getgopherpsupport(self, default = None):
if self.gopherpsupport != None:
return self.gopherpsupport
return default
def setgopherpsupport(self, arg):
self.gopherpsupport = arg
def getea(self, name, default = None):
if self.ea.has_key(name):
return self.ea[name]
return default
def geteadict(self):
return self.ea
def setea(self, name, value):
self.ea[name] = value
def getinfoentry(text, config):
entry = GopherEntry('fake', config)
entry.name = text
entry.host = '(NULL)'
entry.port = 0
entry.type = 'i'
return entry
|