<s> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> import os . path <EOL> import sys <EOL> import io <EOL> import logging <EOL> import datetime <EOL> from copy import deepcopy <EOL> from pprint import pprint <EOL> import yaml <EOL> import tzlocal <EOL> class ConfigFileNotFound ( Exception ) : <EOL> pass <EOL> def _set_default_config ( ) : <EOL> config = { <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:root>" : "<STR_LIT:/>" , <EOL> "<STR_LIT:source>" : "<STR_LIT:content>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:time>" : datetime . datetime . now ( tzlocal . get_localzone ( ) ) , <EOL> } <EOL> return config <EOL> def _post_process ( config ) : <EOL> for k , v in config . items ( ) : <EOL> if v is None : <EOL> config [ k ] = "<STR_LIT>" <EOL> if config [ "<STR_LIT:url>" ] . endswith ( "<STR_LIT:/>" ) : <EOL> config [ "<STR_LIT:url>" ] = config [ "<STR_LIT:url>" ] [ : - <NUM_LIT:1> ] <EOL> return config <EOL> def get_default_config ( ) : <EOL> return _post_process ( _set_default_config ( ) ) <EOL> def parse_config ( config_file ) : <EOL> if not os . path . exists ( config_file ) : <EOL> raise ConfigFileNotFound ( "<STR_LIT>" . format ( config_file ) ) <EOL> default_config = _set_default_config ( ) <EOL> with io . open ( config_file , "<STR_LIT>" , encoding = "<STR_LIT:utf-8>" ) as fd : <EOL> config = yaml . load ( fd ) <EOL> default_config . update ( config ) <EOL> config = _post_process ( default_config ) <EOL> return config <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> """<STR_LIT>""" <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> base_dir = os . path . dirname ( __file__ ) <EOL> _config_file = os . path . join ( base_dir , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> elif len ( sys . argv ) == <NUM_LIT:2> : <EOL> base_dir = os . getcwd ( ) <EOL> _config_file = os . path . join ( base_dir , sys . argv [ <NUM_LIT:1> ] ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> pprint ( parse_config ( _config_file ) ) </s>
