def zotero_full_sync(self, db): zotero_conn, latest_version = self.zotero_connect() zotero_books = zotero_conn.everything(zotero_conn.items(itemType='book')) zotero_books = [i['data'] for i in zotero_books] print('we found {} books in your Zotero. You have {} in Nuthatch.'.format(len(zotero_books), len(self.user_books().all()))) for i in zotero_books: if self.dup_item(i['key']): dup = self.dup_item(i['key']) db.session.delete(dup) db.session.commit() new_book = Book( zotero_key = i['key'], title = i['title'], author = get_authors(i), publisher = i['publisher'], year= i['date'], isbn= i['ISBN'], language = get_language(i), user_id = self.username, timestamp = parse(i['dateModified']) ) db.session.add(new_book) db.session.commit() self.zotero_sync_version = latest_version db.session.commit()
def split_chapter(chapter, snippet=None, index=None): source_dir = book.get_language(chapter) def walk(dir): dir = os.path.abspath(dir) for path in os.listdir(dir): nfile = os.path.join(dir, path) if os.path.isdir(nfile): walk(nfile) elif os.path.splitext(path)[1] in [".java", ".h", ".c"]: split_file(chapter, nfile, snippet, index) walk(source_dir)
def split_file(chapter_name, path, snippet=None, index=None): chapter_number = book.chapter_number(chapter_name) source_dir = book.get_language(chapter_name) relative = os.path.relpath(path, source_dir) directory = os.path.dirname(relative) # Don't split the generated files. if relative == "com/craftinginterpreters/lox/Expr.java": return if relative == "com/craftinginterpreters/lox/Stmt.java": return package = book.get_short_name(chapter_name) if snippet: package = os.path.join("snippets", package, "{:02}-{}".format(index, snippet)) output_path = os.path.join("gen", package, relative) # If we're generating the split for an entire chapter, include all its # snippets. if not snippet: snippet = source_code.last_snippet_for_chapter(chapter_name).name output = source_code.split_chapter(relative, chapter_name, snippet) if output: # Don't overwrite it if it didn't change, so the makefile doesn't think it # was touched. if os.path.exists(output_path): with open(output_path, 'r') as file: previous = file.read() if output == previous: return # Write the changed output. ensure_dir(os.path.join("gen", package, directory)) with codecs.open(output_path, "w", encoding="utf-8") as out: print(output_path) out.write(output) else: # Remove it if it's supposed to be nonexistent. if os.path.exists(output_path): os.remove(output_path)