-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from chnm/stanza-import
Django management command for importing the stanzas
- Loading branch information
Showing
6 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,66 @@ | ||
import logging | ||
import re | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from manuscript.models import Stanza | ||
from manuscript.models import SingleManuscript, Stanza | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Insert stanzas from a text file" | ||
help = "Import poem data from a plain text file" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"file_path", type=str, help="The path to the file that contains the stanzas" | ||
"--filepath", type=str, help="filepath of the plain text file to load" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
file_path = options.get("filepath") | ||
|
||
with open(file_path, "r", encoding="utf-8-sig") as file: | ||
content = file.read() | ||
|
||
self.import_poem(content) | ||
|
||
def import_poem(self, content): | ||
manuscript = SingleManuscript.objects.get(siglum="TEST") | ||
book_pattern = re.compile(r"^\s*LIBRO (\d+)", re.MULTILINE) | ||
stanza_pattern = re.compile( | ||
r"^(\d+)\.\s*(.*?)(?=\n\d+\.|\Z)", re.DOTALL | re.MULTILINE | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
file_path = kwargs["file_path"] | ||
with open(file_path, "r", encoding="utf-8") as file: | ||
line_counter = 0 | ||
for line in file: | ||
line = line.strip() | ||
if line.startswith("[RUBRIC]") or line.isdigit(): | ||
continue | ||
if line != "": | ||
line_counter += 1 | ||
line_number = f"01.02.{line_counter:02d}" | ||
stanza = Stanza(line=line, line_number=line_number) | ||
stanza.save() | ||
else: | ||
line_counter = 0 | ||
book_matches = book_pattern.split(content) | ||
if book_matches[0] == "": | ||
book_matches = book_matches[1:] | ||
|
||
for i in range(0, len(book_matches), 2): | ||
book_number = int( | ||
book_matches[i] | ||
) # Extract book number from "LIBRO X" string | ||
book_text = book_matches[ | ||
i + 1 | ||
].strip() # Get the corresponding book content | ||
|
||
stanza_matches = stanza_pattern.findall(book_text) | ||
|
||
for stanza_number, stanza_text in stanza_matches: | ||
stanza_number = int(stanza_number) | ||
stanza_text = stanza_text.strip() | ||
|
||
stanza_lines = stanza_text.split("\n") | ||
|
||
for line_number, line_text in enumerate(stanza_lines, start=1): | ||
line_code = ( | ||
f"{book_number:02d}.{stanza_number:02d}.{line_number:02d}" | ||
) | ||
# print(f"Line code: {line_code}, Line text: {line_text}") | ||
|
||
Stanza.objects.create( | ||
stanza_line_code_starts=line_code, | ||
stanza_text=line_text, | ||
related_manuscript=manuscript, | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS("Successfully imported the stanzas")) |
20 changes: 20 additions & 0 deletions
20
manuscript/migrations/0071_alter_folio_options_alter_stanza_options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 5.0.2 on 2024-05-30 16:43 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("manuscript", "0070_alter_location_line_code_alter_location_placename_id"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="folio", | ||
options={"ordering": ["folio_number"]}, | ||
), | ||
migrations.AlterModelOptions( | ||
name="stanza", | ||
options={"ordering": ["id"]}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters