def substitute_words(expedir, substitutions): try: for filename in os.listdir(expedir): filepath = os.path.join(expedir, filename) if os.path.isfile(filepath) and \ os.path.splitext(filepath)[1] == ".py": logger.info(creatortrans( u"Substitutions for file {}").format(filepath)) with open(filepath, 'r') as f: filedata = f.read().decode("utf-8") for oldword, newword in substitutions.iteritems(): filedata = filedata.replace(oldword, newword) with open(filepath, 'w') as f: f.write(unicode.encode(filedata, "utf-8")) except IOError as e: logger.info(creatortrans(u"Error: {msg}").format(msg=e)) return 0 else: return 1
def substitute_words(expedir, substitutions): try: for filename in os.listdir(expedir): filepath = os.path.join(expedir, filename) if os.path.isfile(filepath) and \ os.path.splitext(filepath)[1] == ".py": logger.info( creatortrans(u"Substitutions for file {}").format( filepath)) with open(filepath, 'r') as f: filedata = f.read().decode("utf-8") for oldword, newword in substitutions.iteritems(): filedata = filedata.replace(oldword, newword) with open(filepath, 'w') as f: f.write(unicode.encode(filedata, "utf-8")) except IOError as e: logger.info(creatortrans(u"Error: {msg}").format(msg=e)) return 0 else: return 1
def _accept(self): """ check if all the fields are filled in and if the data are consistents """ try: expe_name = unicode( self.ui.lineEdit_experimentname.text().toUtf8(), "utf-8") expe_shortname = unicode( self.ui.lineEdit_experimentshortname.text().toUtf8(), "utf-8").upper() expe_menu = unicode( self.ui.lineEdit_experimentmenu.text().toUtf8(), "utf-8") periods = str(self.ui.spinBox_periods.value()) groups_size = str(self.ui.spinBox_groupsize.value()) groups_eachperiod = \ str(bool(self.ui.comboBox_groupseachperiod.currentIndex())) currency = unicode( self.ui.lineEdit_currency.text().toUtf8(), "utf-8" ) or str(None) if not (expe_name and expe_shortname and expe_menu): raise ValueError( creatortrans(u"All the text field are required")) except ValueError as e: QtGui.QMessageBox.warning( self, creatortrans(u"Warning"), e.message) return else: self._configuration = self.Configuration( expe_name, expe_shortname, expe_menu, groups_size, groups_eachperiod, periods, currency) confirmation = QtGui.QMessageBox.question( self, creatortrans(u"Confirmation"), creatortrans(u"Apply the following configuration?\n{}".format( self._configuration)), QtGui.QMessageBox.No | QtGui.QMessageBox.Yes) if confirmation != QtGui.QMessageBox.Yes: return self._directory = \ str( QtGui.QFileDialog.getExistingDirectory( self, creatortrans(u"Select the target directory " u"(to be loaded by LE2M the files must " u"be in le2m/parts directory)"))) if not self._directory: return self.accept()
def copy_files(expedir, expename): src = os.path.join(get_appdir(), "filestocopy") target = os.path.join(expedir, expename) try: for f in [ i for i in os.listdir(src) if not ("__init__" in i or os.path.splitext(i)[1] == ".pyc") ]: fin = os.path.join(src, f) suf = fin.split("_")[1] if "Decision" in fin: fout = \ os.path.join(target, "{}GuiSrc".format(expename), "{}{}".format(expename, suf)) else: fout = os.path.join(target, "{}{}".format(expename, suf)) logger.info("Copy of {} to {}".format(fin, fout)) shutil.copy(fin, fout) except (IOError, ValueError) as e: logger.info(creatortrans(u"Error: {msg}").format(msg=e)) return 0 else: return 1
def create_directories(expedir, expename): """ Create the directories as well as the __init__ files In the main __init__ file import the part module, it is for sqlalchemy (because the part module is not imported in the server module) :param expedir: :param expename: :return: """ target = gdir(expedir, expename) try: if os.path.exists(target): raise ValueError( creatortrans(u"A directory with the same part name already " u"exists")) os.mkdir(target) with open(os.path.join(target, "__init__.py"), "wb") as f: f.write("import {}Part # for sqlalchemy".format(expename)) os.mkdir(gdir(target, "{}GuiSrc".format(expename))) open(os.path.join( target, "{}GuiSrc".format(expename), "__init__.py"), "wb").close() os.mkdir(gdir(target, "{}Doc".format(expename))) # i18n os.mkdir(gdir(target, "locale")) os.mkdir(os.path.join(target, "locale", "en_US")) os.mkdir(os.path.join(target, "locale", "en_US", "LC_MESSAGES")) os.mkdir(os.path.join(target, "locale", "fr_FR")) os.mkdir(os.path.join(target, "locale", "fr_FR", "LC_MESSAGES")) except (ValueError, IOError) as e: logger.critical(creatortrans(u"Error: {msg}".format(msg=e))) return 0 else: return 1
def create_directories(expedir, expename): """ Create the directories as well as the __init__ files In the main __init__ file import the part module, it is for sqlalchemy (because the part module is not imported in the server module) :param expedir: :param expename: :return: """ target = gdir(expedir, expename) try: if os.path.exists(target): raise ValueError( creatortrans(u"A directory with the same part name already " u"exists")) os.mkdir(target) with open(os.path.join(target, "__init__.py"), "wb") as f: f.write("import {}Part # for sqlalchemy".format(expename)) os.mkdir(gdir(target, "{}GuiSrc".format(expename))) open(os.path.join(target, "{}GuiSrc".format(expename), "__init__.py"), "wb").close() os.mkdir(gdir(target, "{}Doc".format(expename))) # i18n os.mkdir(gdir(target, "locale")) os.mkdir(os.path.join(target, "locale", "en_US")) os.mkdir(os.path.join(target, "locale", "en_US", "LC_MESSAGES")) os.mkdir(os.path.join(target, "locale", "fr_FR")) os.mkdir(os.path.join(target, "locale", "fr_FR", "LC_MESSAGES")) except (ValueError, IOError) as e: logger.critical(creatortrans(u"Error: {msg}".format(msg=e))) return 0 else: return 1
def creator(): def close(): if __name__ == "__main__": sys.exit(0) else: return global logger creatorutil.create_logger() logger = logging.getLogger("creator") screen = GuiCreator() if screen.exec_(): expe_directory, configuration = screen.get_configuration() substitutions = { "EXPERIENCE_NOM": configuration.expe_name, "EXPERIENCE_NOM_COURT": configuration.expe_shortname, "EXPERIENCE_MENU": configuration.expe_menu, "EXPERIENCE_REPETITIONS_NOMBRE": configuration.periods, "EXPERIENCE_GROUPES_TAILLE": configuration.groups_size, "EXPERIENCE_GROUPES_FORMATION_CHAQUE_PERIODE": configuration.groups_eachperiod, "EXPERIENCE_MONNAIE": configuration.currency } steps = [ (create_directories, [expe_directory, configuration.expe_name]), (copy_files, [expe_directory, configuration.expe_name]), (substitute_words, [os.path.join(expe_directory, configuration.expe_name), substitutions])] for s in steps: logger.info(creatortrans( u"Call of {func} with args {args} ...").format( func=s[0].__name__, args=s[1:])) if not s[0](*s[1]): QtGui.QMessageBox.critical( None, creatortrans(u"Error"), creatortrans(u"Something wrong happens! Contact " u"the developer and/or look the logs")) logger.info( creatortrans(u"Files for {expename} created successfully").format( expename=configuration.expe_name)) QtGui.QMessageBox.information( None, creatortrans(u"Success"), creatortrans(u"Files for {expename} created successfully").format( expename=configuration.expe_name)) close() else: close()
def creator(): def close(): if __name__ == "__main__": sys.exit(0) else: return global logger creatorutil.create_logger() logger = logging.getLogger("creator") screen = GuiCreator() if screen.exec_(): expe_directory, configuration = screen.get_configuration() substitutions = { "EXPERIENCE_NOM": configuration.expe_name, "EXPERIENCE_NOM_COURT": configuration.expe_shortname, "EXPERIENCE_MENU": configuration.expe_menu, "EXPERIENCE_REPETITIONS_NOMBRE": configuration.periods, "EXPERIENCE_GROUPES_TAILLE": configuration.groups_size, "EXPERIENCE_GROUPES_FORMATION_CHAQUE_PERIODE": configuration.groups_eachperiod, "EXPERIENCE_MONNAIE": configuration.currency } steps = [(create_directories, [expe_directory, configuration.expe_name]), (copy_files, [expe_directory, configuration.expe_name]), (substitute_words, [ os.path.join(expe_directory, configuration.expe_name), substitutions ])] for s in steps: logger.info( creatortrans(u"Call of {func} with args {args} ...").format( func=s[0].__name__, args=s[1:])) if not s[0](*s[1]): QtGui.QMessageBox.critical( None, creatortrans(u"Error"), creatortrans(u"Something wrong happens! Contact " u"the developer and/or look the logs")) logger.info( creatortrans(u"Files for {expename} created successfully").format( expename=configuration.expe_name)) QtGui.QMessageBox.information( None, creatortrans(u"Success"), creatortrans(u"Files for {expename} created successfully").format( expename=configuration.expe_name)) close() else: close()
def copy_files(expedir, expename): src = os.path.join(get_appdir(), "filestocopy") target = os.path.join(expedir, expename) try: for f in [i for i in os.listdir(src) if not ("__init__" in i or os.path.splitext(i)[1] == ".pyc")]: fin = os.path.join(src, f) suf = fin.split("_")[1] if "Decision" in fin: fout = \ os.path.join(target, "{}GuiSrc".format(expename), "{}{}".format(expename, suf)) else: fout = os.path.join(target, "{}{}".format(expename, suf)) logger.info("Copy of {} to {}".format(fin, fout)) shutil.copy(fin, fout) except (IOError, ValueError) as e: logger.info(creatortrans(u"Error: {msg}").format(msg=e)) return 0 else: return 1
def __init__(self, parent=None): super(GuiCreator, self).__init__(parent) self.ui = creatorscreen.Ui_Dialog() self.ui.setupUi(self) self._configuration = None self.ui.label_experimentname.setText( creatortrans(u"Part name (without spaces or caps, ex: publicGood)") ) self.ui.label_experimentshortname.setText( creatortrans(u"Short part name (ex. PG for Public Good") ) self.ui.label_experimentmenu.setText( creatortrans(u"Name for the menu in the main application " u"(ex: Public Good)") ) self.ui.label_groupsize.setText( creatortrans(u"Group size (0 = no group)") ) self.ui.spinBox_groupsize.setValue(0) self.ui.label_groupseachperiod.setText( creatortrans(u"Do groups have to be formed each period?") ) self.ui.comboBox_groupseachperiod.addItems( [creatortrans(u"No"), creatortrans(u"Yes")] ) self.ui.label_periods.setText( creatortrans(u"Number of periods (0 = one-shot game)") ) self.ui.spinBox_periods.setValue(0) self.ui.label_currency.setText( creatortrans(u"Experimental currency unit (empty if the " u"same that the local currency)") ) self.ui.lineEdit_currency.setText( creatortrans(u"ecu") ) self.ui.buttonBox.accepted.connect(self._accept) self.ui.buttonBox.rejected.connect(self.reject) self.setWindowTitle( creatortrans(u"Configuration of a new part"))