def main(): reading_id = int(sys.argv[1]) session = Session() bible_query = BibleQuery() reading = session.query(Reading).filter(Reading.id == reading_id).one() text = reading.text if reading.text is not None else "" editor = Editor() # Fix wrong quotation marks text = re.sub(ur'"([a-zA-ZàòùèéìÒÀÙÈÉÌ0-9])', ur'“\1', text, count=0) text = re.sub(ur'([a-zA-ZàòùèéìÒÀÙÈÉÌ0-9\.?!])"', ur'\1”', text, count=0) # From http://stackoverflow.com/questions/15120346/emacs-setting-comment-character-by-file-extension PrependStream(editor.tempfile, '# ').write(u'-*- coding: utf-8; comment-start: "#"; -*-\n') PrependStream(editor.tempfile, '# ').write(u'Quote: %s\n' % (reading.quote)) editor.tempfile.write(u'\n') editor.tempfile.write(text) editor.tempfile.write(u'\n') PrependStream(editor.tempfile, '# ').write(u'Useful characters: “”–\n\n') try: converted_quote = convert_quote_psalm_numbering(reading.quote, False) bible_text = bible_query.get_text(decode_quote(converted_quote, allow_only_chap=True)) except: PrependStream(editor.tempfile, '# ').write(u'Quote: %s\nCould not retrieve bible text\n' % (reading.quote)) print decode_quote(reading.quote, allow_only_chap=True) raise else: bible_text = "\n".join(map(lambda x: x.strip(), bible_text.split('\n'))) PrependStream(editor.tempfile, '# ').write(u'Quote: %s\nConverted quote: %s\nBible text:\n\n%s' % (reading.quote, converted_quote, bible_text)) editor.edit() new_text = u''.join(filter(lambda x: not x.startswith(u'#'), editor.edited_content)).strip() + u'\n' if editor.confirmation_request(new_text != reading.text): reading.text = new_text session.commit() else: session.rollback()
def edit_month(year, month, single_day=None): session = Session() bible_query = BibleQuery() lit_years = {} editor = Editor() # From http://stackoverflow.com/questions/15120346/emacs-setting-comment-character-by-file-extension PrependStream(editor.tempfile, '# ').write(u'-*- coding: utf-8; comment-start: "#"; -*-\n') editor.tempfile.write(u'\n') def push_day(day): date = datetime.date(year, month, day) lit_date = get_lit_date(date, lit_years, session) events = map(lambda x: x[1], lit_date.competitors) print_lit_date(lit_date, PrependStream(editor.tempfile, u'# '), with_id=True) editor.tempfile.write(u'\n') editor.tempfile.write(json.dumps(map(lambda x: x.as_dict(), events), ensure_ascii=False, indent=2, sort_keys=True) + u'\n') editor.tempfile.write(u'---===---\n') editor.tempfile.write(u'\n') if single_day is not None: push_day(single_day) else: for day in real_itermonthdays(year, month): push_day(day) editor.edit() while True: lines = filter(lambda x: not x.startswith(u'#'), editor.edited_content) buf = u'' try: for line in lines: if line.strip() == u'---===---': data = json.loads(buf) for piece in data: from_dict(piece, session) buf = u'' else: buf += line session.flush() except: traceback.print_exc() sys.stdout.write("Error while parsing new content. Re-edit? [Y/n] ") answer = sys.stdin.readline().strip() if answer != '': answer = answer[0] if answer == 'n' or answer == 'N': sys.stdout.write("Aborting\n") sys.exit(0) else: sys.stdout.write("Re-editing...\n") session.rollback() edited_content = editor.edited_content editor = Editor() editor.tempfile.write("".join(edited_content)) editor.edit() else: break if editor.confirmation_request(session_has_pending_commit(session)): #reading.text = new_text session.commit() else: session.rollback()