def test_parser(self): self.check_parse(1, "1") self.check_parse(Symbol("a*b"), 'a*b') self.check_parse("ab", '"ab"') self.check_parse('a"b', r'"a\"b"') self.check_parse("blah\n", '"blah\n"') self.check_parse([1, 2, 3], "[1 2 3]") self.check_parse({1, 2, 3}, "#{1 2 3}") self.check_parse([1, True, None], "[1 true nil]") self.check_parse("c", r"\c") self.check_parse("\n", r"\newline") self.check_parse(Keyword("abc"), ":abc") self.check_parse([Keyword("abc"), 1, True, None], "[:abc 1 true nil]") self.check_parse((Keyword("abc"), 1, True, None), "(:abc 1 true nil)") self.check_parse(tuple(), "()") self.check_parse(set(), "#{}") self.check_parse({}, "{}") self.check_parse([], "[]") self.check_parse({"a": [1, 2, 3]}, '{"a" [1 2 3]}') self.check_parse( datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc), '#inst "2012-12-22T19:40:18Z"') self.check_parse(datetime.date(2011, 10, 9), '#inst "2011-10-09"') self.check_parse("|", "\"|\"") self.check_parse("%", "\"%\"") self.check_parse(['bl"ah'], r"""["bl\"ah"]""") self.check_parse("blah\n", '"blah\n"') self.check_parse('"', r'"\""') self.check_parse('\\', r'"\\"') self.check_parse(["abc", "123"], '["abc", "123"]') self.check_parse({"key": "value"}, '{"key" "value"}') self.check_parse( frozenset({ImmutableList([u"ab", u"cd"]), ImmutableList([u"ef"])}), '#{["ab", "cd"], ["ef"]}')
def test_equality(self): self.assertTrue("db/id" != Keyword("db/id")) self.assertTrue("db/id" != Symbol("db/id")) self.assertTrue(Symbol("db/id") != Keyword("db/id")) self.assertTrue("db/id" == "db/id") self.assertTrue(Keyword("db/id") == Keyword("db/id")) self.assertTrue(Symbol("db/id") == Symbol("db/id"))
def test_round_trip_sets(self): step1 = '#{:a (1 2 3) :b}' step2 = loads(step1) step3 = dumps(step2) step4 = loads(step3) self.assertIn(Keyword("a"), step4) self.assertIn(Keyword("b"), step4) self.assertIn((1, 2, 3), step4)
def get_best_pattern(self, user_id, tick): response = self.send_stuff( self._PORT_REQ, {"request-type": Keyword("get-best-pattern"), "user-id": user_id}) pattern_string = isinstance(response[Keyword("pattern")], str) type_correct = (response[Keyword("response-type")] == Keyword("vibrate")) if (not pattern_string) or (not type_correct): raise Exception return self.check_data(response[Keyword("security-token")], tick)
def test_dump(self): self.check_dump("#{1 2 3}", {1, 2, 3}) self.check_dump([ '{:bar [1 2 3] "foo" :gone :a 1}', '{:a 1 "foo" :gone :bar [1 2 3]}' ], { Keyword("a"): 1, "foo": Keyword("gone"), Keyword("bar"): [1, 2, 3] })
def test_with_namespace(self): for (expected, keyword, namespace) in ( ("k", "k", None), ("k", "n/k", None), ("n/k", "k", "n"), ("n/k", "x/k", "n"), ("n/a/b/c", "x/a/b/c", "n"), ): self.assertEqual(Keyword(expected), Keyword(keyword).with_namespace(namespace))
def fromconfig(cls, repository: "Repository", config: Dict, stations_map: Dict[str, Station], handlers_map: Dict[str, Handler]): channel_name = config[Keyword("name")] channel_id = config[Keyword("id")] channel_timetable = Timetable.fromconfig(config[Keyword("timetable")], stations_map) channel_handlers = tuple(handlers_map[name] for name in config[Keyword("handlers")]) return cls(channel_id, channel_name, repository, channel_timetable, channel_handlers)
def test_dump(self): self.check_roundtrip({1, 2, 3}) self.check_roundtrip({ Keyword("a"): 1, "foo": Keyword("gone"), Keyword("bar"): [1, 2, 3] }) self.check_roundtrip(uuid4()) self.check_roundtrip(datetime.date(1354, 6, 7)) self.check_roundtrip( datetime.datetime(1900, 6, 7, 23, 59, 59, tzinfo=pytz.utc))
def send_data(self, user_id, tick): result = [] patterns = self.retrieve_yaml(user_id) for pattern in patterns: request = {"request-type": Keyword("send-data"), "user-id": user_id, "pattern": pattern, "excitement-level": random.random()} response = self.send_stuff(self._PORT_REQ, request) result.append(self.check_data(response[Keyword("security-token")], tick)) return result
def get_patterns(self, user_id, tick): request = {"request-type": Keyword("get-patterns"), "user-id": user_id} response = self.send_stuff(self._PORT_REQ, request) patterns = response[Keyword("patterns")] self.logger.info("patterns for %s are %s", str(user_id), str(patterns)) # store patterns self.store_yaml(user_id, patterns) return self.check_data(response[Keyword("security-token")], tick)
def test_parser(self): self.check_parse(1, "1") self.check_parse([1, 2, 3], "[1 2 3]") self.check_parse({1, 2, 3}, "#{1 2 3}") self.check_parse([1, True, None], "[1 true nil]") self.check_parse("c", r"\c") self.check_parse("\n", r"\newline") self.check_parse(Keyword("abc"), ":abc") self.check_parse([Keyword("abc"), 1, True, None], "[:abc 1 true nil]") self.check_parse((Keyword("abc"), 1, True, None), "(:abc 1 true nil)") self.check_parse({"a": [1, 2, 3]}, '{"a" [1 2 3]}') self.check_parse( datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc), '#inst "2012-12-22T19:40:18Z"')
def test_parser_single_expressions(self): for expected, edn_string in ( (1, "1"), (16768115, "0xFFDC73"), (Symbol("xFF"), "xFF"), (Symbol("a*b"), 'a*b'), ("ab", '"ab"'), ('a"b', r'"a\"b"'), ("blah\n", '"blah\n"'), ([1, 2, 3], "[1 2 3]"), ({1, 2, 3}, "#{1 2 3}"), ([1, True, None], "[1 true nil]"), ("c", r"\c"), ("\n", r"\newline"), (u"Σ", u"\\Σ"), (u"λ", r"\u03bB"), (Keyword("abc"), ":abc"), ([Keyword("abc"), 1, True, None], "[:abc 1 true nil]"), ((Keyword("abc"), 1, True, None), "(:abc 1 true nil)"), (tuple(), "()"), (set(), "#{}"), ({}, "{}"), ([], "[]"), ({ "a": [1, 2, 3] }, '{"a" [1 2 3]}'), (datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc), '#inst "2012-12-22T19:40:18Z"'), (datetime.date(2011, 10, 9), '#inst "2011-10-09"'), ("|", "\"|\""), ("%", "\"%\""), (['bl"ah'], r"""["bl\"ah"]"""), ("blah\n", '"blah\n"'), ('"', r'"\""'), ('\\', r'"\\"'), (["abc", "123"], '["abc", "123"]'), ({ "key": "value" }, '{"key" "value"}'), (frozenset({ImmutableList([u"ab", u"cd"]), ImmutableList([u"ef"])}), '#{["ab", "cd"], ["ef"]}'), (fractions.Fraction(2, 3), "2/3"), ((2, Symbol('/'), 3), "(2 / 3)"), ): self.check_parse(expected, edn_string) self.check_parse_all([expected], edn_string)
def search(current_dict: dict): for key in current_dict.keys(): data = current_dict[key] if not isinstance(data, ImmutableDict): continue param_type = data.get(Keyword('type')) param_example = data.get(Keyword('example')) param_description = data.get(Keyword('description')) if param_type and param_example: param_key = key.name.replace('?', '') if param_type == 'array': param_value = ast.literal_eval(param_example) else: param_value = str(param_example) edn_dump[param_key] = param_value elif param_type and param_description: param_key = key.name.replace('?', '') edn_dump[param_key] = 'STUB' else: search(current_dict[key])
def send_stuff(self, port, painload): self.logger.info("Sending %s", str(painload)) sock = socket(AF_INET, SOCK_STREAM) sock.connect((self._ip, port)) sock.sendall(edn_format.dumps( {Keyword(k): v for k, v in painload.items()}).encode('utf8')) response = sock.recv(65535) self.logger.info('begin response') self.logger.info(response.decode('utf8')) self.logger.info('end response') val = edn_format.loads(response.decode('utf8') + " ") sock.close() return val
def test_keyword_keys(self): unchanged = ( None, True, 1, "foo", {}, {True: 42}, {25: 42}, {3.14: "test"}, {Keyword("foo"): "something"}, {Symbol("foo"): "something"}, ["foo", "bar"], ("foo", "bar"), {1: {2: 3}}, ImmutableDict({1: 2}), ) for case in unchanged: self.check_roundtrip(case, keyword_keys=True) keyworded_keys = ( ("{:foo 42}", {"foo": 42}), ("{:a {:b {:c 42} :d 1}}", {"a": {"b": {"c": 42}, "d": 1}}), ("[{:a 42} {:b 25}]", [{"a": 42}, {"b": 25}]), ("({:a 42} {:b 25})", ({"a": 42}, {"b": 25})), ("{1 [{:a 42}]}", {1: [{"a": 42}]}), ("{:foo 1}", ImmutableDict({"foo": 1})), ) for expected, data in keyworded_keys: self.assertEqual(expected, dumps(data, keyword_keys=True)) self.assertEqual( {Keyword("a"): {Keyword("b"): 2}, 3: 4}, loads(dumps({Keyword("a"): {"b": 2}, 3: 4}, keyword_keys=True)))
def register_user(self): user_id = self.generate_user_id() flag = self.get_flag(self.tick) self.logger.info("registering user %s", user_id) # store users users_blob, users_blob_name = self.get_users_blob() self.logger.info("old users: %s", users_blob) users_blob.append(user_id) self.store_yaml(users_blob_name, users_blob) self.logger.info("new users %s", users_blob) return {"request-type": Keyword("register-user"), "user-id": user_id, "security-token": flag}
def updateVmServiceImage(windowsVersions): vmServiceRepo = "github.com/circleci/vm-service.git" vmConfigFile = "config/config.edn" vmChartFile = "chrats/vm-service/templates/vm-service-config.yaml" vmServiceGit = Gitter("vm_service", vmServiceRepo, "windows-update/" + str(uuid4())) vmServiceGit.clone() vmServiceGit.branch() vmConfigPath = os.path.join(vmServiceGit.repoDir, vmConfigFile) vmChartPath = os.path.join(vmServiceGit.repoDir, vmChartFile) chartData = YAML() chartConfig = None config = None with open(vmChartPath, 'r') as chartFile: imageStubs = ["windows-server-2019-vs2019:", "windows-server-2019-nvidia-small:", "windows-server-2019-nvidia-medium:", "windows-server-2019-nvidia-large:"] imageTags = ["stable", "edge", "canary"] chartConfig = loads(chartData.load(chartFile)["data"]) # image config is in EDN format vmImages = chartConfig[Keyword("vm-manager")][Keyword("images")] for i in imageStubs: for tag in imageTags: vmImages[i+tag] = windowsVersions[i] chartConfig[Keyword("vm-manager")][Keyword("images")] = vmImages with open(vmConfigPath, 'r') as configFile: config = loads(configFile.read()) configImages = config[Keyword("vm-manager")][Keyword("images")]["gce"] configImages["windows-server-2019:e2e-test"] = windowsVersions["windows-server-2019-base"] configImages["windows-server-2019-vs2019:e2e-test"] = windowsVersions["windows-server-2019-nvidia-small"]
def test_list(self): self.check_parse("()", tuple()) self.check_parse("(:abc 1 true nil)", (Keyword("abc"), 1, True, None))
def test_hashing(self): pop_count = len( set(map(hash, ["db/id", Keyword("db/id"), Symbol("db/id")]))) self.assertEqual(pop_count, 3)
def test_indent(self): fixture = {"foo": "bar"} self.check_dumps('{"foo" "bar"}', fixture, indent=None) fixture = {Keyword("a"): {Keyword("b"): "foo"}} self.check_dumps('''\ { :a { :b "foo" } }''', fixture, indent=0) fixture = (1, Keyword("b"), "foo") self.check_dumps('''\ ( 1 :b "foo" )''', fixture, indent=1) fixture = [1, Keyword("b"), "foo", {Keyword("foo"): Keyword("bar")}] self.check_dumps('''\ [ 1 :b "foo" { :foo :bar } ]''', fixture, indent=2) fixture = {1, 2, 3} self.check_dumps('''\ #{ 1 2 3 }''', fixture, indent=4, sort_sets=True) fixture = [{"a": 42}] self.check_dumps('''\ [ { :a 42 } ]''', fixture, keyword_keys=True, indent=2) fixture = [[1, 2, 3]] self.check_dumps('''\ [ [ 1 2 3 ] ]''', fixture, indent=2) fixture = { "a": "foo", "b": { "c": [Keyword("a"), Keyword("b"), Keyword("c")] }, "d": {1, 2, 3}, "e": (datetime.date(2011, 10, 9), UUID("urn:uuid:1e4856a2-085e-45df-93e1-41a0c7aeab2e"), datetime.datetime(2012, 12, 22, 19, 40, 18, 0, tzinfo=pytz.utc)) } self.check_dumps('''\ { :a "foo" :b { :c [ :a :b :c ] } :d #{ 1 2 3 } :e ( #inst "2011-10-09" #uuid "1e4856a2-085e-45df-93e1-41a0c7aeab2e" #inst "2012-12-22T19:40:18.000000Z" ) }''', fixture, keyword_keys=True, sort_keys=True, sort_sets=True, indent=2)
def test_namespace(self): self.assertIsNone(Keyword("foo").namespace) self.assertEqual("a", Keyword("a/b").namespace) self.assertEqual("a", Keyword("a/b/d").namespace)
def test_name(self): for name in ("foo", "a/b", "a/b/c", "a:::::::b"): self.assertEqual(name, Keyword(name).name)
def get_status(self): return {"request-type": Keyword("status")}
import edn_format from edn_format import Keyword from sunflower.core.channel import Channel from sunflower.core.repository import RedisRepository from sunflower.stations import FranceCulture from sunflower.stations import FranceInfo from sunflower.stations import FranceInter from sunflower.stations import FranceInterParis from sunflower.stations import FranceMusique from sunflower.stations import PycolorePlaylistStation from sunflower.stations import RTL2 # read definitions with open("sunflower/conf.edn") as f: definitions = edn_format.loads(f.read()) channels_definitions = definitions[Keyword("channels")] stations_definitions = definitions[Keyword("stations")] # instantiate repository redis_repository = RedisRepository() # instantiate URL stations stations = { station_cls.name: station_cls() for station_cls in [ FranceCulture, FranceInfo, FranceInter, FranceMusique, FranceInterParis, RTL2 ] } # add dynamic stations
def check_success(self, response): try: return response[Keyword("response-type")] == Keyword("success") except: return False
def test_keyword(self): self.check_parse(":abc", Keyword("abc"))
import edn_format from edn_format import Keyword from fastapi import HTTPException from server.proxies import ChannelProxy from server.proxies import Proxy from sunflower.core.repository import RedisRepository from sunflower.core.stations import REVERSE_STATIONS # noinspection PyUnresolvedReferences from sunflower.stations import * # needed for view objects generation redis_repo = RedisRepository() # read definitions with open("sunflower/conf.edn") as f: definitions = edn_format.loads(f.read()) channels_definitions = definitions[Keyword("channels")] stations_definitions = definitions[Keyword("stations")] channels_ids = [channel_def[Keyword("id")] for channel_def in channels_definitions] def get_channel_or_404(channel: str): if channel not in channels_ids: raise HTTPException(404, f"Channel {channel} does not exist") return ChannelProxy(redis_repo, channel) def get_station_or_404(view_function): """Decorator checking if station is a valid endpoint to a dynamic station.""" @functools.wraps(view_function) def wrapper(station: str, *args, **kwargs): station_cls = REVERSE_STATIONS.get(station)
def test_combined(self): self.check_roundtrip({Keyword("a"): 1, "foo": Keyword("gone"), Keyword("bar"): [1, 2, 3]}, from_python=True)
def test_vector(self): self.check_parse("[]", []) self.check_parse("[1 2 3]", [1, 2, 3]) self.check_parse("[1 true nil]", [1, True, None]) self.check_parse('["abc", "123"]', ["abc", "123"]) self.check_parse("[:abc 1 true nil]", [Keyword("abc"), 1, True, None])
def check_status(self, response): return response[Keyword("response-type")] == Keyword("status")