Exemplo n.º 1
0
def main(api_token, output_path):
    from pyutils import connected_to_internet
    if not connected_to_internet():
        raise RuntimeError("Need connection to the internet to generate mock")
    if api_token is None:
        api_token = os.path.join(lib_root, 'config', 'todoist.token')
    if os.path.exists(api_token):
        from pyutils import get_password
        api_token = get_password(api_token)
    else:
        raise RuntimeError("Need api token to generate mock")

    prototype = todoist.TodoistAPI(api_token)
    engine = 'pkl'  # 'json' or 'pkl'
    mock_path = output_path or os.path.join(lib_root, 'app_data', 'mock_TodoistAPI_py{}.{}'.format('3' if is_python_3() else '2', engine))

    from pyutils import Mock
    mock_api = Mock(prototype, dump_path=mock_path, dump_engine=engine)

    # now do sample requests
    mock_api.sync()
    # _ = mock_api.updates # no such field, what was i thinking
    _ = mock_api.activity
    mock_api.commit()
    mock_api.items.all()
    mock_api.projects.all()
 def connect_to_todoist(self, api_token=None):
     if connected_to_internet():
         if api_token is None:
             from dev.old.resources import default_token_path
             api_token = default_token_path
         if os.path.exists(api_token):
             from pyutils import get_token
             api_token = get_token(api_token)
         if True:
             print("WARNING - using mock to generate mock.")
             prototype = TodoistAPI(api_token)
             engine = 'pkl'  # 'json' or 'pkl'
             from secrets import lib_root
             mock_path = os.path.join(lib_root, 'app_data', 'mock_TodoistAPI_py{}.{}'.format('3' if is_python_3() else '2', engine))
             from pyutils import Mock
             self._api = Mock(prototype, dump_path=mock_path, dump_engine=engine)
         else:
             self._api = TodoistAPI(api_token)
     else:
         prototype = TodoistAPI()
         engine = 'pkl'  # 'json' or 'pkl'
         from secrets import lib_root
         mock_path = os.path.join(lib_root, 'app_data', 'mock_TodoistAPI_py{}.{}'.format('3' if is_python_3() else '2', engine))
         from pyutils import Mock
         self._api = Mock(prototype, dump_path=mock_path, dump_engine=engine)
Exemplo n.º 3
0
 def connect_to_todoist(self, api_token=None):
     if connected_to_internet():
         import todoist
         if api_token is None:
             from dev.old.resources import default_token_path
             api_token = default_token_path
         if os.path.exists(api_token):
             from pyutils import get_token
             api_token = get_token(api_token)
         self._api = todoist.TodoistAPI(api_token)
     else:
         raise NotImplementedError(
             "Todo - mock todoist api client that behaves ")
Exemplo n.º 4
0
def main(api_token):

    from pyutils import connected_to_internet
    if not connected_to_internet():
        raise RuntimeError("Need connection to the internet to generate mock")
    if api_token is None:
        from resources import default_token_path
        api_token = default_token_path
    if os.path.exists(api_token):
        from pyutils import get_token
        api_token = get_token(api_token)
    else:
        raise RuntimeError("Need api token to generate mock")

    prototype = todoist.TodoistAPI(api_token)
    from resources import lib_root
    engine = 'pkl'  # 'json' or 'pkl'
    mock_path = os.path.join(lib_root, 'resources',
                             'mock_TodoistAPI.{engine}'.format(engine=engine))
    from pyutils import Mock
    mock_api = Mock(prototype, dump_path=mock_path, dump_engine=engine)

    api = todoist.TodoistAPI(api_token)
    from resources import lib_root
    engine = 'pkl'  # 'json' or 'pkl'
    mock_path = os.path.join(lib_root, 'resources',
                             'mock_TodoistAPI.{engine}'.format(engine=engine))
    from pyutils import Mock
    mock_api = Mock(prototype, dump_path=mock_path, dump_engine=engine)

    # now do sample requests
    print("Sync:\n", mock_api.sync().__repr__()[:200])

    # _ = mock_api.updates # no such field, what was i thinking
    print("\n\tActivity:\n", mock_api.activity.__repr__()[:200])
    print("\n\tcommit:\n", mock_api.commit().__repr__()[:200])
    print("\n\titems:\n", mock_api.items.all().__repr__()[:200])
    print("\n\tprojects:\n", mock_api.projects.all().__repr__()[:200])
    print('It seems mock is doing alright')
Exemplo n.º 5
0
def main():
    while True:
        ts = datetime.datetime.now()

        last_online = get_latest_online_time()

        offline_period = ts - last_online

        if offline_period > datetime.timedelta(minutes=10):
            print("Last online: ",
                  last_online.strftime("%Y/%m/%d at %H:%M:%S"))
            print("Offline period: {} hours, {} minutes".format(
                offline_period.seconds / 3600,
                (offline_period.seconds / 60) % 60))
            write(
                "WARNING: Server relaunch on {}. Offline for ".format(
                    ts.strftime("%Y/%m/%d at %H:%M:%S"), offline_period),
                error_file_path)
        #
        # except:
        #     write("WARNING: Failed to get latest online time on {}".format(ts.strftime("%Y/%m/%d at %H:%M:%S")), error_file_path)
        #     print("Failed get latest online time ")

        file_name = "{ts}.txt".format(ts=ts.strftime("%Y-%m-%d_%H"))
        file_path = os.path.join('data', file_name)

        from pyutils import connected_to_internet
        connected = connected_to_internet()

        message = "Server online on {}. Connected to internet: {} \n".format(
            ts.strftime("%Y/%m/%d at %H:%M:%S"), connected)
        write(message, file_path)
        print(message)
        if not connected:
            log_error("WARNING: No internet connection on {}".format(
                ts.strftime("%Y/%m/%d at %H:%M:%S")))

        time.sleep(60)