~keryx-admins/keryx/1.0

« back to all changes in this revision

Viewing changes to src/libkeryx/definitions/apt_def/minideblib/DpkgDatalist.py

  • Committer: Chris Oliver
  • Date: 2010-01-01 05:23:24 UTC
  • Revision ID: excid3@gmail.com-20100101052324-a3es298620cpivcm
Added minideblib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# DpkgDatalist.py
 
2
#
 
3
# This module implements DpkgDatalist, an abstract class for storing 
 
4
# a list of objects in a file. Children of this class have to implement
 
5
# the load and _store methods.
 
6
#
 
7
# Copyright 2001 Wichert Akkerman <wichert@linux.com>
 
8
#
 
9
# This file is free software; you can redistribute it and/or modify it
 
10
# under the terms of the GNU General Public License as published by
 
11
# the Free Software Foundation; either version 2 of the License, or
 
12
# (at your option) any later version.
 
13
#
 
14
# This program is distributed in the hope that it will be useful, but
 
15
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
# General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this program; if not, write to the Free Software
 
21
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
22
 
 
23
import os, sys
 
24
from UserDict import UserDict
 
25
from OrderedDict import OrderedDict
 
26
import SafeWriteFile
 
27
from types import StringType
 
28
 
 
29
class DpkgDatalistException(Exception):
 
30
    UNKNOWN     = 0
 
31
    SYNTAXERROR = 1
 
32
 
 
33
    def __init__(self, message="", reason=UNKNOWN, file=None, line=None):
 
34
        self.message=message
 
35
        self.reason=reason
 
36
        self.filename=file
 
37
        self.line=line
 
38
 
 
39
class _DpkgDatalist:
 
40
    def __init__(self, fn=""):
 
41
        '''Initialize a DpkgDatalist object. An optional argument is a
 
42
        file from which we load values.'''
 
43
 
 
44
        self.filename=fn
 
45
        if self.filename:
 
46
            self.load(self.filename)
 
47
 
 
48
    def store(self, fn=None):
 
49
        "Store variable data in a file."
 
50
 
 
51
        if fn==None:
 
52
            fn=self.filename
 
53
        # Special case for writing to stdout
 
54
        if not fn:
 
55
            self._store(sys.stdout)
 
56
            return
 
57
 
 
58
        # Write to a temporary file first
 
59
        if type(fn) == StringType:
 
60
            vf=SafeWriteFile(fn+".new", fn, "w")
 
61
        else:
 
62
            vf=fn
 
63
        try:
 
64
            self._store(vf)
 
65
        finally:
 
66
            if type(fn) == StringType:
 
67
                vf.close()
 
68
 
 
69
 
 
70
class DpkgDatalist(UserDict, _DpkgDatalist):
 
71
    def __init__(self, fn=""):
 
72
        UserDict.__init__(self)
 
73
        _DpkgDatalist.__init__(self, fn)
 
74
 
 
75
 
 
76
class DpkgOrderedDatalist(OrderedDict, _DpkgDatalist):
 
77
    def __init__(self, fn=""):
 
78
        OrderedDict.__init__(self)
 
79
        _DpkgDatalist.__init__(self, fn)
 
80
 
 
81
# vim:ts=4:sw=4:et: