Skip to content

Commit

Permalink
Free xml.dom.mindom parsers when finished
Browse files Browse the repository at this point in the history
- A strongly recommended practice (see Python official docs)
  that we haven't been following, the objects returned by
  xml.dom.minidom.parser() should be unlink()ed as soon as
  they're no longer needed
- Also cleaned up some * imports, logging, formatting in
  affected files
  • Loading branch information
ferdnyc committed Apr 28, 2020
1 parent a22fa5d commit f4ef693
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/classes/exporters/final_cut_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def createEffect(xmldoc, name, node, points, scale):
first_value = None
for keyframeTime in sorted(keyframes.keys()):
keyframeValue = keyframes.get(keyframeTime)
if first_value == None:
if first_value is None:
first_value = keyframeValue

# Create keyframe element for each point
Expand Down Expand Up @@ -230,4 +230,6 @@ def export_xml():
file.close()
except IOError as inst:
log.error("Error writing XML export: {}".format(str(inst)))

finally:
# Free up DOM memory
xmldoc.unlink()
3 changes: 3 additions & 0 deletions src/classes/importers/final_cut_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,6 @@ def import_xml():
# Update the preview and reselect current frame in properties
app.window.refreshFrameSignal.emit()
app.window.propertyTableView.select_frame(app.window.preview_thread.player.Position())

# Free up DOM memory
xmldoc.unlink()
28 changes: 21 additions & 7 deletions src/windows/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,25 @@
import xml.dom.minidom as xml
import tempfile

import openshot

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QCoreApplication, QTimer
from PyQt5.QtWidgets import (
QMessageBox, QDialog, QFileDialog, QDialogButtonBox, QPushButton
)
from PyQt5.QtGui import QSize, QIcon

from classes import info
from classes import ui_util
from classes import settings
from classes.logger import log
from classes.app import get_app
from classes.metrics import *
from classes.metrics import track_metric_screen, track_metric_error
from classes.query import File

import json


class Export(QDialog):
""" Export Dialog """

Expand Down Expand Up @@ -229,9 +235,11 @@ def __init__(self):
presets = []
for preset_path in [info.EXPORT_PRESETS_PATH, info.USER_PRESETS_PATH]:
for file in os.listdir(preset_path):
xmldoc = xml.parse(os.path.join(preset_path, file))
type = xmldoc.getElementsByTagName("type")
presets.append(_(type[0].childNodes[0].data))
# Use context manager to automatically unlink xmldoc objects
with xml.parse(os.path.join(preset_path, file)) as xmldoc:
type = xmldoc.getElementsByTagName("type")
presets.append(_(type[0].childNodes[0].data))


# Exclude duplicates
type_index = 0
Expand Down Expand Up @@ -395,6 +403,9 @@ def cboSimpleProjectType_index_changed(self, widget, index):
elif openshot.FFmpegWriter.IsValidCodec(codec_text):
acceleration_types[_(title.childNodes[0].data)] = QIcon(":/hw/hw-accel-none.svg")

# Free up DOM memory
xmldoc.unlink()

# Add all targets for selected project type
preset_index = 0
selected_preset = 0
Expand Down Expand Up @@ -539,6 +550,9 @@ def cboSimpleTarget_index_changed(self, widget, index):
break
layout_index += 1

# Free up DOM memory
xmldoc.unlink()

# init the profiles combo
for item in sorted(profiles_list):
self.cboSimpleVideoProfile.addItem(self.getProfileName(self.getProfilePath(item)), self.getProfilePath(item))
Expand Down
12 changes: 5 additions & 7 deletions src/windows/file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@
"""

import os
import locale
import xml.dom.minidom as xml
import functools

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QDialog, QFileDialog, QDialogButtonBox, QPushButton

import openshot # Python module for libopenshot (required video editing module installed separately)

from classes import info, ui_util, settings
from classes.app import get_app
from classes.logger import log
from classes.metrics import *
from classes.metrics import track_metric_screen

import json


class FileProperties(QDialog):
""" File Properties Dialog """

Expand Down Expand Up @@ -183,7 +181,7 @@ def accept(self):
self.file.data["name"] = self.txtFileName.text()
self.file.data["tags"] = self.txtTags.text()

#experimental: update file path
# experimental: update file path
self.file.data["path"] = self.txtFilePath.text()

# Update Framerate
Expand Down
32 changes: 17 additions & 15 deletions src/windows/title_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
"""

import sys
import os
import re
import shutil
Expand All @@ -35,17 +34,20 @@
import tempfile
from xml.dom import minidom

from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem, QFont
from PyQt5.QtWidgets import *
from PyQt5 import uic, QtSvg, QtGui
from PyQt5.QtCore import Qt, QTimer
from PyQt5 import QtGui
from PyQt5.QtWidgets import (
QGraphicsScene, QMessageBox, QDialog, QColorDialog, QFontDialog,
QPushButton, QTextEdit, QLabel
)

import openshot

from classes import info, ui_util, settings, qt_types, updates
from classes import info, ui_util, settings
from classes.logger import log
from classes.app import get_app
from classes.query import File
from classes.metrics import *
from classes.metrics import track_metric_screen
from windows.views.titles_listview import TitlesListView

import json
Expand Down Expand Up @@ -102,7 +104,7 @@ def __init__(self, edit_file_path=None, duplicate=False):
self.font_family = "Bitstream Vera Sans"
self.tspan_node = None

self.qfont = QFont(self.font_family)
self.qfont = QtGui.QFont(self.font_family)

# Add titles list view
self.titlesTreeView = TitlesListView(self)
Expand Down Expand Up @@ -202,7 +204,7 @@ def load_svg_template(self):
self.font_family = "Bitstream Vera Sans"
if self.qfont:
del self.qfont
self.qfont = QFont(self.font_family)
self.qfont = QtGui.QFont(self.font_family)

# Loop through child widgets (and remove them)
for child in self.settingsContainer.children():
Expand Down Expand Up @@ -276,7 +278,6 @@ def load_svg_template(self):
widget.textChanged.connect(functools.partial(self.txtLine_changed, widget))
self.settingsContainer.layout().addRow(label, widget)


# Add Font button
label = QLabel()
label.setText(_("Font:"))
Expand Down Expand Up @@ -340,7 +341,7 @@ def writeToFile(self, xmldoc):
file.write(bytes(xmldoc.toxml(), 'UTF-8'))
file.close()
except IOError as inst:
log.error("Error writing SVG title")
log.error("Error writing SVG title: {}".format(inst))

def btnFontColor_clicked(self):
app = get_app()
Expand Down Expand Up @@ -624,7 +625,6 @@ def set_font_color_elements(self, color, alpha):
t = ";"
text_child.setAttribute("style", t.join(ar))


# Loop through each TSPAN
for tspan_child in self.tspan_node:

Expand Down Expand Up @@ -660,8 +660,11 @@ def accept(self):
if self.txtFileName.toPlainText().strip():
# Do we have unsaved changes?
if os.path.exists(file_path) and not self.edit_file_path:
ret = QMessageBox.question(self, _("Title Editor"), _("%s already exists.\nDo you want to replace it?") % file_name,
QMessageBox.No | QMessageBox.Yes)
ret = QMessageBox.question(
self, _("Title Editor"),
_("%s already exists.\nDo you want to replace it?") % file_name,
QMessageBox.No | QMessageBox.Yes
)
if ret == QMessageBox.No:
# Do nothing
return
Expand All @@ -682,7 +685,6 @@ def add_file(self, filepath):
filename = os.path.basename(filepath)

# Add file into project
app = get_app()
_ = get_app()._tr

# Check for this path in our existing project data
Expand Down

0 comments on commit f4ef693

Please sign in to comment.