Skip to content

Commit f4ef693

Browse files
committed
Free xml.dom.mindom parsers when finished
- 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
1 parent a22fa5d commit f4ef693

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

src/classes/exporters/final_cut_pro.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def createEffect(xmldoc, name, node, points, scale):
5757
first_value = None
5858
for keyframeTime in sorted(keyframes.keys()):
5959
keyframeValue = keyframes.get(keyframeTime)
60-
if first_value == None:
60+
if first_value is None:
6161
first_value = keyframeValue
6262

6363
# Create keyframe element for each point
@@ -230,4 +230,6 @@ def export_xml():
230230
file.close()
231231
except IOError as inst:
232232
log.error("Error writing XML export: {}".format(str(inst)))
233-
233+
finally:
234+
# Free up DOM memory
235+
xmldoc.unlink()

src/classes/importers/final_cut_pro.py

+3
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,6 @@ def import_xml():
201201
# Update the preview and reselect current frame in properties
202202
app.window.refreshFrameSignal.emit()
203203
app.window.propertyTableView.select_frame(app.window.preview_thread.player.Position())
204+
205+
# Free up DOM memory
206+
xmldoc.unlink()

src/windows/export.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,25 @@
3131
import xml.dom.minidom as xml
3232
import tempfile
3333

34+
import openshot
3435

35-
from PyQt5.QtCore import *
36-
from PyQt5.QtWidgets import *
37-
from PyQt5.QtGui import QIcon
36+
from PyQt5.QtCore import Qt, QCoreApplication, QTimer
37+
from PyQt5.QtWidgets import (
38+
QMessageBox, QDialog, QFileDialog, QDialogButtonBox, QPushButton
39+
)
40+
from PyQt5.QtGui import QSize, QIcon
3841

3942
from classes import info
4043
from classes import ui_util
44+
from classes import settings
45+
from classes.logger import log
4146
from classes.app import get_app
42-
from classes.metrics import *
47+
from classes.metrics import track_metric_screen, track_metric_error
4348
from classes.query import File
4449

4550
import json
4651

52+
4753
class Export(QDialog):
4854
""" Export Dialog """
4955

@@ -229,9 +235,11 @@ def __init__(self):
229235
presets = []
230236
for preset_path in [info.EXPORT_PRESETS_PATH, info.USER_PRESETS_PATH]:
231237
for file in os.listdir(preset_path):
232-
xmldoc = xml.parse(os.path.join(preset_path, file))
233-
type = xmldoc.getElementsByTagName("type")
234-
presets.append(_(type[0].childNodes[0].data))
238+
# Use context manager to automatically unlink xmldoc objects
239+
with xml.parse(os.path.join(preset_path, file)) as xmldoc:
240+
type = xmldoc.getElementsByTagName("type")
241+
presets.append(_(type[0].childNodes[0].data))
242+
235243

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

406+
# Free up DOM memory
407+
xmldoc.unlink()
408+
398409
# Add all targets for selected project type
399410
preset_index = 0
400411
selected_preset = 0
@@ -539,6 +550,9 @@ def cboSimpleTarget_index_changed(self, widget, index):
539550
break
540551
layout_index += 1
541552

553+
# Free up DOM memory
554+
xmldoc.unlink()
555+
542556
# init the profiles combo
543557
for item in sorted(profiles_list):
544558
self.cboSimpleVideoProfile.addItem(self.getProfileName(self.getProfilePath(item)), self.getProfilePath(item))

src/windows/file_properties.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@
2626
"""
2727

2828
import os
29-
import locale
30-
import xml.dom.minidom as xml
31-
import functools
3229

33-
from PyQt5.QtCore import *
34-
from PyQt5.QtWidgets import *
30+
from PyQt5.QtWidgets import QDialog, QFileDialog, QDialogButtonBox, QPushButton
31+
3532
import openshot # Python module for libopenshot (required video editing module installed separately)
3633

3734
from classes import info, ui_util, settings
3835
from classes.app import get_app
3936
from classes.logger import log
40-
from classes.metrics import *
37+
from classes.metrics import track_metric_screen
4138

4239
import json
4340

41+
4442
class FileProperties(QDialog):
4543
""" File Properties Dialog """
4644

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

186-
#experimental: update file path
184+
# experimental: update file path
187185
self.file.data["path"] = self.txtFilePath.text()
188186

189187
# Update Framerate

src/windows/title_editor.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2727
"""
2828

29-
import sys
3029
import os
3130
import re
3231
import shutil
@@ -35,17 +34,20 @@
3534
import tempfile
3635
from xml.dom import minidom
3736

38-
from PyQt5.QtCore import *
39-
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem, QFont
40-
from PyQt5.QtWidgets import *
41-
from PyQt5 import uic, QtSvg, QtGui
37+
from PyQt5.QtCore import Qt, QTimer
38+
from PyQt5 import QtGui
39+
from PyQt5.QtWidgets import (
40+
QGraphicsScene, QMessageBox, QDialog, QColorDialog, QFontDialog,
41+
QPushButton, QTextEdit, QLabel
42+
)
43+
4244
import openshot
4345

44-
from classes import info, ui_util, settings, qt_types, updates
46+
from classes import info, ui_util, settings
4547
from classes.logger import log
4648
from classes.app import get_app
4749
from classes.query import File
48-
from classes.metrics import *
50+
from classes.metrics import track_metric_screen
4951
from windows.views.titles_listview import TitlesListView
5052

5153
import json
@@ -102,7 +104,7 @@ def __init__(self, edit_file_path=None, duplicate=False):
102104
self.font_family = "Bitstream Vera Sans"
103105
self.tspan_node = None
104106

105-
self.qfont = QFont(self.font_family)
107+
self.qfont = QtGui.QFont(self.font_family)
106108

107109
# Add titles list view
108110
self.titlesTreeView = TitlesListView(self)
@@ -202,7 +204,7 @@ def load_svg_template(self):
202204
self.font_family = "Bitstream Vera Sans"
203205
if self.qfont:
204206
del self.qfont
205-
self.qfont = QFont(self.font_family)
207+
self.qfont = QtGui.QFont(self.font_family)
206208

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

279-
280281
# Add Font button
281282
label = QLabel()
282283
label.setText(_("Font:"))
@@ -340,7 +341,7 @@ def writeToFile(self, xmldoc):
340341
file.write(bytes(xmldoc.toxml(), 'UTF-8'))
341342
file.close()
342343
except IOError as inst:
343-
log.error("Error writing SVG title")
344+
log.error("Error writing SVG title: {}".format(inst))
344345

345346
def btnFontColor_clicked(self):
346347
app = get_app()
@@ -624,7 +625,6 @@ def set_font_color_elements(self, color, alpha):
624625
t = ";"
625626
text_child.setAttribute("style", t.join(ar))
626627

627-
628628
# Loop through each TSPAN
629629
for tspan_child in self.tspan_node:
630630

@@ -660,8 +660,11 @@ def accept(self):
660660
if self.txtFileName.toPlainText().strip():
661661
# Do we have unsaved changes?
662662
if os.path.exists(file_path) and not self.edit_file_path:
663-
ret = QMessageBox.question(self, _("Title Editor"), _("%s already exists.\nDo you want to replace it?") % file_name,
664-
QMessageBox.No | QMessageBox.Yes)
663+
ret = QMessageBox.question(
664+
self, _("Title Editor"),
665+
_("%s already exists.\nDo you want to replace it?") % file_name,
666+
QMessageBox.No | QMessageBox.Yes
667+
)
665668
if ret == QMessageBox.No:
666669
# Do nothing
667670
return
@@ -682,7 +685,6 @@ def add_file(self, filepath):
682685
filename = os.path.basename(filepath)
683686

684687
# Add file into project
685-
app = get_app()
686688
_ = get_app()._tr
687689

688690
# Check for this path in our existing project data

0 commit comments

Comments
 (0)