예제 #1
0
    def get_linodes(self):
        """

        From, Create a Linode Using the Linode API
        https://www.linode.com/docs/guides/getting-started-with-the-linode-api/

        """
        result = []
        api_url = "{}instances".format(self.api_url_base)
        response = requests.get(api_url, headers=self.headers)
        if response.status_code == HTTPStatus.OK:
            data = json.loads(response.content.decode("utf-8"))
            # pprint(data, expand_all=True)
            droplets = data["data"]
            for droplet in droplets:
                minion = droplet["label"]
                linode_type = self.get_linode_type(droplet["type"])
                # size = droplet["size"]
                # tags = droplet["tags"]
                specs = droplet["specs"]
                result.append(
                    Droplet(
                        droplet_id=droplet["id"],
                        minion=minion,
                        memory=specs["memory"],
                        disk=specs["disk"],
                        price_monthly=linode_type["price"]["monthly"],
                        tags=[],
                        domains=[],
                    ))
        else:
            pprint(response, expand_all=True)
            raise Exception("Error from the Linode API: {}".format(
                response.status_code))
        return result
예제 #2
0
 def do_pp(self, arg):
     """pp expression
     Rich pretty print.
     """
     try:
         pprint(self._getval(arg), console=self.console)
     except BaseException:
         pass
예제 #3
0
def validate_file(name: str, schema: Schema):
    with console.status(f"Validating {name}...", spinner="dots"):
        errors = schema.validate(get_data_json(f"mtgjson/{name}.json"))

    if errors:
        pprint(errors["data"], max_length=10, console=console)
        console.print(f"❌ [bold red]{name} had {len(errors['data'])} errors")
    else:
        console.print(f"✅ [bold green]{name} validated")
예제 #4
0
def DefaultAttributeChangeCallback(path: TypedAttributePath, transaction: SubscriptionTransaction):
    data = transaction.GetAttribute(path)
    value = {
        'Endpoint': path.Path.EndpointId,
        'Attribute': path.AttributeType,
        'Value': data
    }

    print("Attribute Changed:")
    pprint(value, expand_all=True)
    def CheckData(self, expected):
        tlv = expected.ToTLV()
        actual = expected.FromTLV(tlv)

        if (enable_debug):
            print("Expected Data:")
            pprint(expected, expand_all=True)

            print("Actual Data:")
            pprint(actual, expand_all=True)

        self.assertEqual(actual, expected)
예제 #6
0
    def is_received(self, requester_addr, key, index, is_print=False) -> bool:
        cursor = self.collection.find({
            "requester_addr": requester_addr.lower(),
            "job_key": key,
            "index": index
        })
        if is_print:
            for document in cursor:
                pprint(document)

        if cursor.count() == 0:
            return False

        return True
예제 #7
0
    def get_linode_type(self, linode_type):
        """

        https://www.linode.com/docs/api/linode-types/#type-view

        """
        if linode_type in self.linode_types:
            pass
        else:
            api_url = "{}types/{}".format(self.api_url_base, linode_type)
            response = requests.get(api_url, headers=self.headers)
            if response.status_code == HTTPStatus.OK:
                data = json.loads(response.content.decode("utf-8"))
                self.linode_types["linode_type"] = data
            else:
                pprint(response, expand_all=True)
                raise Exception("Error from the Linode types API: {}".format(
                    response.status_code))
        return self.linode_types["linode_type"]
예제 #8
0
def main():
    # find all the domains in the salt pillar
    domains = dict(sorted(get_domains().items()))
    json_dump_domains(domains)
    # display_domains(domains)

    # find the domain names for each minion (server)
    minions = {}
    for domain_name, config in domains.items():
        minion_id = "{}@{}".format(config["pillar"], config["minion"])
        if not minion_id in minions:
            minions[minion_id] = []
        minions[minion_id].append(domain_name)
    # pprint(minions, expand_all=True)

    # get a list of cloud servers (droplets)
    droplets = []
    droplets = droplets + get_digital_ocean()
    droplets = droplets + Linode().get_linodes()

    # link the domain names to the droplets
    for droplet in droplets:
        minion_id = "{}@{}".format(DEFAULT_PILLAR, droplet.minion)
        if minion_id in minions:
            droplet.domains = minions.pop(minion_id)
    json_dump_droplets(droplets)

    # use the tag to link droplets to a contact
    # contacts = {}
    # for droplet in droplets:
    #     for tag in droplet.tags:
    #         if not tag in contacts:
    #             contacts[tag] = []
    #         contacts[tag].append(droplet)
    # pprint(contacts, expand_all=True)

    # display any minions which are not allocated to a droplet
    print()
    rprint("[cyan]List of Salt minions NOT allocated to a Cloud Server...")
    rprint("[cyan]Note: this is most likely because "
           "the customer is paying for the hosting!")
    pprint(minions, expand_all=True)
예제 #9
0
def get_digital_ocean():
    """

    How To Use Web APIs in Python 3
    https://www.digitalocean.com/community/tutorials/how-to-use-web-apis-in-python-#!/usr/bin/env python3

    """
    result = []
    api_token = environ["DIGITAL_OCEAN_TOKEN"]
    api_url_base = "https://api.digitalocean.com/v2/"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer {0}".format(api_token),
    }
    api_url = "{}droplets".format(api_url_base)
    response = requests.get(api_url, headers=headers)
    if response.status_code == HTTPStatus.OK:
        data = json.loads(response.content.decode("utf-8"))
        droplets = data["droplets"]
        for droplet in droplets:
            minion = droplet["name"]
            size = droplet["size"]
            # 06/05/2022, The project does not appear in the Droplet data, so
            # use the tags for now...
            tags = droplet["tags"]
            result.append(
                Droplet(
                    droplet_id=droplet["id"],
                    minion=minion,
                    memory=droplet["memory"],
                    disk=droplet["disk"],
                    price_monthly=size["price_monthly"],
                    tags=[x for x in tags],
                    domains=[],
                ))
    else:
        pprint(response, expand_all=True)
        raise Exception("Error from the Digital Ocean API: {}".format(
            response.status_code))
    return result
예제 #10
0
def clean(html: bool = False, ):
    report = Report()
    # core = Core(report=report)

    path_setup = setup_paths(None, None, None, None, report)

    if html:
        html_files: List[Path] = []
        files = list(path_setup.input.glob("*.md"))
        for source_file_path in files:
            html_files.append(path_setup.output /
                              (source_file_path.stem + ".html"))
        files = list(path_setup.output.glob("*.html"))
        html_files_to_delete: List[Path] = []
        for output_file_path in files:
            if output_file_path not in html_files:
                html_files_to_delete.append(output_file_path)
        if len(html_files_to_delete) > 0:
            pprint(
                f"This would delete the following files from folder {path_setup.output}"
            )
            pprint([
                file.relative_to(path_setup.output).name
                for file in html_files_to_delete
            ])
            if click.confirm("Do you want to delete these files?"):
                for html_file in html_files_to_delete:
                    html_file.unlink(missing_ok=True)
        else:
            pprint("No files found to delete.")
예제 #11
0
def test_tuples():
    console = Console(color_system=None)
    console.begin_capture()
    pprint((1, ), console=console)
    pprint((1, ), expand_all=True, console=console)
    pprint(((1, ), ), expand_all=True, console=console)
    result = console.end_capture()
    print(repr(result))
    expected = "(1,)\n(\n│   1,\n)\n(\n│   (\n│   │   1,\n│   ),\n)\n"
    assert result == expected
예제 #12
0
def test_pprint_max_string():
    console = Console(color_system=None)
    console.begin_capture()
    pprint(["Hello" * 20], console=console, max_string=8)
    assert console.end_capture() == """['HelloHel'+92]\n"""
예제 #13
0
def log(
    text="",
    color=None,
    filename=None,
    end=None,
    flush=False,
    is_write=True,
    where_back=0,
    is_code=False,
    is_align=False,
    is_err=False,
    is_output=True,
    max_depth=None,
):
    """Log output with own settings.

    * colors:
    __ https://rich.readthedocs.io/en/latest/appendix/colors.html#appendix-colors
    """
    is_bold: bool = False
    if color == "bold":
        is_bold = True
        color = None

    if is_err:
        text = str(text)
        if text:
            if "E: " not in text[3] and "warning:" not in text.lower():
                text = f"E: {text}"
        else:
            return

        text = text.replace("E: warning:", "warning:")

    if isinstance(text, str) and "E: " in text[3:]:
        _text = text.replace("warning: ", "").replace("E: ", "")
        if "E: warning: " not in text:
            if "warning:" in text:
                text = f"{WHERE(where_back)}[bold yellow] warning:[/bold yellow] [bold]{_text}"
            else:
                text = f"{WHERE(where_back)}[bold {c.red}] E:[/bold {c.red}] [bold]{_text}"
        else:
            text = f"{WHERE(where_back)}[bold yellow] warning:[/bold yellow] [bold]{_text}"

    if "-=-=" in str(text):
        is_bold = True

    if is_code:
        text = " \ \n  ".join(textwrap.wrap(text, 80, break_long_words=False, break_on_hyphens=False))

    if is_align:
        text = "\n".join(textwrap.wrap(text, 80, break_long_words=False, break_on_hyphens=False))

    if is_write:
        if threading.current_thread().name != "MainThread" and cfg.IS_THREADING_ENABLED:
            filename = thread_log_files[threading.current_thread().name]
        elif not filename:
            if ll.LOG_FILENAME:
                filename = ll.LOG_FILENAME
            elif DRIVER_LOG:
                filename = DRIVER_LOG
            else:
                filename = "program.log"

        if filename not in ll.console:
            #: Indicated rich console to write into given filename
            # __ https://stackoverflow.com/a/6826099/2402577
            ll.console[filename] = Console(file=open(filename, "a"), force_terminal=True, theme=custom_theme)

    if isinstance(text, dict):
        if max_depth:
            pprint(text, max_depth=max_depth)
        else:
            pprint(text)

        if is_write:
            ll.console[filename].print(text)
    else:
        _log(text, color, is_bold, flush, filename, end, is_write, is_output)
예제 #14
0
def test_pprint_max_items():
    console = Console(color_system=None)
    console.begin_capture()
    pprint({"foo": 1, "bar": 2, "egg": 3}, console=console, max_length=2)
    assert console.end_capture() == """{'foo': 1, 'bar': 2, ... +1}\n"""
예제 #15
0
def test_pprint_max_values():
    console = Console(color_system=None)
    console.begin_capture()
    pprint([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], console=console, max_length=2)
    assert console.end_capture() == "[1, 2, ... +8]\n"
예제 #16
0
def test_pprint():
    console = Console(color_system=None)
    console.begin_capture()
    pprint(1, console=console)
    assert console.end_capture() == "1\n"
예제 #17
0
def DefaultEventChangeCallback(data: EventReadResult,
                               transaction: SubscriptionTransaction):
    print("Received Event:")
    pprint(data, expand_all=True)
예제 #18
0
 def json_print(self, obj):
     try:
         print_json(data=obj)
     except Exception:
         pprint(obj)