def __init__(self, main_file=None, parent=None, id=wx.ID_ANY, title=u"Maple IDE", pos=(50,50), size=(640,700), style=wx.DEFAULT_FRAME_STYLE, name=u"Maple IDE"): basename = os.path.basename(main_file) if main_file else None title = u'Maple IDE {0}'.format(settings.IDE_VERSION) if basename: title += u' | ' + basename wx.Frame.__init__(self, parent, id, title, pos, size, style, name) self._pages = {} # {basename: CPPStyledTextCtrl} self.modified = False # are there unsaved changes? self.__mgr = wx.aui.AuiManager(self) self.menu_bar = self._make_menu_bar() self._make_toolbar() self.CreateStatusBar() # tabbed view of current sketch # FIXME disallow closing of tabs -- might have to switch AUI # implementation self.nb = BetterAuiNotebook(self) # was trying this + SetCloseButton, but !@#$, it doesn't exist # style=wx.aui.AUI_NB_TOP | \ # wx.aui.AUI_NB_TAB_MOVE | \ # wx.aui.AUI_NB_CLOSE_ON_ALL_TABS) # subprocess (compiler/uploader) output goes here self.sub = wx.TextCtrl(self, -1, u'', wx.DefaultPosition, wx.Size(200, 150), wx.NO_BORDER | wx.TE_MULTILINE | wx.TE_READONLY) # add the panes to the manager self.__mgr.AddPane(self.nb, wx.CENTER) self.__mgr.AddPane(self.sub, wx.aui.AuiPaneInfo(). Bottom(). CaptionVisible(True). CloseButton(False). Floatable(False)) self.__mgr.Update() self.Bind(wx.EVT_CLOSE, self.OnClose) # this *must* be done after GUI setup if main_file is None: self.sketch = SB.fresh_sketch(self) else: self.sketch = Sketch(self, main_file) # the sketch setter calls CPPStyledTextCtrl.SetText, but we aren't # really modified yet, and we don't want the user to be able to # 'undo' the changes _we_ made by sticking the file contents into # our notebook's pages. self.not_really_modified() self.nb.active_page = self._pages[self.sketch.main_basename]
def OnOpenSketchToolbar(self, evt): if self._query_user_save() == ABORT: return path = self._query_user_sketch() if path is None: return self.sketch = Sketch(self, path) self.SetTitle(u'MapleIDE | ' + self.sketch.name) self.not_really_modified() self.nb.active_page = self._pages[self.sketch.main_basename]
class SketchFrame(wx.Frame, UserInterface): """wx.Frame for showing a sketch. If main_file is None, will make a new, unsaved sketch. Verify/etc. buttons, tabbed view of the files in the sketch, sketch status, window for compiler output, and last compile's exit status; i.e., it's mostly a rehash of the usual Wiring/Arduino interface. """ def __init__(self, main_file=None, parent=None, id=wx.ID_ANY, title=u"Maple IDE", pos=(50,50), size=(640,700), style=wx.DEFAULT_FRAME_STYLE, name=u"Maple IDE"): basename = os.path.basename(main_file) if main_file else None title = u'Maple IDE {0}'.format(settings.IDE_VERSION) if basename: title += u' | ' + basename wx.Frame.__init__(self, parent, id, title, pos, size, style, name) self._pages = {} # {basename: CPPStyledTextCtrl} self.modified = False # are there unsaved changes? self.__mgr = wx.aui.AuiManager(self) self.menu_bar = self._make_menu_bar() self._make_toolbar() self.CreateStatusBar() # tabbed view of current sketch # FIXME disallow closing of tabs -- might have to switch AUI # implementation self.nb = BetterAuiNotebook(self) # was trying this + SetCloseButton, but !@#$, it doesn't exist # style=wx.aui.AUI_NB_TOP | \ # wx.aui.AUI_NB_TAB_MOVE | \ # wx.aui.AUI_NB_CLOSE_ON_ALL_TABS) # subprocess (compiler/uploader) output goes here self.sub = wx.TextCtrl(self, -1, u'', wx.DefaultPosition, wx.Size(200, 150), wx.NO_BORDER | wx.TE_MULTILINE | wx.TE_READONLY) # add the panes to the manager self.__mgr.AddPane(self.nb, wx.CENTER) self.__mgr.AddPane(self.sub, wx.aui.AuiPaneInfo(). Bottom(). CaptionVisible(True). CloseButton(False). Floatable(False)) self.__mgr.Update() self.Bind(wx.EVT_CLOSE, self.OnClose) # this *must* be done after GUI setup if main_file is None: self.sketch = SB.fresh_sketch(self) else: self.sketch = Sketch(self, main_file) # the sketch setter calls CPPStyledTextCtrl.SetText, but we aren't # really modified yet, and we don't want the user to be able to # 'undo' the changes _we_ made by sticking the file contents into # our notebook's pages. self.not_really_modified() self.nb.active_page = self._pages[self.sketch.main_basename] #--------------------------- Menu bar creation ---------------------------# def _make_menu_bar(self): menu_bar = wx.MenuBar() ## convenience functions so we don't have to repeat ourselves x1e6 def bind_handler(menu, text, handler): item = menu.Append(-1, text) self.Bind(wx.EVT_MENU, handler, item) def make_bind(menu): def handler(txt, handler): bind_handler(menu, txt, handler) return handler ## File menu file_menu = wx.Menu() bind = make_bind(file_menu) # sketchbook submenu/bindings sketchbook_menu = wx.Menu() sketches = SB.sketch_dirs() open_sketch = lambda s: \ lambda evt: self.OpenSketchNewFrame(SB.sketch_dir_get_abs(s)) sketchbook_bind = make_bind(sketchbook_menu) for s in sketches: sketchbook_bind(s, open_sketch(s)) # examples submenu/bindings examples_menu = wx.Menu() categories = EB.categories() open_example = lambda c,e: \ lambda evt: self.OpenSketchNewFrame(EB.example_get_abs(c,e)) for c in categories: cat_menu = wx.Menu() cat_bind = make_bind(cat_menu) for e in EB.category_examples(c): cat_bind(e, open_example(c, e)) examples_menu.AppendMenu(-1, c, cat_menu) # file menu/bindings bind(u"&New\tCtrl-N", self.OnNewSketch) bind(u"&Open...\tCtrl-O", self.OnOpenSketch) file_menu.AppendMenu(-1, u"Sketchbook", sketchbook_menu) file_menu.AppendMenu(-1, u"Examples", examples_menu) bind(u"Close\tCTRL-W", self.OnClose) bind(u"&Save\tCTRL-S", self.OnSave) bind(u"S&ave As...\tSHIFT-CTRL-S", self.OnSaveAs) bind(u"&Upload to I/O Board\tCTRL-U", self.OnUpload) file_menu.AppendSeparator() bind(u"&Page Setup\tSHIFT-CTRL-P", self.OnPageSetup) bind(u"Print\tCTRL-P", self.OnPrint) file_menu.AppendSeparator() prefs_item = file_menu.Append(wx.ID_PREFERENCES, "&Preferences\tCTRL-,") self.Bind(wx.EVT_MENU, self.OnPreferences, prefs_item) menu_bar.Append(file_menu, "&File") ## Edit menu edit_menu = wx.Menu() bind = make_bind(edit_menu) bind(u"&Undo\tCTRL-Z", self.OnUndo) bind(u"&Redo\tCTRL-Y", self.OnRedo) edit_menu.AppendSeparator() bind(u"&Cut\tCtrl-X", self.OnCut) bind(u"C&opy\tCTRL-C", self.OnCopy) bind(u"Copy for &Forum\tSHIFT-CTRL-C", self.OnCopyForForum) bind(u"Copy as &HTML\tALT-CTRL-C", self.OnCopyAsHTML) bind(u"P&aste\tCTRL-V", self.OnPaste) bind(u"Select &All\tCTRL-A", self.OnSelectAll) edit_menu.AppendSeparator() bind(u"Co&mment/Uncomment\tCTRL-/", self.OnCommentUncomment) bind(u"&Increase Indent\tCTRL-]", self.OnIncreaseIndent) bind(u"&Decrease Indent\tCTRL-[", self.OnDecreaseIndent) edit_menu.AppendSeparator() bind(u"&Find\tCTRL-F", self.OnFind) bind(u"Find &Next\tCTRL-G", self.OnFindNext) menu_bar.Append(edit_menu, u"&Edit") ## Sketch menu sketch_menu = wx.Menu() bind = make_bind(sketch_menu) bind(u"&Verify / Compile\tCTRL-R", self.OnVerify) bind(u"&Stop", self.OnStop) sketch_menu.AppendSeparator() bind(u"S&how Sketch Folder\tCTRL-K", self.OnShowSketchFolder) bind(u"IMPORT LIBRARY SUBMENU", self.OnImportLibrary) bind(u"&Add File...", self.OnAddFile) menu_bar.Append(sketch_menu, u"&Sketch") ## Tools menu tools_menu = wx.Menu() bind = make_bind(tools_menu) bind(u"&Auto Format\tCTRL-T", self.OnAutoFormat) bind(u"A&rchive Sketch", self.OnArchiveSketch) bind(u"&Fix Encoding and Reload", self.OnFixEncodingAndReload) tools_menu.AppendSeparator() bind(u"&Serial Monitor\tSHIFT-CTRL-M", self.OnSerialMonitor) tools_menu.AppendSeparator() # boards submenu boards_menu = wx.Menu() boards = PREF_CONFIG['board'].data['values'] memory_targets = PREF_CONFIG['memory_target'].data['values'] boards_values = [(board, memory_target) for \ board in boards for \ memory_target in memory_targets] self.board_item_to_config = {} preferred = (preference('board'), preference('memory_target')) for board, target in boards_values: description = u'Leaflabs {0} to {1}'.format(board, target) item = boards_menu.AppendRadioItem(wx.ID_ANY, description) self.board_item_to_config[item] = (board, target) if (board, target) == preferred: item.Check() tools_menu.AppendMenu(wx.ID_ANY, u'Board', boards_menu) bind(u"SERIAL PORT SUBMENU", self.OnSerialPortSubmenu) # TODO tools_menu.AppendSeparator() bind(u"BURN_BOOTLOADER", self.OnBurnBootloader) # TODO menu_bar.Append(tools_menu, u"&Tools") ## Help menu help_menu = wx.Menu() bind = make_bind(help_menu) bind(u"Getting Started", self.OnGettingStarted) bind(u"Development Environment", self.OnDevelopmentEnvironment) bind(u"Troubleshooting", self.OnTroubleshooting) bind(u"Language Reference", self.OnLanguageReference) bind(u"Visit Arduino.cc", self.OnVisitArduino) bind(u"Visit LeafLabs.com", self.OnVisitLeafLabs) menu_bar.Append(help_menu, u"&Help") self.SetMenuBar(menu_bar) return menu_bar #---------------------------- Toolbar creation ---------------------------# def _make_toolbar(self): self.toolbar = self.CreateToolBar() bitmaps = resources.desprite_bitmap(resources.TOOLBAR_BUTTONS, 7) tooltips = [u'Verify', u'Stop', u'New', u'Open', u'Save', u'Upload', u'Serial Monitor'] verify, stop, new, open_, save, upload, serial = zip(bitmaps, tooltips) self._add_to_toolbar(verify, self.OnVerify) self._add_to_toolbar(stop, self.OnStop) self._add_to_toolbar(new, self.OnNewSketchToolbar) self._add_to_toolbar(open_, self.OnOpenSketchToolbar) self._add_to_toolbar(save, self.OnSave) self._add_to_toolbar(upload, self.OnUpload) self._add_to_toolbar(serial, self.OnSerialMonitor) self.toolbar.Realize() def _add_to_toolbar(self, bmp_tip, click_handler): bitmap, tooltip = bmp_tip tool_id = wx.NewId() self.toolbar.SetToolBitmapSize(bitmap.GetSize()) self.toolbar.AddTool(tool_id, bitmap, shortHelpString=tooltip) self.Bind(wx.EVT_TOOL, click_handler, id=tool_id) return tool_id #------------------------------- Properties ------------------------------# def __get_sketch(self): return self.__sketch def __set_sketch(self, sketch): self.__sketch = sketch self.redisplay(reset=True) sketch = property(fget=__get_sketch, fset=__set_sketch) #----------------------- File Menu event handlers ------------------------# def OnNewSketch(self, evt): sf = SketchFrame(pos=self.child_position()) sf.Show(True) def OnOpenSketch(self, evt): path = self._query_user_sketch() if path is None: return self.OpenSketchNewFrame(path) def OnClose(self, evt): if self._query_user_save(u'closing') == ABORT: return self.sketch.cleanup() self.__mgr.UnInit() self.Destroy() def OnSave(self, evt): if not self.save(): self.OnSaveAs(evt) def OnSaveAs(self, evt): default_file = self.sketch.name or "" path = wx.FileSelector(u"Save Maple sketch as...", default_path=preference('sketchbook'), default_filename=default_file, flags=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT, parent=self) if path == u"": return # user hit cancel self.sketch.reset_directory(path, create=True) self.save() def OnUpload(self, evt): if self._query_user_save(u'uploading') == ABORT: return self.sketch.upload() def OnPageSetup(self, evt): # TODO not_implemented_popup() def OnPrint(self, evt): # TODO not_implemented_popup() def OnPreferences(self, evt): f = PreferencesFrame.get_instance() f.Raise() f.Show(True) #----------------------- Edit Menu event handlers ------------------------# def OnUndo(self, evt): self.nb.active_page.Undo() def OnRedo(self, evt): self.nb.active_page.Redo() def OnCut(self, evt): self.nb.active_page.Cut() def OnCopy(self, evt): self.nb.active_page.Copy() def OnCopyForForum(self, evt): # TODO not_implemented_popup() def OnCopyAsHTML(self, evt): # TODO (use pygments!) not_implemented_popup() def OnPaste(self, evt): self.nb.active_page.Paste() def OnSelectAll(self, evt): self.nb.active_page.SelectAll() def OnCommentUncomment(self, evt): self.nb.active_page.ToggleCommentUncomment() def OnIncreaseIndent(self, evt): self.nb.active_page.IncreaseIndent() def OnDecreaseIndent(self, evt): self.nb.active_page.DecreaseIndent() def OnFind(self, evt): # TODO not_implemented_popup() def OnFindNext(self, evt): # TODO not_implemented_popup() #---------------------- Sketch Menu event handlers -----------------------# def OnVerify(self, evt): if self._query_user_save() == ABORT: return for item, config in self.board_item_to_config.iteritems(): if item.IsChecked(): board, target = config break else: # can't happen, but just to be sure error_popup(u'Please select a board', u'You must select a board and memory target from ' u'the Tools > Board submenu before the Sketch ' u'can be compiled.') return # convert 'Maple Mini' to 'maple_mini', etc. board = board.lower().replace(' ', '_') target = target.lower() # the sketch will call back any results to display via # SketchFrame's UserInterface methods self.sketch.compile(board, target) def OnStop(self, evt): self.sketch.stop_compiling() def OnShowSketchFolder(self, evt): # TODO not_implemented_popup() def OnImportLibrary(self, evt): # TODO not_implemented_popup() def OnAddFile(self, evt): # TODO not_implemented_popup() #----------------------- Tools Menu event handlers -----------------------# def OnAutoFormat(self, evt): # TODO not_implemented_popup() def OnArchiveSketch(self, evt): default_file = SB.fresh_sketch_archive(self.sketch) path = wx.FileSelector(u'Archive Sketch as:', default_path=preference('sketchbook'), default_filename=default_file, flags=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, parent=self) if path == u'': return #user hit cancel self.sketch.archive(path) def OnFixEncodingAndReload(self, evt): # TODO not_implemented_popup() def OnSerialMonitor(self, evt): # TODO not_implemented_popup() def OnBoardsSubmenu(self, evt): # TODO not_implemented_popup() def OnSerialPortSubmenu(self, evt): # TODO not_implemented_popup() def OnBurnBootloader(self, evt): # TODO not_implemented_popup() #----------------------- Help menu event handlers ------------------------# def _open_url(self, url): # unspecified behavior when it's a file:// url, but it seems to work webbrowser.open_new_tab(url) def OnGettingStarted(self, evt): self._open_url(u'file://' + resources.ref_doc(u'quickstart.html')) def OnDevelopmentEnvironment(self, evt): self._open_url(u'file://' + resources.ref_doc(u'index.html')) def OnTroubleshooting(self, evt): self._open_url(u'file://' + \ resources.ref_doc(u'troubleshooting.html')) def OnLanguageReference(self, evt): self._open_url(u'file://' + resources.ref_doc(u'language.html')) def OnVisitArduino(self, evt): self._open_url(u'http://arduino.cc') def OnVisitLeafLabs(self, evt): self._open_url(u'http://leaflabs.com') #------------------------ Toolbar event handlers -------------------------# ## mostly don't need to be written. all but these are taken care of ## by existing menu bar handlers. def OnNewSketchToolbar(self, evt): if self._query_user_save() == ABORT: return self.sketch = SB.fresh_sketch(self) def OnOpenSketchToolbar(self, evt): if self._query_user_save() == ABORT: return path = self._query_user_sketch() if path is None: return self.sketch = Sketch(self, path) self.SetTitle(u'MapleIDE | ' + self.sketch.name) self.not_really_modified() self.nb.active_page = self._pages[self.sketch.main_basename] #------------------------ UserInterface methods --------------------------# def show_warning(self, message, details): warning_popup(message, details) def show_error(self, message, details): error_popup(message, details) def show_notice(self, message, details): notice_popup(message, details) def redisplay(self, reset=False): """Redisplays. If reset=True, wipes all the tabs and assumes that self.sketch is right about everything. Must be called after the sketch is set.""" if self.sketch is None: raise RuntimeError("no sketch set") self.clear_subprocess_window() if reset: self._clear_tabs() # populate the notebook with pages from the sketch for basename, code in self.sketch.sources.iteritems(): page = self.make_new_tab(basename) page.SetText(code.code) self._pages[basename] = page page_count = self.nb.GetPageCount() for basename, code in self.sketch.sources.iteritems(): page = self._pages[basename] # add the page to the notebook if it doesn't exist, but we # really shouldn't have todo this. (right now, this can only # happen if a user closes a tab, which mbolivar is trying to # disallow but hasn't found out how, yet). page_idx = self.nb.GetPageIndex(page) if page_idx == wx.NOT_FOUND: # TODO better error logging print u'WARNING: unknown basename', basename page = self.make_new_tab(basename) page_count -= 1 page.SetText(code.code) if page_count != 0: # TODO better error logging print u'WARNING: page_count=%d, should be 0' % page_count def clear_subprocess_window(self): self.SetStatusText(u'') self.SetCaptionText(u'') self.sub.Clear() def append_subprocess_output(self, line): # TODO parse compiler error lines into clickable links self.sub.AppendText(line) def set_status(self, status, status_type): cap, low = status_type.capitalize(), status_type.lower() self.SetStatusText(u"%s exit status: %d" % (cap, status)) if status == STATUS_SUCCESS: self.SetCaptionText(u'Finished with %s.' % low) else: self.SetCaptionText(u'%s was unsuccessful.' % cap) #------------------------- Other event handlers --------------------------# def OnTextChanged(self, evt): self.modified = True #--------------------- Unorganized auxiliary methods ---------------------# def make_new_tab(self, basename): page = CPPStyledTextCtrl(self.nb) self.Bind(wx.stc.EVT_STC_CHANGE, self.OnTextChanged, page) self.nb.AddPage(page, basename) return page def _clear_tabs(self): self._pages = {} for i in xrange(self.nb.GetPageCount()): # it's awesome that i have to do both of these, and that it's # undocumented. awesome. self.nb.RemovePage(i) self.nb.DeletePage(i) assert self.nb.GetPageCount() == 0 def SetCaptionText(self, text): # just stashing comp_info in __init__ won't work; i think # AuiManager makes a fresh AuiPaneInfo for any window you give it info = self.__mgr.GetPane(self.sub) info.Caption(text) self.__mgr.Update() def OpenSketchNewFrame(self, sketch_file): """Requires an absolute path to either a sketch main file, or a directory containing at least one such file. """ if os.path.isdir(sketch_file): sketch_file = SB.sketch_main_file(sketch_file) if sketch_file is None: error_popup(u"Empty Sketch Directory", u"Directory:\n\t%s\ncontains no %s files." % EXN) return new_frame = SketchFrame(sketch_file, pos=self.child_position()) new_frame.Show(True) def _sync_sketch(self): mine, sketch = set(self._pages.keys()), set(self.sketch.sources.keys()) if mine != sketch: # can't happen raise MalformedSketchError(str(mine) + u' vs. ' + str(sketch)) for basename, page in self._pages.iteritems(): self.sketch.replace_source(basename, page.GetText()) def save(self, message_on_error=True): self._sync_sketch() if self.sketch.save(message_on_error): SB.mark_saved(self.sketch) self.modified = False return True return False def _query_user_save(self, operation='continuing'): # use before operations that the user probably wants to have # saved before doing (compiling a sketch, closing a window, # etc.) returns CONTINUE if you should go ahead and do it # (user said don't save/user said save and it worked), returns # ABORT otherwise. if not self.modified: return CONTINUE result = save_prompt_popup(u"There are unsaved changes", u"Save changes before %s?" % operation) if result == wx.ID_CANCEL: return ABORT elif result == wx.ID_YES: if self.save(): return CONTINUE else: return ABORT # something went wrong saving elif result == wx.ID_NO: return CONTINUE else: raise ValueError(result) def _query_user_sketch(self): dialog = wx.FileDialog(None, u"Open a Maple sketch...", defaultDir=preference('sketchbook'), wildcard=u"*" + EXN, style=wx.FD_FILE_MUST_EXIST | wx.FD_OPEN) if dialog.ShowModal() != wx.ID_OK: return None path = dialog.GetPath() dialog.Destroy() return path def not_really_modified(self): self.modified = False for page in self.nb.pages: page.EmptyUndoBuffer() def child_position(self): # where should a new SketchFrame go so as not to fight with # this one? # TODO be smarter x,y = self.GetScreenPositionTuple() return x + 20, y + 20
def __init__(self, main_file=None, parent=None, id=wx.ID_ANY, title=u"Maple IDE", pos=(50,50), size=(640,700), style=wx.DEFAULT_FRAME_STYLE, name=u"Maple IDE"): basename = os.path.basename(main_file) if main_file else None title = u'Maple IDE | ' + basename if basename else u'Maple IDE' wx.Frame.__init__(self, parent, id, title, pos, size, style, name) self._pages = {} # {basename: CPPStyledTextCtrl} self.modified = False # are there unsaved changes? # this thing totally takes the pain out of panel layout self.__mgr = wx.aui.AuiManager(self) # menu bar self.menu_bar = self._make_menu_bar() self.SetMenuBar(self.menu_bar) # toolbar self._make_toolbar() # status bar self.CreateStatusBar() # tabbed view of current sketch # FIXME disallow closing of tabs -- might have to switch AUI # implementation self.nb = BetterAuiNotebook(self) # was trying this + SetCloseButton, but !@#$, it doesn't exist # style=wx.aui.AUI_NB_TOP | \ # wx.aui.AUI_NB_TAB_MOVE | \ # wx.aui.AUI_NB_CLOSE_ON_ALL_TABS) # print 'dir(self.nb):' # pprint(dir(self.nb)) # subprocess (compiler/uploader) output goes here self.sub = wx.TextCtrl(self, -1, u'', wx.DefaultPosition, wx.Size(200, 150), wx.NO_BORDER | wx.TE_MULTILINE) comp_info = wx.aui.AuiPaneInfo() comp_info.Bottom() comp_info.CaptionVisible(True) comp_info.CloseButton(False) comp_info.Floatable(False) # add the panes to the manager self.__mgr.AddPane(self.nb, wx.CENTER) self.__mgr.AddPane(self.sub, info=comp_info) self.__mgr.Update() # set frame close handler self.Bind(wx.EVT_CLOSE, self.OnClose) # All set up, so initialize the sketch. THIS MUST BE DONE AFTER # THE GUI GETS SET UP. if main_file is None: self.sketch = SB.fresh_sketch(self) else: self.sketch = Sketch(self, main_file) # the sketch setter calls CPPStyledTextCtrl.SetText, but we aren't # really modified yet, and we don't want the user to be able to # 'undo' the changes _we_ made by sticking the file contents into # our notebook's pages. self.not_really_modified() self.nb.active_page = self._pages[self.sketch.main_basename]
# Author: Jayendra Matarage # Title: Sketch to Code from os import path from Sketch import Sketch from Template import Template from View import View from File import File import cv2 image_process = Sketch() generate_template = Template() view = View() file = File() if __name__ == '__main__': print("|-------------------------------------------------------|") print("|\t\t\t\t\tShapes to </>code\t\t\t\t\t|") print("|-------------------------------------------------------|") image_file = input("|\tEnter image file name : ") while True: if image_file == "": image_file = input("|\tPlease enter image file name : ") else: if path.exists(image_file) != True: image_file = input("|\tFile not found or incorrect file name : ") else: break print("|-------------------------------------------------------|")
if __name__ == "__main__": parser = argparse.ArgumentParser(description='Compare documents') parser.add_argument('k', type=int, help='k as in kgrams') parser.add_argument('d', type=int, help='dimension : how fine-grained is your reality?') parser.add_argument('files', nargs='+', help='files to compare') args = parser.parse_args() k = args.k d = args.d files = args.files print(' ', end=' ') for f2 in files: fn = os.path.splitext(f2)[0] fn = os.path.basename(fn) print(fn[:4], end=' ') print() for f1 in files: fn = os.path.splitext(f1)[0] fn = os.path.basename(fn) print(fn[:4], end=' ') with open(f1) as f1_: txt1 = f1_.read() for f2 in files: with open(f2) as f2_: txt2 = f2_.read() sim = Sketch(txt1, k, d).similar_to(Sketch(txt2, k, d)) print('%3.2f' % sim, end=' ') print()