def _generate_libtype_element(self, element=None): # Display header. if element is None: print("New", end=" ") element = {"title": None, "shop": None, "finished": None} print("Game Editor") print() # Get game's elements. title = "" while title == "": title = input( "Title{}: ".format("" if element["title"] is None else "[" + element["title"] + "]")) if element["title"] is not None and title == "": title = element["title"] # Exit editor if the user is trying to create a game, which already exists. if element["title"] is None and self.get_element(title) is not None: print("Game {} already exists.".format(title)) return None # Exit editor if new title already exists. if element["title"] is not None: if title != element["title"] and self.get_element( title) is not None: print("Title '{}' has already been used for a game.".format( title)) return None element["title"] = title shop = "" while shop == "": shop = input( "Shop{}: ".format("" if element["shop"] is None else "[" + element["shop"] + "]")) if element["shop"] is not None and shop == "": shop = element["shop"] element["shop"] = shop finished = Utility.get_answer_yn( "Finished{}:".format("" if element["finished"] is None else "[" + element["finished"] + "]")) if finished == "y": element["finished"] = "Yes" else: element["finished"] = "No" # Get game's installer elements. if Utility.get_answer_yn("Open installer editor?") == "y": element = self._generate_installer(element) # Return game dictionary. return element
def show_add_element(self, element=None): menu = None # Get the element from the user. if element is None: menu = True Utility.clear() # Generate new element. element = self._generate_libtype_element() # Return if element is still None. if element is None: input("Press 'Enter' to return to menu: ") return print("Adding item:") print(element) answer = Utility.get_answer_yn("Add item?") # User wants to quit. if answer == "n": return # Add item. if self.add_element(element) == 0: print("The item {} has been added successfully.".format(element)) else: print("The item {} has not been added.".format(element)) if menu: input("Press 'Enter' to return to menu: ")
def show_restore(self): Utility.clear() backupfile = self._xmlfile + ".back" if os.path.isfile(backupfile): # Validate backup file before continue. if Utility.validate(self._xsdfile, backupfile) != 0: print("'{}' is not a valid library file.".format( self._xmlfile)) print("Nothing has changed.") input("Press 'Enter' to continue: ") return # Ask user for overwriting existing library file. overwrite = Utility.get_answer_yn( "This operation will overwrite existing library if any. Continue?" ) # Making sure that only Y or y will overwrite the backup. if overwrite != "y": print("Nothing has changed.") input("Press 'Enter' to continue: ") return # Restore library file, overwriting existing one if any. if self.restore() == 0: print("Library file has been restored successfully.") else: print("Library restoration process has failed.") print( "Make sure a [valid] '{}' file exists and you have write privilege in containing folder." .format(backupfile)) else: print("No backup file has been found.") input("Press 'Enter' to continue: ")
def _edit_optional_tag(self, element, tag): choice = Utility.get_answer_yn("{} {}:".format("Edit" if tag in element else "Create", tag)) if choice == "y": value = input("{}{} [leave empty to remove]: ".format(tag, "[" + element[tag] + "]" if tag in element else "")) if value == "": # Remove tag if exists. if tag in element: del element[tag] return element element[tag] = value return element
def _generate_new_installer(self, game): # Add new installer. installers = [] while Utility.get_answer_yn("Add new installer?") == "y": newinst = self._get_installer_values() installers.append(newinst) # Add installers to game. if installers: if "installer" in game: game["installer"] += installers else: game["installer"] = installers return game
def validate_configuration(self): # Validate configuration. if Utility.validate(self.__confxsd, self.__confxml) == 0: # Validation was successful. print("Validates") else: # Validation has failed. print("Configuration does not validate.") # Ask user for creating new configuration. newconf = "" while newconf != "y" and newconf != "n": newconf = Utility.get_answer_yn( "Create new configuration files (will overwrite existed files)?" ) if newconf == "y": # Create new configuration. self.configure()
def show_edit_element(self, element=None): menu = None # Get the element's unique key from the user. if element is None: menu = True Utility.clear() element = input("Enter the {} of the item to be edited: ".format( self._uniquekey)) # Get element. item = self.get_element(element) if item is None: print("No item with {} '{}' found.".format(self._uniquekey, element)) elif isinstance(item, int): print("Invalid storage file {}.".format(self._xmlfile)) else: # Create python dictionary parsing element's values. itemdict = self._xmlitem_to_dict(item) # Edit item. itemdict = self._generate_libtype_element(itemdict) # Check if itemdict contains values. if itemdict is not None: #confirm before save. print("Saving item:") print(itemdict) answer = Utility.get_answer_yn("Save item?") # User wants to quit. if answer == "n": return # Persist changes. try: if self.edit_element(element, itemdict) == 0: print( "The edited item {} has been saved successfully.". format(element)) else: print("The edited item {} has not been saved.".format( element)) except OSError: print( "Temporary file ha not been removed, after the edited item {} has been saved." .format(element)) if menu: input("Press 'Enter' to return to menu: ")
def show_restore_schema(self): Utility.clear() if os.path.isfile(self._xsdfile): # Ask user for overwriting existing schema file. overwrite = Utility.get_answer_yn( "This operation will overwrite the existing library schema. Continue?" ) # Making sure that only Y or y will overwrite the existing schema. if overwrite != "y": print("Nothing has changed.") input("Press 'Enter' to continue: ") return # If there isn't any schema file already or the user has entered Y or y # restore the default library schema, overwriting existing one if any. if self.restore_schema() == 0: print("Default library schema has been restored successfully.") else: print("Library schema restoration process has failed.") print("Make sure you have write privilege in '{}' folder.".format( os.path.join(self._storageroot, self._libtype))) input("Press 'Enter' to continue: ")
def show_create_library(self): Utility.clear() if os.path.isfile(self._xmlfile): # Ask user for overwriting existing library. overwrite = Utility.get_answer_yn( "This operation will overwrite the existing library. Continue?" ) # Making sure that only Y or y will overwrite the existing library. if overwrite != "y": print("Nothing has changed.") input("Press 'Enter' to continue: ") return # If there isn't any library file already or the user has entered Y or y # create a new empty library, overwriting existing one if any. if self.create_library() == 0: print("New empty library has been created successfully.") else: print("New library creation process has failed.") print("Make sure you have write privilege in '{}' folder.".format( os.path.join(self._storageroot, self._libtype))) input("Press 'Enter' to continue: ")
def show_backup(self): Utility.clear() if os.path.isfile(self._xmlfile + ".back"): # Ask user for overwriting existing backup file. overwrite = Utility.get_answer_yn( "A backup file already exists. Overwrite?") # Making sure that only Y or y will overwrite the backup. if overwrite != "y": print("Nothing has changed.") input("Press 'Enter' to continue: ") return # If there isn't any backup already or the user has entered Y or y # create a new backup, overwriting existing one if any. if self.backup() == 0: print("New backup file has been created successfully.") else: print("Backup process has failed.") print( "Make sure a [valid] '{}' file exists and you have write privilege in containing folder." .format(self._xmlfile)) input("Press 'Enter' to continue: ")
def _generate_libtype_element(self, element = None): # Display header. if element is None: print("New", end = " ") element = {"title": None, "authors": None, "category": None, "formats": None, "isbn": None, "finished": None} print("Book Editor") print() # Get book's elements. isbn = "" while isbn == "": isbn = input("ISBN{}: ".format("" if element["isbn"] is None else "[" + element["isbn"] + "]")) if element["isbn"] is not None and isbn == "": isbn = element["isbn"] # Check if ISBN has 13 digits. pattern = re.compile("^\d{13}$") match = pattern.match(isbn) if match is None: print("ISBN should be 13 digits.".format(isbn)) return None # Exit editor if the user is trying to create a book, which already exists. if element["isbn"] is None and self.get_element(isbn) is not None: print("Book {} already exists.".format(isbn)) return None # Exit editor if new isbn already exists. if element["isbn"] is not None: if isbn != element["isbn"] and self.get_element(isbn) is not None: print("ISBN '{}' has already been used for a book.".format(isbn)) return None element["isbn"] = isbn # Edit mandatory elements. element = self._edit_mandatory_tag(element, "title") element = self._edit_mandatory_tag(element, "category") # Edit mandatory lists. element = self._generate_mandatory_list(element, "authors") element = self._generate_mandatory_list(element, "formats", ["Hardback", "Paperback", "eBook", "Other"]) finished = Utility.get_answer_yn("Finished{}:".format("" if element["finished"] is None else "[" + element["finished"] + "]")) if finished == "y": element["finished"] = "Yes" else: element["finished"] = "No" # Edit optional elements. publicationdate = None while publicationdate is None: publicationdate = input("Publication date{} [YYYY-MM-DD or leave empty to remove]: ".format("[" + element["publicationdate"] + "]" if "publicationdate" in element else "")) if publicationdate == "": if "publicationdate" in element: del element["publicationdate"] else: publicationdate = Utility.validate_date(publicationdate) if publicationdate is not None: element["publicationdate"] = publicationdate element = self._edit_optional_tag(element, "publisher") element = self._edit_optional_tag(element, "edition") element = self._edit_optional_tag(element, "pagenumber") element = self._edit_optional_tag(element, "lastpageread") element = self._edit_optional_tag(element, "shop") # Return book dictionary. return element