Exemple #1
0
    def filter_notes_by_tags(self, notepaths, criteria):
        """
        Filter out notes that do not have a tag in criteria.
        """

        # If criteria does not have nay tags in, let every note pass through.
        if 'tags' not in criteria:
            return notepaths

        self.tag_index.update_index()

        # Take every note whose tags include all tags in the criteria.
        res = defaultdict(set)
        for t in criteria['tags']:
            # print(self.tag_index.index)
            if t in self.tag_index.index:
                res[t].update(set(self.tag_index[t]))
        if not res.values():
            return []
        else:
            note_ids = reduce(set.intersection, res.values())
            filtered_notepaths = []
            for np in notepaths:
                _, _, _, identifier = extract_filename_info(np)
                if identifier in note_ids:
                    filtered_notepaths.append(np)
            return filtered_notepaths
Exemple #2
0
 def list_dates(self):
     creation_dates, modification_dates = set(), set()
     for notepath in self.list_notes():
         c_date, m_date, _, _ = extract_filename_info(notepath)
         creation_dates.add(c_date.strftime('%Y-%m-%d %a'))
         modification_dates.add(m_date.strftime('%Y-%m-%d %a'))
     return sorted(creation_dates), sorted(modification_dates)
Exemple #3
0
 def build_index(self):
     self.index = defaultdict(set)
     for notepath in glob.glob(self.notepath_pattern):
         _, _, _, note_id = extract_filename_info(notepath)
         note = parse_note(notepath) 
         for tag in note.tags:
             self.index[tag].add(note_id)
     self.save_index()
Exemple #4
0
 def build_index(self):
     self.index = defaultdict(set)
     rex = re.compile('\W')
     for notepath in glob.glob(self.notepath_pattern):
         _, _, _, note_id = extract_filename_info(notepath)
         with open(notepath) as f:
             text = f.read().lower()
         for token in rex.split(text):
             self.index[token].add(note_id)
     self.save_index()
Exemple #5
0
    def update_notepath(self, old_notepath, modification_date, creation_date=None):
        """Update a note's filename when it is edited.

        Also history get rewritten with the new filename.
        """
        # old_notepath is form
        # <poiroot>/notes/YYYY-MM-DD-<num>-YYYY-MM-DD<file_ext>

        # dirname is of form
        # <poiroot>/notes
        dirname = os.path.dirname(old_notepath)

        # basename is of form
        # YYYY-MM-DD-<num>-YYYY-MM-DD<file_ext>
        basename = os.path.basename(old_notepath)

        # root = YYYY-MM-DD-<num>-YYYY-MM-DD
        # ext = <file_ext>
        root, ext = os.path.splitext(basename)

        # Extract creation and modification dates as datetime objects and
        # number as an int:
        c_date, m_date, num, _ = extract_filename_info(old_notepath)

        if creation_date:
            c_date = creation_date
        else:
            c_date = c_date.strftime('%Y-%m-%d')

        # Replace previous modification date by the new one, given as an argument:
        m_date = modification_date

        new_notepath = os.path.join(dirname, c_date + '-' + str(num) + '-' + m_date + ext)

        # Create a backup copy of the old file:
        t = str(int(time.time()))
        backup_path = os.path.join(self.backups, root + '-' + t + ext)
        shutil.copy(old_notepath, backup_path)

        # Update the filename with the new modification date:
        shutil.move(old_notepath, new_notepath)

        # Update listing history with the new notepath:
        for listing in self.history:
            for i, notepath in enumerate(listing):
                if notepath == old_notepath:
                    listing[i] = new_notepath

        # Finally, set the last note path to point to the newly edited note:
        self.last_notepath = new_notepath

        return new_notepath
Exemple #6
0
    def filter_notes_by_date(self, notes, criteria):
        """
        Filter out notes whose date(s) do not meet criteria.

        Parameters
        ----------
        notes : list
            A list of filepaths of notes.
        criteria : dics
        A dictionary possibly containing several filtering criteria. In this
        function, the follwoing criteria are used, each of which is a date
        object:
            - min_creation_date
            - max_creation_date
            - min_modification_date
            - max_modification_date

        Returns
        -------
        A list of notes whose date(s) meet the criteria.
        """
        result = []

        if 'min_creation_date' not in criteria:
            criteria['min_creation_date'] = datetime.date(1970, 1, 1)
        if 'min_modification_date' not in criteria:
            criteria['min_modification_date'] = datetime.date(1970, 1, 1)
        if 'max_creation_date' not in criteria:
            criteria['max_creation_date'] = datetime.date(9999, 12, 31)
        if 'max_modification_date' not in criteria:
            criteria['max_modification_date'] = datetime.date(9999, 12, 31)

        for notepath in notes:
            creation_date, modification_date, _, _ = extract_filename_info(notepath)
            if creation_date < criteria['min_creation_date']:
                continue
            if creation_date > criteria['max_creation_date']:
                continue
            if modification_date < criteria['min_modification_date']:
                continue
            if modification_date > criteria['max_modification_date']:
                continue
            result.append(notepath)
        return result
Exemple #7
0
    def update_index(self):
        """
        Parameter
        ---------
        date : str
            Of form YYYY-MM-DD. Only notes whose modification is greater than
            or equal to this date will be considered when the index is updated.
        """

        # Get the last update date:
        t = os.path.getmtime(self.filepath)
        d = datetime.datetime.fromtimestamp(t)
        date =  d.strftime('%Y-%m-%d')

        for notepath in glob.glob(self.notepath_pattern):
            _, m_date, _, note_id = extract_filename_info(notepath)
            m_date = m_date.strftime('%Y-%m-%d')
            if m_date >= date:
                note = parse_note(notepath) 
                for tag in note.tags:
                    self.index[tag].add(note_id)
        # Save the updated index:
        self.save_index()
Exemple #8
0
    def update_index(self):
        """
        Parameter
        ---------
        date : str
            Of form YYYY-MM-DD. Only notes whose modification is greater than
            or equal to this date will be considered when the index is updated.
        """
        # Get the last update date:
        t = os.path.getmtime(self.filepath)
        d = datetime.datetime.fromtimestamp(t)
        date =  d.strftime('%Y-%m-%d')

        for filepath in glob.glob(self.notepath_pattern):
            _, m_date, _, note_id = extract_filename_info(filepath)
            m_date = m_date.strftime('%Y-%m-%d')
            if m_date >= date:
                with open(filepath, 'rt') as f:
                    text = f.read()
                for token in text.split():
                    self.index[token].add(note_id)  

        # Save the updated index:
        self.save_index()