Esempio n. 1
0
def upsert_notes(store, files, notebook):
    """Create or update notes from content in local files.
    Technical notes:
    We store file path in sourceURL attribute of a note. And
    use this sourceURL to match notes and files.
    """
    result = Counter({'created': 0, 'updated': 0, 'removed': 0})
    note_filter = NoteStore.NoteFilter()
    note_filter.words = 'notebook:{}'.format(notebook.name)
    existing_notes = store.findNotes(dev_token, note_filter, 0,
                                     max_note_count).notes
    # Remove notes
    file_urls = set(utils.path_to_source_url(notebook, f) for f in files)
    removed_notes = [
        n for n in existing_notes if n.attributes.sourceURL not in file_urls
    ]
    for n in removed_notes:
        log('Removed note [{}]({})'.format(n.title, notebook.name))
        store.deleteNote(dev_token, n.guid)
    result.update({'removed': len(removed_notes)})
    # Create or update notes
    url_to_notes = {n.attributes.sourceURL: n for n in existing_notes}
    for f in files:
        url = utils.path_to_source_url(notebook, f)
        note = url_to_notes.get(url)
        last_modified = os.path.getmtime(f)
        if note:
            if last_modified * 1000 > note.updated:
                debug('{} has been modified since last sync.'.format(f))
                update_note(store, note, f)
                log('Updated [{}](Notebook:{}) from {}'.format(
                    note.title, notebook.name, f))
                result.update({'updated': 1})
        else:
            debug('{} is a new note file.'.format(f))
            note = create_note(store, f, notebook)
            log('Created [{}](Notebook:{}) from {}'.format(
                note.title, notebook.name, f))
            result.update({'created': 1})
    return result
Esempio n. 2
0
def upsert_notes(store, files, notebook):
    """Create or update notes from content in local files.
    Technical notes:
    We store file path in sourceURL attribute of a note. And
    use this sourceURL to match notes and files.
    """
    result = Counter({'created':0, 'updated':0, 'removed':0})
    note_filter = NoteStore.NoteFilter()
    note_filter.words = 'notebook:{}'.format(notebook.name)
    existing_notes = store.findNotes(dev_token, note_filter, 0, max_note_count).notes
    # Remove notes
    file_urls = set(utils.path_to_source_url(notebook, f) for f in files)
    removed_notes = [n for n in existing_notes if n.attributes.sourceURL not in file_urls]
    for n in removed_notes:
        log('Removed note [{}]({})'.format(n.title, notebook.name))
        store.deleteNote(dev_token, n.guid)
    result.update({'removed': len(removed_notes)})
    # Create or update notes
    url_to_notes = {n.attributes.sourceURL: n for n in existing_notes}
    for f in files:
        url = utils.path_to_source_url(notebook, f)
        note = url_to_notes.get(url)
        last_modified = os.path.getmtime(f)
        if note:
            if last_modified * 1000 > note.updated:
                debug('{} has been modified since last sync.'.format(f))
                update_note(store, note, f)
                log('Updated [{}](Notebook:{}) from {}'.format(
                    note.title, notebook.name, f))
                result.update({'updated': 1})
        else:
            debug('{} is a new note file.'.format(f))
            note = create_note(store, f, notebook)
            log('Created [{}](Notebook:{}) from {}'.format(
                note.title, notebook.name, f))
            result.update({'created': 1})
    return result
Esempio n. 3
0
def create_note(store, path, notebook):
    """Create a note from the content in a local file
    """
    ext = utils.get_file_ext(path)
    processor_cls = note_processors.get(ext)
    processor = processor_cls(path)
    note = Note()
    note.title = processor.get_title()
    note.content = processor.get_content()
    attributes = NoteAttributes()
    attributes.sourceURL = utils.path_to_source_url(notebook, path)
    note.attributes = attributes
    note.notebookGuid = notebook.guid
    try:
        return store.createNote(dev_token, note)
    except EDAMUserException as e:
        evernote_api_error(e, note)
Esempio n. 4
0
def create_note(store, path, notebook):
    """Create a note from the content in a local file
    """
    ext = utils.get_file_ext(path)
    processor_cls = note_processors.get(ext)
    processor = processor_cls(path)
    note = Note()
    note.title = processor.get_title()
    note.content = processor.get_content()
    attributes = NoteAttributes()
    attributes.sourceURL = utils.path_to_source_url(notebook, path)
    note.attributes = attributes
    note.notebookGuid = notebook.guid
    try:
        return store.createNote(dev_token, note)
    except EDAMUserException as e:
        evernote_api_error(e, note)