/usr/share/pyshared/sponge/core/io.py is in python-sponge 0.3.1-1.
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 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# <Sponge - Lightweight Web Framework>
# Copyright (C) 2009 Gabriel Falcão <gabriel@nacaolivre.org>
# Copyright (C) 2009 Bernardo Heynemann <heynemann@gmail.com>
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
import os
import sys
import codecs
import fnmatch
import zipfile
from glob import glob
from os.path import abspath, join, dirname, curdir, exists
class FileSystem(object):
stack = []
def __init__(self):
self.stack = []
@classmethod
def pushd(cls, path):
if not len(cls.stack):
cls.stack.append(cls.current_dir())
cls.stack.append(path)
os.chdir(path)
@classmethod
def popd(cls):
if cls.stack:
cls.stack.pop()
if cls.stack:
os.chdir(cls.stack[-1])
@classmethod
def filename(cls, path, with_extension=True):
fname = os.path.split(path)[1]
if not with_extension:
fname = os.path.splitext(fname)[0]
return fname
@classmethod
def exists(cls, path):
return exists(path)
@classmethod
def mkdir(cls, path):
try:
os.makedirs(path)
except OSError, e:
# ignore if path already exists
if e.errno not in (17, ):
raise e
else:
if not os.path.isdir(path):
# but the path must be a dir to ignore its creation
raise e
@classmethod
def current_dir(cls, path=""):
'''Returns the absolute path for current dir, also join the
current path with the given, if so.'''
to_return = cls.abspath(curdir)
if path:
return cls.join(to_return, path)
return to_return
@classmethod
def abspath(cls, path):
'''Returns the absolute path for the given path.'''
return abspath(path)
@classmethod
def join(cls, *args):
'''Returns the concatenated path for the given arguments.'''
return join(*args)
@classmethod
def dirname(cls, path):
'''Returns the directory name for the given file.'''
return dirname(path)
@classmethod
def walk(cls, path):
'''Walks through filesystem'''
return os.walk(path)
@classmethod
def locate(cls, path, match, recursive=True):
root_path = cls.abspath(path)
if recursive:
return_files = []
for path, dirs, files in cls.walk(root_path):
for filename in fnmatch.filter(files, match):
return_files.append(cls.join(path, filename))
return return_files
else:
return glob(cls.join(root_path, match))
@classmethod
def extract_zip(cls, filename, base_path='.', verbose=False):
base_path = cls.abspath(base_path)
output = lambda x: verbose and sys.stdout.write("%s\n" % x)
cls.pushd(base_path)
zfile = zipfile.ZipFile(filename)
output("Extracting files to %s" % base_path)
for file_name in zfile.namelist():
try:
output(" -> Unpacking %s" % file_name)
f = cls.open_raw(file_name, 'w')
f.write(zfile.read(file_name))
f.close()
except IOError:
output("---> Creating directory %s" % file_name)
cls.mkdir(file_name)
cls.popd()
@classmethod
def open(cls, name, mode):
path = name
if not os.path.isabs(path):
path = cls.current_dir(name)
return codecs.open(path, mode, 'utf-8')
@classmethod
def open_raw(cls, name, mode):
path = name
if not os.path.isabs(path):
path = cls.current_dir(name)
return open(path, mode)
class ClassLoader(object):
def __init__(self, path):
if not isinstance(path, basestring):
raise TypeError, 'ClassLoader takes a string ' \
'as path parameter, got %s.' % repr(path)
if not os.path.isdir(path):
dir_name, file_name = os.path.split(path)
module_name = os.path.splitext(file_name)[0]
else:
dir_name, module_name = os.path.split(path.rstrip('/'))
sys.path.append(dir_name)
try:
self.module = __import__(module_name)
except ImportError, e:
raise ImportError, \
'There is no module %s at %s. \nThe reason is: %s' % (module_name,
dir_name,
unicode(e))
sys.path.pop()
def get_module(self):
return self.module
def load(self, classname):
return getattr(self.module, classname)
|