def add(self, alias, file_path, fmt): if ' ' in alias or os.sep in alias: raise exceptions.IllegalAliasException(alias=alias) file_path = os.path.abspath(file_path) self._logger.debug( 'Absolute path translation resulted in {0}'.format(file_path)) if not os.path.exists(file_path): raise exceptions.FileNotFoundException(file_path=file_path) if os.path.isdir(file_path): raise exceptions.FileIsDirectoryException(file_path=file_path) if self._exists(alias): raise exceptions.AliasAlreadyExistsException(alias=alias) self._logger.debug( 'Verifying the file can be parsed to {0}'.format(fmt)) parser.load(file_path=file_path, fmt=fmt) state = self._load_state() state['files'][alias] = {'file_path': file_path, 'fmt': fmt} self._save_state(state) self._logger.debug( 'Committing this file ({0}) to retain its original version'.format( file_path)) self.commit(alias, message=ADD_COMMIT_MESSAGE)
def add(self, alias, file_path, fmt): if ' ' in alias or os.sep in alias: raise exceptions.IllegalAliasException(alias=alias) file_path = os.path.abspath(file_path) self._logger.debug('Absolute path translation resulted in {0}'.format(file_path)) if not os.path.exists(file_path): raise exceptions.FileNotFoundException(file_path=file_path) if os.path.isdir(file_path): raise exceptions.FileIsDirectoryException(file_path=file_path) if self._exists(alias): raise exceptions.AliasAlreadyExistsException(alias=alias) self._logger.debug('Verifying the file can be parsed to {0}'.format(fmt)) parser.load(file_path=file_path, fmt=fmt) state = self._load_state() state['files'][alias] = {'file_path': file_path, 'fmt': fmt} self._save_state(state) self._logger.debug('Committing this file ({0}) to retain its original version' .format(file_path)) self.commit(alias, message=ADD_COMMIT_MESSAGE)
def commit(ctx, alias, message): repo = ctx.parent.parent.repo try: parser.load(file_path=repo.path(alias), fmt=repo.fmt(alias)) except exceptions.CorruptFileException as e: e.cause = causes.EDITED_MANUALLY e.possible_solutions = [ solutions.edit_manually(), solutions.reset_to_latest(alias) ] raise repo.commit(alias, message)
def configure(ctx, alias): repo = ctx.parent.repo file_path = repo.path(alias) try: parsed = parser.load(file_path=file_path, fmt=repo.fmt(alias)) except exceptions.CorruptFileException as e: e.cause = causes.EDITED_MANUALLY e.possible_solutions = [ solutions.edit_manually(), solutions.reset_to_latest(alias) ] raise # detect if the file was manually edited since the last command. # if so, warn because it means the current version of the file is not # under version control and will be lost after the change latest = parser.loads(repo.contents(alias=alias, version='latest'), fmt=repo.fmt(alias)) if latest != parsed: exception = click.ClickException(message='Cannot perform operation') exception.cause = causes.DIFFER_FROM_LATEST exception.possible_solutions = [ solutions.reset_to_latest(alias), solutions.commit(alias) ] raise exception patcher = Patcher(parsed) ctx.patcher = patcher
def configure(ctx, alias): repo = ctx.parent.repo file_path = repo.path(alias) try: parsed = parser.load(file_path=file_path, fmt=repo.fmt(alias)) except exceptions.CorruptFileException as e: e.cause = causes.EDITED_MANUALLY e.possible_solutions = [solutions.edit_manually(), solutions.reset_to_latest(alias)] raise # detect if the file was manually edited since the last command. # if so, warn because it means the current version of the file is not # under version control and will be lost after the change latest = parser.loads(repo.contents(alias=alias, version='latest'), fmt=repo.fmt(alias)) if latest != parsed: exception = click.ClickException(message='Cannot perform operation') exception.cause = causes.DIFFER_FROM_LATEST exception.possible_solutions = [solutions.reset_to_latest(alias), solutions.commit(alias)] raise exception patcher = Patcher(parsed, logger=logger) ctx.patcher = patcher
def test_load(resource, fmt, expected): actual = parser.load(file_path=get_resource(resource), fmt=fmt) assert expected == actual
def test_load_unsupported_format(temp_file): with pytest.raises(exceptions.UnsupportedFormatException): parser.load(file_path=temp_file, fmt='unsupported')
def test_corrupt_file(fmt): with pytest.raises(exceptions.CorruptFileException): parser.load(get_resource('test_corrupt_file'), fmt=fmt)
def _load_state(self): return parser.load(file_path=self._state_file, fmt=constants.JSON)