示例#1
0
 def test_basicdump(self):
     assert dump(Person()) == {}
     assert dump(Person('Alfio')) == {'name': 'Alfio'}
     assert dump(Person('Alfio', '33')) == {
         'name': 'Alfio',
         'address': '33'
     }
示例#2
0
    def test_factory_dump(self):
        @attrs
        class A:
            a = attrib(factory=list, metadata={'ciao': 'ciao'}, type=List[int])

        assert dump(A()) == {}
        assert dump(A(), hidedefault=False) == {'a': []}
示例#3
0
    def test_factory_dump(self):
        @dataclass
        class A:
            a: int
            b: List[int] = field(default_factory=list)

        assert dump(A(3)) == {'a': 3}
        assert dump(A(12), hidedefault=False) == {'a': 12, 'b': []}
示例#4
0
    def test_dump(self):
        @dataclass
        class A:
            a: int
            b: int = 0

        assert dump(A(12)) == {'a': 12}
        assert dump(A(12), hidedefault=False) == {'a': 12, 'b': 0}
示例#5
0
    def test_weird_mangle(self):
        @attrs
        class Mangle:
            a = attrib(type=int, metadata={'name': 'b', 'alt': 'q'})
            b = attrib(type=str, metadata={'name': 'a'})

        assert load({'b': 1, 'a': 'ciao'}, Mangle) == Mangle(1, 'ciao')
        assert load({
            'q': 1,
            'b': 'ciao'
        }, Mangle, mangle_key='alt') == Mangle(1, 'ciao')
        assert dump(Mangle(1, 'ciao')) == {'b': 1, 'a': 'ciao'}
        assert dump(Mangle(1, 'ciao'), mangle_key='alt') == {
            'q': 1,
            'b': 'ciao'
        }
示例#6
0
    def test_norepr(self):
        @attrs
        class A:
            i = attrib(type=int)
            j = attrib(type=int, repr=False)

        assert dump(A(1, 1)) == {'i': 1}
示例#7
0
    def test_weird_mangle(self):
        @dataclass
        class Mangle:
            a: int = field(metadata={'name': 'b', 'alt': 'q'})
            b: str = field(metadata={'name': 'a'})

        assert load({'b': 1, 'a': 'ciao'}, Mangle) == Mangle(1, 'ciao')
        assert load({
            'q': 1,
            'b': 'ciao'
        }, Mangle, mangle_key='alt') == Mangle(1, 'ciao')
        assert dump(Mangle(1, 'ciao')) == {'b': 1, 'a': 'ciao'}
        assert dump(Mangle(1, 'ciao'), mangle_key='alt') == {
            'q': 1,
            'b': 'ciao'
        }
示例#8
0
    def test_mangle_load(self):
        @dataclass
        class Mangle:
            value: int = field(metadata={'name': 'va.lue'})

        assert load({'va.lue': 1}, Mangle) == Mangle(1)
        assert dump(Mangle(1)) == {'va.lue': 1}
 def test_dump_load_results(self):
     results = ExamResults([
         (Student('Anna', 1), Result.PASS),
         (Student('Alfio', 2), Result.PASS),
         (Student('Iano', 3, '*****@*****.**'), Result.PASS),
     ])
     assert load(dump(results), ExamResults) == results
示例#10
0
    def test_mangle_rename(self):
        @attrs
        class Mangle:
            a = attrib(type=int, metadata={'name': 'b'})
            b = attrib(type=str, metadata={'name': 'a'})

        assert load({'b': 1, 'a': 'ciao'}, Mangle) == Mangle(1, 'ciao')
        assert dump(Mangle(1, 'ciao')) == {'b': 1, 'a': 'ciao'}
示例#11
0
    def test_mangle_rename(self):
        @dataclass
        class Mangle:
            a: int = field(metadata={'name': 'b'})
            b: str = field(metadata={'name': 'a'})

        assert load({'b': 1, 'a': 'ciao'}, Mangle) == Mangle(1, 'ciao')
        assert dump(Mangle(1, 'ciao')) == {'b': 1, 'a': 'ciao'}
示例#12
0
def analyze(so_fname, cache_settings):
    """
    Try to find strings and references to them from code in binary file (*.so).

    :param so_fname: Path to binary.
    :param cache_settings: Instance of CacheSettings.
    :return: Dict containing all information required for live-patching.
    """

    strs_to_replace = frozenset(
        itertools.chain(_REPLACE_DOMAIN.keys(), _REPLACE_REGION.keys()))

    # TODO: Use lock file to prevent simultaneous read/write of cache file from
    # different threads or processes.
    _L.info(f'Reading cache from "{cache_settings.cache_file}"...')
    try:
        with open(cache_settings.cache_file, 'r') as cache_f:
            cache = typedload.load(
                json.load(cache_f),
                typing.Dict[str, typing.Dict[str, typing.List[r2.BinString]]])
    except FileNotFoundError:
        cache = {}

    _L.debug(f'Calculating sha512("{so_fname}")...')
    with open(so_fname, 'rb') as so_f:
        so_sha512 = hashlib.sha512(so_f.read()).hexdigest()
    _L.info(f'sha512("{so_fname}") == "{so_sha512}"')

    if cache_settings.use_cache:
        strings = cache.get(so_sha512)
    else:
        strings = None

    if strings is None:
        _L.info('Binary not found in cache (slow path)')
        strings = r2.find_strings(so_fname, strs_to_replace)

        if cache_settings.update_cache:
            _L.info(f'Writing cache into "{cache_settings.cache_file}"...')

            cache[so_sha512] = strings

            cache_file_dir = os.path.dirname(cache_settings.cache_file)
            os.makedirs(cache_file_dir, mode=0o700, exist_ok=True)

            with atomicwrites.atomic_write(cache_settings.cache_file,
                                           overwrite=True) as cache_f:
                json.dump(typedload.dump(cache), cache_f)
    else:
        _L.info('Using cached values')

    n_xrefs = sum(
        len(bin_str.xrefs) for bin_strs in strings.values()
        for bin_str in bin_strs)
    _L.info(f'Found {n_xrefs} references to strings in "{so_fname}"')
    _L.debug(f'References: {strings}')

    return strings
示例#13
0
 def save(self, filename: Union[Path, str]) -> None:
     '''
     Saves the relation in a file.
     Will save using the json format
     '''
     with open(filename, 'w') as fp:
         from json import dump as jdump
         from typedload import dump
         jdump(dump(self), fp)
示例#14
0
    def test_case(self):
        @dataclass
        class Mangle:
            value: int = field(metadata={'name': 'Value'})

        assert load({'Value': 1}, Mangle) == Mangle(1)
        assert 'Value' in dump(Mangle(1))
        with self.assertRaises(ValueError):
            load({'value': 1}, Mangle)
示例#15
0
    def test_case(self):
        @attrs
        class Mangle:
            value = attrib(type=int, metadata={'name': 'Value'})

        assert load({'Value': 1}, Mangle) == Mangle(1)
        assert 'Value' in dump(Mangle(1))
        with self.assertRaises(TypeError):
            load({'value': 1}, Mangle)
 def test_ipaddress(self):
     assert dump(IPv4Address('10.10.10.1')) == '10.10.10.1'
     assert dump(IPv4Network('10.10.10.0/24')) == '10.10.10.0/24'
     assert dump(IPv4Interface('10.10.10.1/24')) == '10.10.10.1/24'
     assert dump(IPv6Address('fe80::123')) == 'fe80::123'
     assert dump(IPv6Network('fe80::/64')) == 'fe80::/64'
     assert dump(IPv6Interface('fe80::123/64')) == 'fe80::123/64'
def main() -> None:
    """ Make a jazz noise here """

    args = get_args()
    schema = xmlschema.XMLSchema(args.schema.name)
    num_files = len(list(args.files))

    print(f'Processing {num_files:,} file{"" if num_files == 1 else "s"}.')

    num_written = 0
    errors = []
    for file in track(args.files, description="Processing..."):
        # Determine outfile
        basename = os.path.basename(file)
        root = os.path.splitext(basename)[0]
        out_file = os.path.join(args.outdir, root + '.json')
        # print(file)

        # Skip existing files
        # if os.path.isfile(out_file):
        #     continue

        # Set the "text" to all the distinct words
        # xml = xmltodict.parse(open(file).read())

        xml = open(file).read()
        if not schema.is_valid(xml):
            errors.append(f'Invalid document "{file}"')
            continue

        data = schema.to_dict(xml)
        tree = ElementTree().parse(file)
        all_text = ' '.join(
            set(chain.from_iterable((map(words, flatten(tree))))))

        study = restructure(data, all_text)
        # pprint(study)

        # Convert to JSON
        out_fh = open(out_file, 'wt')
        out_fh.write(json.dumps(typedload.dump(study), indent=4) + '\n')
        out_fh.close()
        num_written += 1

    if errors:
        print('\n'.join([f'{len(errors)} ERRORS:'] + errors), file=sys.stderr)

    print(f'Done, wrote {num_written:,} to "{args.outdir}".')
示例#18
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-u',
                        '--url',
                        help='The URL that points to the ads.txt',
                        required=True,
                        type=str)

    args = parser.parse_args()

    recursive_parser(args.url, sld=True)

    # to json
    as_json = typedload.dump(global_state.results)

    with open('./output/data.json', 'w') as fh:
        json.dump(as_json, fh)
示例#19
0
 def test_nesteddump(self):
     assert dump(
         Students('advanced coursing', [
             Person('Alfio'),
             Person('Carmelo', 'via mulino'),
         ])) == {
             'course':
             'advanced coursing',
             'students': [
                 {
                     'name': 'Alfio'
                 },
                 {
                     'name': 'Carmelo',
                     'address': 'via mulino'
                 },
             ]
         }
示例#20
0
 def diffmsg(self) -> Message:
     m = dump(self.current)
     m['text'] = seddiff(self.previous.text, self.current.text)
     return load(m, Message)
示例#21
0
 def save_alarm(self):
     serialized_alarm = json.dumps(typedload.dump(self.alarm))
     with open("alarm.json", "w") as f:
         f.write(serialized_alarm)
示例#22
0
 def get_status(self) -> bytes:
     '''
     A status string that will be passed back when this is started again
     '''
     return json.dumps(dump(self._status), ensure_ascii=True).encode('ascii')
示例#23
0
 def test_kwargs(self):
     with self.assertRaises(ValueError):
         dump(1, handlers=[])
示例#24
0
 def test_path(self):
     assert dump(Path('/')) == '/'
示例#25
0
 def test_dump_metanames(self):
     assert dump(Mangle(12)) == {'va.lue': 12}
示例#26
0
 def test_enum(self):
     assert load(dump(EnumA.C), EnumA) == EnumA.C
示例#27
0
 def test_dump(self):
     A = NamedTuple('A', [('a', int), ('b', str)])
     assert dump(A(1, '12')) == {'a': 1, 'b': '12'}