Prefer with context manager to open files
This commit is contained in:
parent
f093c8bd79
commit
5dc5615bfd
@ -234,9 +234,8 @@ class CLIDbManager(object):
|
|||||||
except:
|
except:
|
||||||
version = (0, 0, 0)
|
version = (0, 0, 0)
|
||||||
if os.path.isfile(path_name):
|
if os.path.isfile(path_name):
|
||||||
file = open(path_name, 'r', encoding='utf8')
|
with open(path_name, 'r', encoding='utf8') as file:
|
||||||
name = file.readline().strip()
|
name = file.readline().strip()
|
||||||
file.close()
|
|
||||||
|
|
||||||
(tval, last) = time_val(dirpath)
|
(tval, last) = time_val(dirpath)
|
||||||
(enable, stock_id) = self.icon_values(dirpath, self.active,
|
(enable, stock_id) = self.icon_values(dirpath, self.active,
|
||||||
@ -293,9 +292,8 @@ class CLIDbManager(object):
|
|||||||
name_list = [ name[0] for name in self.current_names ]
|
name_list = [ name[0] for name in self.current_names ]
|
||||||
title = find_next_db_name(name_list)
|
title = find_next_db_name(name_list)
|
||||||
|
|
||||||
name_file = open(path_name, "w", encoding='utf8')
|
with open(path_name, "w", encoding='utf8') as name_file:
|
||||||
name_file.write(title)
|
name_file.write(title)
|
||||||
name_file.close()
|
|
||||||
|
|
||||||
if create_db:
|
if create_db:
|
||||||
# write the version number into metadata
|
# write the version number into metadata
|
||||||
@ -409,9 +407,8 @@ class CLIDbManager(object):
|
|||||||
dirpath = os.path.join(dbdir, dpath)
|
dirpath = os.path.join(dbdir, dpath)
|
||||||
path_name = os.path.join(dirpath, NAME_FILE)
|
path_name = os.path.join(dirpath, NAME_FILE)
|
||||||
if os.path.isfile(path_name):
|
if os.path.isfile(path_name):
|
||||||
file = open(path_name, 'r', encoding='utf8')
|
with open(path_name, 'r', encoding='utf8') as file:
|
||||||
name = file.readline().strip()
|
name = file.readline().strip()
|
||||||
file.close()
|
|
||||||
if re.match("^" + dbname + "$", name):
|
if re.match("^" + dbname + "$", name):
|
||||||
match_list.append((name, dirpath))
|
match_list.append((name, dirpath))
|
||||||
if len(match_list) == 0:
|
if len(match_list) == 0:
|
||||||
@ -438,12 +435,10 @@ class CLIDbManager(object):
|
|||||||
Returns old_name, new_name if success, None, None if no success
|
Returns old_name, new_name if success, None, None if no success
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
name_file = open(filepath, "r", encoding='utf8')
|
with open(filepath, "r", encoding='utf8') as name_file:
|
||||||
old_text=name_file.read()
|
old_text=name_file.read()
|
||||||
name_file.close()
|
with open(filepath, "w", encoding='utf8') as name_file:
|
||||||
name_file = open(filepath, "w", encoding='utf8')
|
name_file.write(new_text)
|
||||||
name_file.write(new_text)
|
|
||||||
name_file.close()
|
|
||||||
except (OSError, IOError) as msg:
|
except (OSError, IOError) as msg:
|
||||||
CLIDbManager.ERROR(_("Could not rename Family Tree"),
|
CLIDbManager.ERROR(_("Could not rename Family Tree"),
|
||||||
str(msg))
|
str(msg))
|
||||||
@ -543,11 +538,10 @@ def find_locker_name(dirpath):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
fname = os.path.join(dirpath, "lock")
|
fname = os.path.join(dirpath, "lock")
|
||||||
ifile = open(fname, 'r', encoding='utf8')
|
with open(fname, 'r', encoding='utf8') as ifile:
|
||||||
username = ifile.read().strip()
|
username = ifile.read().strip()
|
||||||
# feature request 2356: avoid genitive form
|
# feature request 2356: avoid genitive form
|
||||||
last = _("Locked by %s") % username
|
last = _("Locked by %s") % username
|
||||||
ifile.close()
|
|
||||||
except (OSError, IOError, UnicodeDecodeError):
|
except (OSError, IOError, UnicodeDecodeError):
|
||||||
last = _("Unknown")
|
last = _("Unknown")
|
||||||
return last
|
return last
|
||||||
|
@ -203,25 +203,22 @@ class DbState(Callback):
|
|||||||
dirpath = os.path.join(dbdir, dpath)
|
dirpath = os.path.join(dbdir, dpath)
|
||||||
path_name = os.path.join(dirpath, "name.txt")
|
path_name = os.path.join(dirpath, "name.txt")
|
||||||
if os.path.isfile(path_name):
|
if os.path.isfile(path_name):
|
||||||
file = open(path_name, 'r', encoding='utf8')
|
with open(path_name, 'r', encoding='utf8') as file:
|
||||||
name = file.readline().strip()
|
name = file.readline().strip()
|
||||||
file.close()
|
|
||||||
if dbname == name:
|
if dbname == name:
|
||||||
locked = False
|
locked = False
|
||||||
locked_by = None
|
locked_by = None
|
||||||
backend = None
|
backend = None
|
||||||
fname = os.path.join(dirpath, "database.txt")
|
fname = os.path.join(dirpath, "database.txt")
|
||||||
if os.path.isfile(fname):
|
if os.path.isfile(fname):
|
||||||
ifile = open(fname, 'r', encoding='utf8')
|
with open(fname, 'r', encoding='utf8') as ifile:
|
||||||
backend = ifile.read().strip()
|
backend = ifile.read().strip()
|
||||||
ifile.close()
|
|
||||||
else:
|
else:
|
||||||
backend = "bsddb"
|
backend = "bsddb"
|
||||||
try:
|
try:
|
||||||
fname = os.path.join(dirpath, "lock")
|
fname = os.path.join(dirpath, "lock")
|
||||||
ifile = open(fname, 'r', encoding='utf8')
|
with open(fname, 'r', encoding='utf8') as ifile:
|
||||||
locked_by = ifile.read().strip()
|
locked_by = ifile.read().strip()
|
||||||
locked = True
|
|
||||||
ifile.close()
|
ifile.close()
|
||||||
except (OSError, IOError):
|
except (OSError, IOError):
|
||||||
pass
|
pass
|
||||||
|
@ -103,9 +103,8 @@ class FilterList(object):
|
|||||||
if os.path.isfile(self.file):
|
if os.path.isfile(self.file):
|
||||||
parser = make_parser()
|
parser = make_parser()
|
||||||
parser.setContentHandler(FilterParser(self))
|
parser.setContentHandler(FilterParser(self))
|
||||||
the_file = open(self.file, 'r', encoding='utf8')
|
with open(self.file, 'r', encoding='utf8') as the_file:
|
||||||
parser.parse(the_file)
|
parser.parse(the_file)
|
||||||
the_file.close()
|
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
print("IO/OSError in _filterlist.py")
|
print("IO/OSError in _filterlist.py")
|
||||||
except SAXParseException:
|
except SAXParseException:
|
||||||
|
@ -603,9 +603,8 @@ class GVDotDoc(GVDocBase):
|
|||||||
if self._filename[-3:] != ".gv":
|
if self._filename[-3:] != ".gv":
|
||||||
self._filename += ".gv"
|
self._filename += ".gv"
|
||||||
|
|
||||||
dotfile = open(self._filename, "wb")
|
with open(self._filename, "wb") as dotfile:
|
||||||
dotfile.write(self._dot.getvalue())
|
dotfile.write(self._dot.getvalue())
|
||||||
dotfile.close()
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
|
@ -146,31 +146,30 @@ class StyleSheetList(object):
|
|||||||
"""
|
"""
|
||||||
Saves the current StyleSheet definitions to the associated file.
|
Saves the current StyleSheet definitions to the associated file.
|
||||||
"""
|
"""
|
||||||
xml_file = open(self.__file, "w")
|
with open(self.__file, "w") as xml_file:
|
||||||
xml_file.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
xml_file.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
||||||
xml_file.write('<stylelist>\n')
|
xml_file.write('<stylelist>\n')
|
||||||
|
|
||||||
for name in sorted(self.map.keys()): # enable diff of archived copies
|
for name in sorted(self.map.keys()): # enable diff of archived copies
|
||||||
if name == "default":
|
if name == "default":
|
||||||
continue
|
continue
|
||||||
sheet = self.map[name]
|
sheet = self.map[name]
|
||||||
xml_file.write('<sheet name="%s">\n' % escxml(name))
|
xml_file.write('<sheet name="%s">\n' % escxml(name))
|
||||||
|
|
||||||
for p_name in sheet.get_paragraph_style_names():
|
for p_name in sheet.get_paragraph_style_names():
|
||||||
self.write_paragraph_style(xml_file, sheet, p_name)
|
self.write_paragraph_style(xml_file, sheet, p_name)
|
||||||
|
|
||||||
for t_name in sheet.get_table_style_names():
|
for t_name in sheet.get_table_style_names():
|
||||||
self.write_table_style(xml_file, sheet, t_name)
|
self.write_table_style(xml_file, sheet, t_name)
|
||||||
|
|
||||||
for c_name in sheet.get_cell_style_names():
|
for c_name in sheet.get_cell_style_names():
|
||||||
self.write_cell_style(xml_file, sheet, c_name)
|
self.write_cell_style(xml_file, sheet, c_name)
|
||||||
|
|
||||||
for g_name in sheet.get_draw_style_names():
|
for g_name in sheet.get_draw_style_names():
|
||||||
self.write_graphics_style(xml_file, sheet, g_name)
|
self.write_graphics_style(xml_file, sheet, g_name)
|
||||||
|
|
||||||
xml_file.write('</sheet>\n')
|
xml_file.write('</sheet>\n')
|
||||||
xml_file.write('</stylelist>\n')
|
xml_file.write('</stylelist>\n')
|
||||||
xml_file.close()
|
|
||||||
|
|
||||||
def write_paragraph_style(self, xml_file, sheet, p_name):
|
def write_paragraph_style(self, xml_file, sheet, p_name):
|
||||||
|
|
||||||
@ -275,9 +274,8 @@ class StyleSheetList(object):
|
|||||||
if os.path.isfile(self.__file):
|
if os.path.isfile(self.__file):
|
||||||
parser = make_parser()
|
parser = make_parser()
|
||||||
parser.setContentHandler(SheetParser(self))
|
parser.setContentHandler(SheetParser(self))
|
||||||
the_file = open(self.__file)
|
with open(self.__file) as the_file:
|
||||||
parser.parse(the_file)
|
parser.parse(the_file)
|
||||||
the_file.close()
|
|
||||||
except (IOError, OSError, SAXParseException):
|
except (IOError, OSError, SAXParseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -458,68 +458,67 @@ class BookList(object):
|
|||||||
"""
|
"""
|
||||||
Saves the current BookList to the associated file.
|
Saves the current BookList to the associated file.
|
||||||
"""
|
"""
|
||||||
f = open(self.file, "w")
|
with open(self.file, "w") as f:
|
||||||
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
|
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
|
||||||
f.write('<booklist>\n')
|
f.write('<booklist>\n')
|
||||||
for name in sorted(self.bookmap): # enable a diff of archived copies
|
for name in sorted(self.bookmap): # enable a diff of archived copies
|
||||||
book = self.get_book(name)
|
book = self.get_book(name)
|
||||||
dbname = book.get_dbname()
|
dbname = book.get_dbname()
|
||||||
f.write(' <book name="%s" database="%s">\n' % (name, dbname) )
|
f.write(' <book name="%s" database="%s">\n' % (name, dbname) )
|
||||||
for item in book.get_item_list():
|
for item in book.get_item_list():
|
||||||
f.write(' <item name="%s" trans_name="%s">\n' %
|
f.write(' <item name="%s" trans_name="%s">\n' %
|
||||||
(item.get_name(), item.get_translated_name() ) )
|
(item.get_name(), item.get_translated_name() ) )
|
||||||
options = item.option_class.handler.options_dict
|
options = item.option_class.handler.options_dict
|
||||||
for option_name in sorted(options.keys()): # enable a diff
|
for option_name in sorted(options.keys()): # enable a diff
|
||||||
option_value = options[option_name]
|
option_value = options[option_name]
|
||||||
if isinstance(option_value, (list, tuple)):
|
if isinstance(option_value, (list, tuple)):
|
||||||
f.write(' <option name="%s" value="" '
|
f.write(' <option name="%s" value="" '
|
||||||
'length="%d">\n' % (
|
'length="%d">\n' % (
|
||||||
escape(option_name),
|
escape(option_name),
|
||||||
len(options[option_name]) ) )
|
len(options[option_name]) ) )
|
||||||
for list_index in range(len(option_value)):
|
for list_index in range(len(option_value)):
|
||||||
option_type = type_name(option_value[list_index])
|
option_type = type_name(option_value[list_index])
|
||||||
value = escape(str(option_value[list_index]))
|
value = escape(str(option_value[list_index]))
|
||||||
|
value = value.replace('"', '"')
|
||||||
|
f.write(' <listitem number="%d" type="%s" '
|
||||||
|
'value="%s"/>\n' % (
|
||||||
|
list_index,
|
||||||
|
option_type,
|
||||||
|
value ) )
|
||||||
|
f.write(' </option>\n')
|
||||||
|
else:
|
||||||
|
option_type = type_name(option_value)
|
||||||
|
value = escape(str(option_value))
|
||||||
value = value.replace('"', '"')
|
value = value.replace('"', '"')
|
||||||
f.write(' <listitem number="%d" type="%s" '
|
f.write(' <option name="%s" type="%s" '
|
||||||
'value="%s"/>\n' % (
|
'value="%s"/>\n' % (
|
||||||
list_index,
|
escape(option_name),
|
||||||
option_type,
|
option_type,
|
||||||
value ) )
|
value) )
|
||||||
f.write(' </option>\n')
|
|
||||||
else:
|
|
||||||
option_type = type_name(option_value)
|
|
||||||
value = escape(str(option_value))
|
|
||||||
value = value.replace('"', '"')
|
|
||||||
f.write(' <option name="%s" type="%s" '
|
|
||||||
'value="%s"/>\n' % (
|
|
||||||
escape(option_name),
|
|
||||||
option_type,
|
|
||||||
value) )
|
|
||||||
|
|
||||||
f.write(' <style name="%s"/>\n' % item.get_style_name() )
|
f.write(' <style name="%s"/>\n' % item.get_style_name() )
|
||||||
f.write(' </item>\n')
|
f.write(' </item>\n')
|
||||||
if book.get_paper_name():
|
if book.get_paper_name():
|
||||||
f.write(' <paper name="%s"/>\n' % book.get_paper_name() )
|
f.write(' <paper name="%s"/>\n' % book.get_paper_name() )
|
||||||
if book.get_orientation() is not None: # 0 is legal
|
if book.get_orientation() is not None: # 0 is legal
|
||||||
f.write(' <orientation value="%s"/>\n' %
|
f.write(' <orientation value="%s"/>\n' %
|
||||||
book.get_orientation() )
|
book.get_orientation() )
|
||||||
if book.get_paper_metric() is not None: # 0 is legal
|
if book.get_paper_metric() is not None: # 0 is legal
|
||||||
f.write(' <metric value="%s"/>\n' % book.get_paper_metric() )
|
f.write(' <metric value="%s"/>\n' % book.get_paper_metric() )
|
||||||
if book.get_custom_paper_size():
|
if book.get_custom_paper_size():
|
||||||
size = book.get_custom_paper_size()
|
size = book.get_custom_paper_size()
|
||||||
f.write(' <size value="%f %f"/>\n' % (size[0], size[1]) )
|
f.write(' <size value="%f %f"/>\n' % (size[0], size[1]) )
|
||||||
if book.get_margins():
|
if book.get_margins():
|
||||||
for pos in range(len(book.get_margins())):
|
for pos in range(len(book.get_margins())):
|
||||||
f.write(' <margin number="%s" value="%f"/>\n' %
|
f.write(' <margin number="%s" value="%f"/>\n' %
|
||||||
(pos, book.get_margin(pos)) )
|
(pos, book.get_margin(pos)) )
|
||||||
if book.get_format_name():
|
if book.get_format_name():
|
||||||
f.write(' <format name="%s"/>\n' % book.get_format_name() )
|
f.write(' <format name="%s"/>\n' % book.get_format_name() )
|
||||||
if book.get_output():
|
if book.get_output():
|
||||||
f.write(' <output name="%s"/>\n' % book.get_output() )
|
f.write(' <output name="%s"/>\n' % book.get_output() )
|
||||||
f.write(' </book>\n')
|
f.write(' </book>\n')
|
||||||
|
|
||||||
f.write('</booklist>\n')
|
f.write('</booklist>\n')
|
||||||
f.close()
|
|
||||||
|
|
||||||
def parse(self):
|
def parse(self):
|
||||||
"""
|
"""
|
||||||
|
@ -504,9 +504,8 @@ class OptionListCollection(_options.OptionListCollection):
|
|||||||
if os.path.isfile(self.filename):
|
if os.path.isfile(self.filename):
|
||||||
p = make_parser()
|
p = make_parser()
|
||||||
p.setContentHandler(OptionParser(self))
|
p.setContentHandler(OptionParser(self))
|
||||||
the_file = open(self.filename, encoding="utf-8")
|
with open(self.filename, encoding="utf-8") as the_file:
|
||||||
p.parse(the_file)
|
p.parse(the_file)
|
||||||
the_file.close()
|
|
||||||
except (IOError, OSError, SAXParseException):
|
except (IOError, OSError, SAXParseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -1000,9 +999,8 @@ class DocOptionListCollection(_options.OptionListCollection):
|
|||||||
if os.path.isfile(self.filename):
|
if os.path.isfile(self.filename):
|
||||||
p = make_parser()
|
p = make_parser()
|
||||||
p.setContentHandler(DocOptionParser(self))
|
p.setContentHandler(DocOptionParser(self))
|
||||||
the_file = open(self.filename, encoding="utf-8")
|
with open(self.filename, encoding="utf-8") as the_file:
|
||||||
p.parse(the_file)
|
p.parse(the_file)
|
||||||
the_file.close()
|
|
||||||
except (IOError, OSError, SAXParseException):
|
except (IOError, OSError, SAXParseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -279,9 +279,8 @@ def resize_to_jpeg_buffer(source, size, crop=None):
|
|||||||
scaled = img.scale_simple(int(size[0]), int(size[1]), GdkPixbuf.InterpType.BILINEAR)
|
scaled = img.scale_simple(int(size[0]), int(size[1]), GdkPixbuf.InterpType.BILINEAR)
|
||||||
os.close(filed)
|
os.close(filed)
|
||||||
scaled.savev(dest, "jpeg", "", "")
|
scaled.savev(dest, "jpeg", "", "")
|
||||||
ofile = open(dest, mode='rb')
|
with open(dest, mode='rb') as ofile:
|
||||||
data = ofile.read()
|
data = ofile.read()
|
||||||
ofile.close()
|
|
||||||
try:
|
try:
|
||||||
os.unlink(dest)
|
os.unlink(dest)
|
||||||
except:
|
except:
|
||||||
|
@ -436,8 +436,7 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
|
|||||||
|
|
||||||
def __log_error(self):
|
def __log_error(self):
|
||||||
mypath = os.path.join(self.get_save_path(),DBRECOVFN)
|
mypath = os.path.join(self.get_save_path(),DBRECOVFN)
|
||||||
ofile = open(mypath, "w")
|
with open(mypath, "w") as ofile:
|
||||||
ofile.close()
|
|
||||||
try:
|
try:
|
||||||
clear_lock_file(self.get_save_path())
|
clear_lock_file(self.get_save_path())
|
||||||
except:
|
except:
|
||||||
|
@ -285,10 +285,8 @@ class HtmlDoc(BaseDoc, TextDoc):
|
|||||||
Copy support files to the datadir that needs to hold them
|
Copy support files to the datadir that needs to hold them
|
||||||
"""
|
"""
|
||||||
#css of textdoc styles
|
#css of textdoc styles
|
||||||
tdfile = open(os.path.join(self._backend.datadirfull(),
|
with open(os.path.join(self._backend.datadirfull(), _TEXTDOCSCREEN), 'w') as tdfile:
|
||||||
_TEXTDOCSCREEN), 'w')
|
tdfile.write(self.style_declaration)
|
||||||
tdfile.write(self.style_declaration)
|
|
||||||
tdfile.close()
|
|
||||||
#css file
|
#css file
|
||||||
if self.css_filename:
|
if self.css_filename:
|
||||||
#we do an extra check in case file does not exist, eg cli call
|
#we do an extra check in case file does not exist, eg cli call
|
||||||
|
@ -30,12 +30,11 @@ from test import test_util
|
|||||||
test_util.path_append_parent()
|
test_util.path_append_parent()
|
||||||
|
|
||||||
def get_potfile(filename):
|
def get_potfile(filename):
|
||||||
fp = open(filename, "r")
|
with open(filename, "r") as fp:
|
||||||
retvals = []
|
retvals = []
|
||||||
for line in fp:
|
for line in fp:
|
||||||
if line and line[0] != "#":
|
if line and line[0] != "#":
|
||||||
retvals.append(line.strip())
|
retvals.append(line.strip())
|
||||||
fp.close()
|
|
||||||
return retvals
|
return retvals
|
||||||
|
|
||||||
# POTFILES.skip
|
# POTFILES.skip
|
||||||
@ -55,9 +54,8 @@ class TestPOT(unittest.TestCase):
|
|||||||
realpath = (dir + "/" + file)
|
realpath = (dir + "/" + file)
|
||||||
pathfile = realpath[3:]
|
pathfile = realpath[3:]
|
||||||
if os.path.exists(realpath):
|
if os.path.exists(realpath):
|
||||||
fp = open(realpath, "r")
|
with open(realpath, "r") as fp:
|
||||||
lines = fp.read()
|
lines = fp.read()
|
||||||
fp.close()
|
|
||||||
found = False
|
found = False
|
||||||
for search in searches:
|
for search in searches:
|
||||||
if search in lines:
|
if search in lines:
|
||||||
@ -88,9 +86,8 @@ class TestMake(unittest.TestCase):
|
|||||||
if pathfile[3:] in excluded_files:
|
if pathfile[3:] in excluded_files:
|
||||||
self.assertTrue(True, "exclude '%s'" % pathfile)
|
self.assertTrue(True, "exclude '%s'" % pathfile)
|
||||||
elif os.path.exists(makefile):
|
elif os.path.exists(makefile):
|
||||||
fp = open(makefile, "r")
|
with open(makefile, "r") as fp:
|
||||||
lines = fp.read()
|
lines = fp.read()
|
||||||
fp.close()
|
|
||||||
self.assertTrue(filename in lines, "'%s' not in %s/Makefile.in" %
|
self.assertTrue(filename in lines, "'%s' not in %s/Makefile.in" %
|
||||||
(filename, path))
|
(filename, path))
|
||||||
else:
|
else:
|
||||||
@ -107,9 +104,8 @@ class TestGetText(unittest.TestCase):
|
|||||||
def helper(self, pofile, searches):
|
def helper(self, pofile, searches):
|
||||||
if not os.path.exists("../../" + pofile):
|
if not os.path.exists("../../" + pofile):
|
||||||
self.assertTrue(False, "'%s' is in POTFILES.in and does not exist" % pofile)
|
self.assertTrue(False, "'%s' is in POTFILES.in and does not exist" % pofile)
|
||||||
fp = open("../../" + pofile, "r")
|
with open("../../" + pofile, "r") as fp:
|
||||||
lines = fp.read()
|
lines = fp.read()
|
||||||
fp.close()
|
|
||||||
found = False
|
found = False
|
||||||
for search in searches:
|
for search in searches:
|
||||||
found = (search in lines) or found
|
found = (search in lines) or found
|
||||||
|
228
po/update_po.py
228
po/update_po.py
@ -166,23 +166,22 @@ def TipsParse(filename, mark):
|
|||||||
"Editor."
|
"Editor."
|
||||||
'''
|
'''
|
||||||
|
|
||||||
tips = open('../data/tips.xml.in.h', 'w')
|
with open('../data/tips.xml.in.h', 'w') as tips:
|
||||||
marklist = root.iter(mark)
|
marklist = root.iter(mark)
|
||||||
for key in marklist:
|
for key in marklist:
|
||||||
tip = ElementTree.tostring(key, encoding="UTF-8", method="xml")
|
tip = ElementTree.tostring(key, encoding="UTF-8", method="xml")
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
tip = tip.replace("<?xml version='1.0' encoding='UTF-8'?>", "")
|
tip = tip.replace("<?xml version='1.0' encoding='UTF-8'?>", "")
|
||||||
tip = tip.replace('\n<_tip number="%(number)s">' % key.attrib, "")
|
tip = tip.replace('\n<_tip number="%(number)s">' % key.attrib, "")
|
||||||
else: # python3 support
|
else: # python3 support
|
||||||
tip = tip.decode("utf-8")
|
tip = tip.decode("utf-8")
|
||||||
tip = tip.replace('<_tip number="%(number)s">' % key.attrib, "")
|
tip = tip.replace('<_tip number="%(number)s">' % key.attrib, "")
|
||||||
tip = tip.replace("<br />", "<br/>")
|
tip = tip.replace("<br />", "<br/>")
|
||||||
#tip = tip.replace("\n</_tip>\n", "</_tip>\n") # special case tip 7
|
#tip = tip.replace("\n</_tip>\n", "</_tip>\n") # special case tip 7
|
||||||
#tip = tip.replace("\n<b>", "<b>") # special case tip 18
|
#tip = tip.replace("\n<b>", "<b>") # special case tip 18
|
||||||
tip = tip.replace("</_tip>\n\n", "")
|
tip = tip.replace("</_tip>\n\n", "")
|
||||||
tip = tip.replace('"', '"')
|
tip = tip.replace('"', '"')
|
||||||
tips.write('char *s = N_("%s");\n' % tip)
|
tips.write('char *s = N_("%s");\n' % tip)
|
||||||
tips.close()
|
|
||||||
print ('Wrote ../data/tips.xml.in.h')
|
print ('Wrote ../data/tips.xml.in.h')
|
||||||
root.clear()
|
root.clear()
|
||||||
|
|
||||||
@ -215,15 +214,14 @@ def HolidaysParse(filename, mark):
|
|||||||
msgid "Yom Kippur"
|
msgid "Yom Kippur"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
holidays = open('../data/holidays.xml.in.h', 'w')
|
with open('../data/holidays.xml.in.h', 'w') as holidays:
|
||||||
for key in ellist:
|
for key in ellist:
|
||||||
if key.attrib.get(mark):
|
if key.attrib.get(mark):
|
||||||
line = key.attrib
|
line = key.attrib
|
||||||
string = line.items
|
string = line.items
|
||||||
# mapping via the line dict (_name is the key)
|
# mapping via the line dict (_name is the key)
|
||||||
name = 'char *s = N_("%(_name)s");\n' % line
|
name = 'char *s = N_("%(_name)s");\n' % line
|
||||||
holidays.write(name)
|
holidays.write(name)
|
||||||
holidays.close()
|
|
||||||
print ('Wrote ../data/holidays.xml.in.h')
|
print ('Wrote ../data/holidays.xml.in.h')
|
||||||
root.clear()
|
root.clear()
|
||||||
|
|
||||||
@ -262,20 +260,19 @@ def XmlParse(filename, mark):
|
|||||||
</p>
|
</p>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
head = open(filename + '.h', 'w')
|
with open(filename + '.h', 'w') as head:
|
||||||
|
|
||||||
for key in root.iter():
|
|
||||||
if key.tag == '{http://www.freedesktop.org/standards/shared-mime-info}%s' % mark:
|
|
||||||
comment = 'char *s = N_("%s");\n' % key.text
|
|
||||||
head.write(comment)
|
|
||||||
|
|
||||||
if root.tag == 'application':
|
|
||||||
for key in root.iter():
|
for key in root.iter():
|
||||||
if key.tag == mark:
|
if key.tag == '{http://www.freedesktop.org/standards/shared-mime-info}%s' % mark:
|
||||||
comment = 'char *s = N_("%s");\n' % key.text
|
comment = 'char *s = N_("%s");\n' % key.text
|
||||||
head.write(comment)
|
head.write(comment)
|
||||||
|
|
||||||
head.close()
|
if root.tag == 'application':
|
||||||
|
for key in root.iter():
|
||||||
|
if key.tag == mark:
|
||||||
|
comment = 'char *s = N_("%s");\n' % key.text
|
||||||
|
head.write(comment)
|
||||||
|
|
||||||
print ('Wrote %s' % filename)
|
print ('Wrote %s' % filename)
|
||||||
root.clear()
|
root.clear()
|
||||||
|
|
||||||
@ -301,22 +298,19 @@ def DesktopParse(filename):
|
|||||||
perform genealogical research and analysis"
|
perform genealogical research and analysis"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
desktop = open('../data/gramps.desktop.in.h', 'w')
|
with open('../data/gramps.desktop.in.h', 'w') as desktop:
|
||||||
|
|
||||||
f = open(filename)
|
with open(filename) as f:
|
||||||
lines = [file.strip() for file in f]
|
lines = [file.strip() for file in f]
|
||||||
f.close()
|
|
||||||
|
for line in lines:
|
||||||
for line in lines:
|
if line[0] == '_':
|
||||||
if line[0] == '_':
|
for i in range(len(line)):
|
||||||
for i in range(len(line)):
|
if line[i] == '=':
|
||||||
if line[i] == '=':
|
val = 'char *s = N_("%s");\n' % line[i+1:len(line)]
|
||||||
val = 'char *s = N_("%s");\n' % line[i+1:len(line)]
|
desktop.write(val)
|
||||||
desktop.write(val)
|
|
||||||
|
|
||||||
desktop.close()
|
|
||||||
print ('Wrote ../data/gramps.desktop.in.h')
|
print ('Wrote ../data/gramps.desktop.in.h')
|
||||||
|
|
||||||
|
|
||||||
def KeyParse(filename, mark):
|
def KeyParse(filename, mark):
|
||||||
"""
|
"""
|
||||||
@ -342,29 +336,26 @@ def KeyParse(filename, mark):
|
|||||||
msgid "Gramps XML database"
|
msgid "Gramps XML database"
|
||||||
msgid "GEDCOM"
|
msgid "GEDCOM"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
key = open('../data/gramps.keys.in.h', 'w')
|
with open('../data/gramps.keys.in.h', 'w') as key:
|
||||||
|
|
||||||
f = open(filename)
|
with open(filename) as f:
|
||||||
lines = [file for file in f]
|
lines = [file for file in f]
|
||||||
f.close()
|
|
||||||
|
temp = []
|
||||||
temp = []
|
|
||||||
|
for line in lines:
|
||||||
for line in lines:
|
for i in range(len(line)):
|
||||||
for i in range(len(line)):
|
if line[i:i+12] == mark:
|
||||||
if line[i:i+12] == mark:
|
temp.append(line.strip())
|
||||||
temp.append(line.strip())
|
|
||||||
|
for t in temp:
|
||||||
for t in temp:
|
for i in range(len(t)):
|
||||||
for i in range(len(t)):
|
if t[i] == '=':
|
||||||
if t[i] == '=':
|
val = 'char *s = N_("%s");\n' % t[i+1:len(t)]
|
||||||
val = 'char *s = N_("%s");\n' % t[i+1:len(t)]
|
key.write(val)
|
||||||
key.write(val)
|
|
||||||
|
|
||||||
key.close()
|
|
||||||
print ('Wrote ../data/gramps.keys.in.h')
|
print ('Wrote ../data/gramps.keys.in.h')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
@ -467,15 +458,14 @@ def create_filesfile():
|
|||||||
dir = os.getcwd()
|
dir = os.getcwd()
|
||||||
topdir = os.path.normpath(os.path.join(dir, '..', 'gramps'))
|
topdir = os.path.normpath(os.path.join(dir, '..', 'gramps'))
|
||||||
lentopdir = len(topdir)
|
lentopdir = len(topdir)
|
||||||
f = open('POTFILES.in')
|
with open('POTFILES.in') as f:
|
||||||
infiles = dict(['../' + file.strip(), None] for file in f if file.strip()
|
infiles = dict(['../' + file.strip(), None] for file in f if file.strip()
|
||||||
and not file[0]=='#')
|
and not file[0]=='#')
|
||||||
f.close()
|
|
||||||
f = open('POTFILES.skip')
|
with open('POTFILES.skip') as f:
|
||||||
notinfiles = dict(['../' + file.strip(), None] for file in f if file
|
notinfiles = dict(['../' + file.strip(), None] for file in f if file
|
||||||
and not file[0]=='#')
|
and not file[0]=='#')
|
||||||
f.close()
|
|
||||||
|
|
||||||
for (dirpath, dirnames, filenames) in os.walk(topdir):
|
for (dirpath, dirnames, filenames) in os.walk(topdir):
|
||||||
root, subdir = os.path.split(dirpath)
|
root, subdir = os.path.split(dirpath)
|
||||||
if subdir.startswith("."):
|
if subdir.startswith("."):
|
||||||
@ -499,11 +489,10 @@ def create_filesfile():
|
|||||||
if full_filename[lentopdir:] in notinfiles:
|
if full_filename[lentopdir:] in notinfiles:
|
||||||
infiles['../gramps' + full_filename[lentopdir:]] = None
|
infiles['../gramps' + full_filename[lentopdir:]] = None
|
||||||
#now we write out all the files in form ../gramps/filename
|
#now we write out all the files in form ../gramps/filename
|
||||||
f = open('tmpfiles', 'w')
|
with open('tmpfiles', 'w') as f:
|
||||||
for file in sorted(infiles.keys()):
|
for file in sorted(infiles.keys()):
|
||||||
f.write(file)
|
f.write(file)
|
||||||
f.write('\n')
|
f.write('\n')
|
||||||
f.close()
|
|
||||||
|
|
||||||
def listing(name, extensionlist):
|
def listing(name, extensionlist):
|
||||||
"""
|
"""
|
||||||
@ -511,22 +500,19 @@ def listing(name, extensionlist):
|
|||||||
Parsing from a textual file (gramps) is faster and easy for maintenance.
|
Parsing from a textual file (gramps) is faster and easy for maintenance.
|
||||||
Like POTFILES.in and POTFILES.skip
|
Like POTFILES.in and POTFILES.skip
|
||||||
"""
|
"""
|
||||||
|
|
||||||
f = open('tmpfiles')
|
with open('tmpfiles') as f:
|
||||||
files = [file.strip() for file in f if file and not file[0]=='#']
|
files = [file.strip() for file in f if file and not file[0]=='#']
|
||||||
f.close()
|
|
||||||
|
with open(name, 'w') as temp:
|
||||||
temp = open(name, 'w')
|
|
||||||
|
for entry in files:
|
||||||
for entry in files:
|
for ext in extensionlist:
|
||||||
for ext in extensionlist:
|
if entry.endswith(ext):
|
||||||
if entry.endswith(ext):
|
temp.write(entry)
|
||||||
temp.write(entry)
|
temp.write('\n')
|
||||||
temp.write('\n')
|
break
|
||||||
break
|
|
||||||
|
|
||||||
temp.close()
|
|
||||||
|
|
||||||
def headers():
|
def headers():
|
||||||
"""
|
"""
|
||||||
Look at existing C file format headers.
|
Look at existing C file format headers.
|
||||||
@ -565,14 +551,13 @@ def extract_xml():
|
|||||||
XmlParse('../data/gramps.appdata.xml.in', '_p')
|
XmlParse('../data/gramps.appdata.xml.in', '_p')
|
||||||
DesktopParse('../data/gramps.desktop.in')
|
DesktopParse('../data/gramps.desktop.in')
|
||||||
KeyParse('../data/gramps.keys.in', '_description')
|
KeyParse('../data/gramps.keys.in', '_description')
|
||||||
|
|
||||||
def create_template():
|
def create_template():
|
||||||
"""
|
"""
|
||||||
Create a new file for template, if it does not exist.
|
Create a new file for template, if it does not exist.
|
||||||
"""
|
"""
|
||||||
template = open('gramps.pot', 'w')
|
with open('gramps.pot', 'w') as template:
|
||||||
template.close()
|
|
||||||
|
|
||||||
def extract_glade():
|
def extract_glade():
|
||||||
"""
|
"""
|
||||||
Extract messages from a temp file with all .glade
|
Extract messages from a temp file with all .glade
|
||||||
@ -622,21 +607,20 @@ def extract_gtkbuilder():
|
|||||||
msgid "At least one rule must apply"
|
msgid "At least one rule must apply"
|
||||||
msgid "Exactly one rule must apply"
|
msgid "Exactly one rule must apply"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
files = ['../gramps/plugins/importer/importgedcom.glade', '../gramps/gui/glade/rule.glade']
|
files = ['../gramps/plugins/importer/importgedcom.glade', '../gramps/gui/glade/rule.glade']
|
||||||
temp = open('gtklist.h', 'w')
|
with open('gtklist.h', 'w') as temp:
|
||||||
|
|
||||||
for filename in files:
|
for filename in files:
|
||||||
tree = ElementTree.parse(filename)
|
tree = ElementTree.parse(filename)
|
||||||
root = tree.getroot()
|
root = tree.getroot()
|
||||||
for line in root.iter():
|
for line in root.iter():
|
||||||
att = line.attrib
|
att = line.attrib
|
||||||
if att == {'id': '0', 'translatable': 'yes'}:
|
if att == {'id': '0', 'translatable': 'yes'}:
|
||||||
col = 'char *s = N_("%s");\n' % line.text
|
col = 'char *s = N_("%s");\n' % line.text
|
||||||
temp.write(col)
|
temp.write(col)
|
||||||
root.clear()
|
root.clear()
|
||||||
|
|
||||||
temp.close()
|
|
||||||
print ('Wrote gtklist.h')
|
print ('Wrote gtklist.h')
|
||||||
|
|
||||||
def retrieve():
|
def retrieve():
|
||||||
|
Loading…
Reference in New Issue
Block a user