snose

Check-in [20b98bc9de]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Move some move common bits out to a method
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | master
Files: files | file ages | folders
SHA3-256: 20b98bc9de336a4d44ef94f71b24c951192d5750fcb04cb9af1916f01011fc81
User & Date: atomicules 2018-11-16 17:46:50
Context
2018-11-17
10:59
Break many more common bits out into methods:

- snitems (iterating through items)
- snwrite (writing a file from disk)
- snread (reading a file from disk)
- snremote (getting a remote note from Simplenote)+def snitems(snose): check-in: 3ee3b5e7a8 user: atomicules tags: trunk, master

2018-11-16
17:46
Move some move common bits out to a method check-in: 20b98bc9de user: atomicules tags: trunk, master
14:59
Add a write_index method, avoid repetition check-in: 410d185b80 user: simon tags: trunk, master
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to snose.py.

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
...
309
310
311
312
313
314
315
316










317
            returned = snclient.add_note({"content": content, "tags": ["snose"]})
            print("Imported %s into Simplenote with key %s" % (filename, returned[0]['key']))
        except IOError as e:
            print("Failed to add note to Simplenote")
            print(e)
        else:
            #Add mapping
            snose[filename] = {'key': returned[0]['key'], 'version': returned[0]['version'], 'modifydate': float(os.path.getmtime(filename)) } #Use actual file mod date
            write_index(snose, "But note was successfully imported to Simplenote with key %s. Try sniffing the file")


def sniff(snclient, key, filename): #How to ensure remote gets or has snose tag?
    # Add a new mapping only
    snose = load_or_new()
    #Get details about current Simplenote file
................................................................................
        remote = snclient.get_note(key)
        #What if can't be found, need to abort...
    except IOError as e:
        print("Failed to find that note on Simplenote")
        print(e)
    else:
        #Add mapping
        snose[filename] = {'key': remote[0]['key'], 'version': remote[0]['version'], 'modifydate': float(os.path.getmtime(filename)) }
        write_index(snose)


def sneeze(snclient, key, filename):
    #place an existing note in current directory
    snose = load_or_new()
    #Get remote note
................................................................................
                with open(filename, 'w', encoding="utf-8") as f:
                    f.write(remote[0]['content'])
        except IOError as e:
            print("Failed to create local copy of that note")
            print(e)
        else:
            #Update index
            snose[filename] = {'key': remote[0]['key'], 'version': remote[0]['version'], 'modifydate': float(os.path.getmtime(filename)) } #Need to set as local modified date as otherwise will want to sync it straight away.
            write_index(snose, "But note was created locally. Try sniffing the file to add it to the index.")


def blow(snclient, key):
    #With given key from .snose file, roll back to the previous version

    #1) Check exists in .snose index
................................................................................
                    with open(name, 'r') as f:
                        content = f.read()
                except IOError as e:
                    print("Failed to read local note %s" % name)
                    print("Skipping synchronisation for this note")
                else:
                    try:
                        returned = snclient.update_note({'key': local['key'], 'version': local['version'], 'content': content, 'modifydate': sysmodifydate, 'tags': ['snose']  })
                        print("Updated remote version of %s" % name)
                    except IOError as e:
                        print("Failed to update remote verison of local note %s" % name)
                    else:
                        #Get returned metadata
                        snose[name]['version'] = returned[0]['version']
                        snose[name]['modifydate'] = sysmodifydate #Use local value to avoid differences in accuracy (decimal places. etc) between local and remote timestamps
................................................................................
    try:
        with open('.snose', 'w') as f:
            json.dump(snose, f, indent=2)
    except IOError as e:
        print("Failed to update index")
        if msg:
            print(msg)











main()







|







 







|







 







|







 







|







 








>
>
>
>
>
>
>
>
>
>

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            returned = snclient.add_note({"content": content, "tags": ["snose"]})
            print("Imported %s into Simplenote with key %s" % (filename, returned[0]['key']))
        except IOError as e:
            print("Failed to add note to Simplenote")
            print(e)
        else:
            #Add mapping
            snose[filename] = snobject(returned[0], filename)
            write_index(snose, "But note was successfully imported to Simplenote with key %s. Try sniffing the file")


def sniff(snclient, key, filename): #How to ensure remote gets or has snose tag?
    # Add a new mapping only
    snose = load_or_new()
    #Get details about current Simplenote file
................................................................................
        remote = snclient.get_note(key)
        #What if can't be found, need to abort...
    except IOError as e:
        print("Failed to find that note on Simplenote")
        print(e)
    else:
        #Add mapping
        snose[filename] = snobject(remote[0], filename)
        write_index(snose)


def sneeze(snclient, key, filename):
    #place an existing note in current directory
    snose = load_or_new()
    #Get remote note
................................................................................
                with open(filename, 'w', encoding="utf-8") as f:
                    f.write(remote[0]['content'])
        except IOError as e:
            print("Failed to create local copy of that note")
            print(e)
        else:
            #Update index
            snose[filename] = snobject(remote[0], filename)
            write_index(snose, "But note was created locally. Try sniffing the file to add it to the index.")


def blow(snclient, key):
    #With given key from .snose file, roll back to the previous version

    #1) Check exists in .snose index
................................................................................
                    with open(name, 'r') as f:
                        content = f.read()
                except IOError as e:
                    print("Failed to read local note %s" % name)
                    print("Skipping synchronisation for this note")
                else:
                    try:
                        returned = snclient.update_note(snobject(local, name, tags=['snose'], content=content))
                        print("Updated remote version of %s" % name)
                    except IOError as e:
                        print("Failed to update remote verison of local note %s" % name)
                    else:
                        #Get returned metadata
                        snose[name]['version'] = returned[0]['version']
                        snose[name]['modifydate'] = sysmodifydate #Use local value to avoid differences in accuracy (decimal places. etc) between local and remote timestamps
................................................................................
    try:
        with open('.snose', 'w') as f:
            json.dump(snose, f, indent=2)
    except IOError as e:
        print("Failed to update index")
        if msg:
            print(msg)


def snobject(returned, filename, tags=None, content=None):
    sno = {'key': returned['key'], 'version': returned['version'], 'modifydate': float(os.path.getmtime(filename)) }
    if tags:
        sno['tags'] = tags
    if content:
        sno['content'] = content
    return sno


main()