Skip to content

Commit

Permalink
Merge pull request #165 from remia/fix/console_progress_extensibility
Browse files Browse the repository at this point in the history
Improve console progress report user extensibility
  • Loading branch information
Rémi Achard authored Sep 13, 2020
2 parents b372122 + 3ac06ba commit 5ceeefb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ As a python library :
# Check DCP VF against OV
status, report = dcp.check(ov_path="/path/to/dcp_ov")

# Check DCP with console progression report
# Check DCP with default console progression report
from clairmeta.utils.file import ConsoleProgress
status, report = dcp.check(hash_callback=ConsoleProgress)
status, report = dcp.check(hash_callback=ConsoleProgress())
# Alternatives
# - function matching utils.file.ConsoleProgress.__call__ signature
# - derived class from utils.file.ConsoleProgress

Profiles
~~~~~~~~
Expand Down Expand Up @@ -184,7 +187,7 @@ Contributing
pipenv shell
# Code...
# Get tests resources
git clone https://github.com/Ymagis/ClairMeta_Data tests/resources
git clone https://github.com/Ymagis/ClairMeta_Data tests/resources
# Run tests
nosetests --nocapture --with-doctest --doctest-options=+ELLIPSIS --with-coverage --cover-package=clairmeta
# Leave virtual environment
Expand Down
2 changes: 1 addition & 1 deletion clairmeta/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def cli_check(args):
if args.log:
check_profile['log_level'] = args.log
if args.progress:
callback = ConsoleProgress
callback = ConsoleProgress()

status, _ = DCP(args.path, kdm=args.kdm, pkey=args.key).check(
profile=check_profile, ov_path=args.ov, hash_callback=callback)
Expand Down
12 changes: 10 additions & 2 deletions clairmeta/dcp_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from clairmeta.profile import get_default_profile
from clairmeta.dcp_utils import list_cpl_assets, cpl_probe_asset
from clairmeta.dcp_check_base import CheckerBase, CheckException
from clairmeta.utils.file import ConsoleProgress
from clairmeta.utils.sys import all_keys_in_dict


Expand All @@ -31,9 +32,16 @@ def __init__(
super(DCPChecker, self).__init__(dcp, profile)
self.ov_path = ov_path
self.ov_dcp = None

self.hash_callback = hash_callback
if self.hash_callback and inspect.isclass(self.hash_callback):
self.hash_callback = hash_callback(self.dcp.size)
if not self.hash_callback:
pass
elif isinstance(self.hash_callback, ConsoleProgress):
self.hash_callback._total_size = self.dcp.size
elif inspect.isclass(self.hash_callback):
raise CheckException(
"Invalid callback, please provide a function"
" or instance of ConsoleProgress (or derivate).")

self.check_modules = {}
self.load_modules()
Expand Down
16 changes: 9 additions & 7 deletions clairmeta/dcp_check_subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,21 @@ def get_font_path(self, xml_dict, folder):

return path, uri

def extract_subtitle_text(self, node, out_text):
def extract_subtitle_text(self, node):
text = []

if isinstance(node, list):
for elem in node:
text += self.extract_subtitle_text(elem, text)
text += self.extract_subtitle_text(elem)
elif isinstance(node, dict):
text += self.extract_subtitle_text(node.get('Font', ''), text)
text += self.extract_subtitle_text(node.get('Text', ''), text)
if 'Font' in node:
text = self.extract_subtitle_text(node['Font'])
if 'Text' in node:
text = self.extract_subtitle_text(node['Text'])
else:
text += [node]
text = [node]

return out_text + text
return text


class Checker(CheckerBase):
Expand Down Expand Up @@ -428,7 +430,7 @@ def check_subtitle_cpl_font_glyph(self, playlist, asset, folder):
# Subtitle Text and Font hierarchy. Note that here we just
# recursively iterate to extract all relevant childs whitout
# checking if the specific hierarchy is valid or not.
all_text = self.st_util.extract_subtitle_text(subtitles[0], [])
all_text = self.st_util.extract_subtitle_text(subtitles[0])
unique_chars = set()
for text in all_text:
for char in text:
Expand Down
14 changes: 5 additions & 9 deletions clairmeta/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,11 @@ def temporary_dir():

class ConsoleProgress(object):

def __init__(self, total_size):
""" ConsoleProgress constructor.
def __init__(self):
""" ConsoleProgress constructor. """
self._total_size = None

Args:
total_size (int): Total size in Bytes to process
"""
self.total_processed = 0
self.total_size = total_size
self.total_elapsed = 0

def __call__(self, file_path, file_processed, file_size, file_elapsed):
Expand All @@ -121,11 +117,11 @@ def __call__(self, file_path, file_processed, file_size, file_elapsed):
file_progress_size = int(file_progress * col_width)
file_bar_size = col_width - file_progress_size

total_progress = min(1, (processed / self.total_size))
total_progress = min(1, (processed / self._total_size))
total_progress_size = int(total_progress * col_width)
total_bar_size = col_width - total_progress_size

eta_sec = (self.total_size - processed) / (processed / elapsed)
eta_sec = (self._total_size - processed) / (processed / elapsed)
eta_str = time.strftime("%H:%M:%S", time.gmtime(eta_sec))

sys.stdout.write("ETA {} [{}] {:.2f}% - File [{}] {:.2f}% - {}\r".format(
Expand Down

0 comments on commit 5ceeefb

Please sign in to comment.