]> git.ipfire.org Git - pakfire.git/blob - python/pakfire/repository/metadata.py
Bump version 0.9.9.
[pakfire.git] / python / pakfire / repository / metadata.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import json
23 import time
24
25 from pakfire.constants import *
26
27 class Metadata(object):
28 def __init__(self, pakfire, index, metafile=None, metadata=None):
29 self.pakfire = pakfire
30 self.index = index
31
32 self.filename = metafile
33
34 # Place where we save the data.
35 self._data = {
36 "database" : None,
37 "revision" : int(time.time()),
38 "version" : METADATA_FORMAT,
39 }
40
41 # If a file was passed, we open it.
42 if self.filename:
43 self.open()
44
45 # ... or parse the one that was passed.
46 elif metadata:
47 self.parse(metadata)
48
49 def __cmp__(self, other):
50 """
51 Compare two sets of metadata by their revision.
52 """
53 return cmp(self.revision, other.revision)
54
55 def parse(self, metadata):
56 try:
57 self._data = json.loads(metadata)
58 except:
59 raise # XXX catch json exceptions here
60
61 def open(self, filename=None):
62 """
63 Open a given file or use the default one and parse the
64 data in it.
65 """
66 if not filename:
67 filename = self.filename
68
69 with open(filename) as f:
70 self.parse(f.read())
71
72 def save(self, filename=None):
73 """
74 Save all data to a file that could be exported to a
75 remote repository.
76 """
77 if not filename:
78 filename = self.filename
79
80 f = open(filename, "w")
81
82 # Write all data to the fileobj.
83 json.dump(self._data, f, indent=2)
84
85 f.close()
86
87 @property
88 def version(self):
89 """
90 Returns the version of the metadata.
91 """
92 return self._data.get("version")
93
94 @property
95 def revision(self):
96 """
97 Returns the revision of the metadata.
98 """
99 return self._data.get("revision")
100
101 def get_database(self):
102 return self._data.get("database")
103
104 def set_database(self, val):
105 self._data["database"] = val
106
107 database = property(get_database, set_database)
108
109 def get_database_hash1(self):
110 return self._data.get("database_hash1", None)
111
112 def set_database_hash1(self, val):
113 self._data["database_hash1"] = val
114
115 database_hash1 = property(get_database_hash1, set_database_hash1)
116
117 def get_database_compression(self):
118 return self._data.get("database_compression", None)
119
120 def set_database_compression(self, val):
121 self._data["database_compression"] = val
122
123 database_compression = property(get_database_compression,
124 set_database_compression)