cctool

A tool for managing contacts and calendars.
git clone https://git.ce9e.org/cctool.git

commit
e2d5a20dde83949cc3801aad579caad9151cbe40
parent
f5cc47d3b51423122900e5baf1991be9f2bdcc0b
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2015-05-08 15:32
add yaml formatter

Diffstat

M cctool.py 25 +++++++++++++++++++++++++
M setup.py 1 +
M tests.py 7 +++++++
M tox.ini 1 +

4 files changed, 34 insertions, 0 deletions


diff --git a/cctool.py b/cctool.py

@@ -53,6 +53,11 @@ try:
   53    53 except ImportError as err:
   54    54 	icalendar = err
   55    55 
   -1    56 try:
   -1    57 	import yaml
   -1    58 except ImportError as err:
   -1    59 	yaml = err
   -1    60 
   56    61 
   57    62 NOTSET = object()
   58    63 
@@ -81,6 +86,9 @@ def formats():
   81    86 		outformats['ics'] = ICal
   82    87 	if not isinstance(ldif, Exception):
   83    88 		informats['ldif'] = LDIF
   -1    89 	if not isinstance(yaml, Exception):
   -1    90 		informats['yml'] = YAML
   -1    91 		outformats['yml'] = YAML
   84    92 	return informats, outformats
   85    93 
   86    94 
@@ -375,6 +383,23 @@ class JSON(Format):
  375   383 		json.dump(list(data), _fh, indent=4, cls=DateTimeJSONEncoder)
  376   384 
  377   385 
   -1   386 class YAML(Format):
   -1   387 	@classmethod
   -1   388 	def load(cls, fh):
   -1   389 		if isinstance(yaml, Exception):
   -1   390 			raise yaml
   -1   391 
   -1   392 		return [MultiDict(d) for d in yaml.load(fh.read())]
   -1   393 
   -1   394 	@classmethod
   -1   395 	def dump(cls, data, fh):
   -1   396 		if isinstance(yaml, Exception):
   -1   397 			raise yaml
   -1   398 
   -1   399 		_fh = codecs.getwriter('utf8')(fh)
   -1   400 		_fh.write(yaml.safe_dump([dict(d) for d in data]))
   -1   401 
   -1   402 
  378   403 class Pickle(Format):
  379   404 	@classmethod
  380   405 	def load(cls, fh):

diff --git a/setup.py b/setup.py

@@ -13,6 +13,7 @@ setup(
   13    13     extras_require={
   14    14         'ldif': ['python-ldap'],
   15    15         'ical': ['icaledar'],
   -1    16         'yaml': ['PyYAML'],
   16    17     },
   17    18     license='GPLv3+',
   18    19     entry_points={'console_scripts': 'cctool=cctool:main'},

diff --git a/tests.py b/tests.py

@@ -143,6 +143,13 @@ class TestPickle(_TestFormat):
  143   143 		pass
  144   144 
  145   145 
   -1   146 @unittest.skipIf(isinstance(cctool.yaml, Exception), 'yaml not available')
   -1   147 class TestYAML(_TestFormat):
   -1   148 	def setUp(self):
   -1   149 		self.format = cctool.YAML()
   -1   150 		self.text = b'- name: [foo]\n'
   -1   151 
   -1   152 
  146   153 class TestArgs(unittest.TestCase):
  147   154 	def test_args(self):
  148   155 		args = cctool.parse_args(['-f', 'abook', '-t', 'bsdcal'])

diff --git a/tox.ini b/tox.ini

@@ -15,3 +15,4 @@ deps =
   15    15     coverage
   16    16     flake8
   17    17     icalendar
   -1    18     PyYAML