<s> from __future__ import unicode_literals <EOL> import os <EOL> from pkg_resources import resource_string <EOL> import sys <EOL> import click <EOL> import six <EOL> from appdirs import AppDirs <EOL> from . types import ExpandedPath , Hostname <EOL> from . . aliases import aliases_database <EOL> from . . backends . registry import backends_registry <EOL> from . . projects import ProjectsDb <EOL> from . . settings import Settings <EOL> from . . timesheet . utils import get_timesheet_collection <EOL> from . . ui . tty import TtyUi <EOL> from . . import __version__ <EOL> xdg_dirs = AppDirs ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> click . disable_unicode_literals_warning = True <EOL> def get_timesheet_collection_for_context ( ctx , entries_file = None ) : <EOL> if not entries_file : <EOL> entries_file = ctx . obj [ '<STR_LIT>' ] . get_entries_file_path ( False ) <EOL> return get_timesheet_collection ( <EOL> entries_file , <EOL> ctx . obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> ctx . obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> ctx . obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> ) <EOL> def populate_aliases ( aliases ) : <EOL> aliases_database . reset ( ) <EOL> aliases_database . update ( aliases ) <EOL> def populate_backends ( backends ) : <EOL> backends_registry . populate ( dict ( backends ) ) <EOL> def create_config_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> import textwrap <EOL> from six . moves . urllib import parse <EOL> if not os . path . exists ( filename ) : <EOL> old_default_config_file = os . path . join ( os . path . dirname ( filename ) , <EOL> '<STR_LIT>' ) <EOL> if os . path . exists ( old_default_config_file ) : <EOL> upgrade = click . confirm ( "<STR_LIT:\n>" . join ( textwrap . wrap ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" , default = True <EOL> ) <EOL> if upgrade : <EOL> settings = Settings ( old_default_config_file ) <EOL> settings . convert_to_4 ( ) <EOL> with open ( filename , '<STR_LIT:w>' ) as config_file : <EOL> settings . config . write ( config_file ) <EOL> os . remove ( old_default_config_file ) <EOL> return <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> response = click . confirm ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % filename , default = True <EOL> ) <EOL> if response : <EOL> config = resource_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) . decode ( '<STR_LIT:utf-8>' ) <EOL> available_backends = backends_registry . _entry_points . keys ( ) <EOL> context = { } <EOL> context [ '<STR_LIT>' ] = click . prompt ( <EOL> "<STR_LIT>" % <EOL> '<STR_LIT:U+002CU+0020>' . join ( available_backends ) , <EOL> type = click . Choice ( available_backends ) <EOL> ) <EOL> context [ '<STR_LIT:username>' ] = click . prompt ( "<STR_LIT>" ) <EOL> context [ '<STR_LIT:password>' ] = parse . quote ( <EOL> click . prompt ( "<STR_LIT>" , hide_input = True ) , <EOL> safe = '<STR_LIT>' <EOL> ) <EOL> context [ '<STR_LIT>' ] = click . prompt ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , type = Hostname ( ) <EOL> ) <EOL> templated_config = config . format ( ** context ) <EOL> directory = os . path . dirname ( filename ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( templated_config ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> settings = Settings ( filename ) <EOL> conversions = settings . needed_conversions <EOL> if conversions : <EOL> for conversion in conversions : <EOL> conversion ( ) <EOL> settings . write_config ( ) <EOL> class AliasedCommand ( click . Command ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . aliases = set ( kwargs . pop ( '<STR_LIT>' , [ ] ) ) <EOL> super ( AliasedCommand , self ) . __init__ ( * args , ** kwargs ) <EOL> class AliasedGroup ( click . Group ) : <EOL> """<STR_LIT>""" <EOL> def get_command ( self , ctx , cmd_name ) : <EOL> rv = super ( AliasedGroup , self ) . get_command ( ctx , cmd_name ) <EOL> if rv is not None : <EOL> return rv <EOL> for name , command in six . iteritems ( self . commands ) : <EOL> if ( isinstance ( command , AliasedCommand ) <EOL> and cmd_name in command . aliases ) : <EOL> return super ( AliasedGroup , self ) . get_command ( ctx , name ) <EOL> matches = [ x for x in self . list_commands ( ctx ) <EOL> if x . startswith ( cmd_name ) ] <EOL> if not matches : <EOL> return None <EOL> elif len ( matches ) == <NUM_LIT:1> : <EOL> return click . Group . get_command ( self , ctx , matches [ <NUM_LIT:0> ] ) <EOL> ctx . fail ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( sorted ( matches ) ) ) <EOL> return None <EOL> def print_version ( ctx , param , value ) : <EOL> if not value or ctx . resilient_parsing : <EOL> return <EOL> click . echo ( '<STR_LIT>' % __version__ ) <EOL> ctx . exit ( ) <EOL> def get_config_file ( ) : <EOL> config_file_name = '<STR_LIT>' <EOL> config_file = os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , '<STR_LIT:.>' + config_file_name ) <EOL> if os . path . isfile ( config_file ) : <EOL> return config_file <EOL> return os . path . join ( xdg_dirs . user_config_dir , config_file_name ) <EOL> def get_data_dir ( ) : <EOL> data_dir = os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> if os . path . isdir ( data_dir ) : <EOL> return data_dir <EOL> return xdg_dirs . user_data_dir <EOL> @ click . group ( cls = AliasedGroup ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT:-c>' , default = get_config_file ( ) , <EOL> type = ExpandedPath ( dir_okay = False ) , <EOL> help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , default = get_data_dir ( ) , <EOL> type = ExpandedPath ( file_okay = False ) , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , callback = print_version , <EOL> expose_value = False , is_eager = True , <EOL> help = "<STR_LIT>" ) <EOL> @ click . pass_context <EOL> def cli ( ctx , config , taxi_dir ) : <EOL> create_config_file ( config ) <EOL> settings = Settings ( config ) <EOL> if not os . path . exists ( taxi_dir ) : <EOL> os . makedirs ( taxi_dir ) <EOL> populate_aliases ( settings . get_aliases ( ) ) <EOL> populate_backends ( settings . get_backends ( ) ) <EOL> ctx . obj = { } <EOL> ctx . obj [ '<STR_LIT>' ] = settings <EOL> ctx . obj [ '<STR_LIT>' ] = TtyUi ( ) <EOL> ctx . obj [ '<STR_LIT>' ] = ProjectsDb ( os . path . expanduser ( taxi_dir ) ) </s>
