Beispiel #1
0
def search(content: str = None,
           from_created: datetime = None,
           to_created: datetime = None,
           from_modified: datetime = None,
           to_modified: datetime = None,
           retweet: bool = None,
           all: bool = False) -> List[Tweet]:
    """
    Performs search on tweets and returns list of results.
    If no parameters are provided, this will yield same results as
    listing tweets.

    :param content: Content to search in tweet.
    :param from_created: Start time for tweet creation.
    :param to_created: End time for tweet creation.
    :param from_modified: Start time for tweet modification.
    :param to_modified: End time for tweet modification.
    :param retweet:
        Flag indication if retweet or original tweets should be searched.
    :param all:
        Flag indicating if all nodes should be searched or only this one.
    :return: Result searching tweets.
    :rtype: [Tweet]
    """
    search_func = partial(get_ops().search_tweets, content, from_created,
                          to_created, from_modified, to_modified, retweet)
    res = [Tweet(*args) for args in get_db().do(search_func)]
    if all:
        others_res = search_others(content, from_created, to_created,
                                   from_modified, to_modified, retweet)
        res.extend(others_res)
    return res
Beispiel #2
0
def get_all(db=None) -> List[Node]:
    if not db:
        db = get_db()
    """
    Returns list of all nodes.
    """
    return [Node(*args) for args in db.do(get_ops().get_all_nodes)]
Beispiel #3
0
 def _enhance_shell():
     """
     Adds some utility stuff to flask interactive shell.
     """
     return {
         'db': get_db(),
     }
Beispiel #4
0
def delete_all() -> bool:
    """
    Deletes all nodes from list of nodes.

    :return:
        Flag indicating if nodes were deleted. That might be false if the
        list was empty previously.
    """
    return get_db().do(get_ops().delete_all_nodes)
Beispiel #5
0
 def test_db(verbose, timeout):
     """
     Tests DB connection by executing simple query.
     """
     end = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
     while True:
         try:
             get_db().test_connection()
         except Exception as e:
             if datetime.datetime.now() < end:
                 time.sleep(0.5)
                 continue
             if verbose:
                 traceback.print_exc()
             else:
                 print(str(e))
             sys.exit(1)
         else:
             break
Beispiel #6
0
def create(content):
    """
    Creates new tweet with provided content.

    :param str content: Tweet content.
    :return: Created tweet.
    :rtype: Tweet
    """
    check_length(content)
    return Tweet(*get_db().do(partial(get_ops().insert_tweet, content)))
Beispiel #7
0
def retweet(server, id_):
    """
    Creates retweet of original tweet.

    :param server: Server name that holds original tweet.
    :param id_: ID of tweet on original server.
    :return: Newly created tweet.
    :rtype: Tweet
    """
    return Tweet(*get_db().do(partial(get_ops().create_retweet, server, id_)))
Beispiel #8
0
def delete(id_):
    """
    Removes tweet with provided ID from database.
    :param id_: ID of tweet to delete.
    :raises NotFound: If tweet with provided ID was not found.
    """
    deleted = get_db().do(partial(get_ops().delete_tweet, id_))
    if not deleted:
        raise NotFound(f'Tweet with ID: {id_} not found.')
    return deleted
Beispiel #9
0
def delete(name: str) -> bool:
    """
    Deletes node from list of nodes.

    :param name: Name of node to delete.
    :return:
        Flag indicating if node was deleted. That might be false if node was
        not found.
    """
    return get_db().do(partial(get_ops().delete_node, name))
Beispiel #10
0
    def __init__(self, version_table='_migrations'):
        """
        :param version_table: Name of table to hold current migration status.
        """
        self.version_table = version_table
        self.db = get_db()
        self.ensure_infrastructure()
        self.migrations = self.collect_migrations()

        logger.info('Found %d migrations. Last applied id is %s.',
                    len(self.migrations), self.current_version())
Beispiel #11
0
def by_id(id_):
    """
    Returns tweet with specified ID.
    :param int id_: ID of tweet to get.
    :return: Tweet with requested ID.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    res = get_db().do(partial(get_ops().get_tweet, id_))
    if res is None:
        raise NotFound(f'Tweet with id: {id_} not found.')
    return Tweet(*res)
Beispiel #12
0
def add(name: str, address: str, update: bool = False) -> Node:
    """
    Adds new node to list of nodes.

    :param name: Name of the node.
    :param address: Address of the node.
    :param update:
        Flag indicating if update of existing node should be done if node is
        already registered.
    :return: Newly created node.
    :raises Conflict:
        If node with same name already exists and update is False.
    """
    found = get_db().do(partial(get_ops().get_node, name))
    if found:
        if not update:
            raise Conflict('Node with same name already registered.')
        else:
            return Node(
                *get_db().do(partial(get_ops().update_node, name, address)))
    return Node(*get_db().do(partial(get_ops().insert_node, name, address)))
Beispiel #13
0
def count(type_: str = None):
    """
    Returns number of tweets in database. If `separate` is True, two values
    are returned. First is number of original tweets and second is number of
    retweets.

    If `separate` if False (default) only one number is returned.

    :param type_:
        Type of tweets to count. Valid values are 'original' and 'retweet'.
    :return:
    """
    return get_db().do(partial(get_ops().count_tweets, type_))
Beispiel #14
0
def modify(id_, content):
    """
    Modifies existing tweet with provided ID. New content will be set to
    whatever is provided.

    :param int id_: ID of tweet to modify.
    :param str content: New tweet content.
    :return: Modified tweet.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    check_length(content)
    updated = get_db().do(partial(get_ops().modify_tweet, id_, content))
    if not updated:
        raise NotFound(f'Tweet for ID: {id_} not found.')
    return Tweet(*updated)
Beispiel #15
0
    def register_handlers():
        """
        Performs some initialization before we start serving clients.

        This initialization is here since here we have application context,
        which we do not have until entire app object is ready.
        """
        with app.app_context():
            db = get_db()

        def handler(signum, frame):
            print('in signal handler', signum)
            unregister_all(db)
            sys.exit(0)

        signal.signal(signal.SIGTERM, handler)
        signal.signal(signal.SIGINT, handler)
Beispiel #16
0
def get_all():
    """
    Returns list of all tweets.
    :rtype: [Tweet]
    """
    return [Tweet(*args) for args in get_db().do(get_ops().get_all_tweets)]
Beispiel #17
0
def get_node(name: str) -> Node:
    """
    Returns list of all nodes.
    """
    return Node(*get_db().do(partial(get_ops().get_node, name)))