<s> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> import json <EOL> from . util import u , slugify <EOL> import codecs <EOL> def get_tags_count ( journal ) : <EOL> """<STR_LIT>""" <EOL> tags = [ tag <EOL> for entry in journal . entries <EOL> for tag in set ( entry . tags ) ] <EOL> tag_counts = set ( [ ( tags . count ( tag ) , tag ) for tag in tags ] ) <EOL> return tag_counts <EOL> def to_tag_list ( journal ) : <EOL> """<STR_LIT>""" <EOL> tag_counts = get_tags_count ( journal ) <EOL> result = "<STR_LIT>" <EOL> if not tag_counts : <EOL> return '<STR_LIT>' <EOL> elif min ( tag_counts ) [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> tag_counts = filter ( lambda x : x [ <NUM_LIT:0> ] > <NUM_LIT:1> , tag_counts ) <EOL> result += '<STR_LIT>' <EOL> result += "<STR_LIT:\n>" . join ( "<STR_LIT>" . format ( tag , n ) for n , tag in sorted ( tag_counts , reverse = True ) ) <EOL> return result <EOL> def to_json ( journal ) : <EOL> """<STR_LIT>""" <EOL> tags = get_tags_count ( journal ) <EOL> result = { <EOL> "<STR_LIT>" : dict ( ( tag , count ) for count , tag in tags ) , <EOL> "<STR_LIT>" : [ e . to_dict ( ) for e in journal . entries ] <EOL> } <EOL> return json . dumps ( result , indent = <NUM_LIT:2> ) <EOL> def to_md ( journal ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> year , month = - <NUM_LIT:1> , - <NUM_LIT:1> <EOL> for e in journal . entries : <EOL> if not e . date . year == year : <EOL> year = e . date . year <EOL> out . append ( str ( year ) ) <EOL> out . append ( "<STR_LIT:=>" * len ( str ( year ) ) + "<STR_LIT:\n>" ) <EOL> if not e . date . month == month : <EOL> month = e . date . month <EOL> out . append ( e . date . strftime ( "<STR_LIT>" ) ) <EOL> out . append ( '<STR_LIT:->' * len ( e . date . strftime ( "<STR_LIT>" ) ) + "<STR_LIT:\n>" ) <EOL> out . append ( e . to_md ( ) ) <EOL> result = "<STR_LIT:\n>" . join ( out ) <EOL> return result <EOL> def to_txt ( journal ) : <EOL> """<STR_LIT>""" <EOL> return journal . pprint ( ) <EOL> def export ( journal , format , output = None ) : <EOL> """<STR_LIT>""" <EOL> maps = { <EOL> "<STR_LIT>" : to_json , <EOL> "<STR_LIT>" : to_txt , <EOL> "<STR_LIT:text>" : to_txt , <EOL> "<STR_LIT>" : to_md , <EOL> "<STR_LIT>" : to_md <EOL> } <EOL> if format not in maps : <EOL> return "<STR_LIT>" . format ( format ) <EOL> if output and os . path . isdir ( output ) : <EOL> return write_files ( journal , output , format ) <EOL> else : <EOL> content = maps [ format ] ( journal ) <EOL> if output : <EOL> try : <EOL> with codecs . open ( output , "<STR_LIT:w>" , "<STR_LIT:utf-8>" ) as f : <EOL> f . write ( content ) <EOL> return "<STR_LIT>" . format ( output ) <EOL> except IOError as e : <EOL> return "<STR_LIT>" . format ( e . filename , e . strerror ) <EOL> else : <EOL> return content <EOL> def write_files ( journal , path , format ) : <EOL> """<STR_LIT>""" <EOL> make_filename = lambda entry : e . date . strftime ( "<STR_LIT>" . format ( slugify ( u ( e . title ) ) , format ) ) <EOL> for e in journal . entries : <EOL> full_path = os . path . join ( path , make_filename ( e ) ) <EOL> if format == '<STR_LIT>' : <EOL> content = json . dumps ( e . to_dict ( ) , indent = <NUM_LIT:2> ) + "<STR_LIT:\n>" <EOL> elif format in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> content = e . to_md ( ) <EOL> elif format in ( '<STR_LIT>' , '<STR_LIT:text>' ) : <EOL> content = e . __unicode__ ( ) <EOL> with codecs . open ( full_path , "<STR_LIT:w>" , "<STR_LIT:utf-8>" ) as f : <EOL> f . write ( content ) <EOL> return "<STR_LIT>" . format ( path ) </s>
