Skip to content

Commit a2d78cf

Browse files
committed
fixup let's roll with wrap_read_cache() for now
1 parent 03138e6 commit a2d78cf

File tree

5 files changed

+42
-64
lines changed

5 files changed

+42
-64
lines changed

gh2slack.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from lib import cli_args
2323
from lib import config_options
2424
from lib import utils
25-
from lib.exceptions import CacheReadError
2625
from lib.exceptions import SlackTokenError
2726

2827
ALIASES = {
@@ -164,21 +163,23 @@ def main():
164163
logger = logging.getLogger("gh2slack")
165164
logger.setLevel(args.log_level)
166165

167-
cache = None
168166
retcode = 0
167+
cache = rss2irc.wrap_read_cache(logger, args.cache_file)
168+
if cache is None:
169+
retcode = utils.mask_retcode(1, args.mask_errors)
170+
sys.exit(retcode)
171+
169172
try:
170173
slack_token = rss2slack.get_slack_token()
171174
url = get_gh_api_url(args.gh_owner, args.gh_repo, args.gh_section)
172175
pages = gh_request(logger, url)
173-
174176
logger.debug("Got %i pages from GH.", len(pages))
175177
if not pages:
176178
logger.info(
177179
"No %s for %s/%s.", args.gh_section, args.gh_owner, args.gh_repo
178180
)
179181
sys.exit(0)
180182

181-
cache = rss2irc.read_cache(logger, args.cache_file)
182183
scrub_items(logger, cache)
183184
# NOTE(zstyblik): I have failed to find web link to repo in GH response.
184185
# Therefore, let's create one.
@@ -211,15 +212,6 @@ def main():
211212
logger.exception("Environment variable SLACK_TOKEN must be set.")
212213
retcode = utils.mask_retcode(1, args.mask_errors)
213214
sys.exit(retcode)
214-
except CacheReadError:
215-
logger.exception(
216-
"Error while reading cache file '%s'.",
217-
args.cache_file,
218-
)
219-
retcode = utils.mask_retcode(1, args.mask_errors)
220-
# NOTE(zstyblik): since cache file couldn't be opened, it doesn't make
221-
# sense writing it. Therefore, call sys.exit().
222-
sys.exit(retcode)
223215
except Exception:
224216
logger.exception("Unexpected exception has occurred.")
225217
retcode = 1

phpbb2slack.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from lib import cli_args
1919
from lib import config_options
2020
from lib import utils
21-
from lib.exceptions import CacheReadError
2221
from lib.exceptions import EmptyResponseError
2322
from lib.exceptions import NoNewsError
2423
from lib.exceptions import NotModifiedError
@@ -81,13 +80,16 @@ def main():
8180
logger = logging.getLogger("phpbb2slack")
8281
logger.setLevel(args.log_level)
8382

84-
cache = None
8583
retcode = 0
84+
cache = rss2irc.wrap_read_cache(logger, args.cache_file)
85+
if cache is None:
86+
retcode = utils.mask_retcode(1, args.mask_errors)
87+
sys.exit(retcode)
88+
89+
source = cache.get_source_by_url(args.rss_url)
8690
try:
8791
slack_token = rss2slack.get_slack_token()
8892
authors = get_authors_from_file(logger, args.authors_file)
89-
cache = rss2irc.read_cache(logger, args.cache_file)
90-
source = cache.get_source_by_url(args.rss_url)
9193

9294
rsp = rss2irc.get_rss(
9395
logger,
@@ -124,15 +126,6 @@ def main():
124126
logger.exception("Environment variable SLACK_TOKEN must be set.")
125127
retcode = utils.mask_retcode(1, args.mask_errors)
126128
sys.exit(retcode)
127-
except CacheReadError:
128-
logger.exception(
129-
"Error while reading cache file '%s'.",
130-
args.cache_file,
131-
)
132-
retcode = utils.mask_retcode(1, args.mask_errors)
133-
# NOTE(zstyblik): since cache file couldn't be opened, it doesn't make
134-
# sense writing it. Therefore, call sys.exit().
135-
sys.exit(retcode)
136129
except NotModifiedError:
137130
logger.debug("No new RSS data since the last run.")
138131
rss2irc.update_items_expiration(
@@ -155,10 +148,7 @@ def main():
155148
retcode = 0
156149
except Exception:
157150
logger.exception("Unexpected exception has occurred.")
158-
if cache:
159-
source = cache.get_source_by_url(args.rss_url)
160-
source.http_error_count += 1
161-
151+
source.http_error_count += 1
162152
retcode = 1
163153

164154
write_retcode = rss2irc.wrap_write_cache(logger, cache, args.cache_file)

rss2irc.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ def main():
9494
logger.error("Ouput '%s' doesn't exist.", args.output)
9595
sys.exit(1)
9696

97-
cache = None
9897
retcode = 0
99-
try:
100-
cache = read_cache(logger, args.cache_file)
101-
source = cache.get_source_by_url(args.rss_url)
98+
cache = wrap_read_cache(logger, args.cache_file)
99+
if cache is None:
100+
retcode = utils.mask_retcode(1, args.mask_errors)
101+
sys.exit(retcode)
102102

103+
source = cache.get_source_by_url(args.rss_url)
104+
try:
103105
rsp = get_rss(
104106
logger,
105107
args.rss_url,
@@ -122,15 +124,6 @@ def main():
122124
cache.scrub_data_sources()
123125
source.http_error_count = 0
124126
retcode = 0
125-
except CacheReadError:
126-
logger.exception(
127-
"Error while reading cache file '%s'.",
128-
args.cache_file,
129-
)
130-
retcode = utils.mask_retcode(1, args.mask_errors)
131-
# NOTE(zstyblik): since cache file couldn't be opened, it doesn't make
132-
# sense writing it. Therefore, call sys.exit().
133-
sys.exit(retcode)
134127
except NotModifiedError:
135128
logger.debug("No new RSS data since the last run.")
136129
update_items_expiration(cache, cache.items, args.cache_expiration)
@@ -149,10 +142,7 @@ def main():
149142
retcode = 0
150143
except Exception:
151144
logger.exception("Unexpected exception has occurred.")
152-
if cache:
153-
source = cache.get_source_by_url(args.rss_url)
154-
source.http_error_count += 1
155-
145+
source.http_error_count += 1
156146
retcode = 1
157147

158148
write_retcode = wrap_write_cache(logger, cache, args.cache_file)
@@ -271,6 +261,18 @@ def update_items_expiration(
271261
cache.items[key] = item_expiration
272262

273263

264+
def wrap_read_cache(logger: logging.Logger, cache_file: str):
265+
"""Call read_cache() and return cached data or log error and return None."""
266+
cache = None
267+
try:
268+
cache = read_cache(logger, cache_file)
269+
except CacheReadError:
270+
logger.exception("Error while reading cache file '%s'.", cache_file)
271+
cache = None
272+
273+
return cache
274+
275+
274276
def wrap_write_cache(
275277
logger: logging.Logger,
276278
cache: CachedData,

rss2slack.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import rss2irc
1919
from lib import cli_args
2020
from lib import utils
21-
from lib.exceptions import CacheReadError
2221
from lib.exceptions import EmptyResponseError
2322
from lib.exceptions import NoNewsError
2423
from lib.exceptions import NotModifiedError
@@ -78,12 +77,15 @@ def main():
7877
logger = logging.getLogger("rss2slack")
7978
logger.setLevel(args.log_level)
8079

81-
cache = None
8280
retcode = 0
81+
cache = rss2irc.wrap_read_cache(logger, args.cache_file)
82+
if cache is None:
83+
retcode = utils.mask_retcode(1, args.mask_errors)
84+
sys.exit(retcode)
85+
86+
source = cache.get_source_by_url(args.rss_url)
8387
try:
8488
slack_token = get_slack_token()
85-
cache = rss2irc.read_cache(logger, args.cache_file)
86-
source = cache.get_source_by_url(args.rss_url)
8789

8890
rsp = rss2irc.get_rss(
8991
logger,
@@ -122,15 +124,6 @@ def main():
122124
logger.exception("Environment variable SLACK_TOKEN must be set.")
123125
retcode = utils.mask_retcode(1, args.mask_errors)
124126
sys.exit(retcode)
125-
except CacheReadError:
126-
logger.exception(
127-
"Error while reading cache file '%s'.",
128-
args.cache_file,
129-
)
130-
retcode = utils.mask_retcode(1, args.mask_errors)
131-
# NOTE(zstyblik): since cache file couldn't be opened, it doesn't make
132-
# sense writing it. Therefore, call sys.exit().
133-
sys.exit(retcode)
134127
except NotModifiedError:
135128
logger.debug("No new RSS data since the last run.")
136129
rss2irc.update_items_expiration(
@@ -153,10 +146,7 @@ def main():
153146
retcode = 0
154147
except Exception:
155148
logger.exception("Unexpected exception has occurred.")
156-
if cache:
157-
source = cache.get_source_by_url(args.rss_url)
158-
source.http_error_count += 1
159-
149+
source.http_error_count += 1
160150
retcode = 1
161151

162152
write_retcode = rss2irc.wrap_write_cache(logger, cache, args.cache_file)

tests/test_rss2slack.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ def test_main_ideal(
246246
],
247247
)
248248
@patch("rss2slack.rss2irc.wrap_write_cache")
249+
@patch("rss2slack.rss2irc.read_cache")
249250
def test_main_slack_token_error(
251+
mock_read_cache,
250252
mock_wrap_write_cache,
251253
extra_args,
252254
expected_retcode,
@@ -267,6 +269,8 @@ def test_main_slack_token_error(
267269
expected_slack_channel = "test"
268270
fixture_cache_file = "/path/not/exist/cache.file"
269271

272+
mock_read_cache.return_value = CachedData()
273+
270274
exception = None
271275
args = [
272276
"./rss2slack.py",

0 commit comments

Comments
 (0)