def read_config(user_config: Optional[str] = None, cli_options: Optional[Dict[str, Union[bool, str]]] = None ) -> configparser.ConfigParser: """Read configuration data. Args: user_config: User defined config file cli_options: Command line options Returns: Parsed configuration data """ # Only base *must* exist conf = configparser.ConfigParser() # No, it *really* must conf.read_string(resources.read_text('rdial', 'config'), 'pkg config') conf['DEFAULT'] = {'xdg_data_location': xdg_basedir.user_data('rdial')} for f in xdg_basedir.get_configs('rdial'): conf.read(f) conf.read(os.path.abspath('.rdialrc')) if user_config: conf.read(user_config) if cli_options: conf.read_dict({ 'rdial': {k: v for k, v in cli_options.items() if v is not None} }) return conf
def read_config( user_config: Optional[str] = None, cli_options: Optional[Dict[str, Union[bool, str]]] = None ) -> configparser.ConfigParser: """Read configuration data. Args: user_config: User defined config file cli_options: Command line options Returns: Parsed configuration data """ # Only base *must* exist conf = configparser.ConfigParser() # No, it *really* must conf.read_string(resources.read_text('rdial', 'config'), 'pkg config') conf['DEFAULT'] = {'xdg_data_location': xdg_basedir.user_data('rdial')} for f in xdg_basedir.get_configs('rdial'): conf.read(f) conf.read(os.path.abspath('.rdialrc')) if user_config: conf.read(user_config) if cli_options: conf.read_dict({ 'rdial': {k: v for k, v in cli_options.items() if v is not None} }) return conf
def write_events(location: str, files: Dict[str, List[str]]) -> None: makedirs(location) for fn, data in files.items(): with open_file(f'{location}/{fn}.data', 'w', atomic=True) as f: f.writelines(data) @command( epilog=('Please report bugs at ' 'https://github.com/JNRowe/rdial/issues'), context_settings={'help_option_names': ['-h', '--help']}) @option( '--database', default=user_data('rdial'), type=Path(exists=True, file_okay=False), help='Path to rdial database') @argument('output', type=Path(exists=False)) def main(database: str, output: str) -> None: """Export rdial data for use with timew. Writes timew compatible data to ‘output’. """ if exists(output): raise BadOptionUsage('output', 'Output path must not exist') files = process_events(database) write_events(output, files) if __name__ == '__main__':
import click import html2text as html2 import jinja2 import misaka from jnrbase import xdg_basedir from jnrbase.colourise import success from jnrbase.human_time import human_timestamp from pygments import highlight as pyg_highlight from pygments.formatters import get_formatter_by_name from pygments.lexers import get_lexer_by_name from . import utils PKG_DATA_DIRS = [os.path.join(xdg_basedir.user_data('hubugs'), 'templates'), ] for directory in xdg_basedir.get_data_dirs('hubugs'): PKG_DATA_DIRS.append(os.path.join(directory, 'templates')) ENV = jinja2.Environment(loader=jinja2.ChoiceLoader( [jinja2.FileSystemLoader(s) for s in PKG_DATA_DIRS])) ENV.loader.loaders.append(jinja2.PackageLoader('hubugs', 'templates')) ENV.filters['relative_time'] = human_timestamp class EmptyMessageError(ValueError): """Error to raise when the user provides an empty message.""" pass
def write_events(location: str, files: Dict[str, List[str]]) -> None: makedirs(location) for fn, data in files.items(): with open_file(f'{location}/{fn}.data', 'w', atomic=True) as f: f.writelines(data) @command( epilog=('Please report bugs at ' 'https://github.com/JNRowe/rdial/issues'), context_settings={'help_option_names': ['-h', '--help']} ) @option( '--database', default=user_data('rdial'), type=Path(exists=True, file_okay=False), help="Path to rdial database" ) @argument('output', type=Path(exists=False)) def main(database: str, output: str) -> None: """Export rdial data for use with timew. Writes timew compatible data to ‘output’. """ if exists(output): raise BadOptionUsage('output', 'Output path must not exist') files = process_events(database) write_events(output, files)
def test_macos_paths(monkeypatch): monkeypatch.setattr('sys.platform', 'darwin') assert '/Library/Application Support/jnrbase' \ in xdg_basedir.user_data('jnrbase')
def test_data_no_home(monkeypatch): monkeypatch.setattr('os.environ', {}) assert xdg_basedir.user_data('jnrbase') == '/.local/share/jnrbase'
def test_data_no_args(monkeypatch): monkeypatch.setenv('XDG_DATA_HOME', '~/.xdg/local') assert '/.xdg/local/jnrbase' in xdg_basedir.user_data('jnrbase')
def test_macos_paths(): expect(xdg_basedir.user_data('jnrbase')).contains( '/Library/Application Support/jnrbase' )
def test_data_no_home(): with patch_env(clear=True): expect(xdg_basedir.user_data('jnrbase')) == '/.local/share/jnrbase'
def test_data_no_args(): with patch_env({'XDG_DATA_HOME': '~/.xdg/local'}): expect(xdg_basedir.user_data('jnrbase')).contains( '/.xdg/local/jnrbase' )