Exemple #1
0
 def test_round_trip_inst_short(self):
     step1 = '#inst "2011"'
     step2 = loads(step1)
     step3 = dumps(step2)
     step4 = loads(step3)
     self.assertEqual('#inst "2011-01-01"', step3)
     self.assertEqual(datetime.date(2011, 1, 1), step4)
Exemple #2
0
    def test_discard_all(self):
        for edn_data in (
                '42',
                '-1',
                'nil',
                'true',
                'false',
                '"foo"',
                '\\space',
                '\\a',
                ':foo',
                ':foo/bar',
                '[]',
                '{}',
                '#{}',
                '()',
                '(a)',
                '(a b)',
                '[a [[[b] c]] 2]',
                '#inst "2017"',
        ):
            self.assertEqual([1], loads('[1 #_ {}]'.format(edn_data)),
                             edn_data)
            self.assertEqual([1], loads('[#_ {} 1]'.format(edn_data)),
                             edn_data)

            self.assertEqual(None, loads('#_ {}'.format(edn_data)))

            for coll in ('[%s]', '(%s)', '{%s}', '#{%s}'):
                expected = coll % ""
                edn_data = coll % '#_ {}'.format(edn_data)
                self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #3
0
 def test_round_trip_inst_short(self):
     step1 = '#inst "2011"'
     step2 = loads(step1)
     step3 = dumps(step2)
     step4 = loads(step3)
     self.assertEqual('#inst "2011-01-01"', step3)
     self.assertEqual(datetime.date(2011, 1, 1), step4)
Exemple #4
0
    def test_custom_tags(self):
        @tag("dog")
        def parse_dog(name):
            return {
                "kind": "dog",
                "name": name,
                "message": "woof-woof",
            }

        @tag("cat")
        class Cat(TaggedElement):
            def __init__(self, name):
                self.name = name

        dog = loads("#dog \"Max\"")
        self.assertEqual({
            "kind": "dog",
            "name": "Max",
            "message": "woof-woof"
        }, dog)

        cat = loads("#cat \"Alex\"")
        self.assertIsInstance(cat, Cat)
        self.assertEqual("Alex", cat.name)

        remove_tag("cat")
        self.assertRaises(NotImplementedError, lambda: loads("#cat \"Alex\""))

        add_tag("cat", Cat)
        cat = loads("#cat \"Alex\"")
        self.assertIsInstance(cat, Cat)
        self.assertEqual("Alex", cat.name)
Exemple #5
0
 def test_dumping(self):
     is_exception = False
     try:
         loads("[1 true nil]")
     except AttributeError as x:
         is_exception = True
     self.assertFalse(is_exception)
Exemple #6
0
 def test_dumping(self):
     is_exception = False
     try:
         loads("[1 true nil]")
     except AttributeError:
         is_exception = True
     self.assertFalse(is_exception)
Exemple #7
0
 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)
Exemple #8
0
 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)
Exemple #9
0
    def test_discard_all(self):
        for edn_data in (
            '42', '-1', 'nil', 'true', 'false', '"foo"', '\\space', '\\a',
            ':foo', ':foo/bar', '[]', '{}', '#{}', '()', '(a)', '(a b)',
            '[a [[[b] c]] 2]', '#inst "2017"',
        ):
            self.assertEqual([1], loads('[1 #_ {}]'.format(edn_data)), edn_data)
            self.assertEqual([1], loads('[#_ {} 1]'.format(edn_data)), edn_data)

            self.check_eof('#_ {}'.format(edn_data))

            for coll in ('[%s]', '(%s)', '{%s}', '#{%s}'):
                expected = coll % ""
                edn_data = coll % '#_ {}'.format(edn_data)
                self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #10
0
def getfile(kimid, filename, contentformat='str'):
    """
    Gets a file associated with a kim-item.

    Parameters
    ----------
    kimid : str
        The kim-id associated with the file. 
    filename : str
        The name of the file to download.
    contentformat : str
        The format to return the file contents as.  Supported values are
        'str' for str, 'edn' a dict of parsed edn content, ...  
        Default value is 'str'.

    Returns
    -------
    any
        The file contents loaded according to contentformat.
    """
    # Download web content
    url = 'https://openkim.org/files/' + shortid(kimid) + '/' + filename
    r = requests.get(url)
    r.raise_for_status()

    if contentformat == 'str':
        contents = r.text
    elif contentformat == 'edn':
        contents = edn_format.loads(r.text)
    else:
        raise ValueError('contentformat value not supported')
    return contents
Exemple #11
0
    def _parse_edn(self, fname, keys):
        """ Wrapper to use end_format to parse output file of 'runner' in edn format.

        Parameters
        ----------

        fname: str
          Name of the output file of OpenKIM test.

        keys: list of str
          Keyword in the edn format(think it as a dictionary), whose value will be returned.
        """

        with open(fname, "r") as fin:
            lines = fin.read()
        parsed = edn_format.loads(lines)
        values = []
        for k in keys:
            try:
                v = parsed[k]["source-value"]
            except KeyError:
                raise KeyError('Keyword "{}" not found in {}.'.format(
                    k, self.outname))
            # make it a 1D array
            # we expect v as a python built-in object (if numpy object, this will fail)
            if isinstance(v, collections.Sequence):
                v = np.array(v).flatten()
            else:
                v = [v]
            values.append(v)
        return np.concatenate(values)
Exemple #12
0
 def test_chained_discards(self):
     for expected, edn_data in (
         ('[]', '[#_ 1 #_ 2 #_ 3]'),
         ('[]', '[#_ #_ 1 2 #_ 3]'),
         ('[]', '[#_ #_ #_ 1 2 3]'),
     ):
         self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #13
0
 def test_chained_discards(self):
     for expected, edn_data in (
         ('[]', '[#_ 1 #_ 2 #_ 3]'),
         ('[]', '[#_ #_ 1 2 #_ 3]'),
         ('[]', '[#_ #_ #_ 1 2 3]'),
     ):
         self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #14
0
def bootstrap_fixtures(config, backend_uri, fixtures=["./tests/fixtures/messages.edn"]):
    bootstrap_schema(config, backend_uri)
    parsed_fixtures = []
    for fixture in fixtures:
        parsed = edn_format.loads(open(fixture).read())
        parsed_fixtures += parsed
    return transact_fn(config.berossus_url, parsed_fixtures)
Exemple #15
0
    def test_round_trip_conversion(self):
        EDN_LITERALS = [
            [r"\c", '"c"'],
            ["[ :ghi ]", "[:ghi]"],
            ["[:a #_foo 42]", "[:a 42]"],
            ["123N", "123"],
            ["-123N", "-123"],
            ["+123", "123"],
            ["+123N", "123"],
            ["123.2", "123.2"],
            ["+32.23M", "32.23M"],
            ["3.23e10", "32300000000.0"],
            ['#{:a (1 2 3) :b}', ['#{:a (1 2 3) :b}',
                                  '#{(1 2 3) :a :b}',
                                  '#{:a :b (1 2 3)}',
                                  '#{:b :a (1 2 3)}']]
        ]

        for literal in EDN_LITERALS:
            step1 = literal[0]
            step2 = loads(step1)
            step3 = dumps(step2)
#            print step1, "->", step2, "->", step3
            if isinstance(literal[1], list):
                self.assertIn(step3, literal[1])
            else:
                self.assertEqual(literal[1], step3)
Exemple #16
0
    def test_round_trip_same(self):
        EDN_LITERALS = ("nil", "true", "false", '"hello world"', ":keyword",
                        ":+", ":!", ":-", ":_", ":$", ":&", ":=", ":.",
                        ":abc/def", "symbol", "123", "-123", "32.23", "32.23M",
                        "-32.23M", "-3.903495E-73M", "3.23e-10", "3e+20",
                        "3E+20M", '["abc"]', '[1]', '[1 "abc"]',
                        '[1 "abc" true]', '[:ghi]', '(:ghi)',
                        '[1 "abc" true :ghi]', '(1 "abc" true :ghi)',
                        '{"a" 2}', '#inst "1985-04-12T23:20:50.000000Z"',
                        '#inst "2011-10-09"',
                        '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"',
                        '#date "19/07/1984"', '#{{"a" 1}}',
                        '#{{"a" #{{:b 2}}}}', '"|"')

        class TagDate(TaggedElement):
            def __init__(self, value):
                super(TagDate, self).__init__()
                self.name = 'date'
                self.value = datetime.datetime.strptime(value,
                                                        "%d/%m/%Y").date()

            def __str__(self):
                return '#{} "{}"'.format(self.name,
                                         self.value.strftime("%d/%m/%Y"))

        add_tag('date', TagDate)

        for literal in EDN_LITERALS:
            step1 = literal
            step2 = loads(step1)
            step3 = dumps(step2)
            self.assertEqual(step1, step3)
Exemple #17
0
    def test_round_trip_same(self):
        EDN_LITERALS = (
            "nil",
            "true",
            "false",
            '"hello world"',
            ":keyword",
            ":+",
            ":!",
            ":-",
            ":_",
            ":$",
            ":&",
            ":=",
            ":.",
            ":abc/def",
            "symbol",
            "123",
            "-123",
            "32.23",
            "32.23M",
            "-32.23M",
            "3.23e-10",
            '["abc"]',
            '[1]',
            '[1 "abc"]',
            '[1 "abc" true]',
            '[:ghi]',
            '(:ghi)',
            '[1 "abc" true :ghi]',
            '(1 "abc" true :ghi)',
            '{"a" 2}',
            '#inst "1985-04-12T23:20:50Z"',
            '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"',
            '#date "19/07/1984"',
            '#{{"a" 1}}',
            '#{{"a" #{{:b 2}}}}',
            '"|"'
        )

        class TagDate(TaggedElement):
            def __init__(self, value):
                self.name = 'date'
                self.value = datetime.datetime.strptime(
                    value,
                    "%d/%m/%Y").date()

            def __str__(self):
                return '#{} "{}"'.format(
                    self.name,
                    self.value.strftime("%d/%m/%Y"))

        add_tag('date', TagDate)

        for literal in EDN_LITERALS:
            step1 = literal
            step2 = loads(step1)
            step3 = dumps(step2)
#            print step1, "->", step2, "->", step3
            self.assertEqual(step1, step3)
Exemple #18
0
    def check_char(self, expected, name):
        edn_data = "\\{}".format(name)
        parsed = loads(edn_data)
        self.assertIsInstance(parsed, unicode)
        self.assertEqual(expected, parsed, edn_data)

        self.assertIsInstance(parsed, Char)
        self.assertEqual(Char(expected), parsed, edn_data)
Exemple #19
0
 def test_set_roundtrip(self):
     rt_value = dumps(loads('#{:a (1 2 3) :b}'))
     possible_vals = ['#{:a (1 2 3) :b}',
                      '#{:a :b (1 2 3)}',
                      '#{(1 2 3) :a :b}',
                      '#{(1 2 3) :b :a}',
                      '#{:b (1 2 3) :a}',
                      '#{:b :a (1 2 3)}']
     self.assertIn(rt_value, possible_vals)
Exemple #20
0
 def test_fractions(self):
     for edn_data in (
             '0/1',
             '1/1',
             '1/2',
             '1/3',
             '-5/3',
             '99999999999999999999999999999999999/999999999999999999999999991',
     ):
         self.assertEqual(edn_data, dumps(loads(edn_data)), edn_data)
Exemple #21
0
def consume_input():
    raw_message = raw_input()
    eprint(raw_message)
    message = dict(edn.loads(raw_message))
    op = message[edn.Keyword("f")]
    op_val = None
    if edn.Keyword("value") in message:
        op_val = dict(message[edn.Keyword("value")])
        message[edn.Keyword("value")] = op_val
    return message, op, op_val
Exemple #22
0
 def test_fractions(self):
     for edn_data in (
         '0/1',
         '1/1',
         '1/2',
         '1/3',
         '-5/3',
         '99999999999999999999999999999999999/999999999999999999999999991',
     ):
         self.assertEqual(edn_data, dumps(loads(edn_data)), edn_data)
def main():
    ednFile = "MattPublic.edn"
    with open(ednFile, 'r+') as f:
        content = f.read()
        # split out unneeded info from beginning and end of file
        content = content.split(":datoms ")[1][:-1]
        data = edn_format.loads(content)
    print(type(data))
    search = 24
    # for sublist in data:
    #     if sublist[0] == search:
    #         print(type(sublist[1]._name))
    pages = []
    search = 'node/title'
    for sublist in data:
        if sublist[1]._name == search:
            # print(sublist[2])
            individPage = {}
            for s in data:
                if s[0] == sublist[0]:
                    individPage["entity/id"] = s[0]
                    # testPage.append(sublist)
                    if s[1]._name == "block/children":
                        child = searchChildren(s[2])
                        individPage.setdefault("children", []).append(child)
                    elif s[1]._name == "node/title":
                        individPage["title"] = str(s[2])
                    elif s[1]._name == "edit/time":
                        individPage["edit-time"] = int(s[2])
                    elif s[1]._name == "create/time":
                        individPage["create-time"] = int(s[2])
                    elif s[1]._name == "block/uid":
                        individPage["uid"] = str(s[2])
                    elif s[1]._name == "block/string":
                        individPage["string"] = str(s[2])
                    elif s[1]._name == "block/heading":
                        individPage["heading"] = int(s[2])
                    elif s[1]._name == "block/text-align":
                        individPage["text-align"] = str(s[2])
                    elif s[1]._name == "children/view-type":
                        individPage["view-type"] = str(s[2])
                    elif s[1]._name == "block/order":
                        individPage["order"] = int(s[2])
                    else:
                        individPage[str(s[1]._name)] = str(s[2])
            pages.append(individPage)

    with open('About.json', 'w') as f:
        # todo sort by block/
        json.dump(pages,
                  f,
                  indent=4,
                  separators=(',', ': '),
                  ensure_ascii=False)
Exemple #24
0
    def test_round_trip_same(self):
        EDN_LITERALS = (
            "nil",
            "true",
            "false",
            '"hello world"',
            ":keyword",
            ":+",
            ":!",
            ":-",
            ":_",
            ":$",
            ":&",
            ":=",
            ":.",
            ":abc/def",
            #"symbol",
            "123",
            "-123",
            "32.23",
            "32.23M",
            "-32.23M",
            "3.23e-10",
            '["abc"]',
            '[1]',
            '[1 "abc"]',
            '[1 "abc" true]',
            '[:ghi]',
            '(:ghi)',
            '[1 "abc" true :ghi]',
            '(1 "abc" true :ghi)',
            #'#myapp/Person {:first "Fred" :last "Mertz',
            '#inst "1985-04-12T23:20:50Z"',
            '#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"',
            '#date "19/07/1984"')

        class TagDate(TaggedElement):
            def __init__(self, value):
                self.name = 'date'
                self.value = datetime.datetime.strptime(value,
                                                        "%d/%m/%Y").date()

            def __str__(self):
                return '#{} "{}"'.format(self.name,
                                         self.value.strftime("%d/%m/%Y"))

        add_tag('date', TagDate)

        for literal in EDN_LITERALS:
            step1 = literal
            step2 = loads(step1)
            step3 = dumps(step2)
            #            print step1, "->", step2, "->", step3
            self.assertEqual(step1, step3)
Exemple #25
0
def edn_loads_clean(x):
    def _clean_key(k):
        return k.name if isinstance(k, edn_format.Keyword) else k

    def _clean_dict(xs):
        if isinstance(xs, (dict, edn_format.immutable_dict.ImmutableDict)):
            return {_clean_key(k): _clean_dict(v) for k, v in xs.items()}
        elif isinstance(xs, (list, tuple, edn_format.immutable_list.ImmutableList)):
            return [_clean_dict(x) for x in xs]
        else:
            return _clean_key(xs)
    return _clean_dict(edn_format.loads(x))
Exemple #26
0
def parse_recipe(recipe_str):
    parsed = edn.loads('{' + recipe_str + '}')
    parse_kws = lambda d: {k.name: d.dict[k] for k in d.dict.keys()}
    recipe = parse_kws(parsed)
    ings = parse_ingredients(recipe['ingredients'])
    title = edn.dumps(recipe['title'])

    return {
        'title': title,
        'ingredients': ings,
        'description': recipe['description']
    }
 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
Exemple #28
0
def compare_file(output: str, f: File) -> List[dto.Assertion]:
    with open(output) as out:
        out_text = out.read()
        loaded_edn = edn_format.loads_all(out_text)
    parsed_edn = to_edn(f)

    results = []

    for (l, p) in zip(loaded_edn, parsed_edn):
        parsed_typed = edn_format.loads(edn_format.dumps(p))

        results.append(
            dto.Assertion(l, parsed_typed, edn_format.dumps(l),
                          edn_format.dumps(p)))
    return results
Exemple #29
0
    def decode_edn_msg(self, msg):
        """Decodes a TCP message from Carabiner to python dictionary"""
        msg = msg.decode()
        striped_msg = msg[msg.index('{'):]
        decoded_msg = edn_format.loads(striped_msg)

        # Because the edf_format package does not return normal dam dicts (or string keywords). What dicts.
        if type(decoded_msg) is edn_format.immutable_dict.ImmutableDict:
            decoded_dict = {}
            for key, value in decoded_msg.dict.items():
                print()
                decoded_dict[str(key).strip(':')] = value
            decoded_msg = decoded_dict

        return decoded_msg
Exemple #30
0
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"]
Exemple #31
0
    def decode_edn_msg(self, msg):
        """Decodes a TCP message from Carabiner to python dictionary"""
        msg = msg.decode()
        msg_type = msg[:msg.index(' ')]
        try:
            striped_msg = msg[msg.index('{'):]
            decoded_msg = edn_format.loads(striped_msg, write_ply_tables=False)
        except:
            decoded_msg = ""

        # Because the edn_format package does not return normal dam dicts (or string keywords). What dicts.
        if type(decoded_msg) is edn_format.immutable_dict.ImmutableDict:
            decoded_msg = {
                str(key).strip(':'): value
                for key, value in decoded_msg.dict.items()
            }

        return msg_type, decoded_msg
Exemple #32
0
 def rest(self, method, uri, data=None, status_codes=None, parse=True, **kwargs):
   """ Rest helpers
   """
   r = self.pool.request_encode_body(method, uri, fields=data, encode_multipart=False)
   if not r.status in (status_codes if status_codes else (200,201)):
     print(cl('\n---------\nURI / REQUEST TYPE : %s %s' % (uri, method), 'red'))
     print(cl(data, 'red'))
     print(r.headers)
     raise Exception("Invalid status code: %s" % r.status)
   if not parse: 
     " return raw urllib3 response"
     return r
   if not self.debug_loads:
     " return parsed edn"
     return loads(r.data)
   "time edn parse time and return parsed edn"
   return self.debug(loads, args=(r_data, ), kwargs={},
         fmt='<<< parsed edn datastruct in {ms}ms', color='green')
Exemple #33
0
    def test_round_trip_conversion(self):
        edn_literals = [
            ["[ :ghi ]", "[:ghi]"],
            ["[:a #_foo 42]", "[:a 42]"],
            ["123N", "123"],
            ["-123N", "-123"],
            ["+123", "123"],
            ["+123N", "123"],
            ["123.2", "123.2"],
            ["+32.23M", "32.23M"],
            ["3.23e10", "32300000000.0"],
            ["3e10", "30000000000.0"],
        ]

        for literal in edn_literals:
            step1 = literal[0]
            step2 = loads(step1)
            step3 = dumps(step2)
            self.assertEqual(literal[1], step3)
def get_openkim_citation(p, it, col):
    extent, error = col.cache_list_of_item_metadata_files(it, p)
    if error == 0:
        cite_lst = []
        for i in range(extent):
            out = col.get_item_metadata_file(i)
            file_name, file_length, file_raw_data, avail_as_str, file_str, error = out
            if file_name == "kimspec.edn":
                edn_dict = edn_format.loads(file_str)
                if "source-citations" in edn_dict.keys():
                    for cite in edn_dict["source-citations"]:
                        cite_dict = {k: cite[k] for k in ["title", "volume", "year", "journal", "doi"] if k in cite.keys()}
                        cite_dict["author"] = cite['author'].split("and")
                        if "issue" in cite.keys():
                            cite_dict["number"] = cite["issue"]
                        name = cite_dict["author"][0].split()[-1] + "_" + cite_dict["year"]
                        cite_lst.append({name: cite_dict})
        return cite_lst
    else:
        return []
Exemple #35
0
def next_data(din, dout):
    skip = False
    data = None

    if isinstance(din, LineMapper):
        return din.next_data()

    unit = din.readline()
    end = not bool(unit)

    if end:
        return skip, end, data

    try:
        data = edn_format.loads(unit)
    except SyntaxError as err:
        print_error(str(err), 500, dout)
        skip = True

    return skip, end, data
Exemple #36
0
def next_data(din, dout):
    skip = False
    data = None

    if isinstance(din, LineMapper):
        return din.next_data()

    unit = din.readline()
    end = not bool(unit)

    if end:
        return skip, end, data

    try:
        data = edn_format.loads(unit)
    except SyntaxError as err:
        print_error(str(err), 500, dout)
        skip = True

    return skip, end, data
Exemple #37
0
 def test_discard(self):
     for expected, edn_data in (
         ('[x]', '[x #_ z]'),
         ('[z]', '[#_ x z]'),
         ('[x z]', '[x #_ y z]'),
         ('{1 4}', '{1 #_ 2 #_ 3 4}'),
         ('[1 2]', '[1 #_ [ #_ [ #_ [ #_ [ #_ 42 ] ] ] ] 2 ]'),
         ('[1 2 11]', '[1 2 #_ #_ #_ #_ 4 5 6 #_ 7 #_ #_ 8 9 10 11]'),
         ('()', '(#_(((((((1))))))))'),
         ('[6]', '[#_ #_ #_ #_ #_ 1 2 3 4 5 6]'),
         ('[4]', '[#_ #_ 1 #_ 2 3 4]'),
         ('{:a 1}', '{:a #_:b 1}'),
         ('[42]', '[42 #_ {:a [1 2 3 4] true false 1 #inst "2017"}]'),
         ('#{1}', '#{1 #_foo}'),
         ('"#_ foo"', '"#_ foo"'),
         (r'[\# _]', r'[\#_]'),
         ('[_]', r'[#_\#_]'),
         ('[1]', '[1 #_\n\n42]'),
         ('{}', '{#_ 1}'),
     ):
         self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #38
0
 def test_discard(self):
     for expected, edn_data in (
         ('[x]', '[x #_ z]'),
         ('[z]', '[#_ x z]'),
         ('[x z]', '[x #_ y z]'),
         ('{1 4}', '{1 #_ 2 #_ 3 4}'),
         ('[1 2]', '[1 #_ [ #_ [ #_ [ #_ [ #_ 42 ] ] ] ] 2 ]'),
         ('[1 2 11]', '[1 2 #_ #_ #_ #_ 4 5 6 #_ 7 #_ #_ 8 9 10 11]'),
         ('()', '(#_(((((((1))))))))'),
         ('[6]', '[#_ #_ #_ #_ #_ 1 2 3 4 5 6]'),
         ('[4]', '[#_ #_ 1 #_ 2 3 4]'),
         ('{:a 1}', '{:a #_:b 1}'),
         ('[42]', '[42 #_ {:a [1 2 3 4] true false 1 #inst "2017"}]'),
         ('#{1}', '#{1 #_foo}'),
         ('"#_ foo"', '"#_ foo"'),
         ('["#" _]', r'[\#_]'),
         ('[_]', r'[#_\#_]'),
         ('[1]', '[1 #_\n\n42]'),
         ('{}', '{#_ 1}'),
     ):
         self.assertEqual(expected, dumps(loads(edn_data)), edn_data)
Exemple #39
0
def parse_edn_spec_file(path: Path) -> dict:
    """Find a possible API param examples in a debug .edn file.
    If the keyword has a 'type', 'example', and 'description' property
    then it's considered to be an API param.
    Example of entry in edn:
    `{:name {:description "Product", :type "string", :example "Whiskey"}}`
    It will be parsed to {"name": "Whiskey"}
    """
    import edn_format
    from edn_format import Keyword
    from edn_format.immutable_dict import ImmutableDict

    edn = edn_format.loads(path.read_text())
    edn_dump = {}

    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])

    search(edn)
    return edn_dump
Exemple #40
0
    def test_round_trip_conversion(self):
        EDN_LITERALS = [
            [r"\c", '"c"'],
            ["[ :ghi ]", "[:ghi]"],
            ["[:a #_foo 42]", "[:a 42]"],
            ["123N", "123"],
            ["-123N", "-123"],
            ["+123", "123"],
            ["+123N", "123"],
            ["123.2", "123.2"],
            ["+32.23M", "32.23M"],
            ["3.23e10", "32300000000.0"],
            ["3e10", "30000000000.0"],
        ]

        for literal in EDN_LITERALS:
            step1 = literal[0]
            step2 = loads(step1)
            step3 = dumps(step2)
            if isinstance(literal[1], list):
                self.assertIn(step3, literal[1])
            else:
                self.assertEqual(literal[1], step3)
Exemple #41
0
    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)))
Exemple #42
0
    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)))
Exemple #43
0
def generate_uuid():
    return loads("#uuid \"%s\"" % uuid.uuid1())
Exemple #44
0
 def parse(self, code):
     return edn_format.loads(code)
Exemple #45
0
 def check_roundtrip(self, data_input):
     self.assertEqual(data_input, loads(dumps(data_input)))
Exemple #46
0
  def __str__(self): 
     return "#myapp/Person " + edn_format.dump(self.value)

edn_format.add_tag("myapp/Person", MyappPerson)

validEdnDir = "../../../../valid-edn"
results = {}

for ednFile in [f for f in listdir(validEdnDir) if isfile(join(validEdnDir, f))]:
  ednFileName = ednFile.split(".")[0]
  validEdn = open(join(validEdnDir, ednFileName + ".edn"), "r").read()
  expectedPy = open(join("..", ednFileName + ".py"), "r").read()
  if debug: 
    print ednFileName
  try: 
    expected = eval(expectedPy)
    parsed = edn_format.loads(validEdn)
    result = expected == parsed
    if not result and debug:
      print "Values did not match", "\n\tEXPECTED: ", edn_format.dumps(expected), "\n\tPARSED: ", edn_format.dumps(parsed), "\n\tEDN: ", validEdn
    results[edn_format.Symbol(ednFileName)] = result
  except:
    e = sys.exc_info()[0] 
    results[edn_format.Symbol(ednFileName)] = False
    if debug:
      print "\tFailed to parse.", validEdn, e

print edn_format.dumps(results)

Exemple #47
0
 def test_exceptions(self):
     with self.assertRaises(EDNDecodeError):
         loads("{")
Exemple #48
0
    def eval(self, string):
        ls = loads(string)
        ret = unicode(self._eval(ls))

        return ret
Exemple #49
0
 def check_roundtrip(self, data_input, expected_value, from_python=False):
     rt_value = (loads(dumps(data_input)) if from_python else
                 dumps(loads(data_input)))
     self.assertEqual(expected_value, rt_value)
Exemple #50
0
 def test_nested_quotes(self):
     self.check_roundtrip('nested "quotes"', from_python=True)
     self.assertEqual(loads(dumps('nested "quotes"')),
                      loads(dumps(loads(dumps('nested "quotes"')))))
Exemple #51
0
def inject_id(d, dbid=loads("#db/id[:db.part/user]")):
    d[K("db/id")] = dbid
    return d
Exemple #52
0
 print("<table class='table table-striped table-hover table-responsive'><thead><tr><th>Test</th>")
 for n in nemeses:
     print("<th><a href='" + cpath + "?filter=*:" + n + "'>" +  n + "</a></th>")
 print("</tr></thead><tbody>")
 for t in tests:
     print("<tr><td><a href='" + cpath + "?filter=" + t + ":*'>" + t.replace("cockroachdb-","") + "</a></td>")
     for n in nemeses:
         print("<td>")
         tn = t + ':' + n
         dr = sorted([x for x in rl if x[0] == tn])
         if len(dr) > 0:
             d = dr[-1]
             dpath = os.path.join(d[0],d[1])
             ednpath = os.path.join(dpath, 'results.edn')
             with open(ednpath) as f:
                 r = edn_format.loads(f.read())
                 if r is not None:
                     ok = r[edn_format.Keyword('valid?')]
                     status = {True:'success',False:'danger'}[ok]
                     ts = d[1][:-5]
                     #lfile = os.path.join(dpath, "latency-raw.png")
                     #if os.path.exists(lfile):
                     #    print("<a href='/" + lfile + "'>" 
                     #          "<img height=60px src='/" + lfile + "' />"
                     #          "</a>")
                     print("<a href='" + cpath + "?entry=" + ts + "#" + ts + "' class='btn btn-%s btn-xs'>" % status + reltime(ts) +
                           ' <span class="glyphicon glyphicon-info-sign"></span>'
                           "</a>")
         print("</td>")
     print("</tr>")
 print("</tbody></table>")
Exemple #53
0
 def test_chars(self):
     # 33-126 = printable ASCII range, except space (code 32)
     for i in range(33, 127):
         ch = chr(i)
         edn_data = "\\{}".format(ch)
         self.assertEqual(ch, loads(edn_data), edn_data)
Exemple #54
0
cur.execute("delete from projects;")

ordering = []
with open("ordering", "r") as f:
	for line in f:
		ordering.append(line.strip().lower())

def order(project):
	title = project[TITLE].lower()
	if title in ordering:
		return ordering.index(title)
	else:
		return len(ordering)

file_names = [f for f in listdir(".") if re.match("^(.*?).clj$", f)]

for file_name in file_names:
	with open(file_name, "r") as f:
		print file_name
		content	= f.read()
		project	= edn.loads(content)

		url = project[TITLE].lower()
		url = re.sub(" ", "-", url)
		url = re.sub("[^\w\-]", "", url)

		cur.execute("insert into projects (url, ordering, data) values (%s, %s, %s)",
				(url, order(project), content))

db.commit()
Exemple #55
0
    def check_eof(self, data_input, **kw):
        with self.assertRaises(EDNDecodeError) as ctx:
            loads(data_input, **kw)

        self.assertEqual('EOF Reached', str(ctx.exception))
Exemple #56
0
 def test_discard_syntax_errors(self):
     for edn_data in ('#_', '#_ #_ 1', '#inst #_ 2017', '[#_]'):
         with self.assertRaises(EDNDecodeError):
             loads(edn_data)
Exemple #57
0
def load_edn(path):
    with open(path, "r") as f:
        return edn_format.loads(f.read())
Exemple #58
0
import edn_format
from sys import argv

if len(argv) < 1 + 1:
	print "Supply data file name"
	exit(1)

with open(argv[1], 'r') as f:
	data_string = f.read()
	print data_string

	data = edn_format.loads(data_string)
	print data