def test_to_structured_logging():
    application_name = "test_structured_logging"

    balsa = Balsa(application_name, __author__, verbose=True)
    balsa.init_logger()

    log = get_logger(application_name)

    question = "life"
    answer = 42
    log.info(sf("test structured logging", question=question, answer=answer))

    # todo: get these to work
    # crazy = 'a "crazy" string'
    # newline_string = "a\nnewline"
    crazy = "a crazy string"
    newline_string = "anewline"

    some_float = 3.3
    a_bool = True
    log.info(
        sf("test",
           "more,stuff",
           question=question,
           answer=answer,
           newline_string=newline_string,
           crazy=crazy,
           some_float=some_float,
           a_bool=a_bool))

    complex_value = 1 + 2j
    log.info(sf(complex_value=complex_value))
Example #2
0
def test_threaded_gui():

    global exception_complete
    timeout = 10

    application_name = 'main_thread'
    log = get_logger(application_name)
    balsa = Balsa(application_name,
                  __author__,
                  verbose=True,
                  log_directory='temp',
                  gui=True,
                  is_root=False,
                  delete_existing_log_files=True)
    balsa.init_logger()
    log.info('starting main thread')

    exception_complete = False
    gui_thread = QtGuiThread()
    gui_thread.start()
    loop_count = 0
    while gui_thread.isRunning() and loop_count < timeout:
        time.sleep(1)
        loop_count += 1
    assert exception_complete

    exception_complete = False
    gui_thread = GuiThread()
    gui_thread.start()
    gui_thread.join(timeout)
    assert exception_complete
Example #3
0
 def init_logger(self):
     balsa_log = Balsa(self.instance_name,
                       author_name,
                       log_directory=os.path.join('temp', 'log'),
                       delete_existing_log_files=True)
     balsa_log.init_logger()
     self.log = get_logger(application_name)
Example #4
0
def main():
    balsa = Balsa(application_name, 'james abel')
    balsa.init_logger()
    log.info('I am a message')
    log.info('So am I')
    for s in balsa.get_string_list():
        print(s)
Example #5
0
def gui_main():

    args = get_arguments()

    try:
        # todo: today we're redirecting api.abel.co so for this we need to go direct.  In the future have everything on AWS including DNS.
        sentry_dsn = requests.get('http://l3wp7vlxe8.execute-api.us-west-2.amazonaws.com/dev/apps/propmtime/sentrydsn').text
        if not (sentry_dsn.startswith('http') and '@sentry.io' in sentry_dsn):
            sentry_dsn = None
    except ConnectionError:
        sentry_dsn = None

    balsa = Balsa( __application_name__, __author__, gui=True, use_sentry=sentry_dsn is not None, sentry_dsn=sentry_dsn)
    balsa.init_logger_from_args(args)

    app_data_folder = appdirs.user_config_dir(appname=__application_name__, appauthor=__author__)
    init_preferences_db(app_data_folder)

    init_exit_control_event()

    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)  # so popup dialogs don't close the system tray icon
    system_tray = PropMTimeSystemTray(app, app_data_folder, balsa.log_path)
    system_tray.show()
    app.exec_()
Example #6
0
def cli_main(args):

    ui_type = UITypes.cli

    balsa = Balsa(__application_name__, __author__)
    balsa.log_console_prefix = "\r"
    balsa.init_logger_from_args(args)
    log.info(f"__application_name__={__application_name__}")
    log.info(f"__author__={__author__}")
    log.info(f"__version__={__version__}")

    try:
        preferences = get_preferences(ui_type)
        preferences.backup_directory = args.path  # backup classes will read the preferences DB directly
        preferences.github_token = args.token
        preferences.aws_profile = args.profile

        # If setting the exclusions, just do it for one backup type at a time.  The values are stored for subsequent runs.
        if args.exclude is not None and len(args.exclude) > 0:
            if args.s3:
                ExclusionPreferences(BackupTypes.S3.name).set(args.exclude)
            elif args.dynamodb:
                ExclusionPreferences(BackupTypes.DynamoDB.name).set(
                    args.exclude)
            elif args.github:
                ExclusionPreferences(BackupTypes.github.name).set(args.exclude)

        did_something = False
        dynamodb_local_backup = None
        s3_local_backup = None
        github_local_backup = None
        if args.s3 or args.aws:
            s3_local_backup = S3Backup(ui_type, log.info, log.warning,
                                       log.error)
            s3_local_backup.start()
            did_something = True
        if args.dynamodb or args.aws:
            dynamodb_local_backup = DynamoDBBackup(ui_type, log.info,
                                                   log.warning, log.error)
            dynamodb_local_backup.start()
            did_something = True
        if args.github:
            github_local_backup = GithubBackup(ui_type, log.info, log.warning,
                                               log.error)
            github_local_backup.start()
            did_something = True
        if not did_something:
            print(
                "nothing to do - please specify a backup to do or -h/--help for help"
            )

        if dynamodb_local_backup is not None:
            dynamodb_local_backup.join()
        if s3_local_backup is not None:
            s3_local_backup.join()
        if github_local_backup is not None:
            github_local_backup.join()
    except Exception as e:
        log.exception(e)
Example #7
0
def test_balsa_simple():
    application_name = 'test_balsa_simple'

    balsa = Balsa(application_name, __author__, is_root=False)
    balsa.init_logger()

    log = get_logger(application_name)
    log.error('test error message')
Example #8
0
def test_balsa_simple():
    application_name = 'test_balsa'

    balsa = Balsa(application_name, __author__, verbose=True, log_directory='temp', gui=True, delete_existing_log_files=True)
    balsa.init_logger()

    log = get_logger(application_name)
    log.error('test error message')
Example #9
0
def main():

    balsa = Balsa(application_name, author, verbose=True)
    balsa.init_logger()

    my_value = 42
    my_name = "me"
    log.info(sf("myapp", my_name=my_name, my_value=my_value))
Example #10
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', action='store_true', help='verbose')
    args = parser.parse_args()

    balsa = Balsa(application_name, author, error_callback=balsa_example_error_callback)
    balsa.init_logger_from_args(args)

    something_useful()
Example #11
0
def test_balsa_sentry():
    application_name = "test_balsa_sentry"

    if "SENTRY_DSN" in os.environ:
        balsa = Balsa(application_name, __author__, use_sentry=True, inhibit_cloud_services=False, is_root=False, sentry_dsn=os.environ["SENTRY_DSN"])
        balsa.init_logger()

        log = get_logger(application_name)
        log.error("test balsa sentry error message")
    else:
        print("Please set SENTRY_DSN environment variable to have a good %s test" % __name__)
Example #12
0
def test_balsa_clone_parent():

    my_balsa = Balsa("test_balsa_clone_parent_a", "parent_a")
    my_balsa_config = my_balsa.config_as_dict()

    new_balsa = balsa_clone(my_balsa_config, "test_balsa_clone_parent_b", my_balsa)
    new_balsa.init_logger()
    new_balsa.log.error("no parent")

    my_balsa.remove()
    new_balsa.remove()
Example #13
0
def test_balsa_simple():
    application_name = 'test_balsa'

    balsa = Balsa(application_name,
                  __author__,
                  verbose=True,
                  log_directory='temp',
                  error_callback=my_callback)
    balsa.init_logger()

    log.error('test error message')
Example #14
0
def test_balsa_callback():
    application_name = "test_balsa_callback"

    balsa = Balsa(application_name,
                  __author__,
                  verbose=True,
                  log_directory="temp",
                  error_callback=my_callback,
                  is_root=False)
    balsa.init_logger()

    log.error("test error message")
def test_multiprocessing():

    balsa = Balsa(application_name, author, verbose=True)
    balsa.init_logger()
    log = get_logger(application_name)
    log.info("process started")

    my_process = MyProcess(balsa.config_as_dict())
    my_process.start()
    my_process.join()

    log.info("process finished")
Example #16
0
def test_balsa_gui():
    application_name = "test_balsa_gui"

    balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", gui=True, is_root=False, delete_existing_log_files=True)
    balsa.init_logger()

    log = get_logger(application_name)

    press_enter_thread = threading.Thread(target=press_enter)
    press_enter_thread.start()
    log.error("test error message")
    press_enter_thread.join()
Example #17
0
def gui_main():

    balsa = Balsa(__application_name__, __author__, gui=True, verbose=get_gui_preferences().verbose)
    balsa.init_logger()
    balsa.handlers[HandlerType.DialogBox].setLevel(logging.ERROR)  # don't pop up warnings

    try:
        app = QApplication([])
        bup_gui = BupDialog()
        bup_gui.show()
        app.exec_()
    except Exception as e:
        log.exception(e)
Example #18
0
def test_gui_rate_limit():
    application_name = "test_gui_rate_limit"

    balsa = Balsa(application_name, __author__, verbose=True, log_directory="temp", gui=True, is_root=False, delete_existing_log_files=True)
    balsa.init_logger()

    log = get_logger(application_name)

    press_enter_thread = threading.Thread(target=press_enter)
    press_enter_thread.start()
    for count in range(0, 4):
        log.warning(str(count))

    press_enter_thread.join()
Example #19
0
def cli_main():

    args = get_arguments()

    balsa = Balsa(__application_name__, __author__)
    balsa.init_logger_from_args(args)

    log_selections(args)

    init_exit_control_cli()

    pmt = PropMTime(args.path, not args.noupdate, args.hidden, args.system)
    pmt.start()
    pmt.join()  # pmt uses exit control
    if not args.silent:
        print()
        print(f'processed {pmt.files_folders_count} files/folders in {pmt.total_time} seconds')
Example #20
0
def test_balsa_sentry():
    application_name = 'test_balsa_sentry'

    if 'SENTRY_DSN' in os.environ:
        balsa = Balsa(application_name,
                      __author__,
                      use_sentry=True,
                      inhibit_cloud_services=False,
                      is_root=False)
        balsa.init_logger()

        log = get_logger(application_name)
        log.error('test balsa sentry error message')
    else:
        print(
            'Please set SENTRY_DSN environment variable to have a good %s test'
            % __name__)
Example #21
0
def test_balsa_exception():
    application_name = 'test_balsa_exception'

    balsa = Balsa(application_name, __author__, gui=True, is_root=False)
    balsa.rate_limits[logging.ERROR]['count'] = 4  # we have 3 messages
    balsa.init_logger()

    log = get_logger(application_name)

    press_enter_thread = threading.Thread(target=press_enter)
    press_enter_thread.start()

    try:
        a = 1.0 / 0.0  # generate an exception for testing (not a real error)
    except ZeroDivisionError:
        log.error('test exception')
        log.error(traceback_string())

    press_enter_thread.join()
Example #22
0
def session_fixture():

    temp_dir.mkdir(parents=True, exist_ok=True)
    cache_dir.mkdir(parents=True, exist_ok=True)

    balsa = Balsa(__application_name__,
                  __author__,
                  log_directory=Path("log"),
                  delete_existing_log_files=True,
                  verbose=False)

    # add handler that will throw an assert on ERROR or greater
    test_handler = TestAWSimpleLoggingHandler()
    test_handler.setLevel(logging.ERROR)
    logging.getLogger().addHandler(test_handler)

    balsa.init_logger()

    print(f"{is_mock()=}")
Example #23
0
def test_global_balsa():

    with pytest.raises(RuntimeError):
        # not yet initialized
        get_global_balsa()

    with pytest.raises(RuntimeError):
        # not yet initialized
        get_global_config()

    balsa = Balsa("test_global_balsa", __author__, is_root=False)
    balsa.init_logger()

    global_balsa = get_global_balsa()
    assert global_balsa is not None
    assert global_balsa.backup_count > 0  # just test some attribute
    global_config = get_global_config()
    assert global_config is not None
    assert global_config["author"] == __author__
Example #24
0
def main():

    epilog = """
    cmpa - directory compare (by abel.co)

    Prints the result of the directory comparison.  Only the differences and the summary flag will be printed, unless 
    verbose is given (or if silent given then nothing will be printed). 

    - means extra files in first directory
    + means extra files in second directory
    ! means file contents mismatch
    = equality flag
    s summary

    process returns 1 if any mis-compares
    """

    parser = argparse.ArgumentParser(
        epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('directories',
                        nargs=2,
                        help='two directories to compare')
    parser.add_argument('-f',
                        '--filters',
                        nargs='*',
                        default=['*'],
                        help='file filters')
    parser.add_argument(
        '-e',
        '--exclude',
        nargs='*',
        default=[],
        help='exclude directories that start with these name(s)')
    parser.add_argument('-s',
                        '--silent',
                        action='store_true',
                        help='do not print')
    parser.add_argument(
        '-t',
        '--text',
        action='store_true',
        help='text based compare that ignores CR/LF differences')
    parser.add_argument('-i',
                        '--image',
                        action='store_true',
                        help='image based compare')
    parser.add_argument('--version',
                        action='store_true',
                        help='display version')
    parser.add_argument('-v', '--verbose', action='store_true', help='verbose')
    args = parser.parse_args()

    balsa = Balsa(__title__, __author__)
    balsa.init_logger_from_args(args)

    if args.version:
        print(__version__)

    cd = Compare(args.directories, args.filters, args.silent, args.text,
                 args.exclude, args.verbose)
    sys.exit(int(not cd.compare_ok_all))  # 0 is all compared OK, 1 otherwise
Example #25
0
import time
import random

from ismain import is_main
from balsa import get_logger, Balsa

from bup import print_log, __application_name__, __author__

log = get_logger(__application_name__)


def test_print_log():
    start = 11
    for value in range(start, 0, -1):
        if value < start - 1:
            time.sleep(1)
        print_log(f"{value-1}" * int(round(50.0 * random.random())))


if is_main():
    balsa = Balsa(__application_name__, __author__)
    balsa.init_logger()
    test_print_log()
Example #26
0
def main():
    balsa = Balsa(application_name, author, gui=True)
    balsa.init_logger()

    something_useful()