Exemplo n.º 1
0
def main():
    if not isfile("urls.xml"):
        make_new_xml_file("urls.xml")

    tree = ET.parse("urls.xml")
    root = tree.getroot()

    decision = check_input(sys.argv, root)

    # If the user has input an invalid input
    if decision == -1:
        print("Invalid Arguments Given: use help to learn more.")

    # If the user has decided to open a short word
    elif decision == 0:
        webbrowser.open_new(get_url(sys.argv[1], root))

    # If the user has decided to add a new url
    elif decision == 1:
        if check_new_url(sys.argv):
            short_url = sys.argv[2]
            long_url = sys.argv[3]
            if not long_url.startswith("http://") and not long_url.startswith(
                    "https://"):
                long_url = "http://" + long_url
            add_url_to_xml(short_url, long_url, tree, "urls.xml")

    # If the user needs help
    elif decision == 2:
        get_help_message()

    # If the user decided to re-name a short word
    elif decision == 3:
        try:
            if check_new_short_is_valid(sys.argv[2]) and check_new_short_is_unique(sys.argv[2], root) and \
                    is_shortened_url(sys.argv[3], root):
                alter_short_url(sys.argv[2], sys.argv[3], tree, "urls.xml")
            else:
                print("Invalid Input: " + sys.argv[3] +
                      " is not a current short word.")

        except IndexError:
            print(
                "Invalid Input: specify the new short word and the old short word to be replaced."
            )

    # If the user want to delete a short word
    elif decision == 4:
        try:
            delete_url(sys.argv[2], tree, "urls.xml")
        except IndexError:
            print("Invalid Input: specify the short word to be deleted.")

    # If the user wants to see a list of all the short words and urls
    elif decision == 5:
        for child in root:
            print(child.get("short") + " : " + child.find("url").text)
Exemplo n.º 2
0
 def test_check_new_url_invalid_help(self):
     args_list = ["./main.py", "-n", "help", "http://www.python.org"]
     self.assertFalse(check_new_url(args_list))
Exemplo n.º 3
0
 def test_check_new_url_valid(self):
     args_list = ["./main.py", "-n", "p", "http://www.python.org"]
     self.assertTrue(check_new_url(args_list))