當前位置: 首頁>>代碼示例>>Python>>正文


Python Repo.replace方法代碼示例

本文整理匯總了Python中repo.Repo.replace方法的典型用法代碼示例。如果您正苦於以下問題:Python Repo.replace方法的具體用法?Python Repo.replace怎麽用?Python Repo.replace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在repo.Repo的用法示例。


在下文中一共展示了Repo.replace方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_it_should_replace_a_given_string_in_repo_conf

# 需要導入模塊: from repo import Repo [as 別名]
# 或者: from repo.Repo import replace [as 別名]
  def test_it_should_replace_a_given_string_in_repo_conf(self):
    mocked_re = MagicMock()
    path = 'tests/fixtures/config.conf'

    mocked_re.sub.return_value = 'another_text'

    with patch.multiple('repo', re=mocked_re):
      repo = Repo(path)
      repo.replace('pattern', 'string')

      with open('tests/fixtures/config.conf') as f:
        eq_(f.read(), 'another_text')

      mocked_re.sub.assert_called_once_with('pattern', 'string',
                                            'another_text')
開發者ID:bcersows,項目名稱:pyolite,代碼行數:17,代碼來源:test_repo.py

示例2: ListUsers

# 需要導入模塊: from repo import Repo [as 別名]
# 或者: from repo.Repo import replace [as 別名]
class ListUsers(object):
  def __init__(self, repository):
    self.repository_model = repository
    self.repo = Repo(Path(repository.path,
                          "conf/repos/%s.conf" % repository.name))

  def with_user(func):
    def decorated(self, string_user, *args, **kwargs):
      try:
        user = User.get(string_user, self.repository_model.path,
                        self.repository_model.git)
      except ValueError:
        user = User(self.repository_model.path, self.repository_model.git,
                    string_user)

      return func(self, user, *args, **kwargs)
    return decorated

  @with_user
  def add(self, user, permission):
    if user.name in self.repo.users:
      raise ValueError('User %s already exists. Please check '
                       'example/repository.py in order to see how you can '
                       'delete or change permissions' % user.name)

    if set(map(lambda permission: permission.upper(), permission)) - \
       ACCEPTED_PERMISSIONS != set([]):
      raise ValueError('Invalid permissions. They must be from %s' %
                       ACCEPTED_PERMISSIONS)

    self.repo.write("    %s     =    %s\n" % (permission, user.name))

    commit_message = 'User %s added to repo %s with permissions: %s' %\
                     (user, self.repository_model.name, permission)
    self.repository_model.git.commit(['conf'], commit_message)

    user.repos.append(self.repo)
    return user

  @with_user
  def edit(self, user, permission):
    pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s\s+' % user.name
    string = r"\n    %s    =    %s" % (permission, user.name)

    self.repo.replace(pattern, string)

    self.repository_model.git.commit(['conf'],
                         "User %s has %s permission for repository %s" %
                         (user.name, permission, self.repository_model.name))
    return user

  @with_user
  def remove(self, user):
    pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s\s+' % user.name
    self.repo.replace(pattern, "")

    self.repository_model.git.commit(['conf'],
                         "Deleted user %s from repository %s" %
                         (user.name, self.repository_model.name))

  def list(self):
    users = []
    for user in self.repo.users:
      if user=="None":
        continue
      pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s\s+' % user
      with open(str(self.repo.path)) as f:
        config = f.read()
        for match in re.compile(pattern).finditer(config):
          perm = match.group(2)
      users.append({"name":user,"permission":perm})
    return users

  def __iter__(self):
    for user in self._user:
      yield user

  def __getitem__(self, item):
    return self._users[item]

  def __setitem__(self, item, value):
    self._users[item] = value

  def __add__(self, items):
    for item in items:
      self.append(item)

  def __str__(self):
    return "['%s']" % ', '.join(self.repo.users)
開發者ID:bcersows,項目名稱:pyolite,代碼行數:91,代碼來源:users.py


注:本文中的repo.Repo.replace方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。