<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> from . import Journal <EOL> from . import DayOneJournal <EOL> from . import util <EOL> from . import exporters <EOL> from . import install <EOL> import jrnl <EOL> import os <EOL> import argparse <EOL> import sys <EOL> import logging <EOL> xdg_config = os . environ . get ( '<STR_LIT>' ) <EOL> CONFIG_PATH = os . path . join ( xdg_config , "<STR_LIT>" ) if xdg_config else os . path . expanduser ( '<STR_LIT>' ) <EOL> PYCRYPTO = install . module_exists ( "<STR_LIT>" ) <EOL> log = logging . getLogger ( __name__ ) <EOL> def parse_args ( args = None ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:version>' , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> composing = parser . add_argument_group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> composing . add_argument ( '<STR_LIT:text>' , metavar = '<STR_LIT>' , nargs = "<STR_LIT:*>" ) <EOL> reading = parser . add_argument_group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = "<STR_LIT>" , help = '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = "<STR_LIT>" , help = '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = "<STR_LIT>" , help = '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , dest = '<STR_LIT:strict>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> reading . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , default = None , metavar = "<STR_LIT:N>" , help = "<STR_LIT>" , nargs = "<STR_LIT:?>" , type = int ) <EOL> exporting = parser . add_argument_group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> exporting . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> exporting . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> exporting . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , dest = '<STR_LIT>' , choices = [ '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , help = '<STR_LIT>' , default = False , const = None ) <EOL> exporting . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' , default = False , const = None ) <EOL> exporting . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = False , const = None ) <EOL> exporting . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = False , const = None ) <EOL> exporting . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' , action = "<STR_LIT:store_true>" ) <EOL> return parser . parse_args ( args ) <EOL> def guess_mode ( args , config ) : <EOL> """<STR_LIT>""" <EOL> compose = True <EOL> export = False <EOL> if args . decrypt is not False or args . encrypt is not False or args . export is not False or any ( ( args . short , args . tags , args . edit ) ) : <EOL> compose = False <EOL> export = True <EOL> elif any ( ( args . start_date , args . end_date , args . on_date , args . limit , args . strict , args . starred ) ) : <EOL> compose = False <EOL> elif args . text and all ( word [ <NUM_LIT:0> ] in config [ '<STR_LIT>' ] for word in "<STR_LIT:U+0020>" . join ( args . text ) . split ( ) ) : <EOL> compose = False <EOL> return compose , export <EOL> def encrypt ( journal , filename = None ) : <EOL> """<STR_LIT>""" <EOL> password = util . getpass ( "<STR_LIT>" ) <EOL> journal . make_key ( password ) <EOL> journal . config [ '<STR_LIT>' ] = True <EOL> journal . write ( filename ) <EOL> if util . yesno ( "<STR_LIT>" , default = True ) : <EOL> util . set_keychain ( journal . name , password ) <EOL> util . prompt ( "<STR_LIT>" . format ( filename or journal . config [ '<STR_LIT>' ] ) ) <EOL> def decrypt ( journal , filename = None ) : <EOL> """<STR_LIT>""" <EOL> journal . config [ '<STR_LIT>' ] = False <EOL> journal . config [ '<STR_LIT:password>' ] = "<STR_LIT>" <EOL> journal . write ( filename ) <EOL> util . prompt ( "<STR_LIT>" . format ( filename or journal . config [ '<STR_LIT>' ] ) ) <EOL> def touch_journal ( filename ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( filename ) : <EOL> log . debug ( '<STR_LIT>' , filename ) <EOL> util . prompt ( "<STR_LIT>" . format ( filename ) ) <EOL> open ( filename , '<STR_LIT:a>' ) . close ( ) <EOL> def list_journals ( config ) : <EOL> """<STR_LIT>""" <EOL> sep = "<STR_LIT:\n>" <EOL> journal_list = sep . join ( config [ '<STR_LIT>' ] ) <EOL> return journal_list <EOL> def update_config ( config , new_config , scope , force_local = False ) : <EOL> """<STR_LIT>""" <EOL> if scope and type ( config [ '<STR_LIT>' ] [ scope ] ) is dict : <EOL> config [ '<STR_LIT>' ] [ scope ] . update ( new_config ) <EOL> elif scope and force_local : <EOL> config [ '<STR_LIT>' ] [ scope ] = { "<STR_LIT>" : config [ '<STR_LIT>' ] [ scope ] } <EOL> config [ '<STR_LIT>' ] [ scope ] . update ( new_config ) <EOL> else : <EOL> config . update ( new_config ) <EOL> def configure_logger ( debug = False ) : <EOL> logging . basicConfig ( level = logging . DEBUG if debug else logging . INFO , <EOL> format = '<STR_LIT>' ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . INFO ) <EOL> def run ( manual_args = None ) : <EOL> args = parse_args ( manual_args ) <EOL> configure_logger ( args . debug ) <EOL> args . text = [ p . decode ( '<STR_LIT:utf-8>' ) if util . PY2 and not isinstance ( p , unicode ) else p for p in args . text ] <EOL> if args . version : <EOL> version_str = "<STR_LIT>" . format ( jrnl . __title__ , jrnl . __version__ ) <EOL> print ( util . py2encode ( version_str ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if not os . path . exists ( CONFIG_PATH ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> config = install . install_jrnl ( CONFIG_PATH ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' , CONFIG_PATH ) <EOL> config = util . load_and_fix_json ( CONFIG_PATH ) <EOL> install . upgrade_config ( config , config_path = CONFIG_PATH ) <EOL> if args . ls : <EOL> print ( util . py2encode ( list_journals ( config ) ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> log . debug ( '<STR_LIT>' , config ) <EOL> original_config = config . copy ( ) <EOL> if config [ '<STR_LIT>' ] and not PYCRYPTO : <EOL> util . prompt ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> journal_name = args . text [ <NUM_LIT:0> ] if ( args . text and args . text [ <NUM_LIT:0> ] in config [ '<STR_LIT>' ] ) else '<STR_LIT:default>' <EOL> if journal_name is not '<STR_LIT:default>' : <EOL> args . text = args . text [ <NUM_LIT:1> : ] <EOL> if not args . limit and args . text and args . text [ <NUM_LIT:0> ] . startswith ( "<STR_LIT:->" ) : <EOL> try : <EOL> args . limit = int ( args . text [ <NUM_LIT:0> ] . lstrip ( "<STR_LIT:->" ) ) <EOL> args . text = args . text [ <NUM_LIT:1> : ] <EOL> except : <EOL> pass <EOL> log . debug ( '<STR_LIT>' , journal_name ) <EOL> journal_conf = config [ '<STR_LIT>' ] . get ( journal_name ) <EOL> if type ( journal_conf ) is dict : <EOL> log . debug ( '<STR_LIT>' , journal_conf ) <EOL> config . update ( journal_conf ) <EOL> else : <EOL> config [ '<STR_LIT>' ] = journal_conf <EOL> if config [ '<STR_LIT>' ] is None : <EOL> util . prompt ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> config [ '<STR_LIT>' ] = os . path . expanduser ( os . path . expandvars ( config [ '<STR_LIT>' ] ) ) <EOL> touch_journal ( config [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' , config ) <EOL> mode_compose , mode_export = guess_mode ( args , config ) <EOL> if os . path . isdir ( config [ '<STR_LIT>' ] ) : <EOL> if config [ '<STR_LIT>' ] . strip ( "<STR_LIT:/>" ) . endswith ( "<STR_LIT>" ) or "<STR_LIT>" in os . listdir ( config [ '<STR_LIT>' ] ) : <EOL> journal = DayOneJournal . DayOne ( ** config ) <EOL> else : <EOL> util . prompt ( "<STR_LIT>" . format ( config [ '<STR_LIT>' ] ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> journal = Journal . Journal ( journal_name , ** config ) <EOL> if "<STR_LIT:win32>" in sys . platform : <EOL> _exit_multiline_code = "<STR_LIT>" <EOL> else : <EOL> _exit_multiline_code = "<STR_LIT>" <EOL> if mode_compose and not args . text : <EOL> if not sys . stdin . isatty ( ) : <EOL> raw = util . py23_read ( ) <EOL> elif config [ '<STR_LIT>' ] : <EOL> raw = util . get_text_from_editor ( config ) <EOL> else : <EOL> try : <EOL> raw = util . py23_read ( "<STR_LIT>" + _exit_multiline_code + "<STR_LIT>" ) <EOL> except KeyboardInterrupt : <EOL> util . prompt ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if raw : <EOL> args . text = [ raw ] <EOL> else : <EOL> mode_compose = False <EOL> if mode_compose : <EOL> raw = "<STR_LIT:U+0020>" . join ( args . text ) . strip ( ) <EOL> if util . PY2 and type ( raw ) is not unicode : <EOL> raw = raw . decode ( sys . getfilesystemencoding ( ) ) <EOL> log . debug ( '<STR_LIT>' , raw , journal_name ) <EOL> journal . new_entry ( raw ) <EOL> util . prompt ( "<STR_LIT>" . format ( journal_name ) ) <EOL> journal . write ( ) <EOL> else : <EOL> old_entries = journal . entries <EOL> if args . on_date : <EOL> args . start_date = args . end_date = args . on_date <EOL> journal . filter ( tags = args . text , <EOL> start_date = args . start_date , end_date = args . end_date , <EOL> strict = args . strict , <EOL> short = args . short , <EOL> starred = args . starred ) <EOL> journal . limit ( args . limit ) <EOL> if not mode_compose and not mode_export : <EOL> print ( util . py2encode ( journal . pprint ( ) ) ) <EOL> elif args . short : <EOL> print ( util . py2encode ( journal . pprint ( short = True ) ) ) <EOL> elif args . tags : <EOL> print ( util . py2encode ( exporters . to_tag_list ( journal ) ) ) <EOL> elif args . export is not False : <EOL> print ( util . py2encode ( exporters . export ( journal , args . export , args . output ) ) ) <EOL> elif ( args . encrypt is not False or args . decrypt is not False ) and not PYCRYPTO : <EOL> util . prompt ( "<STR_LIT>" ) <EOL> elif args . encrypt is not False : <EOL> encrypt ( journal , filename = args . encrypt ) <EOL> if not args . encrypt or args . encrypt == config [ '<STR_LIT>' ] : <EOL> update_config ( original_config , { "<STR_LIT>" : True } , journal_name , force_local = True ) <EOL> install . save_config ( original_config , config_path = CONFIG_PATH ) <EOL> elif args . decrypt is not False : <EOL> decrypt ( journal , filename = args . decrypt ) <EOL> if not args . decrypt or args . decrypt == config [ '<STR_LIT>' ] : <EOL> update_config ( original_config , { "<STR_LIT>" : False } , journal_name , force_local = True ) <EOL> install . save_config ( original_config , config_path = CONFIG_PATH ) <EOL> elif args . edit : <EOL> if not config [ '<STR_LIT>' ] : <EOL> util . prompt ( "<STR_LIT>" . format ( CONFIG_PATH ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> other_entries = [ e for e in old_entries if e not in journal . entries ] <EOL> old_num_entries = len ( journal ) <EOL> edited = util . get_text_from_editor ( config , journal . editable_str ( ) ) <EOL> journal . parse_editable_str ( edited ) <EOL> num_deleted = old_num_entries - len ( journal ) <EOL> num_edited = len ( [ e for e in journal . entries if e . modified ] ) <EOL> prompts = [ ] <EOL> if num_deleted : <EOL> prompts . append ( "<STR_LIT>" . format ( num_deleted , "<STR_LIT>" if num_deleted == <NUM_LIT:1> else "<STR_LIT>" ) ) <EOL> if num_edited : <EOL> prompts . append ( "<STR_LIT>" . format ( num_edited , "<STR_LIT>" if num_edited == <NUM_LIT:1> else "<STR_LIT>" ) ) <EOL> if prompts : <EOL> util . prompt ( "<STR_LIT>" . format ( "<STR_LIT:U+002CU+0020>" . join ( prompts ) . capitalize ( ) ) ) <EOL> journal . entries += other_entries <EOL> journal . sort ( ) <EOL> journal . write ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run ( ) </s>
