def cli(ctx, config, verbose): """Decentralised, minimalist microblogging service for hackers.""" init_logging(debug=verbose) if ctx.invoked_subcommand == "quickstart": return # Skip initializing config file try: if config: conf = Config.from_file(config) else: conf = Config.discover() except ValueError as e: if "Error in config file." in str(e): click.echo( "✗ Please correct the errors mentioned above an run twtxt again." ) else: click.echo( "✗ Config file not found or not readable. You may want to run twtxt quickstart." ) sys.exit() ctx.default_map = conf.build_default_map() ctx.obj = {'conf': conf}
def check_if_following(follower): config = Config.discover() if config.get_source_by_nick(follower): return True return False
def test_add_get_remove_source(): conf = Config.discover() conf.cfg.remove_section("following") assert conf.remove_source_by_nick("foo") is False assert conf.get_source_by_nick("baz") is None conf.add_source(Source("foo", "bar")) source = conf.get_source_by_nick("foo") assert source.nick == "foo" assert source.url == "bar" assert conf.remove_source_by_nick("baz") is False assert conf.remove_source_by_nick("foo") is True assert conf.following == []
def test_create_config(config_dir): config_dir_old = Config.config_dir Config.config_dir = str(config_dir.join("new")) conf_w = Config.create_config("bar", "batz.txt", True) conf_r = Config.discover() assert conf_r.nick == "bar" assert conf_r.twtfile == "batz.txt" assert conf_r.following[0].nick == "twtxt" assert conf_r.following[0].url == "https://buckket.org/twtxt_news.txt" assert set(conf_r.options.keys()) == {"nick", "twtfile"} conf_r.cfg.remove_section("twtxt") assert conf_r.options == {} conf_r.cfg.remove_section("following") assert conf_r.following == [] Config.config_dir = config_dir_old
def cli(ctx, config, verbose): """Decentralised, minimalist microblogging service for hackers.""" init_logging(debug=verbose) try: if config: conf = Config.from_file(config) else: conf = Config.discover() except ValueError: click.echo("Error loading config file.") if not config: if click.confirm("Do you want to run the twtxt quickstart wizard?", abort=True): pass ctx.default_map = conf.build_default_map() ctx.obj = {'conf': conf}
def test_create_config(config_dir): config_dir_old = Config.config_dir Config.config_dir = str(config_dir.join("new")) conf_w = Config.create_config("bar", "batz.txt", False, True) conf_r = Config.discover() assert conf_r.nick == "bar" assert conf_r.twtfile == "batz.txt" assert conf_r.character_limit == 140 assert conf_r.following[0].nick == "twtxt" assert conf_r.following[0].url == "https://buckket.org/twtxt_news.txt" assert set(conf_r.options.keys()) == {"nick", "twtfile", "disclose_identity", "character_limit"} conf_r.cfg.remove_section("twtxt") assert conf_r.options == {} conf_r.cfg.remove_section("following") assert conf_r.following == [] Config.config_dir = config_dir_old
def cli(ctx, config, verbose): """Decentralised, minimalist microblogging service for hackers.""" init_logging(debug=verbose) if ctx.invoked_subcommand == "quickstart": return try: if config: conf = Config.from_file(config) else: conf = Config.discover() except ValueError: click.echo("✗ Config file not found or not readable. You may want to run twtxt quickstart.") sys.exit() ctx.default_map = conf.build_default_map() ctx.obj = {'conf': conf}
def cli(ctx, config, verbose): """Decentralised, minimalist microblogging service for hackers.""" init_logging(debug=verbose) if ctx.invoked_subcommand == "quickstart": return try: if config: conf = Config.from_file(config) else: conf = Config.discover() except ValueError: click.echo( "✗ Config file not found or not readable. You may want to run twtxt quickstart." ) sys.exit() ctx.default_map = conf.build_default_map() ctx.obj = {'conf': conf}
def cli(ctx, config, verbose): """Decentralised, minimalist microblogging service for hackers.""" init_logging(debug=verbose) if ctx.invoked_subcommand == "quickstart": return # Skip initializing config file try: if config: conf = Config.from_file(config) else: conf = Config.discover() except ValueError as e: if "Error in config file." in str(e): click.echo("✗ Please correct the errors mentioned above an run twtxt again.") else: click.echo("✗ Config file not found or not readable. You may want to run twtxt quickstart.") sys.exit() ctx.default_map = conf.build_default_map() ctx.obj = {'conf': conf}
def test_create_config(config_dir): config_dir_old = Config.config_dir Config.config_dir = str(config_dir.join("new")) conf_w = Config.create_config( os.path.join(Config.config_dir, Config.config_name), "bar", "batz.txt", False, True) conf_r = Config.discover() assert conf_r.nick == "bar" assert conf_r.twtfile == "batz.txt" assert conf_r.character_limit == 140 assert conf_r.character_warning == 140 assert conf_r.following[0].nick == "twtxt" assert conf_r.following[0].url == "https://buckket.org/twtxt_news.txt" assert set(conf_r.options.keys()) == { "nick", "twtfile", "disclose_identity", "character_limit", "character_warning" } conf_r.cfg.remove_section("twtxt") assert conf_r.options == {} conf_r.cfg.remove_section("following") assert conf_r.following == [] Config.config_dir = config_dir_old
def test_discover(): conf = Config.discover() check_cfg(conf)
def retrieve_file(client, source, limit, cache): is_cached = cache.is_cached(source.url) if cache else None headers = {"If-Modified-Since": cache.last_modified(source.url)} if is_cached else {} try: response = yield from client.request("get",source.url, headers=headers,allow_redirects=False) content = yield from response.text() except Exception as e: if is_cached: logger.debug("{}: {} - using cached content".format(source.url, e)) return cache.get_tweets(source.url, limit) #comp490 elif e==ssl.CertificateError: click.echo("Warning the source: "+source.nick+" is unsafe: Hostname does not match name on SSL certificate") return [] elif e==aiohttp.errors.ClientOSError: if "[[SSL: CERTIFICATE_VERIFY_FAILED" in str(e): click.echo("Warning the source: "+source.nick+" is unsafe: The ssl certificate has expired") return [] elif "[SSL: EXCESSIVE_MESSAGE_SIZE]" in str(e): click.echo("Warning the source: "+source.nick+" is unsafe: source has sent an invalid response") #COMP490 else: logger.debug(e) return [] if response.status == 200: tweets = parse_tweets(content.splitlines(), source) if cache: last_modified_header = response.headers.get("Last-Modified") if last_modified_header: logger.debug("{} returned 200 and Last-Modified header - adding content to cache".format(source.url)) cache.add_tweets(source.url, last_modified_header, tweets) else: logger.debug("{} returned 200 but no Last-Modified header - can’t cache content".format(source.url)) else: logger.debug("{} returned 200".format(source.url)) return sorted(tweets, reverse=True)[:limit] #comp490 elif response.status==301: cache = Cache.discover() conf=Config.discover() tweets=cache.get_tweets(source.url) conf.remove_source_by_nick(source.nick) url=response.headers["Location"] conf.add_source(Source(source.nick,url)) for tweet in tweets: cache.add_tweet(url,0,tweet) #comp490 elif response.status == 410 and is_cached: # 410 Gone: # The resource requested is no longer available, # and will not be available again. logger.debug("{} returned 410 - deleting cached content".format(source.url)) cache.remove_tweets(source.url) return [] elif is_cached: logger.debug("{} returned {} - using cached content".format(source.url, response.status)) return cache.get_tweets(source.url, limit) else: logger.debug("{} returned {}".format(source.url, response.status)) return []