def get_newest_date(database: str = None): """ :rtype: datetime.datetime :param database: a str representing the location of a journal database :return: a datetime representing the date of the newest entry in the database """ db = default_database(database) if database else default_database() try: return get_all_dates(db)[-1] except IndexError: return None
def import_entries(): """Imports journal entries ('.mjson' files) and their associated attachments and creates a new entry in the db """ loc = imports_location() db = default_database() if not exists(loc): makedirs(loc) for file in scandir(loc): if '.mjson' in file.path: with open(file, 'r') as down: j = load(down) down.close() if not j['imported']: try: date = datetime.strptime(j['date'], '%Y-%m-%d-%H-%M-%S') except ValueError: date = datetime.now() body = j['body'] tags = tuple(j['tags']) attachments = j['attachments'] tmp = [] for att in attachments: path = join(loc, att) if exists(path): tmp.append(path) attachments = tuple(tmp) create_entry(database=db, body=body, tags=tags, date=date, attachments=attachments) j['imported'] = True with open(file, 'w') as up: dump(j, up) up.close()
def get_all_entry_ids(database: str = None): """Gets the id of every entry in the database :rtype: list :param database: a str representing the database that is being queried :return: a list of ints representing the entry ids """ db = connect(database) if database else connect(default_database()) with closing(db) as d: t = d.execute('SELECT entry_id FROM dates ORDER BY created').fetchall() return [x[0] for x in t]
def get_all_tags(database: str = None): """Gets all tags in the database :rtype: list :param database: a str representing the database that is being queried :return: a list of str representing all tags used in the database """ db = connect(database) if database else connect(default_database()) with closing(db) as d: t = set(d.execute('SELECT tag FROM tags ORDER BY tag').fetchall()) return [x[0] for x in t]
def get_number_of_entries(database: str = None): """Counts the number of entries in the database. :rtype: int :param database: a Connection or str representing the database that is being queried :return: an int representing the number of entries in the database """ db = connect(database) if database else connect(default_database()) with closing(db) as d: count = d.execute('SELECT COUNT() FROM bodies').fetchone()[0] return count
def get_all_relations(database: str = None): """Gets all relation pairs from the database :rtype: tuple :param database: a Connection or str representing the database that is being queried :return: a collection of linked pairs, each representing a parent-child relationship """ db = connect(database) if database else connect(default_database()) with closing(db) as d: pairs = d.execute('SELECT child,parent FROM relations') return pairs
def get_all_parents(database: str = None): """Gets the ids of all entries that have generated another entry :rtype: list :param database: a Connection or str representing the database that is being queried :return: a list of ints representing entries """ db = connect(database) if database else connect(default_database()) with closing(db) as d: return [ x[0] for x in d.execute('SELECT parent from relations').fetchall() ]
def get_years(database: str = None): """Lists, in order, the years in which the database has entries :rtype: list :param database: a Connection or str representing the database that is being queried :return: a list representing the years in which the database has entries """ db = connect(database) if database else connect(default_database()) with closing(db) as d: years = list({x[0] for x in d.execute('SELECT year FROM dates')}) years.sort() return years
def get_all_dates(database: str = None): """Gets the date for every entry in the database :rtype: list :param database: a str representing the database that is being queried :return: a list of datetime objects """ types = PARSE_DECLTYPES | PARSE_COLNAMES db = connect(database, detect_types=types) if database else connect( default_database(), detect_types=types) with closing(db) as d: dates = [ x[0] for x in d.execute( 'SELECT created FROM dates ORDER BY created').fetchall() ] return dates
def database_is_empty(database: str = None): if len(get_all_entry_ids( database if database else default_database())) == 0: return True else: return False