def main(args=None): #Check the options and arguments parsed parser = functions.defineParser() (options, args) = parser.parse_args(args) if len(args) == 0: parser.error("You have to put a file name") elif len(args) == 1: parser.error("You didn't put any name for the database") elif len(args) != 2: parser.error("Incorrect number of arguments") logging.basicConfig(format = '%(asctime)s -> %(levelname)s : %(message)s', level = options.level, datefmt = '%m/%d/%Y %I:%M:%S %p') for arg in args: logging.info("Argument passed %s", arg) if os.path.exists(args[0]): print "Started ..." logging.debug('Started') tarball = tarfile.open(args[0]) db = DataBase(args[1]) #Go throw the tarball and extract the tar file # k -> name of the job # v dictionary with the data ( { 'connections':[], 'inputs': [], 'site': '' } ) for k,v in functions.extract(tarball, condition = myCondition_1, extractor = myExtractor_1): print "Add data to DB... => ",k ,v try: db.add_Data(k, v) except KeyError: logging.warning("Wrong tarfile") logging.debug("************************************************************************************") print "Extraction done" logging.debug('Done') return SUCCESS else: print 'The file does not exist' return FAILURE
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.database = DataBase() self.add_new_book_btn = QtWidgets.QPushButton("Agregar Nuevo Libro") self.add_new_book_btn.clicked.connect(self.add_book) self.add_new_book_btn.setIcon(QtGui.QIcon("icons/add.png")) self.remove_book_btn = QtWidgets.QPushButton("Remover Libro") self.remove_book_btn.clicked.connect(self.remove_book) self.remove_book_btn.setIcon(QtGui.QIcon("icons/delete.png")) self.add_reading_book_btn = QtWidgets.QPushButton("Agregar Lectura") self.add_reading_book_btn.setIcon(QtGui.QIcon("icons/change.png")) self.add_reading_book_btn.clicked.connect(self.add_reading_book) layout_buttons = QtWidgets.QHBoxLayout() layout_buttons.addWidget(self.add_new_book_btn) layout_buttons.addWidget(self.remove_book_btn) layout_buttons.addWidget(self.add_reading_book_btn) self.items = [] self.customListWidget = QtWidgets.QListWidget() self.customListWidget.itemDoubleClicked.connect(self.register) self.reading_books = self.database.get_reading_books() for book in self.reading_books: bookStatusWidget = BookStatusWidget(book) self.items.append(bookStatusWidget) item = QtWidgets.QListWidgetItem(self.customListWidget) item.setSizeHint(bookStatusWidget.sizeHint()) self.customListWidget.addItem(item) self.customListWidget.setItemWidget(item, bookStatusWidget) layoutPincipal = QtWidgets.QVBoxLayout() layoutPincipal.addWidget(self.customListWidget) layoutPincipal.addLayout(layout_buttons) self.setLayout(layoutPincipal) self.setMinimumSize(600, 400) self.setWindowTitle("Seguimiento de Lectura") self.setWindowIcon(QtGui.QIcon("icons/bookmark.png"))
def __init__(self, db, auth=None, define_tables=True): self.request = current.request self.cache = current.cache self.db = db self.auth = auth self.MAXITEMS = 5 if define_tables: DataBase(db=self.db, auth=self.auth, request=self.request, tables=['t_comment'])
def dbName_intent(req): global persistData # create model db = database.DataBase(req.dbname, None) data = userdata.UserData(req.user, db, None) # save data save_data(data) # create response text = "Saved!\nNow tell me the name of the table you want add to the database." r = response.Response(text, None, [], [], None) res = r.response return res
def populate_database(): from model import DataBase DataBase(db=db, auth=auth, request=request, tables=['all']) from gluon.contrib.populate import populate skip = ['auth_group', 'auth_permission', 'auth_cas', 'auth_event'] import time try: for table in db.tables(): if table not in skip: print 'populating ' + table populate(db.__getattr__(table), 50) #population is done in a transaction, we need to commit to get the references db.commit() time.sleep(1) except Exception as e: print str(e) return "Done"
from model import DataBase db = DataBase('DataResm.db') with open('DataVanessa.sql', 'r') as file: for row in file: db.exec_sql(row) print(row)
class Controller: """Attributes and methods (install database, scenario_fetch_substitute, etc...) of object type Controller""" def __init__(self): """Attributes of Controller : Instantiation of view's and model's object""" self.database = DataBase() self.view = View() def fetch_categories_on_off(self): """Fetch categories on API Open Food Facts with request method""" # Collect categories name from API OFF json r = requests.get('https://fr.openfoodfacts.org/categories.json') j_son = r.json() j_son = j_son['tags'] category_to_collect = ["name"] default_value = "Donnée manquante" category_collected = [] # Collect 11 categories names recovered from json # in category_collected list variable for data in j_son: if len(category_collected) <= 10: for title in range(len(category_to_collect)): category_collected.append( data.get(category_to_collect[title], default_value)) return category_collected def fetch_products_on_off(self): """Fetch products of different categories collected in fetch_categories_on_OFF method on API Open Food Facts with request method""" # Collect categories json url from API OFF json r = requests.get('https://fr.openfoodfacts.org/categories.json') j_son = r.json() j_son = j_son['tags'] category_url_to_collect = ["url"] default_value = "Donnée manquante" category_url_collected = [] name_collected = [] description_collected = [] nutri_score_collected = [] stores_collected = [] url_collected = [] category_id = [] # Collect 11 categories urls recovered from json # in category_url_collected list variable for data in j_son: if len(category_url_collected) <= 10: for title in range(len(category_url_to_collect)): category_url_collected.append( data.get(category_url_to_collect[title], default_value) + '.json') # Collect products name, description, # nutriscore, stores and url from API OFF # json in category_url_collected for index in category_url_collected: r1 = requests.get(index) j2_son = r1.json() try: j2_son = j2_son["products"] except KeyError: default_value = "Donnée manquante" try: name_to_collect = ['product_name_fr'] description_to_collect = ['generic_name_fr'] nutri_score_to_collect = ['nutriscore_grade'] stores_to_collect = ['stores'] url_to_collect = ['url'] # Collect products name, description, # nutriscore, stores and url recovered from json # in different list variable (name_to_collect, # description_to_collect, etc...) for data in j2_son: for title in range(len(name_to_collect)): name_collected.append(data.get(name_to_collect[title])) for title in range(len(description_to_collect)): description_collected.append( data.get(description_to_collect[title])) for title in range(len(nutri_score_to_collect)): nutri_score_collected.append( data.get(nutri_score_to_collect[title])) for title in range(len(stores_to_collect)): stores_collected.append( data.get(stores_to_collect[title])) for title in range(len(url_to_collect)): url_collected.append(data.get(url_to_collect[title])) for i in range(len(category_url_collected)): if index == category_url_collected[i]: category_id.append(i + 1) except KeyError: default_value = "Donnée manquante" # Zip all lists build # (name_to_collect, # description_to_collect, # etc...) in # products_collect list variable products_collected = list( zip(name_collected, description_collected, nutri_score_collected, stores_collected, url_collected, category_id)) return products_collected def install_database(self): """Create and fill database and tables of open_food_facts database""" self.database.create_database() self.database.fill_database() if len(self.database.check()) == 0: self.database.fill_categories(self.fetch_categories_on_off()) self.database.fill_products(self.fetch_products_on_off()) def restart(self): """Mechanics of restart question""" self.view.print_restart_text() user_input_restart = self.view.user_input() if user_input_restart == 1: self.user_scenario() if user_input_restart == 2: exit() else: self.view.input_1_2_error(user_input_restart, self.restart()) def save(self, rows, user_input): """Mechanic of save proposition""" self.view.print_saved_text() user_input_saved = self.view.user_input() if user_input_saved == 1: self.database.saved_substitute(rows, user_input) elif user_input_saved == 2: pass else: self.view.input_1_2_error(user_input_saved, self.save(rows)) def scenario_fetch_substitute(self): """Mechanics of "Which aliment do you want to replace?" scenario""" # Load categories self.database.fetch_all_categories() rows_categories = self.database.rows_cursor() # Print categories self.view.print_category_text(rows_categories) # Insertion of the category chosen by the user user_input_categories = self.view.input_categories() # Load products of category self.database.fetch_products_of_categories(user_input_categories) rows_products = self.database.rows_cursor() # Print products of category self.view.print_products_text(rows_products) # Insertion of the product chosen by the user user_input_product = self.view.input_products(user_input_categories) # Load substitute self.database.fetch_substitute(user_input_product) rows_substitute = self.database.rows_cursor() # Print substitute self.view.print_substitute(rows_substitute) # Save substitute question self.save(rows_substitute, user_input_product) # Restart question self.restart() def scenario_my_substitutes(self): """Mechanics of "Find my saved substitute aliments" scenario""" # Load categories self.database.fetch_all_categories() rows_categories = self.database.rows_cursor() # Print categories self.view.print_category_text(rows_categories) # Insertion of the category chosen by the user user_input_categories = self.view.user_input() # Load substitutes of category self.database.fetch_substitute_of_categories(user_input_categories) rows_substitutes = self.database.rows_cursor() # Print substitutes of category self.view.print_substitute_text(rows_substitutes) # Insertion of the substitute chosen by the user user_input_substitute = self.view.user_input() # Load substitute self.database.fetch_substitute_saved(user_input_substitute) rows_substitute = self.database.rows_cursor() # Print substitute self.view.print_substitute(rows_substitute) # Print original products question self.view.print_original_product_text() user_input_original_products = self.view.user_input() # If Yes if user_input_original_products == 1: # Find original products self.database.fetch_original_products_from_substitute( user_input_substitute) rows_original_products = self.database.rows_cursor() # Print original products self.view.print_original_products(rows_original_products) # Restart question self.restart() # If No else: # Restart question self.restart() def user_scenario(self): """Mechanics of the program""" self.view.print_onset_text() user_input = self.view.user_input() if user_input == 1: self.scenario_fetch_substitute() if user_input == 2: self.scenario_my_substitutes() else: self.view.input_1_2_error(user_input, self.user_scenario())
def __init__(self): """Attributes of Controller : Instantiation of view's and model's object""" self.database = DataBase() self.view = View()
class MainWindow(QtWidgets.QWidget): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.database = DataBase() self.add_new_book_btn = QtWidgets.QPushButton("Agregar Nuevo Libro") self.add_new_book_btn.clicked.connect(self.add_book) self.add_new_book_btn.setIcon(QtGui.QIcon("icons/add.png")) self.remove_book_btn = QtWidgets.QPushButton("Remover Libro") self.remove_book_btn.clicked.connect(self.remove_book) self.remove_book_btn.setIcon(QtGui.QIcon("icons/delete.png")) self.add_reading_book_btn = QtWidgets.QPushButton("Agregar Lectura") self.add_reading_book_btn.setIcon(QtGui.QIcon("icons/change.png")) self.add_reading_book_btn.clicked.connect(self.add_reading_book) layout_buttons = QtWidgets.QHBoxLayout() layout_buttons.addWidget(self.add_new_book_btn) layout_buttons.addWidget(self.remove_book_btn) layout_buttons.addWidget(self.add_reading_book_btn) self.items = [] self.customListWidget = QtWidgets.QListWidget() self.customListWidget.itemDoubleClicked.connect(self.register) self.reading_books = self.database.get_reading_books() for book in self.reading_books: bookStatusWidget = BookStatusWidget(book) self.items.append(bookStatusWidget) item = QtWidgets.QListWidgetItem(self.customListWidget) item.setSizeHint(bookStatusWidget.sizeHint()) self.customListWidget.addItem(item) self.customListWidget.setItemWidget(item, bookStatusWidget) layoutPincipal = QtWidgets.QVBoxLayout() layoutPincipal.addWidget(self.customListWidget) layoutPincipal.addLayout(layout_buttons) self.setLayout(layoutPincipal) self.setMinimumSize(600, 400) self.setWindowTitle("Seguimiento de Lectura") self.setWindowIcon(QtGui.QIcon("icons/bookmark.png")) def add_book(self): dlg = AddBookDialog() if dlg.exec_(): values = dlg.values() self.database.add_new_book(values["titulo"], values["objetivo"], values["fecha"], values["capitulos"]) def remove_book(self): currentRow = self.customListWidget.currentRow() self.customListWidget.takeItem(currentRow) self.database.update_book_reading_state( 0, self.reading_books[currentRow].id) self.reading_books.pop(currentRow) self.items.pop(currentRow) def add_reading_book(self): books = self.database.get_not_reading_books() items = [] for book in books: items.append(book.title) item, ok = QtWidgets.QInputDialog.getItem(self, "Agregar libro para leer", "Libro a leer", items, 0, False) if ok and item: for book in books: if book.title == item: self.database.update_book_reading_state(1, book.id) bookStatusWidget = BookStatusWidget(book) self.items.append(bookStatusWidget) self.reading_books.append(book) item = QtWidgets.QListWidgetItem(self.customListWidget) item.setSizeHint(bookStatusWidget.sizeHint()) self.customListWidget.addItem(item) self.customListWidget.setItemWidget(item, bookStatusWidget) break def register(self): currentRow = self.customListWidget.currentRow() dlg_register = AddReadingDialog(self.reading_books[currentRow]) if dlg_register.exec_(): valor = dlg_register.valores() self.items[currentRow].update_book_chapters(valor["capitulo"]) self.database.add_reading(self.reading_books[currentRow], valor["fecha"], valor["capitulo"], valor["comentario"])