Пример #1
0
def slack_print(s: str) -> None:
    """
    Prints a string in a code block if the Op is ran in Slack, prints normally otherwise
    """
    final_msg = slack_format(s)
    for i in final_msg:
        ux.print(i)
Пример #2
0
def main():
    """
    Prompts user for a MySQL database and allows them to print any data they query for
    """
    logo_print()

    db_creds = login.Db_credentials()

    # Prompts
    try:
        db_creds.get_credentials()
    except KeyboardInterrupt:
        print("💤  Exiting Op")
        return

    # Connection
    try:
        connection = make_connection(db_creds)
    except:
        ux.print("❗ Could not connect to " + db_creds.host)
        return

    ux.print("Successfully connected to " + db_creds.host)

    # Select
    try:
        op_cursor(connection)
    except KeyboardInterrupt:
        print("💤  Exiting Op")
Пример #3
0
def main():
    howdy = "👋 How are you today?"
    tags = ["demo", "track", "python"]
    metadata = {"language": "python"}

    sdk.track(tags, howdy, metadata)
    answer = prompt.input(name="answer", message=howdy, allowEmpty=False)

    response = f"👉 Answer: {answer}"

    sdk.track(tags, response, metadata)
    ux.print(response)
Пример #4
0
def print_table_list(cursor) -> None:
    """
    Prints a list of the tables in the database
    """
    cursor.execute("SHOW TABLES")
    tables = ""
    table_count = 0
    for (table_name, ) in cursor:
        tables += table_name + '\n'
        table_count += 1

    slack_print(tables)
    ux.print('{} tables available'.format(table_count))
Пример #5
0
def op_cursor(connection) -> None:
    """
    Master control for making queries after a successful connection to a database
    """
    loop = True
    fetch = "One line"

    cursor = connection.cursor(buffered=True)

    loop_options = [
        "Make Query", "Change Fetch Amount ({:s})".format(fetch), "Table List",
        "Column List", "Finish"
    ]
    while loop:
        action = prompt.list("action",
                             "\n✨ What would you like to do?",
                             choices=loop_options)

        if action == "Make Query":
            try:
                make_query(cursor, fetch)
            except mysql.connector.errors.ProgrammingError as e:
                ux.print("❗{}".format(e))
            except:
                ux.print("❗ Error connecting to server")

        if action == "Change Fetch Amount ({:s})".format(fetch):
            if fetch == "One line":
                fetch = "All lines"
            else:
                fetch = "One line"

            ux.print(fetch)
            loop_options[1] = "Change Fetch Amount ({:s})".format(fetch)

        if action == "Table List":
            try:
                print_table_list(cursor)
            except:
                ux.print("❗ Failed to get Tables")

        if action == "Column List":
            try:
                print_column_list(cursor)
            except:
                ux.print("❗ Failed to get Columns for the requested Table")

        if action == "Finish":
            break
Пример #6
0
def slack_print(s: str) -> None:
    """
    Prints a string in a code block if the Op is ran in Slack, prints normally otherwise
    """
    final_msg = slack_format(s)
    blocks = len(final_msg)
    print_check = True
    if blocks > 1:
        print_check = prompt.confirm(
            "print_check",
            "✅  The data will be broken up into {} segments.  Continue with print?"
            .format(blocks))
    if print_check:
        for i in final_msg:
            ux.print(i)
Пример #7
0
    def map_credentials(self, creds: str):
        """
        Maps fields from a retrieved Secret
        """
        try:
            loaded_creds = json.loads(creds)
        except:
            ux.print("❗ Error: Credentials not in correct format")
            return

        try:
            self.host = loaded_creds["Host"]
            self.username = loaded_creds["Username"]
            self.password = loaded_creds["Password"]
            self.db = loaded_creds["Database"]
            self.port = loaded_creds["Port"]
        except:
            ux.print("❗ Error: Failed to set all fields")
            return
Пример #8
0
def logo_print():
    if sdk.get_interface_type() == 'terminal':
        ux.print(cto_terminal)
    else:
        ux.print(cto_slack)

    ux.print(intro)
Пример #9
0
def main() -> None:
    # Logo branding.
    logos.logo_print()
    ux.print(logos.intro)

    public_read = 'public-read'
    region = 'us-west-2'  # hardcoded.
    object_name = 'views/home.html'

    # Create Secrets objects.
    secret_ = secrets.Secrets()
    access_key_id = secret_.prompt_aws_access_key_id()
    secret_access_key = secret_.prompt_aws_secret_access_key()

    # Create Client objects.
    cb = create_bucket.CreateBucket()
    s3_client = cb.create_client(access_key_id, secret_access_key)

    # Prompt for a bucket-name and check for existing bucket.
    for n in range(5):
        bucket_name = cb.prompt_bucket_name()
        bucket_already_exist = cb.check_existing_bucket(s3_client, bucket_name)
        if not bucket_already_exist:
            break
        if n == 5:
            ux.print("You've exceeded the limit of retries. Exiting Op.")
            os._exit(0)

    # Create a S3 bucket.
    cb.create_s3_bucket(s3_client, bucket_name, public_read,
                        region)  # Create bucket.
    cb.website_config(s3_client, bucket_name)  # Enable static website hosting.

    # Upload a file to a S3 bucket.
    generator = cb.absolutepath_dir()
    for filename in generator:
        cb.upload_file(s3_client, filename, bucket_name, object_name=None)

    # Print URL.
    cb.construct_url(bucket_name, object_name, region)
Пример #10
0
 def set_credentials_instructions(self):
     """
     Prints message on the appropriate format for setting all fields as one
     """
     ux.print("Please set your secrets in the following format:")
     if sdk.get_interface_type() == "slack":
         ux.print(
             '```{"Host": <host>,\n "Username": <username>,\n "Password": <password>,\n "Database": <database>,\n "Port": <port>}```'
         )
     else:
         ux.print(
             '{"Host": <host>,\n "Username": <username>,\n "Password": <password>,\n "Database": <database>,\n "Port": <port>}'
         )
Пример #11
0
 def construct_url(self, bucket_name, object_name, region=None) -> None:
     """Construct object URL and print it."""
     url = "https://%s.s3-%s.amazonaws.com/%s" % (bucket_name, region,
                                                  object_name)
     ux.print(url)