Skip to content

Commit

Permalink
Other random cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
unho committed Nov 15, 2014
1 parent de3793e commit 2fbe440
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 84 deletions.
5 changes: 1 addition & 4 deletions integration-tests/checkdownloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ def _remove_tmp_directory(self):
if os.path.exists(self.temp_dir):
shutil.rmtree(self.temp_dir)

def check_zipfile(self,
filename,
extensions,
expected_files,
def check_zipfile(self, filename, extensions, expected_files,
minimum_size):
tmp_file = tempfile.NamedTemporaryFile()
link = self._get_link_from_filename(filename)
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/checksearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def __init__(self, url):
self.url = url

def search_source(self, term):
url = '{0}/web_search.py?source={1}&project=tots&json=1'
url = url.format(self.url, term)
url = ('{0}/web_search.py?source={1}&project=tots'
'&json=1'.format(self.url, term))

urllib.urlretrieve(url, 'file.txt')
with open('file.txt') as json_data:
Expand All @@ -57,8 +57,8 @@ def _assert_greater(self, actual, minimum):
raise Exception(text.format(minimum, actual))

def _check_integration_data(self):
string = u'Palindromics numbers remain the same when ' \
u'its digits are reversed'
string = (u'"Palindromics numbers remain the same when its digits are '
u'reversed"')
data = self.search_source(string)

self._assert_greater(len(data), 1)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def read_parameters():
'localhost': 'http://localhost:8080/',
'dev': 'http://www.softcatala.org/recursos/dev/',
'preprod': 'http://www.softcatala.org/recursos/preprod/',
'prod': 'http://www.softcatala.org/recursos/'
'prod': 'http://www.softcatala.org/recursos/',
}

opt_enviroments = "localhost, dev, prepod, prod"
Expand Down
5 changes: 3 additions & 2 deletions src/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -119,10 +120,10 @@ def read_parameters():


def load_projects_from_json():
global softcatala_only

json = JsonBackend(projects_json)
json.load()

global softcatala_only

msg = 'Projects defined in json file {0}'.format(len(json.projects))
logging.info(msg)
Expand Down
8 changes: 5 additions & 3 deletions src/gerritdirectoryfileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def set_project(self, project):
self.project = project

def _remove_first_line_from_file(self, filename):
'''Garbage prefix inserted before JSON output to prevent XSSI.'''
'''This prefix is ")]}'\n" and is designed to prevent a web browser'''
'''from executing the response body'''
"""Garbage prefix inserted before JSON output to prevent XSSI.
This prefix is ")]}'\n" and is designed to prevent a web browser from
executing the response body.
"""
working_file = filename + ".old"
shutil.copy(filename, working_file)

Expand Down
10 changes: 5 additions & 5 deletions src/jsonbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self, name):
self.filesets = []

def __str__(self):
text = 'ProjectDTO. Name: {0}, filename: {1}, project web: {2}, ' \
'disabled {3}'
text = ('ProjectDTO. Name: {0}, filename: {1}, project web: {2}, '
'disabled {3}')
return text.format(self.name, self.filename, self.projectweb,
self.disabled)

Expand All @@ -52,8 +52,8 @@ def __init__(self):
self.pattern = ''

def __str__(self):
text = 'FileSetDTO. Name: {0}, url: {1}, type: {2}, excluded: {3}, ' \
'target: {4}, pattern: {5}'
text = ('FileSetDTO. Name: {0}, url: {1}, type: {2}, excluded: {3}, '
'target: {4}, pattern: {5}')
return text.format(self.name, self.url, self.type, self.excluded,
self.target, self.pattern)

Expand Down Expand Up @@ -88,7 +88,7 @@ def _process_file_set_attributes(self, fileset, fileset_value):
elif fileset_properties_attr == 'pattern':
fileset.pattern = fileset_properties_value
else:
msg = 'Field \'{0}\' not recognized'
msg = "Field '{0}' not recognized"
logging.error(msg.format(fileset_properties_attr))

def load(self):
Expand Down
23 changes: 13 additions & 10 deletions src/projectmetadatadao.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def __init__(self):

def create_model(self):
c = self.connection.cursor()

command = '''create table if not exists projects (name text primary key, \
last_fetch timestamp, last_translation_update timestamp, \
words integer, checksum text);'''
command = ('CREATE TABLE IF NOT EXISTS projects ('
'name TEXT PRIMARY KEY,'
'last_fetch TIMESTAMP,'
'last_translation_update TIMESTAMP,'
'words INTEGER,'
'checksum TEXT'
');')
c.execute(command)

command = '''create index if not exists [ix_name] on [projects] ([name]);'''
command = 'CREATE INDEX IF NOT EXISTS [ix_name] ON [projects] ([name]);'
c.execute(command)
self.connection.commit()

Expand All @@ -46,16 +49,16 @@ def open(self, database_name):

def put(self, dto):
c = self.connection.cursor()
command = u"insert or replace into 'projects' values ('{0}', '{1}', '{2}', {3}, '{4}');". \
format(dto.name, dto.last_fetch, dto.last_translation_update,
dto.words, dto.checksum)

command = (u"INSERT OR REPLACE INTO 'projects' VALUES ('{0}', '{1}', "
u"'{2}', {3}, '{4}');".format(dto.name, dto.last_fetch,
dto.last_translation_update,
dto.words, dto.checksum))
c.execute(command)
self.connection.commit()

def get(self, name):
c = self.connection.cursor()
command = u'SELECT * FROM projects where name=\'{0}\''.format(name)
command = u"SELECT * FROM projects WHERE name='{0}'".format(name)
result = c.execute(command)
row = result.fetchone()

Expand Down
8 changes: 4 additions & 4 deletions src/projectmetadatadto.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def last_fetch(self, value):
self._last_fetch = value

def __str__(self):
text = 'ProjectMetaDataDto. Name: {0}, last_translation_update: {1}, ' \
'last_fetch: {2}, words {3}'
return text.format(self.name, self._last_translation_update,
self._last_fetch, self.words)
text = ('ProjectMetaDataDto. Name: {0}, last_translation_update: {1}, '
'last_fetch: {2}, words {3}')
return text.format(self.name, self._last_translation_update,
self._last_fetch, self.words)
4 changes: 2 additions & 2 deletions src/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -85,8 +86,7 @@ def create_tm_for_all_projects(self):
backup_file = 'tm-previous.po'
shutil.copy(self.tm_file, backup_file)
cmd = 'msgcat -tutf-8 --use-first -o {0} {1} {2} 2> /dev/null'
os.system(cmd.format(self.tm_file,
backup_file,
os.system(cmd.format(self.tm_file, backup_file,
project.get_filename()))
os.remove(backup_file)
else:
Expand Down
8 changes: 3 additions & 5 deletions src/subversionfileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
class SubversionFileSet(FileSet):

def do(self):

if len(self.filename) == 0:
CMD = 'cd {0} && svn co --trust-server-cert ' \
'--non-interactive {1}'
CMD = 'cd {0} && svn co --trust-server-cert --non-interactive {1}'
else:
# Single file checkout
CMD = 'cd {0} && svn export --trust-server-cert ' \
'--non-interactive {1}'
CMD = ('cd {0} && svn export --trust-server-cert '
'--non-interactive {1}')

self.create_tmp_directory()
cmd = CMD.format(self.temp_dir, self.url)
Expand Down
16 changes: 8 additions & 8 deletions src/terminology/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2013 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,10 +26,11 @@


class Corpus(object):
'''Loads different PO files that build the corpus'''
'''Strings that are not suitable candidates are discarded'''
'''We do a minimum clean up of strings'''
"""Loads different PO files that build the corpus.
Strings that are not suitable candidates are discarded. We do a minimum
clean up of strings.
"""
def __init__(self, directory):
self.directory = directory
self.source_words = set()
Expand All @@ -44,8 +46,7 @@ def _read_stop_words(self, stopwords_file):
line = stopwords_file.readline()
if not line:
break
word = line.strip()
word = word.lower()
word = line.strip().lower()
self.stop_words.add(word)

def _clean_string(self, result):
Expand All @@ -56,9 +57,8 @@ def _clean_string(self, result):
for c in CHARS:
result = result.replace(c, '')

#remove all the leading and trailing whitespace characters
result = result.strip()
result = result.lower()
# Remove all the leading and trailing whitespace characters.
result = result.strip().lower()
return result

def _should_select_string(self, source, target):
Expand Down
3 changes: 2 additions & 1 deletion src/terminology/glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2014 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -22,7 +23,7 @@


class Glossary(object):
'''Represents all the data need to generate a glossary'''
"""Represents all the data need to generate a glossary."""

def __init__(self):
self.date = datetime.date.today().strftime("%d/%m/%Y")
Expand Down
5 changes: 3 additions & 2 deletions src/terminology/glossaryentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2014 Jordi Mas i Hernandez <jmas@softcatala.org>
# Copyright (c) 2014 Leandro Regueiro Iglesias <leandro.regueiro@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -20,12 +21,12 @@


class GlossaryEntry(object):
'''Represents an entry to be written in a glossary'''
"""Represents an entry to be written in a glossary."""

def __init__(self):
self.source_term = u''
self.translations = []
self.percentage = 0 # Percentage of frequency across all options
self.percentage = 0 # Percentage of frequency across all options.

def get_dict(self):
translations = []
Expand Down
6 changes: 4 additions & 2 deletions src/terminology/referencesources.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def __init__(self, name, short_name):


class ReferenceSources(object):
'''Loads different PO files that we use as reference sources'''
'''like TERMCAT or Microsoft glossaries'''
"""Loads different PO files that we use as reference sources.
Like TERMCAT or Microsoft glossaries.
"""
def __init__(self):
self.stop_words = set()
self.references = []
Expand Down
14 changes: 8 additions & 6 deletions src/terminology/term-extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@


def process_projects():
global glossary_file, glossary_description
global glossary_description
global glossary_file

corpus = Corpus(src_directory)
corpus.process()
Expand All @@ -57,7 +58,8 @@ def process_projects():

# Select terms
MAX_TERMS = 1000
sorted_terms_by_tfxdf = sorted(metrics.tfxdf, key=metrics.tfxdf.get, reverse=True)
sorted_terms_by_tfxdf = sorted(metrics.tfxdf, key=metrics.tfxdf.get,
reverse=True)

# Developer report
glossary_entries = OrderedDict()
Expand All @@ -68,7 +70,8 @@ def process_projects():
glossary_entries[term] = translations.create_for_word_sorted_by_frequency(corpus.documents, term, reference_sources)

dev_glossary_serializer = DevGlossarySerializer()
dev_glossary_serializer.create(u"dev-" + glossary_file + ".html", glossary_description, corpus,
dev_glossary_serializer.create(u"dev-" + glossary_file + ".html",
glossary_description, corpus,
glossary_entries, reference_sources)

# User report
Expand Down Expand Up @@ -125,9 +128,8 @@ def init_logging():

def using():
usage=resource.getrusage(resource.RUSAGE_SELF)
return '''usertime=%s systime=%s mem=%s mb
'''%(usage[0],usage[1],
(usage[2]*resource.getpagesize())/1000000.0)
return ("usertime=%s systime=%s mem=%s mb" % (usage[0],usage[1],
(usage[2]*resource.getpagesize())/1000000.0))


def main():
Expand Down
3 changes: 1 addition & 2 deletions src/terminology/termcat/term-to-po.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def main():
continue

stored_terms[source] = translation
entry = polib.POEntry(msgid=source,
msgstr=translation)
entry = polib.POEntry(msgid=source, msgstr=translation)
pofile.append(entry)
terms += 1

Expand Down
6 changes: 4 additions & 2 deletions src/terminology/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class Translations(object):
'''for the glossary, grouping same translation units and sorting them by'''
'''frequency'''

def _add_reference_translations(self, term, reference_sources, translations):
def _add_reference_translations(self, term, reference_sources,
translations):

# Translations from references (TERMCAT only for now)
reference_translations = reference_sources.get_translations_for_term_in_reference(term, 't')
Expand Down Expand Up @@ -73,7 +74,8 @@ def _add_reference_translations(self, term, reference_sources, translations):

return translations_with_references

def create_for_word_sorted_by_frequency(self, documents, term, reference_sources):
def create_for_word_sorted_by_frequency(self, documents, term,
reference_sources):
translations = {} # key: english keyword -> value: list of translation objects
for document_key_filename in documents.keys():
if term not in documents[document_key_filename]:
Expand Down
3 changes: 2 additions & 1 deletion src/tests/testbazaarfileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def test_has_filename_filename(self):
self.assertTrue(bazaar._has_filename())

def test_has_filename_path(self):
bazaar = BazaarFileSet('project none', 'filesetname', 'lp:~mailman-l10n-ca/', 'none.po')
bazaar = BazaarFileSet('project none', 'filesetname',
'lp:~mailman-l10n-ca/', 'none.po')
self.assertFalse(bazaar._has_filename())


Expand Down
7 changes: 2 additions & 5 deletions src/transifexhubfileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,8 @@ def _get_base_url(self, url):
def _download_page(self):
request = urllib2.Request(self.url)
handle = urllib2.build_opener()
self.content = unicode(
handle.open(request).read(),
'utf-8',
errors='replace'
)
self.content = unicode(handle.open(request).read(), 'utf-8',
errors='replace')

def _process_options(self):
parser = OptionsExtractor(self.url)
Expand Down
Loading

0 comments on commit 2fbe440

Please sign in to comment.