|
| 1 | +import os |
| 2 | +import importlib |
| 3 | +from sqlalchemy import create_engine |
| 4 | + |
| 5 | +book_list = os.listdir("books") |
| 6 | +book_list = [book for book in book_list if ".git" not in book] |
| 7 | +eng = create_engine(os.environ["DEV_DBURL"]) |
| 8 | + |
| 9 | +for book in sorted(book_list): |
| 10 | + try: |
| 11 | + # WARNING: This imports from ``applications.<runestone application name>.books.<book name>``. Since ``runestone/books/<book_name>`` lacks an ``__init__.py``, it will be treated as a `namespace package <https://www.python.org/dev/peps/pep-0420/>`_. Therefore, odd things will happen if there are other modules named ``applications.<runestone application name>.books.<book name>`` in the Python path. |
| 12 | + config = importlib.import_module("books.{}.conf".format(book)) |
| 13 | + except Exception as e: |
| 14 | + print(f"Error adding book {book} to library list: {e}") |
| 15 | + continue |
| 16 | + book_info = {} |
| 17 | + book_info.update(course_description="") |
| 18 | + book_info.update(key_words="") |
| 19 | + if hasattr(config, "navbar_title"): |
| 20 | + book_info["title"] = config.navbar_title |
| 21 | + elif hasattr(config, "html_title"): |
| 22 | + book_info["title"] = config.html_title |
| 23 | + elif hasattr(config, "html_short_title"): |
| 24 | + book_info["title"] = config.html_short_title |
| 25 | + else: |
| 26 | + book_info["title"] = "Runestone Book" |
| 27 | + # update course description if found in the book's conf.py |
| 28 | + if hasattr(config, "course_description"): |
| 29 | + book_info.update(course_description=config.course_description) |
| 30 | + # update course key_words if found in book's conf.py |
| 31 | + if hasattr(config, "key_words"): |
| 32 | + book_info.update(key_words=config.key_words) |
| 33 | + if hasattr(config, "publisher") and config.publisher == "PTX": |
| 34 | + bks = "ns" |
| 35 | + book_info["source"] = "PTX" |
| 36 | + else: |
| 37 | + bks = "ns" |
| 38 | + book_info["source"] = "Runestone" |
| 39 | + |
| 40 | + if hasattr(config, "shelf_section"): |
| 41 | + book_info["section"] = config.shelf_section |
| 42 | + else: |
| 43 | + book_info["section"] = "Computer Science" |
| 44 | + |
| 45 | + book_info["url"] = "/{}/books/published/{}/index.html".format(bks, book) |
| 46 | + book_info["regname"] = book |
| 47 | + |
| 48 | + if hasattr(config, "is_private") and config.is_private == True: |
| 49 | + pass |
| 50 | + else: |
| 51 | + title = book_info["title"] |
| 52 | + subtitle = "" |
| 53 | + description = book_info["course_description"] |
| 54 | + shelf = book_info["section"] |
| 55 | + course = book |
| 56 | + res = eng.execute(f"select * from library where basecourse = '{course}'") |
| 57 | + if res.rowcount == 0: |
| 58 | + eng.execute( |
| 59 | + f"""insert into library |
| 60 | + (title, subtitle, description, shelf_section, basecourse ) |
| 61 | + values('{title}', '{subtitle}', '{description}', '{shelf}', '{course}') """ |
| 62 | + ) |
| 63 | + else: |
| 64 | + eng.execute( |
| 65 | + f"""update library set |
| 66 | + title = '{title}', |
| 67 | + subtitle = '{subtitle}', |
| 68 | + description = '{description}', |
| 69 | + shelf_section = '{shelf}' |
| 70 | + where basecourse = '{course}' |
| 71 | + """ |
| 72 | + ) |
0 commit comments