diff options
author | Jon Bernard <jbernard@tuxion.com> | 2016-02-18 07:24:34 -0500 |
---|---|---|
committer | Jon Bernard <jbernard@tuxion.com> | 2016-02-18 07:24:34 -0500 |
commit | 3a4132886c22032ca8960d2983296267f2d38353 (patch) | |
tree | ac4a334ae46648518f05db9dd42c0da8f8ff0bf6 | |
parent | e382b7378e97d1485928745a91b2bef01c203a2c (diff) | |
download | dotfiles-3a4132886c22032ca8960d2983296267f2d38353.tar.gz dotfiles-3a4132886c22032ca8960d2983296267f2d38353.tar.bz2 dotfiles-3a4132886c22032ca8960d2983296267f2d38353.zip |
Add support for configuration files
At the moment, only a global configuration file is considered at
'~/.config/dotfiles/config.ini'. Further more, only the 'repositories'
field is being used at the moment, but this will change in the next
commit. If you to specify a repository (or several) in the config file,
you could use something like:
[dotfiles]
repositories = ['~/public/dotfiles', '~/private/dotfiles']
Still some polishing to be done, but it's coming along.
-rw-r--r-- | dotfiles/cli.py | 66 | ||||
-rw-r--r-- | dotfiles/repositories.py | 21 |
2 files changed, 60 insertions, 27 deletions
diff --git a/dotfiles/cli.py b/dotfiles/cli.py index b2170b8..f468a63 100644 --- a/dotfiles/cli.py +++ b/dotfiles/cli.py @@ -1,16 +1,71 @@ import os +import copy import click +import py.path +try: + import ConfigParser as configparser +except ImportError: + import configparser + +from .repository import Repository from .exceptions import DotfileException -from .repositories import Repositories DEFAULT_DOT = False DEFAULT_REPO = os.path.expanduser('~/Dotfiles') +DEFAULT_IGNORE_PATTERNS = ['README*', '.git', '.hg', '*~'] + CONTEXT_SETTINGS = dict(auto_envvar_prefix='DOTFILES', help_option_names=['-h', '--help']) +class Config(object): + + def __init__(self, paths): + self.settings = self.parse_config({}) + + # repositories specified on the command line take precedence + if paths: + self.settings.update({'repositories': str(set(paths))}) + + # assume default repository at this point + if not 'repositories' in self.settings: + self.settings.update({'repositories': str([DEFAULT_REPO])}) + + # TODO: apply repository configuration, if available + + def parse_config(path, settings): + settings = copy.deepcopy(settings) + cfg = os.path.join(click.get_app_dir('Dotfiles'), 'config.ini') + parser = configparser.RawConfigParser() + parser.read([cfg]) + try: + settings.update(parser.items('dotfiles')) + except configparser.NoSectionError: + pass + return settings + + +class Repositories(object): + + def __init__(self, paths, dot): + config = Config(paths) + + self.repos = [] + for path in eval(config.settings['repositories']): + self.repos.append( + Repository(py.path.local(path, expanduser=True), + ignore_patterns=DEFAULT_IGNORE_PATTERNS, + preserve_leading_dot=dot)) + + def __len__(self): + return len(self.repos) + + def __getitem__(self, index): + return self.repos[index] + + def get_single_repo(repos): if len(repos) > 1: raise click.BadParameter('Must specify exactly one repository.', @@ -42,13 +97,12 @@ def perform(method, files, repo, debug): pass_repos = click.make_pass_decorator(Repositories) - +repo_help = 'A repository path.' +dot_help = 'Preserve the leading dot.' @click.group(context_settings=CONTEXT_SETTINGS) -@click.option('-r', '--repo', type=click.Path(), multiple=True, - default=[DEFAULT_REPO], show_default=True, - help='A repository path.') -@click.option('-d', '--dot', is_flag=True, help='Preserve the leading dot.') +@click.option('-r', '--repo', type=click.Path(), multiple=True, help=repo_help) +@click.option('-d', '--dot', is_flag=True, help=dot_help) @click.version_option(None, '-v', '--version') @click.pass_context def cli(ctx, repo, dot): diff --git a/dotfiles/repositories.py b/dotfiles/repositories.py deleted file mode 100644 index 69dedf3..0000000 --- a/dotfiles/repositories.py +++ /dev/null @@ -1,21 +0,0 @@ -from .repository import Repository - - -DEFAULT_IGNORE_PATTERNS = ['README*', '.git', '.hg', '*~'] - - -class Repositories(object): - - def __init__(self, paths, dot): - self.repos = [] - for path in paths: - self.repos.append( - Repository(path, - ignore_patterns=DEFAULT_IGNORE_PATTERNS, - preserve_leading_dot=dot)) - - def __len__(self): - return len(self.repos) - - def __getitem__(self, index): - return self.repos[index] |