-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Attachments, Tags to and from xml, Bracket-access for tags, Recursive SimpleTags #28
base: main
Are you sure you want to change the base?
Conversation
All my changes at the same place.
It seems that the getsizeof() I was doing in some tests about the attachments was not returning the same value as on my machine. It depends on the filesystem I guess. So, instead, I take the integer division by 1000 and compare this approximate value. |
enzyme/mkv.py
Outdated
@@ -330,7 +338,7 @@ def fromXML(cls, xmlElement): | |||
:type xmlElement: :class:`xml.etree.ElementTree.Element` | |||
|
|||
""" | |||
targets = Targets.fromXML(xmlElement.find('Targets')) if xmlElement.find('Targets') else Targets() | |||
targets = Targets() if xmlElement.find('Targets') is None else Targets.fromXML(xmlElement.find('Targets')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference with the previous code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the order of execution of the statement, having xmlElement.find('Targets') == None
would raise an error when Targets.fromXML(...)
is evaluated. No what we want. Second version tests before evaluating.
|
||
def to_dict(self): | ||
info_dict = self.__dict__.copy() | ||
info_dict['duration'] = self.duration.seconds + self.duration.microseconds/1e6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is self.duration.microseconds/1e6
pep8-valid? I think it needs extra spaces around the /
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked on python.org and it says :
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies).
I guess that applies here : the /
has higher priority than +
.
def to_dict(self): | ||
info_dict = self.__dict__.copy() | ||
info_dict['duration'] = self.duration.seconds + self.duration.microseconds/1e6 | ||
info_dict['date_utc'] = ('{0:%Y}-{0:%m}-{0:%d} {0:%H}:{0:%M}:{0:%S}:{0:%f}'.format(self.date_utc))[:-3], # The official Mastroka date standard |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not store the date as datetime object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, the idea behind the to_dict
function was to have a convenient way to serialize the object, like with JSON. And json.dump()
does not accept datetime object. Also, this output is the standard for Matroska.
enzyme/mkv.py
Outdated
@@ -547,6 +555,11 @@ def fromelement(cls, element): | |||
return cls(start, hidden, enabled, end, uid, string, language) | |||
return cls(start, hidden, enabled, end, uid) | |||
|
|||
def to_dict(self): | |||
chap_dict = self.__dict__.copy() | |||
chap_dict['start'] = self.start.seconds + self.start.microseconds/1e6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same regarding pep8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Operation priorities again.
I'm not sure about the bracket access for tags, I find it confusing. mkv[50] isn't something very explicit. However something like mkv.tags[50] looks better. |
For the div functionality, I was thinking of the |
There. Removed the div's and simply renamed the |
Can you remove the last commit and submit a new PR for that change? Also you need to update the unittests accordingly as travis fails. |
This reverts commit 30b3a60. This will be in another pull request.
Woups. Forgot about the unittests this time... Sorry. But another pull request it will be. |
Restarting from my old pull requests, here are all my changes in one request.
MKV has now as "attachments" list of Attachment object with all the Mastroka elements: Name, Description, Mime Type and Data. Data is a BytesIO object with the raw binary data of the attachment.
A keyword argument in MKV.init "load_attachments" can turn off the rawData reading, saving some memory space.
Like in the Mastroka docs, SimpleTags can contain children SimpleTags.
Convenience functions using xml.etree.ElementTree Elements
mkv.tags_from_xml(xmlElement)
: Load Tag data from a xml Element and append it to the mkv.tags.xmlElement = mkv.tags_to_xml()
: Write a xml element from the Tags in a mkv.This is useful for saving changed tag data with mkvpropedit. Also, it could be used (as I do) to create a MKV-like object from another type of file (not mkv).
Convenience getitem. MKV object can be bracket-accessed with a targettype (or targettypevalue) and this returns a list of all tags corresponding to this targettype. The sames apply for tags which return a list of simpletags corresponding to the requested name. They both return lists because you could have multiple SimpleTags with the same Name.
Ex.
Return the first tag with a targettypevalue of 50:
mkv[50][0]
Return this tag's first simpletag with name 'TITLE':
mkv[50][0]['TITLE'][0]
To easily add SimpleTags to a Tag, you can now use the division operator.
Ex. Adding a SimpleTag ACTOR with string "John Smith" and sub-simpletag CHARACTER with string "Foo Bar" to the first tag with targettypevalue of 50.
mkv[50][0] / SimpleTag('ACTOR', string='John Smith') / SimpleTag('CHARACTER', string='Foo Bar')
This formalism is, I think, useful when using enzyme interactively.