eric6/Project/CreateDialogCodeDialog.py

changeset 7785
9978016560ec
parent 7775
4a1db75550bd
child 7836
2f0d208b8137
equal deleted inserted replaced
7784:3257703e10c5 7785:9978016560ec
419 self.tr( 419 self.tr(
420 """<p>No code template file available for""" 420 """<p>No code template file available for"""
421 """ project type "{0}".</p>""") 421 """ project type "{0}".</p>""")
422 .format(self.project.getProjectType())) 422 .format(self.project.getProjectType()))
423 return 423 return
424 tmplFile = open(tmplName, 'r', encoding="utf-8") 424 with open(tmplName, 'r', encoding="utf-8") as tmplFile:
425 template = tmplFile.read() 425 template = tmplFile.read()
426 tmplFile.close()
427 except IOError as why: 426 except IOError as why:
428 E5MessageBox.critical( 427 E5MessageBox.critical(
429 self, 428 self,
430 self.tr("Code Generation"), 429 self.tr("Code Generation"),
431 self.tr( 430 self.tr(
455 indentStr = line.replace(line.lstrip(), "") 454 indentStr = line.replace(line.lstrip(), "")
456 break 455 break
457 else: 456 else:
458 # extend existing file 457 # extend existing file
459 try: 458 try:
460 srcFile = open(self.srcFile, 'r', encoding="utf-8") 459 with open(self.srcFile, 'r', encoding="utf-8") as srcFile:
461 sourceImpl = srcFile.readlines() 460 sourceImpl = srcFile.readlines()
462 srcFile.close()
463 if not sourceImpl[-1].endswith("\n"): 461 if not sourceImpl[-1].endswith("\n"):
464 sourceImpl[-1] = "{0}{1}".format(sourceImpl[-1], "\n") 462 sourceImpl[-1] = "{0}{1}".format(sourceImpl[-1], "\n")
465 except IOError as why: 463 except IOError as why:
466 E5MessageBox.critical( 464 E5MessageBox.critical(
467 self, 465 self,
545 sourceImpl.extend(slotsCode) 543 sourceImpl.extend(slotsCode)
546 else: 544 else:
547 sourceImpl[appendAtIndex:appendAtIndex] = slotsCode 545 sourceImpl[appendAtIndex:appendAtIndex] = slotsCode
548 546
549 # write the new code 547 # write the new code
548 if self.project.useSystemEol():
549 newline = None
550 else:
551 newline = self.project.getEolString()
552 fn = self.filenameEdit.text()
550 try: 553 try:
551 if self.project.useSystemEol(): 554 with open(fn, 'w', encoding="utf-8", newline=newline) as srcFile:
552 newline = None 555 srcFile.write("".join(sourceImpl))
553 else:
554 newline = self.project.getEolString()
555 srcFile = open(self.filenameEdit.text(), 'w', encoding="utf-8",
556 newline=newline)
557 srcFile.write("".join(sourceImpl))
558 srcFile.close()
559 except IOError as why: 556 except IOError as why:
560 E5MessageBox.critical( 557 E5MessageBox.critical(
561 self, 558 self,
562 self.tr("Code Generation"), 559 self.tr("Code Generation"),
563 self.tr("""<p>Could not write the source file "{0}".</p>""" 560 self.tr("""<p>Could not write the source file "{0}".</p>"""
564 """<p>Reason: {1}</p>""") 561 """<p>Reason: {1}</p>""")
565 .format(self.filenameEdit.text(), str(why))) 562 .format(fn, str(why)))
566 return 563 return
567 564
568 self.project.appendFile(self.filenameEdit.text()) 565 self.project.appendFile(fn)
569 566
570 @pyqtSlot(int) 567 @pyqtSlot(int)
571 def on_classNameCombo_activated(self, index): 568 def on_classNameCombo_activated(self, index):
572 """ 569 """
573 Private slot to handle the activated signal of the classname combo. 570 Private slot to handle the activated signal of the classname combo.

eric ide

mercurial