Skip to content

Commit

Permalink
cleanit 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ratoaq2 committed Feb 27, 2016
1 parent 63404cd commit 4cbc4fa
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 41 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion cleanit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__title__ = 'cleanit'
__author__ = 'Rato'
__version__ = '0.1.1'
__version__ = '0.2'
5 changes: 4 additions & 1 deletion cleanit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ 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')
api.save_subtitle(sub)
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):
Expand Down
37 changes: 0 additions & 37 deletions cleanit/subtitle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
import chardet
import logging
import magic
import pysrt


Expand All @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down

0 comments on commit 4cbc4fa

Please sign in to comment.