File: Directory.py

package info (click to toggle)
pyrite 0.9.3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,504 kB
  • ctags: 1,924
  • sloc: python: 6,064; ansic: 5,094; makefile: 275; sh: 172
file content (158 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download
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
#
#  $Id: Directory.py,v 1.12 1999/12/11 12:35:12 rob Exp $
#
#  Copyright 1999 Rob Tillotson <robt@debian.org>
#  All Rights Reserved
#
#  Permission to use, copy, modify, and distribute this software and
#  its documentation for any purpose and without fee or royalty is
#  hereby granted, provided that the above copyright notice appear in
#  all copies and that both the copyright notice and this permission
#  notice appear in supporting documentation or portions thereof,
#  including modifications, that you you make.
#
#  THE AUTHOR ROB TILLOTSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
#  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
#  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
#  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE!
#
"""Store in a local directory.  (Pure version)
"""

__version__ = '$Id: Directory.py,v 1.12 1999/12/11 12:35:12 rob Exp $'

__copyright__ = 'Copyright 1999 Rob Tillotson <robt@debian.org>'

import os, time

import Pyrite
import Pyrite.Store
from Pyrite import protect_name
from Pyrite import pdbfile, Database
from Pyrite import _

from Pyrite.prc import File

class DirectoryStore(Pyrite.Store.BaseStore):
    properties = ('read','create','delete','list')
    db_properties = ('id-unique', 'id-replace')
    
    def __init__(self, path='.'):
	Pyrite.Store.BaseStore.__init__(self)

	self.path = path

    def _open(self, name, mode='rs', properties=()):
	# this will be another inner API that we can put in a distributed object
	# return value is (inner-db-object, info-structure, mode, properties)
	# suitable for directly passing to the dbclass for instantiation
	if 'w' in mode:
	    raise RuntimeError, _("cannot open an existing database for write")
	t = self._find_db(name)
	if t is None: raise IOError, _("no such database: %s") % name
	return (t[0], t[1], mode, self.db_properties)
    
    def _create(self, name, creator, type, flags=0, version=1, filename=None,
		info=None):
	if flags is None: flags = 0
	i = { 'name': name,
	      'creator': creator,
	      'type': type,
	      'index': 0,
	      'more': 0,
	      'modnum': 0,
	      'version': version,
	      'createDate': int(time.time()),
	      'modifyDate': int(time.time()),
	      'backupDate': 0,
	      }
	i.update(pdbfile.flags_to_info(flags))
	if info is not None: i.update(info)
	if len(i['name']) > 31: i['name'] = i['name'][:31]

	if not filename:
	    filename = protect_name(name)+(flags & 0x0001 and '.prc' or '.pdb')
	f = File(os.path.join(self.path, filename), 0, 1, i)
	info = f.getDBInfo()
	return (f, info, 'rws', self.db_properties)
    
    def delete(self, name):
	db, i, n = self._find_db(name)
	if db is None: raise IOError, _("no such database: %s") % name
	db.close()
	os.unlink(os.path.join(self.path, n))

    def info(self, name):
	db, i, n = self._find_db(name)
	if db is None: raise IOError, _("no such database: %s") % name
	return db.info

    def list(self):
	return map(lambda x: x[1]['name'], pdbfile.listdir(self.path))

    def listinfo(self, name=None, creator=None, type=None):
	l = []
	for i in map(lambda x: x[1], pdbfile.listdir(self.path)):
	    if name is not None and i['name'] != name: continue
	    if creator is not None and i['creator'] != creator: continue
	    if type is not None and i['type'] != type: continue
	    l.append(i)

	return l	    

    def getpref(self, creator, id, saved=1):
	if saved: db, i, n = self._find_db('Saved Preferences')
	else: db, i, n = self._find_db('Unsaved Preferences')
	if db is None: raise RuntimeError, _("preferences database not found")

	for r in db:
	    if r.type == creator and r.id == id:
		v = struct.unpack('>H',r.raw[:2])[0]
		return r.raw[2:], v

	raise RuntimeError, _("preference '%s' %s not found") % (creator, id)

    # new stuff in preparation for dynamic objects
    def _try_open_db(self, fname, dbname=None):
	try:
	    f = File(fname)
	    i = f.getDBInfo()

	    if dbname is None or i['name'] == dbname: return (f, i)
	    else: return None
	except:
	    return None

    def _find_db(self, name):
	# %(name).pdb
	n = protect_name(name)+'.pdb'
	t = self._try_open_db(os.path.join(self.path,n), name)
	if t is not None: return t+(n,)
	# %(name).prc
	n = protect_name(name)+'.prc'
	t = self._try_open_db(os.path.join(self.path,n), name)
	if t is not None: return t+(n,)
	# %(name)
	n = protect_name(name)
	t = self._try_open_db(os.path.join(self.path,n), name)
	if t is not None: return t+(n,)
	# other arbitrary files
	for n, i in pdbfile.listdir(self.path):
	    if i['name'] == name:
		t = self._try_open_db(os.path.join(self.path,n), name)
		if t is not None: return t+(n,)
	return None
    # end new stuff
	

class Store(Pyrite.Store.Store):
    name = 'Directory'
    author = Pyrite.author
    version = Pyrite.version
    description = _("PRC/PDB files in a directory.")
    properties = ['read','create','delete','list']

    store_class = DirectoryStore