Code Repositories xandikos / 59685b1
Support file based configuration. Jelmer Vernooń≥ 29 days ago
3 changed file(s) with 47 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
4949 class FileBasedCollectionMetadata(CollectionMetadata):
5050 """Metadata for a configuration."""
5151
52 def __init__(self, cp=None):
52 def __init__(self, cp=None, save=None):
5353 if cp is None:
5454 cp = configparser.ConfigParser()
5555 self._configparser = cp
56 self._save_cb = save
57
58 def _save(self):
59 if self._save_cb is None:
60 return
61 self._save_cb(self._configparser)
5662
5763 @classmethod
5864 def from_file(cls, f):
7783 self._configparser['DEFAULT']['color'] = color
7884 else:
7985 del self._configparser['DEFAULT']['color']
86 self._save()
8087
8188 def set_displayname(self, displayname):
8289 if displayname is not None:
8390 self._configparser['DEFAULT']['displayname'] = displayname
8491 else:
8592 del self._configparser['DEFAULT']['displayname']
93 self._save()
8694
8795 def set_description(self, description):
8896 if description is not None:
8997 self._configparser['DEFAULT']['description'] = description
9098 else:
9199 del self._configparser['DEFAULT']['description']
100 self._save()
92101
93102 def set_comment(self, comment):
94103 if comment is not None:
95104 self._configparser['DEFAULT']['comment'] = comment
96105 else:
97106 del self._configparser['DEFAULT']['comment']
107 self._save()
1919 """Git store.
2020 """
2121
22 import configparser
2223 from io import BytesIO
2324 import logging
2425 import os
4041 open_by_extension,
4142 )
4243 from .config import (
44 FILENAME as CONFIG_FILENAME,
4345 CollectionMetadata,
4446 FileBasedCollectionMetadata,
4547 )
165167 self._check_for_duplicate_uids = check_for_duplicate_uids
166168 # Set of blob ids that have already been scanned
167169 self._fname_to_uid = {}
168
169 @property
170 def config(self):
171 return FileBasedCollectionMetadata()
172
173 @property
174 def git_config(self):
175 return RepoCollectionMetadata(self.repo)
170 try:
171 cf = self._get_raw(CONFIG_FILENAME)
172 except KeyError:
173 self.config = RepoCollectionMetadata(self.repo)
174 else:
175 cp = configparser.ConfigParser()
176 cp.read_string(cf)
177
178 def save_config(cp):
179 f = BytesIO()
180 cp.write(f)
181 self._import_one(
182 CONFIG_FILENAME, f.getvalue(), "Update configuration")
183 self.config = FileBasedCollectionMetadata(cp, save=save_config)
176184
177185 def __repr__(self):
178186 return "%s(%r, ref=%r)" % (type(self).__name__, self.repo, self.ref)
339347 try:
340348 return self.config.get_description()
341349 except KeyError:
342 try:
343 return self.git_config.get_description()
344 except KeyError:
345 return None
350 return None
346351
347352 def set_description(self, description):
348353 """Set extended description.
349354
350355 :param description: repository description as string
351356 """
352 self.git_config.set_description(description)
357 self.config.set_description(description)
353358
354359 def set_comment(self, comment):
355360 """Set comment.
356361
357362 :param comment: Comment
358363 """
359 self.git_config.set_comment(comment)
364 self.config.set_comment(comment)
360365
361366 def get_comment(self):
362367 """Get comment.
366371 try:
367372 return self.config.get_comment()
368373 except KeyError:
369 try:
370 return self.git_config.get_comment()
371 except KeyError:
372 return None
374 return None
373375
374376 def get_color(self):
375377 """Get color.
379381 try:
380382 return self.config.get_color()
381383 except KeyError:
382 try:
383 return self.git_config.get_color()
384 except KeyError:
385 return None
384 return None
386385
387386 def set_color(self, color):
388387 """Set the color code for this store."""
389 self.git_config.set_color(color)
388 self.config.set_color(color)
390389
391390 def get_displayname(self):
392391 """Get display name.
396395 try:
397396 return self.config.get_displayname()
398397 except KeyError:
399 try:
400 return self.git_config.get_displayname()
401 except KeyError:
402 return None
398 return None
403399
404400 def set_displayname(self, displayname):
405401 """Set the display name.
406402
407403 :param displayname: New display name
408404 """
409 self.git_config.set_displayname(displayname)
405 self.config.set_displayname(displayname)
410406
411407 def set_type(self, store_type):
412408 """Set store type.
2121 See https://github.com/pimutils/vdirsyncer/blob/master/docs/vdir.rst
2222 """
2323
24 import configparser
2425 import errno
2526 import hashlib
2627 import logging
3839 open_by_content_type,
3940 open_by_extension,
4041 )
41 from .config import FileBasedCollectionMetadata
42 from .config import (
43 FileBasedCollectionMetadata,
44 FILENAME as CONFIG_FILENAME,
45 )
4246
4347
4448 DEFAULT_ENCODING = 'utf-8'
5963 self._fname_to_uid = {}
6064 # Maps uids to (sha, fname)
6165 self._uid_to_fname = {}
62
63 @property
64 def config(self):
65 return FileBasedCollectionMetadata()
66 cp = configparser.ConfigParser()
67 cp.read([os.path.join(self.path, CONFIG_FILENAME)])
68
69 def save_config(cp):
70 with open(os.path.join(self.path, CONFIG_FILENAME), 'wb') as f:
71 cp.write(f)
72 self.config = FileBasedCollectionMetadata(cp, save=save_config)
6673
6774 def __repr__(self):
6875 return "%s(%r)" % (type(self).__name__, self.path)