Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add ability to determine filename from first few lines of a file
Wanted to do this for awhile, but was too lazy until now. Most of my snose files have the filepath as the first or second line in the |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | master |
Files: | files | file ages | folders |
SHA3-256: |
ba8e456fb643dcbc3bad43a63c2cc464 |
User & Date: | simon 2018-10-11 10:25:22 |
Context
2018-10-13
| ||
10:40 |
More Python 3 and 2 compatibility - UTF-8 write files
I suspect I didn't get everything quite as good as I could in simplenote.py I | |
2018-10-11
| ||
10:25 |
Add ability to determine filename from first few lines of a file
Wanted to do this for awhile, but was too lazy until now. Most of my snose files have the filepath as the first or second line in the | |
07:54 | Merge in Python3 stuff check-in: 5d0589b3be user: simon tags: master, trunk | |
Changes
Changes to snose.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/python #snose - Simplenote Object Synchronisation (Explicit) import sys import json import simplenote #Need to install this import os.path, time from optparse import OptionParser import netrc def main(): parser = OptionParser() parser.add_option("--snort", action="store", type="string", help="Import a new file SNORT to Simplenote") | > | | > > > > | > > > | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #!/usr/bin/python #snose - Simplenote Object Synchronisation (Explicit) import sys import json import simplenote #Need to install this import os.path, time from optparse import OptionParser import netrc import re def main(): parser = OptionParser() parser.add_option("--snort", action="store", type="string", help="Import a new file SNORT to Simplenote") parser.add_option("--sniff", action="store", nargs=1, type="string", help="Link a file with an already existing note in Simplenote", metavar="<key> <filename>") parser.add_option("--sneeze", action="store", nargs=1, type="string", help="Export an existing file from Simplenote", metavar="<key> <filename>") parser.add_option("--blow", action="store", type="string", help="Roll back note of key BLOW to previous version") parser.add_option("--sync", help="Sync files in index", default=False, action='store_true') parser.add_option("--hanky", help="Use with --sync to perform a dry run", default=False, action='store_true') parser.add_option("--snot", help="List notes available for export (tagged snose)", default=False, action='store_true') parser.add_option("--file", help="Filename for snort, sniff, sneeze", default=None, action='store') parser.add_option("--username", action="store", type="string", help="Your Simplenote email address") parser.add_option("--password", action="store", type="string", help="Your Simplenote password") (options, args) = parser.parse_args() if not options.username or not options.password: #Check to see if stored somewhere try: options.username = netrc.netrc().authenticators("simple-note.appspot.com")[0] options.password = netrc.netrc().authenticators("simple-note.appspot.com")[2] except IOError as e: print('Username and password must be supplied or exist .netrc file under domain of simple-note.appspot.com') exit() snclient = simplenote.Simplenote(options.username, options.password) if options.snort: if options.file is None: print('--file required') exit snort(snclient, options.file) elif options.sniff: if options.filename is None: print('--file required') exit sniff(snclient, options.sniff, options.file) elif options.sneeze: sneeze(snclient, options.sneeze, options.file) elif options.blow: blow(snclient, options.blow) elif options.snot: snot(snclient) elif options.sync and options.hanky: sync(snclient, True) elif options.sync: |
︙ | ︙ | |||
122 123 124 125 126 127 128 129 130 131 132 133 134 135 | try: remote = snclient.get_note(key) except IOerror as e: print("Failed to find that note on Simplenote") print(e) else: #Write file try: with open(filename, 'w') as f: f.write(remote[0]['content']) except IOError as e: print("Failed to create local copy of that note") print(e) else: | > > > > > > > > > > > > > > | 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | try: remote = snclient.get_note(key) except IOerror as e: print("Failed to find that note on Simplenote") print(e) else: #Write file #If no filename supplied try to figure out from first three lines of file itself if filename is None: firstlines = remote[0]['content'].splitlines()[:3] for line in firstlines: try: filename = re.search(r'file:(\S+)\s', line).group(1) except IndexError: pass except AttributeError: pass if filename is None: print("Failed to identify filename within note, please provide on command line") exit filename = os.path.expanduser(filename) try: with open(filename, 'w') as f: f.write(remote[0]['content']) except IOError as e: print("Failed to create local copy of that note") print(e) else: |
︙ | ︙ |