From 4cbc4fafe5dc862f0d8ad2f0369048a97992b9f3 Mon Sep 17 00:00:00 2001 From: Rato Date: Sat, 27 Feb 2016 17:39:21 +0100 Subject: [PATCH] cleanit 0.2 --- HISTORY.rst | 5 +++++ cleanit/__init__.py | 2 +- cleanit/cli.py | 5 ++++- cleanit/subtitle.py | 37 ------------------------------------- setup.py | 3 +-- 5 files changed, 11 insertions(+), 41 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 4f35931..03c9575 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,11 @@ Changelog --------- +0.2 +^^^^^ +**release date:** 2016-02-27 +* Removing chardet and python-magic dependencies. Either encoding is specified or it should be guessed by pysrt + 0.1 ^^^^^ **release date:** 2015-10-16 diff --git a/cleanit/__init__.py b/cleanit/__init__.py index 53dffa8..e377e77 100644 --- a/cleanit/__init__.py +++ b/cleanit/__init__.py @@ -1,3 +1,3 @@ __title__ = 'cleanit' __author__ = 'Rato' -__version__ = '0.1.1' +__version__ = '0.2' diff --git a/cleanit/cli.py b/cleanit/cli.py index 1582cf8..0d6cc49 100644 --- a/cleanit/cli.py +++ b/cleanit/cli.py @@ -38,7 +38,8 @@ def cleanit(config, force, test, debug, verbose, path): click.echo('Discarded %s' % discarded_paths, color='red') click.echo('Collected %d subtitles' % len(collected_subtitles), color='green') - for sub in collected_subtitles: + for i in reversed(range(len(collected_subtitles))): + sub = collected_subtitles[i] modified = api.clean_subtitle(sub, cfg.rules) if (modified or force) and not test: click.echo("Saving '%s'" % sub.path, color='green') @@ -46,6 +47,8 @@ def cleanit(config, force, test, debug, verbose, path): click.echo("Saved '%s'" % sub.path, color='green') elif verbose > 0: click.echo("No modification for '%s'" % sub.path, color='green') + # to free up memory + del collected_subtitles[i] def scan(path, collected, discarded): diff --git a/cleanit/subtitle.py b/cleanit/subtitle.py index 35d7066..09d5898 100644 --- a/cleanit/subtitle.py +++ b/cleanit/subtitle.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -import chardet import logging -import magic import pysrt @@ -19,46 +17,11 @@ def __repr__(self): return '<%s [%s]>' % (self.__class__.__name__, self.path) def load(self): - if not self.encoding: - self.encoding = self.guess_encoding() self.subtitle = pysrt.open(self.path, encoding=self.encoding) def save(self, path=None, encoding=None): self.subtitle.save(path=path if path else self.path, encoding=encoding if encoding else self.encoding) - def guess_encoding(self): - filename = self.path.decode('utf-8') - - # Unfortunately there are several magic modules... - encoding = None - - # Debian/Ubuntu python-magic - if hasattr(magic.Magic, 'file'): - m = magic.open(magic.MAGIC_MIME_ENCODING) - m.load() - encoding = m.file(filename) - logger.debug("Guessed encoding '%s' for '%s' using debian's python-magic" % (encoding, self.path)) - m.close() - - # https://pypi.python.org/pypi/python-magic - elif hasattr(magic.Magic, 'from_file'): - m = magic.Magic(mime_encoding=True) - encoding = m.from_file(filename) - logger.debug("Guessed encoding '%s' for '%s' using pypi's python-magic" % (encoding, self.path)) - - # https://pypi.python.org/pypi/filemagic - elif hasattr(magic.Magic, 'id_filename'): - with magic.Magic(flags=magic.MAGIC_MIME_ENCODING) as m: - encoding = m.id_filename(filename) - logger.debug("Guessed encoding '%s' for '%s' using pypi's filemagic" % (encoding, self.path)) - - if not encoding or encoding in ['unknown-8bit']: - with open(filename, 'rb') as f: - encoding = chardet.detect(f.read())['encoding'] - logger.debug("Guessed encoding '%s' for '%s' using chardet" % (encoding, self.path)) - - return encoding - def clean(self, rules, clean_indexes=True): self.load() diff --git a/setup.py b/setup.py index d044f0e..fb01261 100644 --- a/setup.py +++ b/setup.py @@ -27,8 +27,7 @@ def run_tests(self): sys.exit(errno) # requirements -install_requirements = ['appdirs>=1.4.0', 'chardet>=2.3.0', 'click>=4.0', 'jsonschema>=2.5.1', 'pysrt>=1.0.1', - 'python-magic>=0.4.6', 'pyyaml>=3.11'] +install_requirements = ['appdirs>=1.4.0', 'click>=4.0', 'jsonschema>=2.5.1', 'pysrt>=1.0.1', 'pyyaml>=3.11'] test_requirements = ['pytest', 'pytest-pep8', 'pytest-flakes', 'pytest-cov']