Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# Copyright (c) 2014 TrilioData, Inc
# All Rights Reserved.


from __future__ import print_function
import os
import re
import sys
import uuid

import six
import prettytable
import textwrap

from workloadmgrclient import exceptions
from workloadmgrclient.openstack.common import strutils
from datetime import datetime

import json
import ast
import pytz
import time


def get_local_time(record_time, input_format, output_format, tz, tz1=None):
    """
    Convert and return the date and time - from GMT to local time
    """
    try:
        if record_time in (0, None, ""):
            return ""
        else:
            if not input_format or input_format is None or input_format == "":
                input_format = "%Y-%m-%dT%H:%M:%S.%f"
            if not output_format or output_format is None or output_format == "":
                output_format = "%m/%d/%Y %I:%M:%S %p"

            local_time = datetime.strptime(record_time, input_format)

            local_tz = pytz.timezone(tz)
            from_zone = pytz.timezone("UTC")
            if tz1 is not None:
                from_zone = pytz.timezone(tz1)
            local_time = local_time.replace(tzinfo=from_zone)
            local_time = local_time.astimezone(local_tz)
            local_time = datetime.strftime(local_time, output_format)
            return local_time
    except Exception as ex:
        return record_time


def arg(*args, **kwargs):
    """Decorator for CLI args."""

    def _decorator(func):
        add_arg(func, *args, **kwargs)
        return func

    return _decorator


def env(*vars, **kwargs):
    """
    returns the first environment variable set
    if none are non-empty, defaults to '' or keyword arg default
    """
    for v in vars:
        value = os.environ.get(v, None)
        if value:
            return value
    return kwargs.get("default", "")


def add_arg(f, *args, **kwargs):
    """Bind CLI arguments to a shell.py `do_foo` function."""
    if not hasattr(f, "arguments"):
        f.arguments = []
    # NOTE(sirp): avoid dups that can occur when the module is shared across
    # tests.
    if (args, kwargs) not in f.arguments:
        # Because of the sematics of decorator composition if we just append
        # to the options list positional options will appear to be backwards.
        f.arguments.insert(0, (args, kwargs))


def add_resource_manager_extra_kwargs_hook(f, hook):
    """Adds hook to bind CLI arguments to ResourceManager calls.

    The `do_foo` calls in shell.py will receive CLI args and then in turn pass
    them through to the ResourceManager. Before passing through the args, the
    hooks registered here will be called, giving us a chance to add extra
    kwargs (taken from the command-line) to what's passed to the
    ResourceManager.
    """
    if not hasattr(f, "resource_manager_kwargs_hooks"):
        f.resource_manager_kwargs_hooks = []

    names = [h.__name__ for h in f.resource_manager_kwargs_hooks]
    if hook.__name__ not in names:
        f.resource_manager_kwargs_hooks.append(hook)


def get_resource_manager_extra_kwargs(f, args, allow_conflicts=False):
    """Return extra_kwargs by calling resource manager kwargs hooks."""
    hooks = getattr(f, "resource_manager_kwargs_hooks", [])
    extra_kwargs = {}
    for hook in hooks:
        hook_name = hook.__name__
        hook_kwargs = hook(args)

        conflicting_keys = set(hook_kwargs.keys()) & set(extra_kwargs.keys())
        if conflicting_keys and not allow_conflicts:
            raise Exception(
                "Hook '%(hook_name)s' is attempting to redefine"
                " attributes '%(conflicting_keys)s'" % locals()
            )

        extra_kwargs.update(hook_kwargs)

    return extra_kwargs


def unauthenticated(f):
    """
    Adds 'unauthenticated' attribute to decorated function.
    Usage:
        @unauthenticated
        def mymethod(f):
            ...
    """
    f.unauthenticated = True
    return f


def isunauthenticated(f):
    """
    Checks to see if the function is marked as not requiring authentication
    with the @unauthenticated decorator. Returns True if decorator is
    set to True, False otherwise.
    """
    return getattr(f, "unauthenticated", False)


def service_type(stype):
    """
    Adds 'service_type' attribute to decorated function.
    Usage:
        @service_type('volume')
        def mymethod(f):
            ...
    """

    def inner(f):
        f.service_type = stype
        return f

    return inner


def get_service_type(f):
    """
    Retrieves service type from function
    """
    return getattr(f, "service_type", None)


def pretty_choice_list(l):
    return ", ".join("'%s'" % i for i in l)


def print_list(objs, fields, formatters={}, headers=[], colwidth=[]):
    mixed_case_fields = [
        "serverId",
        "UserName",
        "ObjectName",
        "Timestamp",
        "UserId",
        "Details",
    ]
    if headers:
        pt = prettytable.PrettyTable(
            [f for f in headers], caching=False, colWidths=colwidth
        )
    else:
        pt = prettytable.PrettyTable(
            [f for f in fields], caching=False, colWidths=colwidth
        )
    pt.aligns = ["l" for f in fields]

    sortby_index = 0

    for o in objs:
        row = []
        i = 0
        for field in fields:
            if field in formatters:
                row.append(formatters[field](o))
            else:
                if field in mixed_case_fields:
                    field_name = field.replace(" ", "_")
                else:
                    field_name = field.lower().replace(" ", "_")

                if field_name.lower() in ("created_at", "project_id", "timestamp"):
                    sortby_index = i
                if type(o) is dict:
                    data = o[field_name]
                else:
                    data = getattr(o, field_name, "")
                row.append(data)
            i = i + 1
        pt.add_row(row)

    if len(pt._rows) > 0:
        print(strutils.safe_encode(pt.get_string(sortby=fields[sortby_index])).decode())


def print_object(objs, fields, formatters={}, headers=[], colwidth=[]):
    mixed_case_fields = ["serverId"]
    if headers:
        pt = prettytable.PrettyTable(
            [f for f in headers], caching=False, colWidths=colwidth
        )
    else:
        pt = prettytable.PrettyTable(
            [f for f in fields], caching=False, colWidths=colwidth
        )
    pt.aligns = ["l" for f in fields]

    sortby_index = 0

    row = []
    i = 0
    for field in fields:
        if field in mixed_case_fields:
            field_name = field.replace(" ", "_")
        else:
            field_name = field.lower().replace(" ", "_")

        data = getattr(objs, field_name, "")
        row.append(data)
        i = i + 1
    pt.add_row(row)

    if len(pt._rows) > 0:
        print(strutils.safe_encode(pt.get_string(sortby=fields[sortby_index])).decode())


def print_file_search_dict(d, dict_property="Property", dict_value="Value", wrap=0):
    for k, v in sorted(d.items()):
        pt = prettytable.PrettyTable(
            ["Disks of " + k, "File paths", "Metadata"], caching=False
        )
        pt.align = "l"
        if v and isinstance(v, list):
            for drive in v:
                val1 = ""
                for k1, v1 in sorted(drive.items()):
                    if type(v1) is not list:
                        continue
                    if len(v1) == 0:
                        val1 = "File path not existing"
                        pt.add_row([k1, val1, ""])
                    i = 0
                    for path in v1:
                        val1 = path
                        pt.add_row([k1, val1, drive[path]])
                        i = i + 1
        print(strutils.safe_encode(pt.get_string()).decode())


def print_dict(d, dict_property="Property", dict_value="Value", wrap=0):
    pt = prettytable.PrettyTable([dict_property, dict_value], caching=False)
    pt.align = "l"
    for k, v in sorted(d.items()):
        # convert dict to str to check length
        if any([isinstance(v, dict), isinstance(v, list)]):
            v = json.dumps(v)
            v = str(v)
        if wrap > 0:
            v = textwrap.fill(str(v), wrap)
        # if value has a newline, add in multiple rows
        # e.g. fault with stacktrace
        if v and isinstance(v, six.string_types) and r"\n" in v:
            lines = v.strip().split(r"\n")
            col1 = k
            for line in lines:
                pt.add_row([col1, line])
                col1 = ""
        else:
            pt.add_row([k, v])
    print(strutils.safe_encode(pt.get_string()).decode())


def print_data_vertically(data, cols):
    pt = prettytable.PrettyTable(cols)
    for item in data:
        if isinstance(item, dict):
            for k, v in list(item.items()):
                pt.add_row([k, v])
            pt.add_row(["", ""])
        elif isinstance(item, list):
            for value in item:
                pt.add_row([value])
    print(strutils.safe_encode(pt.get_string()).decode())


def find_resource(manager, name_or_id):
    """Helper for the _find_* methods."""
    # first try to get entity as integer id
    try:
        if isinstance(name_or_id, int) or name_or_id.isdigit():
            return manager.get(int(name_or_id))
    except exceptions.NotFound:
        pass

    # now try to get entity as uuid
    try:
        uuid.UUID(strutils.safe_decode(name_or_id))
        return manager.get(name_or_id)
    except (ValueError, exceptions.NotFound):
        pass

    try:
        try:
            obj = manager.find(human_id=name_or_id)
            return manager.get(obj.id)
        except exceptions.NotFound:
            pass

        # finally try to find entity by name
        try:
            obj = manager.find(name=name_or_id)
            return manager.get(obj.id)
        except exceptions.NotFound:
            try:
                obj = manager.find(display_name=name_or_id)
                return manager.get(obj.id)
            except (UnicodeDecodeError, exceptions.NotFound):
                try:
                    # Volumes does not have name, but display_name
                    obj = manager.find(display_name=name_or_id)
                    return manager.get(obj.id)
                except exceptions.NotFound:
                    msg = "No %s with a name or ID of '%s' exists." % (
                        manager.resource_class.__name__.lower(),
                        name_or_id,
                    )
                    raise exceptions.CommandError(msg)
    except exceptions.NoUniqueMatch:
        msg = (
            "Multiple %s matches found for '%s', use an ID to be more"
            " specific." % (manager.resource_class.__name__.lower(), name_or_id)
        )
        raise exceptions.CommandError(msg)


def _format_servers_list_networks(server):
    output = []
    for (network, addresses) in list(server.networks.items()):
        if len(addresses) == 0:
            continue
        addresses_csv = ", ".join(addresses)
        group = "%s=%s" % (network, addresses_csv)
        output.append(group)

    return "; ".join(output)


class HookableMixin(object):
    """Mixin so classes can register and run hooks."""

    _hooks_map = {}

    @classmethod
    def add_hook(cls, hook_type, hook_func):
        if hook_type not in cls._hooks_map:
            cls._hooks_map[hook_type] = []

        cls._hooks_map[hook_type].append(hook_func)

    @classmethod
    def run_hooks(cls, hook_type, *args, **kwargs):
        hook_funcs = cls._hooks_map.get(hook_type) or []
        for hook_func in hook_funcs:
            hook_func(*args, **kwargs)


def safe_issubclass(*args):
    """Like issubclass, but will just return False if not a class."""

    try:
        if issubclass(*args):
            return True
    except TypeError:
        pass

    return False


def import_class(import_str):
    """Returns a class from a string including module and class."""
    mod_str, _sep, class_str = import_str.rpartition(".")
    __import__(mod_str)
    return getattr(sys.modules[mod_str], class_str)


_slugify_strip_re = re.compile(r"[^\w\s-]")
_slugify_hyphenate_re = re.compile(r"[-\s]+")


# http://code.activestate.com/recipes/
#   577257-slugify-make-a-string-usable-in-a-url-or-filename/
def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.

    From Django's "django/template/defaultfilters.py".
    """
    import unicodedata

    if not isinstance(value, six.text_type):
        value = six.text_type(value)
    value = unicodedata.normalize("NFKD", value).encode("ascii", "ignore")
    value = six.text_type(_slugify_strip_re.sub("", value).strip().lower())
    return _slugify_hyphenate_re.sub("-", value)


def bytes_fmt(num, suffix="B"):
    num = int(num)
    for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
        if abs(num) < 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, "Y", suffix)


def humanize_time(seconds, granularity=5):
    try:
        result = []
        intervals = (
            ("weeks", 604800),  # 60 * 60 * 24 * 7
            ("days", 86400),  # 60 * 60 * 24
            ("hours", 3600),  # 60 * 60
            ("minutes", 60),
            ("seconds", 1),
        )
        for name, count in intervals:
            value = seconds // count
            if value:
                seconds -= value * count
                if value == 1:
                    name = name.rstrip("s")
                result.append("{} {}".format(value, name))
        return ", ".join(result[:granularity])
    except Exception as ex:
        return seconds


def read_file(file_obj, eval=False):
    try:
        data = file_obj.read()
        if len(data) == 0:
            message = "File %s is empty." % file_obj.name
            raise exceptions.CommandError(message)
        if eval is True:
            try:
                data = ast.literal_eval(data)
            except Exception as ex:
                print("Error: " + ex.msg + "\nIn parsing " + file_obj.name)
                exit()
        return data
    except Exception as ex:
        raise ex
    finally:
        file_obj.close()