Program Listing for File tty.py

Return to documentation for file (upref/tty.py)

#!/usr/bin/env python
# -*- coding: utf-8 -*-




import logging
import sys
import os
import os.path
import tempfile
import getpass

__all__ = ['get_data']


def get_data(data_description):
    gui = data_description.get('__gui__')
    if 'title' in gui:
        print(gui['title'])
        print('-' * len(gui['title']))
        print()

    for key in data_description:
        if not key.endswith("__") and not key.startswith("__"):
            data = data_description[key]
            if 'label' in data:
                print(data['label'])
            if 'description' in data:
                print(data['description'])

            if 'type' in data_description[key] and \
                    data_description[key]['type'].upper().startswith("PASS"):
                data_description[key]['value'] = getpass.getpass("-->")
            else:
                data_description[key]['value'] = input("-->")

            print()

    if 'button_label' in gui:
        print(gui['button_label'])

    return data_description


def message(msg_txt, title):
    print(title)
    print(msg_txt)


def is_frozen():
    return getattr(sys, 'frozen', False)


def __get_this_folder():
    return os.path.split(os.path.abspath(os.path.realpath(
        __get_this_filename())))[0]


def __get_this_filename():
    result = ""

    if is_frozen():
        # frozen
        result = sys.executable
    else:
        # unfrozen
        result = __file__

    return result



def __set_logging_system():
    log_filename = os.path.splitext(os.path.abspath(
        os.path.realpath(__get_this_filename())))[0] + '.log'

    if is_frozen():
        log_filename = os.path.abspath(os.path.join(
            tempfile.gettempdir(),
            os.path.basename(__get_this_filename()) + '.log'))

    logging.basicConfig(filename=log_filename, level=logging.DEBUG,
                        format='%(asctime)s: %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p')
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    # set a format which is simpler for console use
    formatter = logging.Formatter('%(asctime)s: %(levelname)-8s %(message)s')
    # tell the handler to use this format
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger('').addHandler(console)



def __main():
    # ------------------------------------
    logging.info('Started %s', __get_this_filename())
    logging.info('The Python version is %s.%s.%s',
                 sys.version_info[0], sys.version_info[1], sys.version_info[2])

    conf = {
        '__gui__': {
            'title': 'The title here',
            'icon': 'src/python/upref/tower.ico',
            'button_label': 'Cool baby',
        },
        'url': {
            'label': 'URL',
            'description': 'Could you give me a coffee not an URL',
        },
        'login': {
            'label': 'Login',
            'description': 'Could you give me a coffee again',
        },
        'logsdfin11': {
            'label': 'Login new one',
            'description': 'Could you give me a coffee black',
        },
        'logqqqin13': {
            'label': 'Logoff',
            'description': 'Could you give me a\nTEA',
        },
        'loginfsdf12': {
            'label': 'Password',
            'description': 'Could you give me a coffee again',
            'value': "lkjhlkhj",
            'type': "pass",
        },
    }

    get_data(conf)

    logging.info('Finished')
    # ------------------------------------



if __name__ == '__main__':
    __set_logging_system()
    __main()