Skip to content

Commit

Permalink
Python3 compatibility tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Pradan committed Jul 27, 2016
1 parent 81e08b8 commit f73a033
Show file tree
Hide file tree
Showing 28 changed files with 1,750 additions and 1,769 deletions.
16 changes: 8 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
master_doc = 'index'

# General information about the project.
project = u'pycaption'
copyright = u'2012, PBS.org (available under the Apache License, Version 2.0)'
project = 'pycaption'
copyright = '2012, PBS.org (available under the Apache License, Version 2.0)'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down Expand Up @@ -199,8 +199,8 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'pycaption.tex', u'pycaption Documentation',
u'PBS', 'manual'),
('index', 'pycaption.tex', 'pycaption Documentation',
'PBS', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -229,8 +229,8 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'pycaption', u'pycaption Documentation',
[u'PBS'], 1)
('index', 'pycaption', 'pycaption Documentation',
['PBS'], 1)
]

# If true, show URL addresses after external links.
Expand All @@ -243,8 +243,8 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'pycaption', u'pycaption Documentation',
u'PBS', 'pycaption', 'One line description of project.',
('index', 'pycaption', 'pycaption Documentation',
'PBS', 'pycaption', 'One line description of project.',
'Miscellaneous'),
]

Expand Down
58 changes: 30 additions & 28 deletions pycaption/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from .exceptions import CaptionReadError, CaptionReadTimingError

DEFAULT_LANGUAGE_CODE = u'en-US'
DEFAULT_LANGUAGE_CODE = 'en-US'


def force_byte_string(content):
try:
return content.encode(u'UTF-8')
return content.encode('UTF-8')
except UnicodeEncodeError:
raise RuntimeError(u'Invalid content encoding')
raise RuntimeError('Invalid content encoding')
except UnicodeDecodeError:
return content

Expand Down Expand Up @@ -64,10 +64,11 @@ def __init__(self, relativize=True, video_width=None, video_height=None,
converted were made. This is necessary for relativization.
:param video_height: The height of the video for which the captions
being converted were made. This is necessary for relativization.
:param fit_to_screen: If extent is not set or if origin + extent > 100%,
(re)calculate it based on origin. It is a pycaption fix for caption
files that are technically valid but contains inconsistent settings
that may cause long captions to be cut out of the screen.
:param fit_to_screen: If extent is not set or
if origin + extent > 100%, (re)calculate it based on origin.
It is a pycaption fix for caption files that are technically valid
but contains inconsistent settings that may cause long captions to
be cut out of the screen.
"""
self.relativize = relativize
self.video_width = video_width
Expand Down Expand Up @@ -130,11 +131,11 @@ def __repr__(self):
if t == CaptionNode.TEXT:
return repr(self.content)
elif t == CaptionNode.BREAK:
return repr(u'BREAK')
return repr('BREAK')
elif t == CaptionNode.STYLE:
return repr(u'STYLE: %s %s' % (self.start, self.content))
return repr('STYLE: %s %s' % (self.start, self.content))
else:
raise RuntimeError(u'Unknown node type: ' + unicode(t))
raise RuntimeError('Unknown node type: ' + str(t))

@staticmethod
def create_text(text, layout_info=None):
Expand Down Expand Up @@ -175,13 +176,13 @@ def __init__(self, start, end, nodes, style={}, layout_info=None):
:type layout_info: Layout
"""
if not isinstance(start, Number):
raise CaptionReadTimingError(u"Captions must be initialized with a"
u" valid start time")
raise CaptionReadTimingError("Captions must be initialized with a"
" valid start time")
if not isinstance(end, Number):
raise CaptionReadTimingError(u"Captions must be initialized with a"
u" valid end time")
raise CaptionReadTimingError("Captions must be initialized with a"
" valid end time")
if not nodes:
raise CaptionReadError(u"Node list cannot be empty")
raise CaptionReadError("Node list cannot be empty")
self.start = start
self.end = end
self.nodes = nodes
Expand All @@ -208,7 +209,7 @@ def format_end(self, msec_separator=None):

def __repr__(self):
return repr(
u'{start} --> {end}\n{text}'.format(
'{start} --> {end}\n{text}'.format(
start=self.format_start(),
end=self.format_end(),
text=self.get_text()
Expand All @@ -223,29 +224,29 @@ def get_text_for_node(node):
if node.type_ == CaptionNode.TEXT:
return node.content
if node.type_ == CaptionNode.BREAK:
return u'\n'
return u''
return '\n'
return ''
text_nodes = [get_text_for_node(node) for node in self.nodes]
return u''.join(text_nodes).strip()
return ''.join(text_nodes).strip()

def _format_timestamp(self, value, msec_separator=None):
datetime_value = timedelta(milliseconds=(int(value / 1000)))

str_value = text_type(datetime_value)[:11]
if not datetime_value.microseconds:
str_value += u'.000'
str_value += '.000'

if msec_separator is not None:
str_value = str_value.replace(u".", msec_separator)
str_value = str_value.replace(".", msec_separator)

return u'0' + str_value
return '0' + str_value


class CaptionList(list):
""" A list of captions with a layout object attached to it """
def __init__(self, iterable=None, layout_info=None):
"""
:param iterator: An iterator used to populate the caption list
:param iterable: An iterator used to populate the caption list
:param Layout layout_info: A Layout object with the positioning info
"""
self.layout_info = layout_info
Expand All @@ -258,10 +259,9 @@ def __getslice__(self, i, j):

def __getitem__(self, y):
item = list.__getitem__(self, y)
if isinstance(item, Caption) :
if isinstance(item, Caption):
return item
return CaptionList(item
, layout_info=self.layout_info)
return CaptionList(item, layout_info=self.layout_info)

def __add__(self, other):
add_is_safe = (
Expand Down Expand Up @@ -305,7 +305,7 @@ def set_captions(self, lang, captions):
self._captions[lang] = captions

def get_languages(self):
return self._captions.keys()
return list(self._captions.keys())

def get_captions(self, lang):
return self._captions.get(lang, [])
Expand Down Expand Up @@ -334,7 +334,7 @@ def set_styles(self, styles):

def is_empty(self):
return all(
[len(captions) == 0 for captions in self._captions.values()]
[len(captions) == 0 for captions in list(self._captions.values())]
)

def set_layout_info(self, lang, layout_info):
Expand Down Expand Up @@ -364,6 +364,7 @@ def adjust_caption_timing(self, offset=0, rate_skew=1.0):
out_captions.append(caption)
self.set_captions(lang, out_captions)


# Functions
def merge_concurrent_captions(caption_set):
"""Merge captions that have the same start and end times"""
Expand Down Expand Up @@ -391,6 +392,7 @@ def merge_concurrent_captions(caption_set):
caption_set.set_captions(lang, merged_captions)
return caption_set


def merge(captions):
"""
Merge list of captions into one caption. The start/end times from the first
Expand Down
2 changes: 0 additions & 2 deletions pycaption/dfxp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from __future__ import absolute_import

from .base import *
from .extras import SinglePositioningDFXPWriter, LegacyDFXPWriter
Loading

0 comments on commit f73a033

Please sign in to comment.