Ejemplo n.º 1
0
    def __init__(self, argv):
        if argv[1][0] == "-":
            for arg in argv[1]:
                if arg == "r":
                    self.flags["is_report"] = True
                elif arg == "p":
                    self.flags["is_purge"] = True
                    self.flags["search_term"] = argv[2]
                elif arg == "m":
                    self.flags["is_monochrome"] = True
                elif arg == "d":
                    self.flags["is_default"] = True
                    self.flags["is_entry"] = True
                    self.flags["entry_text"] = argv[2]
                elif arg == "c":
                    self.flags["is_complete"] = True
                    self.flags["is_entry"] = True
                    self.flags["entry_text"] = argv[2]
                elif arg == "h":
                    self.flags["is_help"] = True
                elif arg == "t":
                    self.flags["is_tag"] = True
                    self.flags["is_search"] = True
                    self.flags["is_report"] = True
                elif arg == "s":
                    self.flags["is_search"] = True
                    self.flags["is_report"] = True
            if self.flags["is_complete"] == True and self.flags["is_report"] == True:
                self.flags["is_help"] = True
        else:
            self.flags["is_entry"] = True
            self.flags["entry_text"] = argv[1]
            # If anything was wrong, send to help
        if self.flags["is_help"]:
            print_help()
            # Lets get all the report/search stuff together!
        if self.flags["is_report"]:
            self.flags["search_query"] = "select * from `" + config.values["domain"] + "`"
            # It's a report, first try to parse it
            # print "It's a report!, num of args:"+str(len(argv))
            if self.flags["is_search"]:
                self.flags["search_query"] += " WHERE "
                # print "It's a search!"
                if len(argv) < 3:
                    print_error("No Search term or tag detected; use -h to see options")
                else:
                    if argv[2].count(",") > 0:
                        for i, v in enumerate(argv[2].split(",")):
                            self.flags["search_term"].append(v)
                    else:
                        self.flags["search_term"].append(argv[2])
                    # TODO Now we iterate and make there where clause, IN if search, tag= if tag
                    for key, term in enumerate(self.flags["search_term"]):
                        if self.flags["is_tag"]:
                            if key == 0:
                                self.flags["search_query"] += "tag IN ("
                            if key > 0:
                                self.flags["search_query"] += ","
                            self.flags["search_query"] += "'" + term + "'"
                        else:
                            if key > 0:
                                self.flags["search_query"] += " AND "
                            self.flags["search_query"] += " entry LIKE '%" + term + "%'"
                    if self.flags["is_tag"]:
                        self.flags["search_query"] += ")"

                if len(argv) >= 4:
                    # term and start date
                    self.flags["start_date"] = argv[3]
                    self.flags["search_query"] += " AND date>'" + self.flags["start_date"] + "'"
                if len(argv) == 5:
                    # term, start and finish date
                    self.flags["end_date"] = argv[4]
                    self.flags["search_query"] += " AND date<'" + self.flags["end_date"] + "'"
            else:
                if len(argv) >= 3:
                    # start date only
                    self.flags["start_date"] = argv[2]
                    self.flags["search_query"] += " WHERE date>'" + self.flags["start_date"] + "'"
                if len(argv) == 4:
                    # term and start date
                    self.flags["end_date"] = argv[3]
                    self.flags["search_query"] += " AND date<'" + self.flags["end_date"] + "'"