Beispiel #1
0
 def getChangable(self):
     ret = {}
     for key, gener in self.flat_params.items():
         temp = gener.x
         if isinstance(temp, unicode): temp = temp.encode('ascii', 'ignore')
         ret[key] = temp
     return unflatten(ret)
Beispiel #2
0
 def reset(self, jsonStr):
     raw = json.loads(jsonStr)
     self.flat_params = flatten(raw)
     for key in self.flat_params:
         val, dtype = checkAndConvert(self.flat_params[key])
         self.flat_params[key] = val
     self.params = unflatten(self.flat_params)
Beispiel #3
0
def parse_params(path, naked_params):

    flat_params = {}

    for param in naked_params:

        if 'SecureString' in param.values():
            param['Value'] = encrypt(param['Value'])

        key = param['Name']
        key = key.replace(f'{path}', '')
        value = param['Value']

        try:
            value = int(value)
        except:
            logger.info('Value is not an int.')

        new_param = {key: value}
        flat_params = {**flat_params, **new_param}
    
    logger.info('Flat params parsed!')
    
    unflat_params = unflatten(flat_params, '.')
    logger.info(f'Params: {unflat_params}')

    params = json.dumps(unflat_params)

    return params
Beispiel #4
0
def analyse_meta(soup=None, analyse_elements=None, website=None):
    meta_data_dict = {}
    all_meta_elems = soup.select('meta')
    for meta_elem in all_meta_elems:
        meta_property = meta_elem.get('property')
        if meta_property:
            meta_data_dict[meta_property.replace(":", "__")] = meta_elem.get('content')

    return unflatten(meta_data_dict, separator="__")
Beispiel #5
0
 def set(self, *args):
     assert len(args) > 1, 'key,..., value'
     key = getKey(args[:-1])
     val, dtype = checkAndConvert(args[-1])
     if key in self.params:
         self.params[key] = val
         self.flat_params = flatten(self.params)
     elif key in self.flat_params:
         self.flat_params[key] = val
         self.params = unflatten(self.flat_params)
Beispiel #6
0
 def reset(self, jsonStr):
     if isinstance(jsonStr, string_types):
         raw = json.loads(jsonStr)
     else:
         raw = jsonStr
     self.flat_params = flatten(raw)
     for key in self.flat_params:
         val = checkAndConvertObject(self.flat_params[key])
         self.flat_params[key] = val
     self.params = unflatten(self.flat_params)
Beispiel #7
0
 def test_unflatten_no_list(self):
     dic = {
         'a': 1,
         'b_a': 2,
         'b_b': 3,
         'c_a_b': 5
     }
     expected = {
         'a': 1,
         'b': {'a': 2, 'b': 3},
         'c': {'a': {'b': 5}}
     }
     actual = unflatten(dic)
     self.assertEqual(actual, expected)
Beispiel #8
0
 def test_unflatten_no_list(self):
     dic = {
         'a': 1,
         'b_a': 2,
         'b_b': 3,
         'c_a_b': 5
     }
     expected = {
         'a': 1,
         'b': {'a': 2, 'b': 3},
         'c': {'a': {'b': 5}}
     }
     actual = unflatten(dic)
     self.assertEqual(actual, expected)
Beispiel #9
0
def read_config():
    if request.method == 'GET':
        try:
            with open(CONFIG_LOCATION, 'r') as f:
                config = json.load(f)
        except Exception as e:
            return str(e)
        flat_json = [{
            'name': k,
            'value': v
        } for k, v in flatten_json(config).items()]
        sorted_flat_json = sorted(flat_json, key=lambda k: k['name'])
        return jsonify(sorted_flat_json)
    elif request.method == 'POST':
        result = request.form
        result = unflatten(result)
        with open(CONFIG_LOCATION, 'w') as f:
            json.dump(result, f, indent=4)

        return redirect(url_for('scoreboard'))
def read_all():
    """
    This function responds to a request for /api/landingZoneWANs
    with the complete lists of landingZoneWANs

    :return:        json string of list of landingZoneWANs
    """

    # Create the list of people from our data
    landingZoneWANs = LandingZoneWAN.query.all()
    landingZoneWANArr = []
    for lzw in landingZoneWANs:
        landingZoneWANArr.append(unflatten(lzw.__dict__, delimiter))

    # Serialize the data for the response
    eschema = ExtendedLandingZoneWANSchema(many=True)
    data = eschema.dump(landingZoneWANArr)
    app.logger.debug("landingZoneWAN data:")
    app.logger.debug(pformat(data))
    return data
Beispiel #11
0
def _parse_params(path_params):
    from flatten_json import unflatten
    from yaml import safe_load, YAMLError
    from dvc.dependency.param import ParamsDependency

    ret = {}
    for path_param in path_params:
        path, _, params_str = path_param.rpartition(":")
        # remove empty strings from params, on condition such as `-p "file1:"`
        params = {}
        for param_str in filter(bool, params_str.split(",")):
            try:
                # interpret value strings using YAML rules
                key, value = param_str.split("=")
                params[key] = safe_load(value)
            except (ValueError, YAMLError):
                raise InvalidArgumentError(
                    f"Invalid param/value pair '{param_str}'")
        if not path:
            path = ParamsDependency.DEFAULT_PARAMS_FILE
        ret[path] = unflatten(params, ".")
    return ret
def read_one(oid):
    """
    This function responds to a request for /api/landingZoneWAN/{oid}
    with one matching landingZoneWAN from landingZoneWANs

    :param landingZoneWAN:   id of the landingZoneWAN to find
    :return:              landingZoneWAN matching the id
    """

    landingZoneWAN = (LandingZoneWAN.query.filter(
        LandingZoneWAN.id == oid).one_or_none())

    if landingZoneWAN is not None:
        # Serialize the data for the response
        landingZoneWAN_schema = ExtendedLandingZoneWANSchema(many=False)
        unflattened_landingZoneWAN = unflatten(landingZoneWAN.__dict__,
                                               delimiter)
        data = landingZoneWAN_schema.dump(unflattened_landingZoneWAN)
        app.logger.debug("landingZoneWAN data:")
        app.logger.debug(pformat(data))
        return data
    else:
        abort(404, f"LandingZoneWAN with id {oid} not found".format(id=oid))
def _to_jsonld(microdata):
    context = 'http://schema.org'
    properties = 'properties_'
    typestr = 'type'
    jsonld_data = {}
    jsonld_data["@context"] = context
    for data in microdata:
        data = flatten_json.flatten(data)
        for key in data.keys():
            value = data[key]
            if context in value:
                value = value.replace(context + "/", "")
            if (properties in key):
                keyn = key.replace(properties, "")
                jsonld_data[keyn] = value
                if (typestr in keyn):
                    keyn = keyn.replace(typestr, "@" + typestr)
                    jsonld_data[keyn] = value
            if (typestr is key):
                keyn = key.replace(typestr, "@" + typestr)
                jsonld_data[keyn] = value
        del data
    jsonld_data = flatten_json.unflatten(jsonld_data)
    return [jsonld_data]
Beispiel #14
0
 def __str__(self):
     ret_dict = {}
     for key in self.flat_params:
         ret_dict[key] = str(self.flat_params[key])
     ret_dict = unflatten(ret_dict)
     return json.dumps(ret_dict, indent=4)
def group_underscore_key(row):
    pprint.pprint(row)
    return unflatten(row, '__')
Beispiel #16
0
    def refsolver(self, fileloc):
        try:
            json_path = fileloc
        except Exception as e:
            print('json-path-file-issue', e)
        makeJsoncCommon = []
        makeJsoncCommon = json_path.split('/')
        cPathPath = ''
        for cPath in makeJsoncCommon[:len(makeJsoncCommon) - 1]:
            cPathPath = cPathPath + '/' + cPath
        json_common = cPathPath[1:] + '/'
        list = []
        JDF_list = []
        InnerDictJSON = dict()

        def find_nth(string, substring, n):
            if (n == 1):
                return string.find(substring)
            else:
                return string.find(substring,
                                   find_nth(string, substring, n - 1) + 1)

        def update_json(json_path):
            with open(json_path) as json_file:
                data = json.load(json_file)
                data['properties'].update(InnerDictJSON)
            return data

        with open(json_path) as json_file:
            data = json.load(json_file)
        d = flatten(data)
        for key in d:
            if key[len(key) - 4:len(key)] == '$ref':
                a = json_common + d[key][:d[key].find('#')]
                list = d[key][d[key].find('properties/'):].split('/')
                with open(a) as json_file:
                    data1 = json.load(json_file)
                    x = "data1" + "['" + list[0] + "']"
                    eval_val = eval(x)
                    for test1 in eval_val:
                        for j in list:
                            if j in (test1):
                                dict1 = {j: eval_val[test1]}
                                InnerDictJSON.update(dict1)
        fnl_json = update_json(json_path)
        FltJSON = flatten(fnl_json)

        def merge_json(FltJSON, InnerDictJSON):
            for InFltJSON in FltJSON:
                if InFltJSON[len(InFltJSON) - 4:len(InFltJSON)] == '$ref':
                    str = InFltJSON
                    refListVals = []
                    refListVals = FltJSON[InFltJSON].split('/')
                    #teston feb2020
                    refKeyTag = refListVals[len(refListVals) - 1]
                    if refKeyTag in InnerDictJSON:
                        f = dict({str: InnerDictJSON[refKeyTag]})
                        FltJSON.update(f)
            return FltJSON

        FltJSON = merge_json(FltJSON, InnerDictJSON)
        x = flatten(FltJSON)

        def json_key_replace(x, charval, replace_with):
            for y in x:
                InFltJSON = y.replace(charval, replace_with)
                x[InFltJSON] = x.pop(y)
            return x

        x = json_key_replace(x, '_$ref_', '_')
        fnl_json = unflatten(x)
        build_flat = flatten(fnl_json)
        build_flat = json_key_replace(build_flat, '_$ref_', '_')
        try:
            fnl_json = unflatten(build_flat)
        except Exception as e:
            print('Issue Raised with unFlatten', e)
        try:
            FltJSON = flatten(fnl_json)
        except Exception as e:
            print('Issue Raised while Flatten', e)
        FltJSON = merge_json(FltJSON, InnerDictJSON)
        x = flatten(FltJSON)
        try:
            x = json_key_replace(x, '_$ref', '')
            x = json_key_replace(x, '_$ref_', '')
            x = json_key_replace(x, '$ref_', '')
            x = json_key_replace(x, '_0', '0')
            x = json_key_replace(x, '_1', '1')
        except Exception as e:
            print('issue raised with json_key_replace', e)
        try:
            fnl_json = unflatten(x)
        except Exception as e:
            e
        modlist = []
        for diffnode in fnl_json['properties']:
            if not diffnode in data['properties']:
                modlist.append(diffnode)
        for fnlreplacement in modlist:
            fnl_json['properties'].pop(fnlreplacement)
        try:
            fnl_json = json.dumps(fnl_json)
        except Exception as e:
            fnl_json = ''
            print('Failed:', e)
        return fnl_json
Beispiel #17
0
 def unflatten(self, flatten):
     return flatten_json.unflatten(flatten, separator='__')