Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated next_partname generation #646

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ _scratch/
/spec/gen_spec/spec*.db
tags
/tests/debug.py
venv
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A git-ignore particular to your personal development environment belongs in your own gitignore file, not the project's. You find that somewhere like ~/.config/git/ignore or perhaps ~/.gitconfig.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for that, will check it and update.

16 changes: 9 additions & 7 deletions pptx/opc/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from __future__ import absolute_import

import os
from collections import defaultdict

from pptx.util import lazyproperty

from .constants import RELATIONSHIP_TYPE as RT
Expand All @@ -26,6 +29,7 @@ class OpcPackage(object):

def __init__(self):
super(OpcPackage, self).__init__()
self.partnames = defaultdict(int)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid adding instance variables to the interface of the class (in other words, make instance variables private. If you need the value outside the class, make a property for it). So this would be self._partnames.


def after_unmarshal(self):
"""
Expand Down Expand Up @@ -104,17 +108,15 @@ def main_document_part(self):

def next_partname(self, tmpl):
"""
Return a |PackURI| instance representing the next available partname
Return a |PackURI| instance representing the next partname
matching *tmpl*, which is a printf (%)-style template string
containing a single replacement item, a '%d' to be used to insert the
integer portion of the partname. Example: '/ppt/slides/slide%d.xml'
"""
partnames = [part.partname for part in self.iter_parts()]
for n in range(1, len(partnames) + 2):
candidate_partname = tmpl % n
if candidate_partname not in partnames:
return PackURI(candidate_partname)
raise Exception("ProgrammingError: ran out of candidate_partnames")
name = tmpl.split(os.sep)[2]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unreliable. Are you sure you can count on the name always being the third item? Better to figure it is the last item, like tmpl.split(os.sep)[-1]. Also, I'm not sure there's a compelling reason to split out the name at all. What is lost by keeping the path in there? The more important thing would be to split before the %d so you could match existing part-names with something like part_name.startswith("/ppt/slides/slide"), although be careful because /ppt/slides/slideMaster42.xml also starts with "/ppt/slides/slide". You'll probably need to use a regular expression or similar method to separate out the numeric suffix.

self.partnames[name] += 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing where ._partname gets initialized with the part-names already present in the package. I think a better strategy would be to make ._partnames an @lazyproperty (rather than an instance variable) and do the initialization in that lazyproperty (which only gets executed the first time it is called).

candidate_partname = tmpl % self.partnames[name]
return PackURI(candidate_partname)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the tests? You would have found most of these problems with good tests.

@classmethod
def open(cls, pkg_file):
Expand Down