def store_config(cls, config, **kwarg): with git_config.GitConfigParser(config, read_only=False) as config: section = 'gitrepo "{}"'.format(cls.name) for option, value in kwarg.items(): if option not in cls.config_options: raise ArgumentError( 'Option {} is invalid and cannot be setup.') config.set_value(section, option, value)
def get_config(cls, config): out = {} with git_config.GitConfigParser(config, read_only=True) as config: section = 'gitrepo "{}"'.format(cls.name) if config.has_section(section): for option in cls.config_options: if config.has_option(section, option): out[option] = config.get(section, option) return out
def get_service(cls, repository, command): '''Accessor for a repository given a command :param repository: git-python repository instance :param command: aliased name of the service :return: instance for using the service ''' if not repository: config = git_config.GitConfigParser(cls.get_config_path()) else: config = repository.config_reader() target = cls.command_map.get(command, command) conf_section = list( filter(lambda n: 'gitrepo' in n and target in n, config.sections())) http_section = [ config._sections[scheme] for scheme in ('http', 'https') if scheme in config.sections() ] # check configuration constraints if len(conf_section) == 0: if not target: raise ValueError('Service {} unknown'.format(target)) else: config = dict() elif len(conf_section) > 1: raise ValueError( 'Too many configurations for service {}'.format(target)) # get configuration section as a dict else: config = config._sections[conf_section[0]] if target in cls.service_map: service = cls.service_map.get(target, cls) service.name = target else: if 'type' not in config: raise ValueError('Missing service type for custom service.') if config['type'] not in cls.service_map: raise ValueError('Service type {} does not exists.'.format( config['type'])) service = cls.service_map.get(config['type'], cls) cls._current = service(repository, config, http_section) return cls._current
def use_personal_token(): """ ..note:: token was limited when user use strict permission :return: """ git_config_path = os.path.expanduser('~/.gitconfig') if os.path.exists(git_config_path): git_config = pygit.GitConfigParser(git_config_path) git_config.read() access_token = git_config.get_value('github', 'token', default='') username = git_config.get_value('github', 'user', default='') pass if access_token and username: return (username, access_token) pass
def set_alias(cls, config): with git_config.GitConfigParser(config, read_only=False) as config: config.set_value('alias', cls.command, 'repo {}'.format(cls.command))