snose

Check-in [410d185b80]
Login

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

Overview
Comment:Add a write_index method, avoid repetition
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | master
Files: files | file ages | folders
SHA3-256: 410d185b807dd749ce75c0b5f82b9454feba5e9c95fe91fc1cb6e5aacc00d87e
User & Date: simon 2018-11-16 14:59:42
Original Comment: Add a write_index method, avoid repitition
Context
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
14:46
Minor formatting changes check-in: bf33015cce user: simon tags: trunk, master
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to snose.py.

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343










344
            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
            try:
                #Write back out
                with open('.snose', 'w') as f:
                    json.dump(snose, f, indent=2)
            except IOError as e:
                print("Failed to update .snose index file")
                #But note was added to Simplenote so?
                print("But note was successfully imported to Simplenote with key %s. Try sniffing the file" % returned[0]['key'])


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
    try:
        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:
        try:
            #Add mapping
            snose[filename] = {'key': remote[0]['key'], 'version': remote[0]['version'], 'modifydate': float(os.path.getmtime(filename)) }
            #Write back out
            with open('.snose', 'w') as f:
                json.dump(snose, f, indent=2)
        except IOError as e:
            print("Failed to update .snose index file")
            #Don't need to do anything else)



def sneeze(snclient, key, filename):
    #place an existing note in current directory
    snose = load_or_new()
    #Get remote note
    try:
................................................................................
                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
            try:
                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 back out
                with open('.snose', 'w') as f:
                    json.dump(snose, f, indent=2)
            except IOError as e:
                print("Failed to update .snose index file")
                print("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
    #2) Get previous version of remote
................................................................................
                        print("Rolled back remote version")
                    except IOError as e:
                        print("Failed to roll back remote version")
                    else:
                        #Get returned metadata
                        snose[filename]['version'] = returned[0]['version']
                        snose[filename]['modifydate'] = sysmodifydate
                        try:
                            #5) Update index file with results
                            with open('.snose', 'w') as f:
                                json.dump(snose, f, indent=2)
                        except IOError as e:
                             print("Failed to update .snose index file")
                             print("Try running a sync to get index corrected.")


def snot(snclient):
    #List simplenote notes tagged with "snose"
    notelist = snclient.get_note_list()
    #That gets list of keys. Then need to iterate through and get first line of text.
    #This is going to be slow.
................................................................................
                                print("Merged local content for %s" % name)
                                #Override the returned value? As otherwise next sync will immediately update the remote version for no reason.
                                snose[name]['modifydate'] = os.path.getmtime(name) 
                            except IOError as e:
                                print("Failed to merge content locally for %s" % name)
                                print("Therefore skipping updating the index for this note")#I think this is a good idea?
                        #Update the index file
                        try:
                            with open('.snose', 'w') as f:
                                json.dump(snose, f, indent=2)
                        except IOError as e:
                            print("Failed to update index for changes regarding local file %s" % name)
                            print("But remote and local copy of the file itself have been updated.")
                            #What now? I don't know.
            elif dry:
                print("Updated remote version of %s" % name)
                #For dry run, collect list of "updated remotes" to ignore in local updates
                dryremotes.append(name)
        #Fetch details from Simplenote
        try:
            remote = snclient.get_note(local['key'])
................................................................................
                    except IOError as e:
                        print("Failed to update local note %s with remote content" % name)
                        print("Will not updatet the .snose index file for this file")
                    else:
                        #Also update .snose index
                        snose[name]['version'] = remote[0]['version']
                        snose[name]['modifydate'] = os.path.getmtime(name) #As if set remote modify date, local file will immediately appear 'modified'
                        try:
                            with open('.snose', 'w') as f:
                                json.dump(snose, f, indent=2)
                        except IOError as e:
                            print("Failed to update index")
                            print("But local copy of the file %s has been updated with remote changes" % name)
                        #Some feedback
                elif (dry and (not (name in dryremotes))):
                    print("Updated local version of %s" % name)

def load_or_new():
    try:
        with open('.snose', 'r') as f:
            snose = json.load(f)
    except IOError as e:
        #Doesn't exist so create new
        snose = {}
    return snose











main()







<
<
<
<
<
<
<
|













<
|
|
<
<
<
<
<
<
>







 







<
|
<
<
<
<
<
|







 







<
<
<
<
<
<
|







 







<
<
<
<
<
|
<







 







<
<
<
<
<
|
<












>
>
>
>
>
>
>
>
>
>

71
72
73
74
75
76
77







78
79
80
81
82
83
84
85
86
87
88
89
90
91

92
93






94
95
96
97
98
99
100
101
...
127
128
129
130
131
132
133

134





135
136
137
138
139
140
141
142
...
195
196
197
198
199
200
201






202
203
204
205
206
207
208
209
...
258
259
260
261
262
263
264





265

266
267
268
269
270
271
272
...
287
288
289
290
291
292
293





294

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
            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
    try:
        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
    try:
................................................................................
                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
    #2) Get previous version of remote
................................................................................
                        print("Rolled back remote version")
                    except IOError as e:
                        print("Failed to roll back remote version")
                    else:
                        #Get returned metadata
                        snose[filename]['version'] = returned[0]['version']
                        snose[filename]['modifydate'] = sysmodifydate






                        write_index(snose, "Try running a sync to get index corrected.")


def snot(snclient):
    #List simplenote notes tagged with "snose"
    notelist = snclient.get_note_list()
    #That gets list of keys. Then need to iterate through and get first line of text.
    #This is going to be slow.
................................................................................
                                print("Merged local content for %s" % name)
                                #Override the returned value? As otherwise next sync will immediately update the remote version for no reason.
                                snose[name]['modifydate'] = os.path.getmtime(name) 
                            except IOError as e:
                                print("Failed to merge content locally for %s" % name)
                                print("Therefore skipping updating the index for this note")#I think this is a good idea?
                        #Update the index file





                        write_index(snose, "But remote and local copy of the file itself have been updated.")

            elif dry:
                print("Updated remote version of %s" % name)
                #For dry run, collect list of "updated remotes" to ignore in local updates
                dryremotes.append(name)
        #Fetch details from Simplenote
        try:
            remote = snclient.get_note(local['key'])
................................................................................
                    except IOError as e:
                        print("Failed to update local note %s with remote content" % name)
                        print("Will not updatet the .snose index file for this file")
                    else:
                        #Also update .snose index
                        snose[name]['version'] = remote[0]['version']
                        snose[name]['modifydate'] = os.path.getmtime(name) #As if set remote modify date, local file will immediately appear 'modified'





                        write_index(snose, "But local copy of the file %s has been updated with remote changes" % name)

                elif (dry and (not (name in dryremotes))):
                    print("Updated local version of %s" % name)

def load_or_new():
    try:
        with open('.snose', 'r') as f:
            snose = json.load(f)
    except IOError as e:
        #Doesn't exist so create new
        snose = {}
    return snose


def write_index(snose, msg=None):
    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()