def _set_preferences_logging(self): from qiskit_aqua_chemistry.preferences import Preferences from qiskit_aqua_chemistry._logging import set_logging_config preferences = Preferences() config = preferences.get_logging_config() if config is not None: set_logging_config(config)
def __init__(self, parent, **options): super(QconfigView, self).__init__(parent, **options) self.pack(fill=tk.BOTH, expand=tk.TRUE) self._notebook = ttk.Notebook(self) self._notebook.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.TRUE) preferences = Preferences() self._mainpage = MainPage(self._notebook, preferences) self._proxiespage = ProxiesPage(self._notebook, preferences) self._notebook.add(self._mainpage, text='Main') self._notebook.add(self._proxiespage, text='Proxies') self._notebook.bind('<<NotebookTabChanged>>', self._tab_changed) frame = ttk.Frame(self) frame.pack(side=tk.BOTTOM, fill=tk.X, expand=tk.TRUE) ttk.Label(frame, text="Path:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, padx=6, sticky='nsew') ttk.Label(frame, text=preferences.get_qconfig_path(''), borderwidth=0, anchor=tk.W).grid(row=0, column=1, sticky='nsw') self.initial_focus = self._mainpage.initial_focus self.update_idletasks() self._notebook.configure(height=self._mainpage.winfo_reqheight())
def apply(self): try: level_name = self._levelCombo.get() levels = [ key for key, value in PreferencesDialog._LOG_LEVELS.items() if value == level_name ] loglevel = levels[0] preferences = Preferences() self._qconfigview.apply(preferences) self._packagesPage.apply(preferences) preferences.save() logging_config = build_logging_config(loglevel) preferences = Preferences() preferences.set_logging_config(logging_config) preferences.save() set_logging_config(logging_config) uipreferences = UIPreferences() populate = self._populateDefaults.get() uipreferences.set_populate_defaults(False if populate == 0 else True) uipreferences.save() self._controller.get_available_backends() except Exception as e: self.controller.outputview.write_line(str(e))
def main(): parser = argparse.ArgumentParser( description='Qiskit Aqua Chemistry Command Line Tool') parser.add_argument('input', metavar='input', help='Chemistry input file or saved JSON input file') group = parser.add_mutually_exclusive_group(required=False) group.add_argument('-o', metavar='output', help='Algorithm Results Output file name') group.add_argument('-jo', metavar='json output', help='Algorithm JSON Output file name') args = parser.parse_args() # update logging setting with latest external packages preferences = Preferences() logging_level = logging.INFO if preferences.get_logging_config() is not None: set_logging_config(preferences.get_logging_config()) logging_level = get_logging_level() preferences.set_logging_config(build_logging_config(logging_level)) preferences.save() set_logging_config(preferences.get_logging_config()) solver = AquaChemistry() # check to see if input is json file params = None try: with open(args.input) as json_file: params = json.load(json_file) except: pass if params is not None: solver.run_algorithm_from_json(params, args.o) else: if args.jo is not None: solver.run_drive_to_jsonfile(args.input, args.jo) else: result = solver.run(args.input, args.o) if result is not None and 'printable' in result: print( '\n\n--------------------------------- R E S U L T ------------------------------------\n' ) for line in result['printable']: print(line)
def set_preferences_logging(): """ Update logging setting with latest external packages """ from qiskit_aqua_chemistry._logging import get_logging_level, build_logging_config, set_logging_config from qiskit_aqua_chemistry.preferences import Preferences preferences = Preferences() logging_level = logging.INFO if preferences.get_logging_config() is not None: set_logging_config(preferences.get_logging_config()) logging_level = get_logging_level() preferences.set_logging_config(build_logging_config(logging_level)) preferences.save()
def discover_preferences_configurations(self): """ Discovers the configuration.json files on the directory and subdirectories of the preferences package and attempts to load them. """ preferences = Preferences() packages = preferences.get_packages( Preferences.PACKAGE_TYPE_DRIVERS, []) for package in packages: try: mod = importlib.import_module(package) if mod is not None: self.discover_configurations( os.path.dirname(mod.__file__), mod.__name__) else: # Ignore package that could not be initialized. logger.debug( 'Failed to import package {}'.format(package)) except Exception as e: # Ignore package that could not be initialized. logger.debug('Failed to load package {} error {}'.format( package, str(e)))
def discover_preferences_chemistry_operators(): """ Discovers the chemistry operators on the directory and subdirectories of the preferences package and attempts to register them. Chem.Operator modules should subclass ChemistryOperator Base class. """ preferences = Preferences() packages = preferences.get_packages(Preferences.PACKAGE_TYPE_CHEMISTRY, []) for package in packages: try: mod = importlib.import_module(package) if mod is not None: _discover_local_chemistry_operators( os.path.dirname(mod.__file__), mod.__name__, names_to_exclude=['__main__'], folders_to_exclude=['__pycache__']) else: # Ignore package that could not be initialized. logger.debug('Failed to import package {}'.format(package)) except Exception as e: # Ignore package that could not be initialized. logger.debug('Failed to load package {} error {}'.format( package, str(e)))
def __init__(self, parent, preferences, **options): super(PackagesPage, self).__init__(parent, **options) size = font.nametofont('TkHeadingFont').actual('size') ttk.Style().configure("PackagesPage.Treeview.Heading", font=(None, size, 'bold')) self._tree = ttk.Treeview(self, style='PackagesPage.Treeview', selectmode=tk.BROWSE, height=4, columns=['value']) self._tree.heading('#0', text='Type') self._tree.heading('value', text='Name') self._tree.column('#0', minwidth=0, width=150, stretch=tk.NO) self._tree.column('value', minwidth=0, width=500, stretch=tk.YES) self._tree.bind('<<TreeviewSelect>>', self._on_tree_select) self._tree.bind('<Button-1>', self._on_tree_edit) self.init_widgets(self._tree) self._preferences = Preferences() self._popup_widget = None self.pack(fill=tk.BOTH, expand=tk.TRUE) self.populate() self.initial_focus = self._tree
def set_logging(self, level=logging.INFO): """Sets logging output of the logging messages. Sets the output of logging messages (above level `level`) by configuring the logger accordingly. Disables logging if set to logging.NOTSET Params: level (int): minimum severity of the messages that are displayed. """ logging_config = build_logging_config( ['qiskit_aqua_chemistry', 'qiskit_aqua'], level) preferences = Preferences() preferences.set_logging_config(logging_config) preferences.save() set_logger_config(logging_config)
def main(): if sys.platform == 'darwin': from Foundation import NSBundle bundle = NSBundle.mainBundle() if bundle: info = bundle.localizedInfoDictionary() or bundle.infoDictionary() info['CFBundleName'] = 'QISkit Aqua Chemistry' root = tk.Tk() root.withdraw() root.update_idletasks() preferences = UIPreferences() geometry = preferences.get_geometry() if geometry is None: ws = root.winfo_screenwidth() hs = root.winfo_screenheight() w = int(ws / 1.3) h = int(hs / 1.3) x = int(ws/2 - w/2) y = int(hs/2 - h/2) geometry = '{}x{}+{}+{}'.format(w,h,x,y) preferences.set_geometry(geometry) preferences.save() root.geometry(geometry) # update logging setting with latest external packages preferences = Preferences() logging_level = logging.INFO if preferences.get_logging_config() is not None: set_logging_config(preferences.get_logging_config()) logging_level = get_logging_level() preferences.set_logging_config(build_logging_config(logging_level)) preferences.save() set_logging_config(preferences.get_logging_config()) MainView(root) root.after(0, root.deiconify) root.mainloop()
def body(self, parent, options): preferences = Preferences() logging_config = preferences.get_logging_config() if logging_config is not None: set_logging_config(logging_config) uipreferences = UIPreferences() populate = uipreferences.get_populate_defaults(True) self._populateDefaults.set(1 if populate else 0) qiskitGroup = ttk.LabelFrame(parent, text='Qiskit Configuration', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) qiskitGroup.grid(padx=(7, 7), pady=6, row=0, column=0, sticky='nsew') self._qconfigview = QconfigView(qiskitGroup) defaultsGroup = ttk.LabelFrame(parent, text='Defaults', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) defaultsGroup.grid(padx=(7, 7), pady=6, row=1, column=0, sticky='nsw') defaultsGroup.columnconfigure(1, pad=7) self._checkButton = ttk.Checkbutton(defaultsGroup, text="Populate on file new/open", variable=self._populateDefaults) self._checkButton.grid(row=0, column=1, sticky='nsw') packagesGroup = ttk.LabelFrame(parent, text='Packages', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) packagesGroup.grid(padx=(7, 7), pady=6, row=2, column=0, sticky='nsw') packagesGroup.columnconfigure(1, pad=7) frame = ttk.Frame(packagesGroup) frame.grid(row=0, column=0, sticky='nsew') self._packagesPage = PackagesPage(frame, preferences) self._packagesPage.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.TRUE) self._packagesPage.show_add_button(True) self._packagesPage.show_remove_button( self._packagesPage.has_selection()) self._packagesPage.show_defaults_button(False) loggingGroup = ttk.LabelFrame(parent, text='Logging Configuration', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) loggingGroup.grid(padx=(7, 7), pady=6, row=3, column=0, sticky='nsw') loggingGroup.columnconfigure(1, pad=7) loglevel = get_logging_level() ttk.Label(loggingGroup, text="Level:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, sticky='nsew') self._levelCombo = ttk.Combobox( loggingGroup, exportselection=0, state='readonly', values=list(PreferencesDialog._LOG_LEVELS.values())) index = list(PreferencesDialog._LOG_LEVELS.keys()).index(loglevel) self._levelCombo.current(index) self._levelCombo.grid(row=0, column=1, sticky='nsw') self.entry = self._qconfigview.initial_focus return self.entry # initial focus
class PackagesPage(ToolbarView): def __init__(self, parent, preferences, **options): super(PackagesPage, self).__init__(parent, **options) size = font.nametofont('TkHeadingFont').actual('size') ttk.Style().configure("PackagesPage.Treeview.Heading", font=(None, size, 'bold')) self._tree = ttk.Treeview(self, style='PackagesPage.Treeview', selectmode=tk.BROWSE, height=4, columns=['value']) self._tree.heading('#0', text='Type') self._tree.heading('value', text='Name') self._tree.column('#0', minwidth=0, width=150, stretch=tk.NO) self._tree.column('value', minwidth=0, width=500, stretch=tk.YES) self._tree.bind('<<TreeviewSelect>>', self._on_tree_select) self._tree.bind('<Button-1>', self._on_tree_edit) self.init_widgets(self._tree) self._preferences = Preferences() self._popup_widget = None self.pack(fill=tk.BOTH, expand=tk.TRUE) self.populate() self.initial_focus = self._tree def clear(self): if self._popup_widget is not None and self._popup_widget.winfo_exists( ): self._popup_widget.destroy() self._popup_widget = None for i in self._tree.get_children(): self._tree.delete([i]) def populate(self): self.clear() packages = self._preferences.get_packages( Preferences.PACKAGE_TYPE_DRIVERS, []) for package in packages: self._populate(Preferences.PACKAGE_TYPE_DRIVERS, package) packages = self._preferences.get_packages( Preferences.PACKAGE_TYPE_CHEMISTRY, []) for package in packages: self._populate(Preferences.PACKAGE_TYPE_CHEMISTRY, package) def _populate(self, package_type, package): package_type = '' if type is None else str(package_type) package_type = package_type.replace('\r', '\\r').replace('\n', '\\n') package = '' if package is None else str(package) package = package.replace('\r', '\\r').replace('\n', '\\n') self._tree.insert('', tk.END, text=package_type, values=[package]) def has_selection(self): return self._tree.selection() def _on_tree_select(self, event): for item in self._tree.selection(): self.show_remove_button(True) return def _on_tree_edit(self, event): rowid = self._tree.identify_row(event.y) if not rowid: return column = self._tree.identify_column(event.x) if column == '#1': x, y, width, height = self._tree.bbox(rowid, column) pady = height // 2 item = self._tree.identify("item", event.x, event.y) package_type = self._tree.item(item, "text") package = self._tree.item(item, 'values')[0] self._popup_widget = PackagePopup(self, package_type, self._tree, package, state=tk.NORMAL) self._popup_widget.selectAll() self._popup_widget.place(x=x, y=y + pady, anchor=tk.W, width=width) def onadd(self): dialog = PackageComboDialog(self.master, self) dialog.do_init(tk.LEFT) dialog.do_modal() if dialog.result is not None and self._preferences.add_package( dialog.result[0], dialog.result[1]): self.populate() self.show_remove_button(self.has_selection()) def onremove(self): for item in self._tree.selection(): package_type = self._tree.item(item, 'text') package = self._tree.item(item, 'values')[0] if self._preferences.remove_package(package_type, package): self.populate() self.show_remove_button(self.has_selection()) break def on_package_set(self, package_type, old_package, new_package): new_package = new_package.strip() if len(new_package) == 0: return False if self._preferences.change_package(package_type, old_package, new_package): self.populate() self.show_remove_button(self.has_selection()) return True return False def is_valid(self): return True def validate(self): return True def apply(self, preferences): changed = False packages = self._preferences.get_packages( Preferences.PACKAGE_TYPE_DRIVERS, []) if packages != preferences.get_packages( Preferences.PACKAGE_TYPE_DRIVERS, []): preferences.set_packages(Preferences.PACKAGE_TYPE_DRIVERS, packages) changed = True packages = self._preferences.get_packages( Preferences.PACKAGE_TYPE_CHEMISTRY, []) if packages != preferences.get_packages( Preferences.PACKAGE_TYPE_CHEMISTRY, []): preferences.set_packages(Preferences.PACKAGE_TYPE_CHEMISTRY, packages) changed = True if changed: preferences.save() refresh_operators() configuration_mgr = ConfigurationManager() configuration_mgr.refresh_drivers()
def _create_pane(self): label_font = font.nametofont('TkHeadingFont').copy() label_font.configure(size=12, weight='bold') ttk.Style().configure('TLabel',borderwidth=1,relief='solid') style = ttk.Style() style.configure('Title.TLabel', borderwidth=0, anchor=tk.CENTER) label = ttk.Label(self, style='Title.TLabel', padding=(5,5,5,5), textvariable=self._controller._title) label['font'] = label_font label.pack(side=tk.TOP, expand=tk.NO, fill=tk.X) main_pane = ttk.PanedWindow(self,orient=tk.VERTICAL) main_pane.pack(expand=tk.YES, fill=tk.BOTH) top_pane = ttk.PanedWindow(main_pane, orient=tk.HORIZONTAL) top_pane.pack(expand=tk.YES, fill=tk.BOTH) main_pane.add(top_pane) self._controller._sectionsView = SectionsView(self._controller,top_pane) self._controller._sectionsView.pack(expand=tk.YES, fill=tk.BOTH) top_pane.add(self._controller._sectionsView,weight=1) main_container = tk.Frame(top_pane) main_container.pack(expand=tk.YES, fill=tk.BOTH) style = ttk.Style() style.configure('PropViewTitle.TLabel', borderwidth=1, relief=tk.RIDGE, anchor=tk.CENTER) label = ttk.Label(main_container, style='PropViewTitle.TLabel', padding=(5,5,5,5), textvariable=self._controller._sectionView_title) label['font'] = label_font label.pack(side=tk.TOP, expand=tk.NO, fill=tk.X) container = tk.Frame(main_container) container.pack(side=tk.BOTTOM, expand=tk.YES, fill=tk.BOTH) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self._controller._emptyView = EmptyView(container) self._controller._emptyView.grid(row=0,column=0,sticky='nsew') self._controller._textView = SectionTextView(self._controller,container) self._controller._textView.grid(row=0,column=0,sticky='nsew') self._controller._propertiesView = SectionPropertiesView(self._controller,container) self._controller._propertiesView.grid(row=0,column=0,sticky='nsew') self._controller._emptyView.tkraise() top_pane.add(main_container,weight=1) self._controller._outputView = ThreadSafeOutputView(main_pane) self._controller._outputView.pack(expand=tk.YES, fill=tk.BOTH) main_pane.add(self._controller._outputView) # redirect output sys.stdout = self._controller._outputView sys.stderr = self._controller._outputView # reupdate logging after redirect preferences = Preferences() config = preferences.get_logging_config() if config is not None: set_logging_config(config) self.update_idletasks() self._controller._sectionsView.show_add_button(False) self._controller._sectionsView.show_remove_button(False) self._controller._sectionsView.show_defaults_button(False) self._controller._emptyView.set_toolbar_size(self._controller._sectionsView.get_toolbar_size())