Ejemplo n.º 1
0
def configure(server=None, username=None, password=None, tid=None, auto=False):
    """
    Configure tmc.py to use your account.
    """
    if not server and not username and not password and not tid:
        if Config.has():
            if not yn_prompt("Override old configuration", False):
                return False
    reset_db()
    if not server:
        while True:
            defaulturl = "https://src.aalto.fi/tmc/org/aalto/"
            server = input("Server url [" + defaulturl + "]: ").strip()
            if len(server) == 0:
                server = defaulturl
            if not server.endswith('/'):
                server += '/'
            if not (server.startswith("http://")
                    or server.startswith("https://")):
                ret = custom_prompt(
                    "Server should start with http:// or https://\n" +
                    "R: Retry, H: Assume http://, S: Assume https://",
                    ["r", "h", "s"], "r")
                if ret == "r":
                    continue
                # Strip previous schema
                if "://" in server:
                    server = server.split("://")[1]
                if ret == "h":
                    server = "http://" + server
                elif ret == "s":
                    server = "https://" + server
            break

        print("Using URL: '{0}'".format(server))
    while True:
        if not username:
            username = input("Username: "******"Password: "******"{0}:{1}".format(username, password), encoding='utf-8')
        ).decode("utf-8")

        try:
            api.configure(url=server, token=token, test=True)
        except APIError as e:
            print(e)
            if auto is False and yn_prompt("Retry authentication"):
                username = password = None
                continue
            return False
        break
    if tid:
        select(course=True, tid=tid, auto=auto)
    else:
        select(course=True)
Ejemplo n.º 2
0
def configure(server=None, username=None, password=None, tid=None, auto=False):
    """
    Configure tmc.py to use your account.
    """
    if not server and not username and not password and not tid:
        if Config.has():
            if not yn_prompt("Override old configuration", False):
                return False
    reset_db()
    if not server:
        while True:
            defaulturl = "https://src.aalto.fi/tmc/org/aalto/"
            server = input("Server url [" + defaulturl + "]: ").strip()
            if len(server) == 0:
                server = defaulturl
            if not server.endswith('/'):
                server += '/'
            if not (server.startswith("http://")
                    or server.startswith("https://")):
                ret = custom_prompt(
                    "Server should start with http:// or https://\n" +
                    "R: Retry, H: Assume http://, S: Assume https://",
                    ["r", "h", "s"], "r")
                if ret == "r":
                    continue
                # Strip previous schema
                if "://" in server:
                    server = server.split("://")[1]
                if ret == "h":
                    server = "http://" + server
                elif ret == "s":
                    server = "https://" + server
            break

        print("Using URL: '{0}'".format(server))
    while True:
        if not username:
            username = input("Username: "******"Password: "******"{0}:{1}".format(username, password),
                  encoding='utf-8')).decode("utf-8")

        try:
            api.configure(url=server, token=token, test=True)
        except APIError as e:
            print(e)
            if auto is False and yn_prompt("Retry authentication"):
                username = password = None
                continue
            return False
        break
    if tid:
        select(course=True, tid=tid, auto=auto)
    else:
        select(course=True)
Ejemplo n.º 3
0
def main():
    parser = argh.ArghParser()
    parser.add_commands(commands)

    needs_update = should_update()
    Config.set("needs_update", "1" if needs_update else "0")
    if needs_update:
        infomsg("Update available to tmc.py. See tmc check-for-updates",
                "for more info.")

    # By default Argh only shows shortened help when no command is given.
    # This makes it print out the full help instead.
    if len(sys.argv) == 1:
        return parser.dispatch(argv=["help"])

    try:
        parser.dispatch()
    except TMCError as e:
        print(e)
        exit(-1)
Ejemplo n.º 4
0
def main():
    parser = argh.ArghParser()
    parser.add_commands(commands)

    needs_update = should_update()
    Config.set("needs_update", "1" if needs_update else "0")
    if needs_update:
        infomsg("Update available to tmc.py. See tmc check-for-updates",
                "for more info.")

    # By default Argh only shows shortened help when no command is given.
    # This makes it print out the full help instead.
    if len(sys.argv) == 1:
        return parser.dispatch(argv=["help"])

    try:
        parser.dispatch()
    except TMCError as e:
        print(e)
        exit(-1)
Ejemplo n.º 5
0
def should_update():
    from xmlrpc.client import ServerProxy
    from distutils.version import StrictVersion
    from datetime import datetime
    import calendar

    current_version = StrictVersion(__version__)
    last_value = (Config.has_name("needs_update")
                  and Config.get_value("needs_update") == "1")
    last_version = (0, 0, 0)
    if Config.has_name("last_version"):
        last_version = StrictVersion(Config.get_value("last_version"))

    # Return false if an upgrade has happened
    if last_value and (last_version < current_version):
        return False

    Config.set("last_version", __version__)

    # Next lets check the time
    last_time = None
    if Config.has_name("last_update_check"):
        last_time = datetime.utcfromtimestamp(
            int(Config.get_value("last_update_check")))
    else:
        last_time = datetime.now()

    if (last_time - datetime.now()).days < 7:
        return False

    Config.set("last_update_check",
               calendar.timegm(datetime.now().timetuple()))

    # Lastly lets check pypi for versions
    pypi = ServerProxy("http://pypi.python.org/pypi")
    pypiversion = StrictVersion(pypi.package_releases("tmc")[0])

    if pypiversion > current_version:
        return True
    return False
Ejemplo n.º 6
0
def should_update():
    from xmlrpc.client import ServerProxy
    from distutils.version import StrictVersion
    from datetime import datetime
    import calendar

    current_version = StrictVersion(__version__)
    last_value = (Config.has_name("needs_update")
                  and Config.get_value("needs_update") == "1")
    last_version = (0, 0, 0)
    if Config.has_name("last_version"):
        last_version = StrictVersion(Config.get_value("last_version"))

    # Return false if an upgrade has happened
    if last_value and (last_version < current_version):
        return False

    Config.set("last_version", __version__)

    # Next lets check the time
    last_time = None
    if Config.has_name("last_update_check"):
        last_time = datetime.utcfromtimestamp(int(
            Config.get_value("last_update_check")))
    else:
        last_time = datetime.now()

    if (last_time - datetime.now()).days < 7:
        return False

    Config.set("last_update_check",
               calendar.timegm(datetime.now().timetuple()))

    # Lastly lets check pypi for versions
    pypi = ServerProxy("http://pypi.python.org/pypi")
    pypiversion = StrictVersion(pypi.package_releases("tmc")[0])

    if pypiversion > current_version:
        return True
    return False
Ejemplo n.º 7
0
    def configure(self, url=None, token=None, test=False):
        """
        Configure the api to use given url and token or to get them from the
        Config.
        """

        if url is None:
            url = Config.get_value("url")
        if token is None:
            token = Config.get_value("token")

        self.server_url = url
        self.auth_header = {"Authorization": "Basic {0}".format(token)}
        self.configured = True

        if test:
            self.test_connection()

        Config.set("url", url)
        Config.set("token", token)