Skip to content

Commit

Permalink
Merge pull request #5111 from tripleee/watch-fix
Browse files Browse the repository at this point in the history
Fix watch (regression from #3884)
  • Loading branch information
tripleee committed Nov 12, 2020
2 parents 10df006 + 9b2af59 commit fbda771
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
33 changes: 24 additions & 9 deletions blacklists.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, filename):
def parse(self):
return []

def add(self, item):
def add(self, item, **kwargs):
pass

def delete(self, item):
Expand All @@ -67,7 +67,7 @@ def parse(self):
return [self._normalize(line)
for line in f if len(line.rstrip()) > 0 and line[0] != '#']

def add(self, item: str):
def add(self, item: str, **kwargs):
with open(self._filename, 'a+', encoding='utf-8') as f:
last_char = f.read()[-1:]
if last_char not in ['', '\n']:
Expand Down Expand Up @@ -107,7 +107,7 @@ class WhoWhatWhenString(str):
"""
str wrapper with additional attributes for TSVDictParser to generate
"""
def __new__(cls, seq, who, when, filename, lineno, *args, **kwargs):
def __new__(cls, seq, who, when, filename=None, lineno=None, *args, **kwargs):
self = super().__new__(cls, seq, *args, **kwargs)
self._who = who
self._when = when
Expand Down Expand Up @@ -143,10 +143,25 @@ def parse(self):
seq=what, who=by_whom, when=when,
filename=self._filename, lineno=lineno)

def add(self, item: Union[str, WhoWhatWhenString]):
def _validate(self, item: str):
fields = item.split('\t')
if len(fields) != 3:
raise ValueError('Format error: TSVDict expects three fields, tab separated')
try:
_ = int(fields[0])
except ValueError:
raise ValueError('Format error: first field must be numeric timestamp')
listed, where = self.exists(fields[2])
if listed:
raise ValueError('Item already listed on %s line %i' % (self._filename, where))

def add(self, item: Union[str, WhoWhatWhenString], **kwargs):
if isinstance(item, str) and 'who' in kwargs and 'when' in kwargs:
item = WhoWhatWhenString(item, kwargs['who'], kwargs['when'])
if isinstance(item, WhoWhatWhenString):
item = '{}\t{}\t{}'.format(item.when(), item.who(), item)
self._validate(item)
with open(self._filename, 'a+', encoding='utf-8') as f:
if isinstance(item, WhoWhatWhenString):
item = '{}\t{}\t{}'.format(item.when(), item.who(), item)
last_char = f.read()[-1:]
if last_char not in ['', '\n']:
item = '\n' + item
Expand Down Expand Up @@ -287,7 +302,7 @@ def _add_format(self, item):
"""
return {self.SCHEMA_PRIKEY: item}

def add(self, item):
def add(self, item, **kwargs):
if isinstance(item, str):
item = self._add_format(item)
try:
Expand Down Expand Up @@ -449,8 +464,8 @@ def resolve(identifier):
def parse(self):
return self._parser.parse()

def add(self, item):
return self._parser.add(item)
def add(self, item, **kwargs):
return self._parser.add(item, **kwargs)

def delete(self, item):
return self._parser.delete(item)
Expand Down
4 changes: 2 additions & 2 deletions gitmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ def add_to_blacklist(cls, blacklist='', item_to_blacklist='',
watch_removed = []
if not blacklister.watchtype():
for watcher in GlobalVars.git_black_watch_lists.values():
if not watcher.watchtype():
if not hasattr(watcher, 'watchtype') or not watcher.watchtype():
continue
watched, where = watcher.exists(item_to_blacklist)
if watched:
watch_removed.append(watcher.filename())
watcher.delete(item_to_blacklist)

try:
blacklister.add(item_to_blacklist)
blacklister.add(item_to_blacklist, who=username, when=now)
except ValueError as exc:
for rollback_watch in watch_removed:
git.restore(rollback_watch)
Expand Down
38 changes: 38 additions & 0 deletions test/test_blacklists.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,41 @@ def test_yaml_nses():

yaml_validate_existing(NetBlacklist, 'blacklisted_nses.yml', YAMLParserNS)
yaml_validate_existing(NetWatchlist, 'watched_nses.yml', YAMLParserNS)


def test_tsv_watchlist():
with open('test_watched_keywords.txt', 'w') as t:
t.write('\t'.join(['1495006487', 'tripleee', 'one two\n']))
t.write('\t'.join(['1495006488', 'tripleee', 'three four\n']))
watchlist = Watchlist('test_watched_keywords.txt', TSVDictParser)
parsed = list(watchlist.parse())
assert 'one two' in parsed
assert 'one' not in parsed
assert 'three four' in parsed
assert 'five six' not in parsed
with pytest.raises(ValueError) as e:
watchlist.add('one two', who='tripleee', when=1495006489)
with pytest.raises(ValueError) as e:
watchlist.add('five six')
watchlist.add('five six', who='tripleee', when=1495006490)
assert 'five six' in watchlist.parse()
unlink('test_watched_keywords.txt')


def test_keyword_blacklist():
with open('test_blacklist.txt', 'w') as t:
t.write('one two\n')
t.write('three four\n')
blacklist = KeywordBlacklist('test_blacklist.txt', BasicListParser)
parsed = blacklist.parse()
assert 'one two' in parsed
assert 'one' not in parsed
assert 'three four' in parsed
assert 'five six' not in parsed
# with pytest.raises(ValueError) as e:
# blacklist.add('*invalid regex[')
# with pytest.raises(ValueError) as e:
# blacklist.add('one two')
blacklist.add('five six')
assert 'five six' in blacklist.parse()
unlink('test_blacklist.txt')

0 comments on commit fbda771

Please sign in to comment.