Erik Grinaker is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againRevelation / src / lib / io.py
- Tag
- revelation-0.3.1
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 | #
# Revelation 0.3.1 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module for IO-related functionality
#
#
# Copyright (c) 2003-2004 Erik Grinaker
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import revelation, gobject, os, os.path
class DetectError(Exception):
"Error for failed filetype detection"
class DataFile(gobject.GObject):
"Processes data files"
def __init__(self, file = None, handler = revelation.datahandler.Revelation, password = None):
gobject.GObject.__init__(self)
self.file = file
self.password = password
self.handler = None
if handler is not None:
self.handler = handler()
def check_file(self):
"Checks if a file is valid for loading"
if not file_exists(self.file):
raise IOError
data = file_read(self.file, 4096)
self.handler.check_data(data)
def detect_type(self):
"Attempts to find a suitable handler for the file"
data = file_read(self.file, 4096)
for handler in revelation.datahandler.get_import_handlers():
handler = handler()
if handler.detect_type(data):
self.handler = handler
return handler
else:
raise DetectError
def load(self):
"Loads data from a file into an entrystore"
self.check_file()
data = file_read(self.file)
if self.needs_password():
entrystore = self.handler.import_data(data, self.password)
else:
entrystore = self.handler.import_data(data)
entrystore.set_file(self.file, self.password)
return entrystore
def needs_password(self):
"Checks if the current data handler requires a password"
return self.handler.encryption
def save(self, entrystore):
"Saves data from an entrystore to a file"
if self.needs_password():
data = self.handler.export_data(entrystore, self.password)
else:
data = self.handler.export_data(entrystore)
file_write(self.file, data)
def dir_create(dir):
"Creates a directory, and parents if needed"
try:
if dir is None:
raise IOError
dir = os.path.abspath(dir)
if not file_exists(dir):
os.makedirs(dir)
except OSError:
raise IOError
def execute(command, input = None):
"Runs a command, returns its status code and output"
p = os.popen(command)
if input is not None:
p.write(input)
output = p.read()
status = p.close()
return output, status
def file_exists(file):
"Checks if a file exists"
file = os.path.abspath(file)
return os.access(file, os.F_OK)
def file_read(file, bytes = -1):
"Reads data from a file"
if file is None:
raise IOError
file = os.path.abspath(file)
fp = open(file, "rb", 0)
data = fp.read(bytes)
fp.close()
return data
def file_write(file, data):
"Writes data to a file"
if file is None:
raise IOError
file = os.path.abspath(file)
# create directory if needed
dir_create(os.path.dirname(file))
fp = open(file, "wb", 0)
fp.write(data)
fp.flush()
fp.close()
|