- commit
- 5f9ad73666a9739096ce58d82cea77accd626bf8
- parent
- d2cd8c9fc8197b9d09b86f50c29f5ebcd66ca257
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2015-04-11 07:04
split functions to reduce complexity
Diffstat
| M | cctool.py | 60 | +++++++++++++++++++++++++++++++++++++++--------------------- |
1 files changed, 39 insertions, 21 deletions
diff --git a/cctool.py b/cctool.py
@@ -316,6 +316,17 @@ class VCard(Format): 316 316 yield d 317 317 318 318 @classmethod -1 319 def _dump_field(cls, key, item, vcard): -1 320 if key == 'name': -1 321 vcard.add('fn').value = item.join(key) -1 322 elif key == 'nick': -1 323 for value in item[key]: -1 324 vcard.add('nickname').value = value -1 325 elif key in cls.fields: -1 326 for value in item[key]: -1 327 vcard.add(key).value = value -1 328 -1 329 @classmethod 319 330 def dump(cls, data, fh): 320 331 if isinstance(vobject, Exception): 321 332 raise vobject @@ -324,14 +335,7 @@ class VCard(Format): 324 335 vcard = vobject.vCard() 325 336 vcard.add('n').value = '' 326 337 for key in item:327 -1 if key == 'name':328 -1 vcard.add('fn').value = item.join(key)329 -1 elif key == 'nick':330 -1 for value in item[key]:331 -1 vcard.add('nickname').value = value332 -1 elif key in cls.fields:333 -1 for value in item[key]:334 -1 vcard.add(key).value = value-1 338 cls._dump_field(key, item, vcard) 335 339 vcard.serialize(fh) 336 340 337 341 @@ -370,32 +374,46 @@ def parse_args(argv=None): 370 374 return parser.parse_args(argv) 371 375 372 376373 -1 def main():-1 377 def get_outformat(args): 374 378 informats, outformats = formats() 375 379376 -1 args = parse_args()377 -1378 -1 if args.outformat is None and args.output is not None:-1 380 if args.outformat is not None: -1 381 return args.outformat -1 382 elif args.output is not None: 379 383 ext = args.output.split(os.path.extsep)[-1] 380 384 if ext in outformats:381 -1 args.outformat = ext382 -1 if args.outformat is None:383 -1 print("Missing output format")-1 385 return ext -1 386 -1 387 print("Missing output format") -1 388 sys.exit(1) -1 389 -1 390 -1 391 def get_informat(filename): -1 392 informats, outformats = formats() -1 393 ext = filename.split(os.path.extsep)[-1] -1 394 -1 395 if ext in informats: -1 396 return ext -1 397 else: -1 398 print("Missing input format") 384 399 sys.exit(1) 385 400 -1 401 -1 402 def main(): -1 403 informats, outformats = formats() -1 404 args = parse_args() -1 405 386 406 reload(sys) 387 407 sys.setdefaultencoding('utf-8') 388 408 -1 409 outformat = get_outformat(args) -1 410 389 411 data = [] 390 412 for fn in args.input:391 -1 ext = fn.split(os.path.extsep)[-1]392 413 if args.informat is not None: 393 414 informat = args.informat394 -1 elif ext in informats:395 -1 informat = ext396 415 else:397 -1 print("Missing input format")398 -1 sys.exit(1)-1 416 informat = get_informat(fn) 399 417 400 418 infile = sys.stdin if fn == '-' else open(fn) 401 419 try: @@ -414,7 +432,7 @@ def main(): 414 432 415 433 outfile = sys.stdout if args.output is None else open(args.output, 'w') 416 434 try:417 -1 outformats[args.outformat]().dump(data, outfile)-1 435 outformats[outformat]().dump(data, outfile) 418 436 except Exception as e: 419 437 log.error(e) 420 438 sys.exit(1)