Example #1
0
def render_defaults(feedback):
    defaults, types = plumb.GetDefaults(False)
    tradier_key_warning = ""
    if ("tradier key" in defaults):
        if (defaults['tradier key'] == ""
                or defaults['tradier key'] == "demo"):
            tradier_key_warning = "Remember to obtain your lifetime API key from tradier, this is needed for getting the market calendar and quotes"
    IEX_key_warning = ""
    if ("IEX key" in defaults):
        if (defaults['IEX key'] == "" or defaults['IEX key'] == "demo"):
            IEX_key_warning = "Remember to obtain your lifetime API key from IEX cloud, this is needed for getting company ticker information"
    coin_key_warning = ""
    if ("coin key" in defaults):
        if (defaults['coin key'] == "" or defaults['coin key'] == "demo"):
            coin_key_warning = "Remember to obtain your lifetime API key from coin market cap, this is needed for getting cryptocoin information"
    table, column_options, name_options, folder_options = plumb.PrintDefaults(
        False)
    hide_folder = ""
    if (name_options == ""):
        hide_folder = "hidden"
    notes, initialize_day = plumb.GetAIMNotes(10, False)
    if os.name == 'nt':
        pid = plumb.get_pid("poll_stocks.py")
    else:
        pid = plumb.get_pid("folder_daemon.py")
    daemon_check = ""
    daemon_color = "green;"
    daemon_action = ""
    if (pid == []):
        daemon_check = "WARNING: stock polling is not running, your stocks are not updating, please start"
        daemon_color = "red;"
        daemon_action = "start"
    else:
        daemon_check = "stock polling is running on pid: {0}".format(pid[0])
        daemon_action = "restart"
    daemon_table, status = plumb.PrintDaemon("all", False)
    daemon_info = ""
    checkOpen = plumb.Holiday(False)
    if checkOpen:
        if (checkOpen['status'] == "closed"):
            daemon_info = checkOpen['description']
        else:
            daemon_info = "{0}, Last active status: {1}".format(
                checkOpen['description'], status)
    return (render_template('defaults.html',
                            table=table,
                            feedback=feedback,
                            column_options=column_options,
                            notes=notes,
                            daemon_table=daemon_table,
                            daemon_check=daemon_check,
                            daemon_color=daemon_color,
                            daemon_info=daemon_info,
                            daemon_action=daemon_action,
                            name_options=name_options,
                            folder_options=folder_options,
                            hide_folder=hide_folder,
                            tradier_key_warning=tradier_key_warning,
                            IEX_key_warning=IEX_key_warning,
                            coin_key_warning=coin_key_warning))
Example #2
0
def render_folder(ticker_style, feedback, symbol):
    defaults, types = plumb.GetDefaults(False)
    folder_name = "folder"
    if ("folder name" in defaults):
        folder_name = defaults['folder name']
    table, symbol_options, balance_options, amount_options = plumb.PrintFolder(
        False)
    notes, initialize_day = plumb.GetAIMNotes(10, False)
    co = {}
    if (symbol > ""):
        co = plumb.Company(symbol, False)
        if not co:
            feedback = "symbol not found."
            ticker_style = "display: none;"
    worksheet_table, worksheet_warning = plumb.PrintWorksheet(False)
    worksheet_style = "display: none;"
    if worksheet_table > "":
        worksheet_style = "display: block;"

    return (render_template('folder.html',
                            table=table,
                            ticker_style=ticker_style,
                            symbol_options=symbol_options,
                            balance_options=balance_options,
                            notes=notes,
                            ticker=co,
                            feedback=feedback,
                            amount_options=amount_options,
                            folder_name=folder_name,
                            worksheet_table=worksheet_table,
                            worksheet_style=worksheet_style,
                            worksheet_warning=worksheet_warning))
Example #3
0
def do_something(verbose):
    start = True
    log = {}
    log['status'] = "start"
    log['pid'] = os.getpid()
    log['working directory'] = os.getcwd()
    log['username'] = getpass.getuser()
    log['script'] = os.path.basename(__file__)[:-3]
    plumb.LogDaemon(log, verbose)
    plumb.Holiday(verbose)
    while True:
        defaults, types = plumb.GetDefaults(verbose)
        dm = 0
        if (defaults['poll minutes'] is None):
            dm = 10
        else:
            dm = defaults['poll minutes']
        df = ""
        if (defaults['folder name'] is None):
            log['status'] = 'error'
            log['content'] = "folder name is missing in defaults, cannot continue"
            plumb.LogDaemon(log, verbose)
            break
        else:
            df = plumb.GetDB(False)
        log['poll minutes'] = dm
        log['dbase name'] = df
        market_open = False
        if plumb.DayisOpen(False) and (not plumb.DayisClosed(False)):
            market_open = True
            log['open'] = defaults['open']
            log['final poll'] = time.ctime()
            log['status'] = 'wake'
            plumb.LogDaemon(log, False)
        else:
            log['status'] = 'closed'
            plumb.LogDaemon(log, verbose)
        log['status'] = 'sleep'
        plumb.LogDaemon(log, verbose)

        result, resultError, exceptionError = price_poll(
            True, verbose)  # always poll because crypto is open 24/7
        if not result:
            if resultError > "":
                log['status'] = 'error'
                log['content'] = resultError
                plumb.LogDaemon(log, verbose)
            if exceptionError > "":
                log['status'] = 'exception'
                log['content'] = exceptionError
                plumb.LogDaemon(log, verbose)
        else:
            log['status'] = 'success'
            plumb.LogDaemon(log, verbose)

        time.sleep(dm * 60)
Example #4
0
def usage():
    defaults, types = plumb.GetDefaults(False)
    keys = defaults.keys()
    key_list = ""
    for key in keys:
        if key != "username":
            key_list += "{0}\n\t\t\t\t".format(key)
    usage = """
    *********************
    **  Defaults Tool  **
    *********************

    -h  --help          prints this help
    -v  --verbose       increases the information level
    -t  --test          tests the default routines
    -c  --crypto        use crypto-coin API (used with --symbol or --quote)

    -s  --symbol        retrieves company data from ticker symbol
    -q  --quote         get stock quote from ticker symbol(s)
    -w  --when          retrieves the stock exchange holiday information

    -k  --key           keys in dbase to update (used with --item switch)
                            *** keys currently in dbase ***
                                {0}
    -i  --item          item value to update (used with --key switch)

    -p  --print         print out the defaults database (in HTML table format)
    -z  --zap           zap user back to standard defaults
    -g  --get           Gets/Shows all default fields

    -l  --log           show daemon log
                            --log='' (entire log), --log='wake' (wake status)
    -r  --run           runs stock polling app
    -c  --check         checks if stock polling app is running
    -k  --kill          kills stock polling app
    """.format(key_list)
    print(usage)
Example #5
0
def main(argv):
    verbose = False
    test = False
    quote = ""
    symbol = ""
    key = ""
    item = ""
    printout = False
    zap = False
    log = ""
    when = False
    get = False
    run = False
    daemon = False
    kill = False
    crypto = False
    try:
        opts, args = getopt.getopt(argv, "rdkgwl:zcpi:k:hvts:q:", [
            "help", "verbose", "test", "quote=", "key=", "symbol=", "item=",
            "print", "zap", "log=", "when", "get", "run", "daemon", "kill",
            "crypto"
        ])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-t", "--test"):
            test = True
        elif o in ("-g", "--get"):
            get = True
        elif o in ("-r", "--run"):
            run = True
        elif o in ("-d", "--daemon"):
            daemon = True
        elif o in ("-c", "--crypto"):
            crypto = True
        elif o in ("-k", "--kill"):
            kill = True
        elif o in ("-w", "--when"):
            when = True
        elif o in ("-z", "--zap"):
            zap = True
        elif o in ("-p", "--print"):
            printout = True
        elif o in ("-h", "--help"):
            usage()
            exit()
        elif o in ("-i", "--item"):
            item = a
        elif o in ("-q", "--quote"):
            quote = a.upper()
        elif o in ("-s", "--symbol"):
            symbol = a.upper()
        elif o in ("-k", "--key"):
            key = a
        elif o in ("-l", "--log"):
            log = a.lower()
        else:
            assert False, "unhandled option"
    if (test):
        testResult = plumb.TestDefaults(False, verbose)
        print(testResult)
        exit()
    if (key > "" and item > ""):
        if item > "":
            result = plumb.UpdateDefaultItem(key, item, verbose)
            if (result):
                print("saved.")
            else:
                print("failed.")
        else:
            print("you must use --item switch with the --key switch")
        exit()
    if (quote > "" and not crypto):
        quoteResult = plumb.QuoteTradier(quote, verbose)
        pprint.pprint(quoteResult)
        exit()
    if (symbol > "" and not crypto):
        companyResult = plumb.Company(symbol, verbose)
        pprint.pprint(companyResult)
        exit()
    if (quote > "" and crypto):
        quoteResult = plumb.QuoteCrypto(quote, verbose)
        pprint.pprint(quoteResult)
        exit()
    if (symbol > "" and crypto):
        companyResult = plumb.CryptoCompany(symbol, verbose)
        pprint.pprint(companyResult)
        exit()
    if (printout):
        printResult, column_options, name_options, folder_options = plumb.PrintDefaults(
            verbose)
        pprint.pprint(printResult)
        pprint.pprint(column_options)
        pprint.pprint(folder_options)
        pprint.pprint(name_options)
        exit()
    if (zap):
        endResult = plumb.ResetDefaults(verbose)
        if (endResult):
            print("saved.")
        else:
            print("failed.")
        exit()
    if (run):
        if os.name == 'nt':
            runResult = plumb.run_script("poll_stocks.py")
        else:
            runResult = plumb.run_script("./folder_daemon.py")
        exit()
    if (daemon):
        if os.name == 'nt':
            checkResult = plumb.get_pid("poll_stocks.py")
        else:
            checkResult = plumb.get_pid("folder_daemon.py")
        if (checkResult != []):
            log = "stock polling app is running at pid: {0}".format(
                checkResult)
            print(log)
        else:
            print("stock polling app is not running")
        exit()
    if (kill):
        if os.name == 'nt':
            checkResult = plumb.get_pid("poll_stocks.py")
        else:
            checkResult = plumb.get_pid("folder_daemon.py")
        if (checkResult != []):
            plumb.kill_pid(checkResult[0])
        else:
            print("stock polling app is not running")
        exit()
    if (log > ""):
        logResult, status = plumb.PrintDaemon(log, verbose)
        if (logResult > ""):
            pprint.pprint(logResult)
        else:
            print("failed.")
        print("last status: {0}".format(status))
        exit()
    if (when):
        whenResult = plumb.Holiday(verbose)
        if (whenResult):
            pprint.pprint(whenResult)
        else:
            print("failed.")
        exit()
    if (get):
        getResult, types = plumb.GetDefaults(verbose)
        if (getResult):
            pprint.pprint(getResult)
        else:
            print("failed.")
        exit()
    usage()
Example #6
0
def main(argv):
    verbose = False
    test = False
    update = False
    dollars = ""
    add = ""
    remove = ""
    symbol = ""
    balance = ""
    shares = ""
    printout = False
    get = False
    crypto = False
    try:
        opts, args = getopt.getopt(argv, 'cgpub:n:s:a:r:d:hvt', ['help', 'verbose', 'test', 'crypto', 'dollars=', 'add=', 'remove=', 'symbol=', 'balance=', 'number=', 'update', 'print', 'get'])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-t", "--test"):
            test = True
        elif o in ("-g", "--get"):
            get = True
        elif o in ("-c", "--crypto"):
            crypto = True
        elif o in ("-u", "--update"):
            update = True
        elif o in ("-p", "--print"):
            printout = True
        elif o in ("-h", "--help"):
            usage()
            exit()
        elif o in ("-a", "--add"):
            add = a.upper()
        elif o in ("-d", "--dollars"):
            dollars = a
        elif o in ("-r", "--remove"):
            remove = a.upper()
        elif o in ("-s", "--symbol"):
            symbol = a.upper()
        elif o in ("-b", "--balance"):
            balance = a
        elif o in ("-n", "--number"):
            shares = a
        else:
            assert False, "unhandled option"
    if (test):
        testResult = plumb.TestFolder(False, verbose)
        print(testResult)
        exit()
    defaults, types = plumb.GetDefaults(verbose)
    if defaults['folder name'] is None:
        print ("\tWarning, the database name is not set, please correct this")
        exit()
    if (dollars > ""):
        cashResult = plumb.Balance("$", 0, dollars, verbose)
        if (cashResult):
            print ("balance updated.")
        else:
            print ("failed.")
        exit()
    if (add > "" and not crypto):
        addResult = plumb.Add(add, "", verbose)
        if (addResult):
            print ("added.")
        else:
            print ("failed.")
        exit()
    if (add > "" and crypto):
        addResult = plumb.Add(add, "coinbase", verbose)
        if (addResult):
            print ("added.")
        else:
            print ("failed.")
        exit()
    if (remove > "" and not crypto):
        removeResult = plumb.Remove(remove, "", verbose)
        if (removeResult):
            print ("removed.")
        else:
            print ("failed.")
        exit()
    if (remove > "" and crypto):
        removeResult = plumb.Remove(remove, "coinbase", verbose)
        if (removeResult):
            print ("removed.")
        else:
            print ("failed.")
        exit()
    if (balance > "" and not crypto):
        if (symbol == ""):
            print ("\tWarning, to update the balance you also need a --symbol switch")
            exit()
        balanceResult = plumb.Balance(symbol, 0, balance, verbose)
        if (balanceResult['status']):
            print ("stock symbol: {0}, balance: {1}, new shares: {2}".format(symbol, balanceResult['balance'], balanceResult['shares']))
        else:
            print ("failed.")
        exit()
    if (balance > "" and crypto):
        if (symbol == ""):
            print ("\tWarning, to update the balance you also need a --symbol switch")
            exit()
        balanceResult = plumb.Balance(symbol, 1, balance, verbose)
        if (balanceResult['status']):
            print ("crypto symbol: {0}, balance: {1}, new shares: {2}".format(symbol, balanceResult['balance'], balanceResult['shares']))
        else:
            print ("failed.")
        exit()
    if (shares > "" and not crypto):
        if (symbol == ""):
            print ("\tWarning, to update the shares you also need a --symbol switch")
            exit()
        sharesResult = plumb.Shares(symbol, 0, shares, verbose)
        if (sharesResult['status']):
            print ("stock symbol: {0}, shares: {1}, new balance: {2}".format(symbol, sharesResult['shares'], sharesResult['balance']))
        else:
            print ("failed.")
        exit()
    if (shares > "" and crypto):
        if (symbol == ""):
            print ("\tWarning, to update the shares you also need a --symbol switch")
            exit()
        sharesResult = plumb.Shares(symbol, 1, shares, verbose)
        if (sharesResult['status']):
            print ("crypto symbol: {0}, shares: {1}, new balance: {2}".format(symbol, sharesResult['shares'], sharesResult['balance']))
        else:
            print ("failed.")
        exit()
    if (update):
        updateResult, updateError = plumb.Update(True, verbose)
        if (updateResult):
            print ("prices updated.")
        else:
            print ("failed. Error: {0}".format(updateError))
        exit()
    if (printout):
        symbol_options, balance_options, amount_options, items, keys = plumb.PrintFolder(verbose)
        if (symbol_options > ""):
            pprint.pprint(symbol_options)
        else:
            print ("failed.")
        if (balance_options > ""):
            pprint.pprint(balance_options)
        else:
            print ("failed.")
        if (amount_options is not None):
            pprint.pprint(amount_options)
        else:
            print ("failed.")
        if (items is not None):
            pprint.pprint(items)
        else:
            print ("failed.")
        if (keys is not None):
            pprint.pprint(keys)
        else:
            print ("failed.")
        exit()
    if (get):
        getResult = plumb.GetFolder(verbose)
        pprint.pprint(getResult)
        exit()
    usage()
Example #7
0
def main(argv):
    verbose = False
    test = ""
    aim = ""
    initialize = False
    notes = ""
    look = False
    update = False
    allo = False
    export = ""
    save = False
    try:
        opts, args = getopt.getopt(argv, "se:ailun:hvt:", ["help", "verbose", "test=", "notes=", "look", "update", "initialize", "allocation", "export=", "save"])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-a", "--allocation"):
            allo = True
        elif o in ("-t", "--test"):
            test = a
        elif o in ("-h", "--help"):
            usage()
            exit()
        elif o in ("-n", "--notes"):
            notes = a
        elif o in ("-e", "--export"):
            export = a
        elif o in ("-i", "--initialize"):
            initialize = True
        elif o in ("-l", "--look"):
            look = True
        elif o in ("-s", "--save"):
            save = True
        elif o in ("-u", "--update"):
            update = True
        else:
            assert False, "unhandled option"
    defaults, types = plumb.GetDefaults(False)
    if defaults['folder name'] is None:
        print ("\tWarning, the folder name is missing, please correct")
        exit()
    if defaults['test root'] is None:
        print ("\tWarning, the test root path is not set, please correct")
        exit()
    if (test > ""):
        testResult = plumb.TestAIM(test, verbose)
        print (testResult['output'])
        exit()
    if (notes > ""):
        notesResult, initialize_day = plumb.GetAIMNotes(notes, verbose)
        print (notesResult)
        if initialize_day:
            print("AIM system was initialized today")
        exit()
    if (initialize):
        nowResult, log = plumb.CreateAIM(verbose)
        print (nowResult, log)
        exit()
    if (look):
        lookResult, lookHTML, lookDB = plumb.Look(verbose)
        pprint.pprint(lookResult)
        pprint.pprint(lookHTML)
        pprint.pprint(lookDB)
        exit()
    if (update):
        postResult = plumb.Post(verbose)
        if (postResult):
             print ("updated.")
        else:
            print ("failed.")
        exit()
    if (allo):
        allocation_list, trending_list, life_list = plumb.AllocationTrends(verbose)
        pprint.pprint(allocation_list)
        pprint.pprint(trending_list)
        pprint.pprint(life_list)
        exit()
    if (export > ""):
        exportResult = plumb.Export(export, "", verbose)
        print (exportResult)
        exit()
    if (save):
        saveResult = plumb.Archive(verbose)
        if (saveResult):
             print ("updated.")
        else:
            print ("failed.")
        exit()
    usage()
Example #8
0
def main(argv):
    verbose = False
    test = False
    printyear = ""
    export = ""
    save = False
    archive = False
    delete = ""
    try:
        opts, args = getopt.getopt(argv, "d:ase:p:hvt", [
            "help", "verbose", "test", "notes=", "look", "update", "print=",
            "initialize", "allocation", "export=", "save", "archive", "delete="
        ])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-t", "--test"):
            test = True
        elif o in ("-a", "--archive"):
            archive = True
        elif o in ("-h", "--help"):
            usage()
            exit()
        elif o in ("-e", "--export"):
            export = a
        elif o in ("-d", "--delete"):
            delete = a
        elif o in ("-s", "--save"):
            save = True
        elif o in ("-p", "--print"):
            if (a[0] == '-'):
                if ("verbose" in a):
                    verbose = True
                printyear = str(datetime.datetime.now().year)
            else:
                printyear = a.lower()
        else:
            assert False, "unhandled option"
    defaults, types = plumb.GetDefaults(False)
    if defaults['folder name'] is None:
        print("\tWarning, the folder name is missing, please correct")
        exit()
    if defaults['test root'] is None:
        print("\tWarning, the test root path is not set, please correct")
        exit()
    if (test):
        testResult = plumb.TestHistory(False, verbose)
        print(testResult)
        exit()
    if (printyear > ""):
        printResult = plumb.PrintAIM(printyear, verbose)
        if (printResult > ""):
            pprint.pprint(printResult)
        else:
            print("failed.")
        exit()
    if (export > ""):
        exportResult = plumb.Export(export, "", verbose)
        print(exportResult)
        exit()
    if (delete > ""):
        deleteResult = plumb.DeleteSnapshot(delete, verbose)
        if (deleteResult):
            print("deleted.")
        else:
            print("failed.")
        exit()
    if (save):
        saveResult = plumb.Archive(verbose)
        if (saveResult):
            print("updated.")
        else:
            print("failed.")
        exit()
    if (archive):
        archiveResult = plumb.PrintSummary(verbose)
        if (archiveResult > ""):
            pprint.pprint(archiveResult)
        else:
            print("failed.")
        exit()
    usage()
Example #9
0
def do_something():
    start = True
    log = {}
    log['status'] = "start"
    log['pid'] = os.getpid()
    log['working directory'] = os.getcwd()
    log['username'] = getpass.getuser()
    log['script'] = os.path.basename(__file__)[:-3]
    filename = "/tmp/{0}_{1}.txt".format(log['username'], log['script'])
    log['filename'] = filename
    plumb.LogDaemon(log, False)
    plumb.Holiday(False)
    while True:
        defaults, types = plumb.GetDefaults(False)
        dm = 0
        if (defaults['poll minutes'] is None):
            dm = 10
        else:
            dm = defaults['poll minutes']
        df = ""
        if (defaults['folder name'] is None):
            log['status'] = 'error'
            log['content'] = "folder name is missing in defaults, cannot continue"
            with open(filename, "a") as f:
                f.write("pid: {0}, error: {1}, continuing".format(log['pid'], log['content']))
            plumb.LogDaemon(log, False)
            break
        else:
            df = plumb.GetDB(False)
        log['poll minutes'] = dm
        log['dbase name'] = df
        if plumb.DayisOpen(False) and (not plumb.DayisClosed(False)):
            log['open'] = defaults['open']
            log['final poll'] = time.ctime()
            log['status'] = 'wake'
            plumb.LogDaemon(log, False)
            result, resultError, exceptionError = price_poll()
            if not result:
                if resultError > "":
                    log['status'] = 'error'
                    log['content'] = resultError
                    plumb.LogDaemon(log, False)
                    with open(filename, "a") as f:
                        f.write("pid: {0}, error: {1}, continuing".format(log['pid'], resultError))
                if exceptionError > "":
                    log['status'] = 'exception'
                    log['content'] = exceptionError
                    plumb.LogDaemon(log, False)
                    with open(filename, "a") as f:
                        f.write("pid: {0}, exception: {1}, continuing".format(log['pid'], exceptionError))
            else:
                log['status'] = 'success'
                plumb.LogDaemon(log, False)
                with open(filename, "w") as f:
                    f.write("pid: {0}, {1} updated on: {2}. (sleeping for {3} minutes)".format(log['pid'], df, time.ctime(), dm))
        else:
            log['status'] = 'closed'
            plumb.LogDaemon(log, False)
            with open(filename, "w") as f:
                f.write("pid: {0}, now: {1}, open: {2}".format(log['pid'], time.ctime(), defaults['open']))
        log['status'] = 'sleep'
        plumb.LogDaemon(log, False)
        time.sleep(dm * 60)