Пример #1
0
 def notify_by_email(self):
     img_path = '<img src="http://XXX:9000%s" />' % self.get_chart()
     text = "%s\n%s\n%s\n%s\n\n%s" % (
         self.system.name.upper(), self.pair.name, self.tf, self.account_name, datetime_to_string(self.client_time))
     html = "%s<br />%s<br />%s<br />%s<br /><br />%s<br /><br />%s" % (
         self.system.name.upper(), self.pair.name, self.tf, self.account_name, datetime_to_string(self.client_time),
         img_path)
     sendMail('SIGNAL | %s %s %s' % (self.pair.name, self.tf, datetime_to_string(self.client_time)), html, text)
Пример #2
0
 def __unicode__(self):
     return '%s / %s (%s)' % (self.pair, self.tf, datetime_to_string(self.time))
Пример #3
0
def main():
    default_file = "krono.sqlite"

    ap = argparse.ArgumentParser()
    ap.add_argument("-a", "--autosave", default=60, type=int)
    ap.add_argument("-f", "--file", default=default_file)
    ap.add_argument("-i", "--interactive", action="store_true")
    ap.add_argument("-p", "--project", default="")
    ap.add_argument("-n", "--notes", default="")
    ap.add_argument("-t", "--tags", default="")
    ap.add_argument("-v", "--view", action="store_true")
    ap.add_argument("--debug", action="store_true")
    args = vars(ap.parse_args())

    if args["debug"]:
        logging_level = logging.DEBUG
    else:
        logging_level = logging.INFO
    logging.basicConfig(level=logging_level,
                        format="[%(levelname)s] %(message)s")

    filepath = os.path.abspath(args["file"])

    if args["interactive"]:
        # If interactive mode chosen, enter curses-based command line
        # interface via CLI class.
        CLI().cmdloop()
    elif args["view"]:
        # If view chosen, view using Log.view() curses interface.
        try:
            log = Log()
            log.load_db(filepath)
            log.select_all()
            log.view()
            log.unload_db()
        except Exception as e:
            logging.error(e)
    else:
        # Instantiate Log object. Create DB if necessary, else load existing.
        log = Log()
        if not os.path.isfile(filepath):
            logging.info("Creating database file {}".format(filepath))
            try:
                log.create_db(filepath)
            except Exception as e:
                logging.error(e)
        else:
            try:
                log.load_db(filepath)
                logging.info("Loaded database file {}".format(filepath))
            except Exception as e:
                logging.error(e)

        # Add new row to end of DB with current datetime as start time.
        start_time = datetime.datetime.now()
        log.add_row({
            "start": datetime_to_string(start_time),
            "project": args["project"],
            "tags": args["tags"],
            "notes": args["notes"]
        })

        # Get id of last row added. This will be used to periodically update
        # DB with new end time in Session thread, as well as at the end of
        # this function (__main__.main()).
        last_row_id = log.get_last_row_id()

        # Use lock to manage access to DB between this and Session threads.
        db_lock = threading.Lock()
        sess = Session(log,
                       last_row_id,
                       autosave_interval=int(args["autosave"]),
                       lock=db_lock)
        sess.start()

        logging.info("New session started. Press Enter to stop.")
        if sys.version_info.major < 3:
            raw_input()
        else:
            input()

        # Write current datetime as end time before exiting.
        current_datetime = datetime_to_string(datetime.datetime.now())

        try:
            db_lock.acquire()
            log.update_row(last_row_id, {"end": current_datetime})
        except Exception as e:
            logging.error(e)
        finally:
            db_lock.release()
            log.unload_db()