Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add a load_or_new() method
Haven't really touched this in years so could do with starting to tidy this up |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | master |
Files: | files | file ages | folders |
SHA3-256: |
d217d13956d2fa213cd3c8fdea3f762b |
User & Date: | simon 2018-11-16 13:37:45 |
Context
2018-11-16
| ||
14:46 | Minor formatting changes check-in: bf33015cce user: simon tags: master, trunk | |
13:37 |
Add a load_or_new() method
Haven't really touched this in years so could do with starting to tidy this up | |
2018-10-28
| ||
12:20 | Those exits should have been sys.exit(). Whoops./ check-in: 3735e6b8ec user: simon tags: master, trunk | |
Changes
Changes to snose.py.
︙ | ︙ | |||
53 54 55 56 57 58 59 | elif options.sync: sync(snclient) else: print('No options supplied') def snort(snclient,filename): | < < < < < < | | 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | elif options.sync: sync(snclient) else: print('No options supplied') def snort(snclient,filename): snose = load_or_new() #Add new file to Simplenote #Need to get file contents try: with open(filename, 'r') as f: content = f.read() except IOError as e: print("Failed to read file %s" % filename) |
︙ | ︙ | |||
89 90 91 92 93 94 95 | 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 | < < < < < | | 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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) |
︙ | ︙ | |||
116 117 118 119 120 121 122 | 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 | < < < < < | | 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 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: remote = snclient.get_note(key) except IOerror as e: print("Failed to find that note on Simplenote") print(e) else: |
︙ | ︙ | |||
177 178 179 180 181 182 183 | #2) Get previous version of remote #3) Write it out locally #4) Use that to update remote #5) Update index file with results #1) Check exists in .snose index try: | < | | | 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | #2) Get previous version of remote #3) Write it out locally #4) Use that to update remote #5) Update index file with results #1) Check exists in .snose index try: snose = load_or_new() #Need to get filename of note, loop through, performance should be fine as .snose likely to be small if sys.version_info < (3, 0): sitems = snose.iteritems() else: sitems = snose.items() filename = [name for name, local in sitems if local['key'] == key][0] print("Attempting to rollback file %s" % filename) except IndexError as e: print("Note doesn't exist in local .snose index") else: #2) Get previous version of remote try: #fetch once to know version remote = snclient.get_note(key) rollback = snclient.get_note(key, remote[0]['version']-1) |
︙ | ︙ | |||
256 257 258 259 260 261 262 263 264 265 266 | remote = snclient.get_note(note['key']) print(remote[0]['key'] + " \t" + remote[0]['content'].splitlines()[0]) def sync(snclient, dry=False): #Need to read in mappings and sync those notes. dryremotes = [] try: with open('.snose', 'r') as f: snose = json.load(f) except IOError as e: | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < | | 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 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 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 | remote = snclient.get_note(note['key']) print(remote[0]['key'] + " \t" + remote[0]['content'].splitlines()[0]) def sync(snclient, dry=False): #Need to read in mappings and sync those notes. dryremotes = [] snose = load_or_new() #Need to iterate through list. if sys.version_info < (3, 0): sitems = snose.iteritems() else: sitems = snose.items() for name, local in sitems: #First of all check for local modifications sysmodifydate = float(os.path.getmtime(name)) if sysmodifydate > float(local['modifydate']): #ensure full timestamp if not dry: #Update remote try: 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 #Update local file if merged content if 'content' in returned[0]: try: if sys.version_info < (3, 0): with open(name, 'w') as f: f.write(returned[0]['content'].encode("utf-8")) else: with open(name, 'w', encoding="utf-8") as f: f.write(returned[0]['content']) 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 fetch remote copy of note %s" % name) print("Skipping synchronisation for this file") else: if remote[0]['version'] > local['version']: if not dry: try: if sys.version_info < (3, 0): with open(name, 'w') as f: f.write(remote[0]['content'].encode("utf-8")) else: with open(name, 'w', encoding="utf-8") as f: f.write(remote[0]['content']) print("Updated local version of %s" % name) 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() |