示例#1
0
    def test_with_format(self):
        """bitmath.format context mgr sets and restores formatting"""
        to_print = [
            bitmath.Byte(101),
            bitmath.KiB(202),
            bitmath.MB(303),
            bitmath.GiB(404),
            bitmath.TB(505),
            bitmath.PiB(606),
            bitmath.EB(707)
        ]

        str_reps = [
            "101.00-Byte",
            "202.00-KiB",
            "303.00-MB",
            "404.00-GiB",
            "505.00-TB",
            "606.00-PiB",
            "707.00-EB"
        ]

        # Make sure formatting looks right BEFORE the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")

        with bitmath.format("{value:.2f}-{unit}"):
            for (inst, inst_str) in zip(to_print, str_reps):
                self.assertEqual(str(inst), inst_str)

        # Make sure formatting looks right AFTER the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")
示例#2
0
    def test_with_format(self):
        """bitmath.format context mgr sets and restores formatting"""
        to_print = [
            bitmath.Byte(101),
            bitmath.KiB(202),
            bitmath.MB(303),
            bitmath.GiB(404),
            bitmath.TB(505),
            bitmath.PiB(606),
            bitmath.EB(707)
        ]

        str_reps = [
            "101.00-Byte", "202.00-KiB", "303.00-MB", "404.00-GiB",
            "505.00-TB", "606.00-PiB", "707.00-EB"
        ]

        # Make sure formatting looks right BEFORE the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")

        with bitmath.format("{value:.2f}-{unit}"):
            for (inst, inst_str) in zip(to_print, str_reps):
                self.assertEqual(str(inst), inst_str)

        # Make sure formatting looks right AFTER the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")
示例#3
0
    def test_print_GiB_singular_fmt_in_mgr(self):
        """TiB(1/3.0) prints out units in singular form, setting the fmt str in the mgr"""
        expected_result = "341.3GiB"

        with bitmath.format(fmt_str="{value:.1f}{unit}"):
            third_tibibyte = bitmath.TiB(1 / 3.0).best_prefix()
            actual_result = str(third_tibibyte)
            self.assertEqual(expected_result, actual_result)
示例#4
0
    def test_print_GiB_plural_fmt_in_mgr(self):
        """TiB(1/3.0) prints out units in plural form, setting the fmt str in the mgr"""
        expected_result = "3Bytes"

        with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
            three_Bytes = bitmath.Byte(3.0)
            actual_result = str(three_Bytes)
            self.assertEqual(expected_result, actual_result)
示例#5
0
    def test_print_GiB_singular_fmt_in_mgr(self):
        """TiB(1/3.0) prints out units in singular form, setting the fmt str in the mgr"""
        expected_result = "341.3GiB"

        with bitmath.format(fmt_str="{value:.1f}{unit}"):
            third_tibibyte = bitmath.TiB(1 / 3.0).best_prefix()
            actual_result = str(third_tibibyte)
            self.assertEqual(expected_result, actual_result)
示例#6
0
    def test_print_GiB_plural_fmt_in_mgr(self):
        """TiB(1/3.0) prints out units in plural form, setting the fmt str in the mgr"""
        expected_result = "3Bytes"

        with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
            three_Bytes = bitmath.Byte(3.0)
            actual_result = str(three_Bytes)
            self.assertEqual(expected_result, actual_result)
示例#7
0
    def test_print_byte_plural(self):
        """Byte(3.0) prints out units in plural form"""
        expected_result = "3Bytes"
        fmt_str = "{value:.1g}{unit}"
        three_Bytes = bitmath.Byte(3.0)

        with bitmath.format(plural=True):
            actual_result = three_Bytes.format(fmt_str)
            self.assertEqual(expected_result, actual_result)
示例#8
0
    def test_print_byte_plural(self):
        """Byte(3.0) prints out units in plural form"""
        expected_result = "3Bytes"
        fmt_str = "{value:.1g}{unit}"
        three_Bytes = bitmath.Byte(3.0)

        with bitmath.format(plural=True):
            actual_result = three_Bytes.format(fmt_str)
            self.assertEqual(expected_result, actual_result)
示例#9
0
def parse_profile(system_profile, display_name, logger):
    """
    break complex data structures into more simple structures that can be compared easily.
    display_name is added to data structure for sorting and is later stripped from data structure
    """
    def _parse_lists_of_strings(names, verb):
        """
        helper method to convert lists of strings to comparable facts
        """
        for list_of_strings in names:
            for item in system_profile.get(list_of_strings, []):
                parsed_profile[list_of_strings + "." + item] = verb

    def _parse_running_processes(processes):
        """
        helper method to convert running process lists to facts. We output a
        fact for each process name, with its count as a value. The count is
        returned as a string to match the API spec.
        """
        for process in processes:
            process_fact_name = "running_processes." + process
            if process_fact_name in parsed_profile:
                parsed_profile[process_fact_name] = str(
                    int(parsed_profile[process_fact_name]) + 1)
            else:
                parsed_profile[process_fact_name] = "1"

    def _parse_yum_repo(name):
        """
        helper method to convert yum repo objects to comparable facts
        """
        parsed_profile["yum_repos." + name + ".base_url"] = yum_repo.get(
            "base_url", "N/A")
        parsed_profile["yum_repos." + name + ".enabled"] = str(
            yum_repo.get("enabled", "N/A"))
        parsed_profile["yum_repos." + name + ".gpgcheck"] = str(
            yum_repo.get("gpgcheck", "N/A"))

    def _canonicalize_ipv6_addr(addr):
        """
        helper method to display ipv6 address strings unambiguously. If the address
        is not parsable (for example: 'N/A'), just keep the string as-is.
        """
        try:
            return str(ipaddress.IPv6Address(addr).compressed)
        except AddressValueError:
            return addr

    def _parse_interface(name):
        """
        helper method to convert network interface objects to comparable facts
        """
        ipv6_addresses = [
            _canonicalize_ipv6_addr(addr)
            for addr in interface.get("ipv6_addresses", ["N/A"])
        ]
        parsed_profile["network_interfaces." + name +
                       ".ipv4_addresses"] = ", ".join(
                           interface.get("ipv4_addresses", ["N/A"]))
        parsed_profile["network_interfaces." + name +
                       ".ipv6_addresses"] = ", ".join(ipv6_addresses)
        parsed_profile["network_interfaces." + name +
                       ".mac_address"] = interface.get("mac_address", "N/A")
        parsed_profile["network_interfaces." + name + ".mtu"] = str(
            interface.get("mtu", "N/A"))
        parsed_profile["network_interfaces." + name +
                       ".state"] = interface.get("state", "N/A")
        parsed_profile["network_interfaces." + name + ".type"] = interface.get(
            "type", "N/A")

    def _parse_tags(tags):
        """
        helper method to convert tag lists to facts. We output a
        fact for each unique tag (combination of namespace and key).
        The value is a comma-separated ordered list of all values
        for the tag.
        """
        tag_dict = {}
        for tag in tags:
            tag_name = "{}.{}".format(tag["namespace"], tag["key"])
            value_list = tag_dict.setdefault(tag_name, [])
            if tag["value"]:
                value_list.append(tag["value"])
        for tag_name in sorted(tag_dict):
            parsed_profile["tags." + tag_name] = (", ".join(
                sorted(tag_dict[tag_name])) if tag_dict[tag_name] else
                                                  "(no value)")

    # start with metadata that we have brought down from the system record
    parsed_profile = {
        "id": system_profile[SYSTEM_ID_KEY],
        "name": display_name
    }

    # add all strings as-is
    for key in SYSTEM_PROFILE_STRINGS:
        parsed_profile[key] = system_profile.get(key, None)

    # add all integers, converting to str
    for key in SYSTEM_PROFILE_INTEGERS | SYSTEM_PROFILE_BOOLEANS:
        parsed_profile[key] = str(system_profile.get(key, "N/A"))

    _parse_lists_of_strings(SYSTEM_PROFILE_LISTS_OF_STRINGS_ENABLED, "enabled")
    _parse_lists_of_strings(SYSTEM_PROFILE_LISTS_OF_STRINGS_INSTALLED,
                            "installed")

    _parse_running_processes(system_profile.get("running_processes", []))

    _parse_tags(system_profile.get("tags", []))

    if "sap_sids" in system_profile:
        parsed_profile["sap_sids"] = ", ".join(
            sorted(system_profile["sap_sids"]))

    # convert bytes to human readable format
    if "system_memory_bytes" in system_profile:
        with bitmath.format(fmt_str="{value:.2f} {unit}"):
            formatted_size = bitmath.Byte(
                system_profile["system_memory_bytes"]).best_prefix()
            parsed_profile["system_memory"] = str(formatted_size)
        system_profile.pop("system_memory_bytes")

    for package in system_profile.get("installed_packages", []):
        try:
            name, vra = _get_name_vra_from_string(package)
            if name != "gpg-pubkey":
                parsed_profile["installed_packages." + name] = vra
        except UnparsableNEVRAError as e:
            logger.warn(e.message)

    for interface in system_profile.get("network_interfaces", []):
        try:
            name = interface["name"]
            _parse_interface(name)
        except KeyError:
            logger.warn("network interface has no name, skipping")
            continue

    for yum_repo in system_profile.get("yum_repos", []):
        try:
            name = yum_repo["name"]
            _parse_yum_repo(name)
        except KeyError:
            logger.warn("yum repo has no name, skipping")
            continue

    return parsed_profile
示例#10
0
    # The 'stream' keyword lets us http GET files in
    # chunks. http://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content
    r = requests.get(f, stream=True)
    # We haven't began receiving the payload content yet, we have only
    # just received the response headers. Of interest is the
    # 'content-length' header which describes our payload in bytes
    #
    # http://bitmath.readthedocs.org/en/latest/classes.html#bitmath.Byte
    size = bitmath.Byte(int(r.headers['Content-Length']))

    # Demonstrate 'with' context handler, allowing us to customize all
    # bitmath string printing within the indented block. We don't need
    # all that precision anyway, just two points should do.
    #
    # http://bitmath.readthedocs.org/en/latest/module.html#bitmath-format
    with bitmath.format("{value:.2f} {unit}"):
        print("Downloading %s (%s) in %s chunks" % (f,
                                                    size.best_prefix(),
                                                    args.down.best_prefix()))

    # We have to save these files somewhere
    save_path = os.path.join(DESTDIR, fname)
    print("Saving to: %s" % save_path)
    print("")

    # OK. Let's create our actual progress bar now. See the 'maxval'
    # keyword? That's the size of our payload in bytes.
    pbar = progressbar.ProgressBar(
        widgets=widgets,
        maxval=int(size)).start()