Skip to content

Commit 44b1bab

Browse files
committed
Get the lines files both from API sources and the device itself
They are named differently, so we'll just try both names. In the process, this cleans up the handling of the content_dict, keeping only the list of page IDs, and passing that directly into the individual pages. Also, clearer names around templates.
1 parent dfa5319 commit 44b1bab

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

rcu/src/savepdf.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,14 @@ def save_pdf(source, vector=True, prog_cb=lambda x: None):
121121
# ...
122122

123123
# Generate page information
124-
contentdict = {}
125-
if source.exists('{ID}.content'):
126-
with source.open('{ID}.content', 'r') as f:
127-
contentdict = json.load(f)
128124
# If a PDF file was uploaded, but never opened, there may not be
129125
# a .content file. So, just load a barebones one with a 'pages'
130126
# key of zero length, so it doesn't break the rest of the
131-
# process. This is here, instead of in __init__, because it is
132-
# more explainable/straightforward here.
133-
if not 'pages' in contentdict:
134-
contentdict['pages'] = []
127+
# process.
128+
pages = []
129+
if source.exists('{ID}.content'):
130+
with source.open('{ID}.content', 'r') as f:
131+
pages = json.load(f).get('pages', [])
135132

136133
# Render each page as a pdf
137134
tmpfh = tempfile.TemporaryFile()
@@ -143,13 +140,13 @@ def save_pdf(source, vector=True, prog_cb=lambda x: None):
143140
# iteration so they get released by garbage collector.
144141
changed_pages = []
145142
annotations = []
146-
for i in range(0, len(contentdict['pages'])):
147-
page = DocumentPage(source, contentdict, i)
143+
for i in range(0, len(pages)):
144+
page = DocumentPage(source, pages[i], i)
148145
if source.exists(page.rmpath):
149146
changed_pages.append(i)
150147
page.render_to_painter(pdf_canvas, vector)
151148
annotations.append(page.get_grouped_annotations())
152-
prog_cb((i + 1) / len(contentdict['pages']) * 50)
149+
prog_cb((i + 1) / len(pages) * 50)
153150
pdf_canvas.save()
154151
tmpfh.seek(0)
155152

@@ -603,18 +600,17 @@ def merge_pages(basepage, rmpage, changed_page):
603600

604601
class DocumentPage:
605602
# A single page in a document
606-
def __init__(self,
607-
source,
608-
doc_contentdict, # Added
609-
pagenum):
603+
def __init__(self, source, pid, pagenum):
610604
# Page 0 is the first page!
611605
self.source = source
612606
self.num = pagenum
613607

614-
# get page id
615-
pid = str(pagenum) #For download; from device: doc_contentdict['pages'][pagenum]
616-
608+
# On disk, these files are named by a UUID
617609
self.rmpath = f'{{ID}}/{pid}.rm'
610+
if not source.exists(self.rmpath):
611+
# From the API, these files are just numbered
612+
pid = str(pagenum)
613+
self.rmpath = f'{{ID}}/{pid}.rm'
618614

619615
# Try to load page metadata
620616
self.metadict = None
@@ -625,23 +621,21 @@ def __init__(self,
625621

626622
# Try to load template
627623
self.template = None
628-
tmpnamearray = []
624+
template_names = []
629625
pagedatapath = '{ID}.pagedata'
630626
if source.exists(pagedatapath):
631627
with source.open(pagedatapath, 'r') as f:
632-
lines = f.read()
633-
for line in lines.splitlines():
634-
tmpnamearray.append(line)
628+
template_names = f.read().splitlines()
635629

636-
if tmpnamearray:
630+
if template_names:
637631
# I have encountered an issue with some PDF files, where the
638632
# rM won't save the page template for later pages. In this
639633
# case, just take the last-available page template, which
640634
# is usually 'Blank'.
641-
tmpname = tmpnamearray[max(self.num, len(tmpnamearray) - 1)]
642-
tmparchivepath = TEMPLATE_PATH / f'{tmpname}.svg'
643-
if tmpname != 'Blank' and tmparchivepath.exists():
644-
self.template = str(tmparchivepath)
635+
template_name = template_names[max(self.num, len(template_names) - 1)]
636+
template_path = TEMPLATE_PATH / f'{template_name}.svg'
637+
if template_name != 'Blank' and template_path.exists():
638+
self.template = str(template_path)
645639

646640
# Load layers
647641
self.layers = []
@@ -877,3 +871,11 @@ def write_to_output(stream, fn):
877871
save_pdf(ZipSource(zf), vector, print),
878872
f'testing/output-zip-{format_}.pdf'
879873
)
874+
# On disk
875+
write_to_output(
876+
save_pdf(
877+
FSSource('testing/ondisk', 'cb736ad2-b869-4253-979a-dbc5c01a9000'),
878+
vector, print
879+
),
880+
f'testing/output-disk-{format_}.pdf'
881+
)

0 commit comments

Comments
 (0)