def on_open_example(self, action): example_dir = ide_utils.get_example_dir() filename = os.path.join(example_dir, action.get_name()[len('ShoebotOpenExample'):].strip()) drive, directory = os.path.splitdrive(os.path.abspath(os.path.normpath(filename))) uri = "file:///%s%s" % (drive, directory.replace(os.sep, '/')) self.window.create_tab_from_uri(uri, gedit.encoding_get_current(), 0, False, # Do not create a new file True) # Switch to tab
def on_open_example(self, action, user_data): b64_path = action.get_name()[len("open_example__") :].encode("UTF-8") example_dir = ide_utils.get_example_dir() rel_path = base64.b64decode(b64_path).decode("UTF-8") path = os.path.join(example_dir, rel_path) drive, directory = os.path.splitdrive(os.path.abspath(os.path.normpath(path))) uri = "file://%s%s" % (drive, directory) gio_file = Gio.file_new_for_uri(uri) self.window.create_tab_from_location( gio_file, None, 0, 0, False, True # encoding # column # Do not create an empty file ) # Switch to the tab
def on_open_example(self, action): example_dir = ide_utils.get_example_dir() filename = os.path.join(example_dir, action.get_name()[len('ShoebotOpenExample'):].strip()) drive, directory = os.path.splitdrive(os.path.abspath(os.path.normpath(filename))) uri = "file:///%s%s" % (drive, directory) gio_file = Gio.file_new_for_uri(uri) self.window.create_tab_from_location( gio_file, None, # encoding 0, 0, # column False, # Do not create an empty file True) # Switch to the tab
def on_open_example(self, action): example_dir = ide_utils.get_example_dir() filename = os.path.join( example_dir, action.get_name()[len('ShoebotOpenExample'):].strip()) drive, directory = os.path.splitdrive( os.path.abspath(os.path.normpath(filename))) uri = "file:///%s%s" % (drive, directory.replace(os.sep, '/')) self.window.create_tab_from_uri( uri, gedit.encoding_get_current(), 0, False, # Do not create a new file True) # Switch to tab
def on_open_example(self, action): example_dir = ide_utils.get_example_dir() filename = os.path.join( example_dir, action.get_name()[len('ShoebotOpenExample'):].strip()) drive, directory = os.path.splitdrive( os.path.abspath(os.path.normpath(filename))) uri = "file:///%s%s" % (drive, directory) gio_file = Gio.file_new_for_uri(uri) self.window.create_tab_from_location( gio_file, None, # encoding 0, 0, # column False, # Do not create an empty file True) # Switch to the tab
def on_open_example(self, action, user_data): b64_path = action.get_name()[len('open_example__'):].encode("UTF-8") example_dir = ide_utils.get_example_dir() rel_path = base64.b64decode(b64_path).decode("UTF-8") path = os.path.join(example_dir, rel_path) drive, directory = os.path.splitdrive(os.path.abspath(os.path.normpath(path))) uri = "file://%s%s" % (drive, directory) gio_file = Gio.file_new_for_uri(uri) self.window.create_tab_from_location( gio_file, None, # encoding 0, 0, # column False, # Do not create an empty file True) # Switch to the tab
def examples_menu(root_dir=None, depth=0): """ :return: xml for menu, [(bot_action, label), ...], [(menu_action, label), ...] """ # pre 3.12 menus examples_dir = ide_utils.get_example_dir() if not examples_dir: return "", [], [] root_dir = root_dir or examples_dir file_tmpl = '<menuitem name="{name}" action="{action}"/>' dir_tmpl = '<menu name="{name}" action="{action}">{menu}</menu>' file_actions = [] submenu_actions = [] xml = "" for fn in sorted(os.listdir(root_dir)): path = os.path.join(root_dir, fn) rel_path = path[len(examples_dir):] if os.path.isdir(path): action = "ShoebotExampleMenu {0}".format(rel_path) label = fn.capitalize() sm_xml, sm_file_actions, sm_menu_actions = examples_menu( os.path.join(root_dir, fn), depth + 1) submenu_actions.extend(sm_menu_actions) file_actions.extend(sm_file_actions) submenu_actions.append((action, label)) xml += dir_tmpl.format(name=fn, action=action, menu=sm_xml) elif os.path.splitext(path)[1] in [".bot", ".py" ] and not fn.startswith("_"): action = "ShoebotExampleOpen {0}".format(rel_path) label = ide_utils.make_readable_filename(fn) xml += file_tmpl.format(name=fn, action=action) file_actions.append((action, label)) return xml, file_actions, submenu_actions
def examples_menu(root_dir=None, depth=0): """ :return: xml for menu, [(bot_action, label), ...], [(menu_action, label), ...] """ # pre 3.12 menus examples_dir = ide_utils.get_example_dir() if not examples_dir: return "", [], [] root_dir = root_dir or examples_dir file_tmpl = '<menuitem name="{name}" action="{action}"/>' dir_tmpl = '<menu name="{name}" action="{action}">{menu}</menu>' file_actions = [] submenu_actions = [] xml = "" for fn in sorted(os.listdir(root_dir)): path = os.path.join(root_dir, fn) rel_path = path[len(examples_dir):] if os.path.isdir(path): action = 'ShoebotExampleMenu {0}'.format(rel_path) label = fn.capitalize() sm_xml, sm_file_actions, sm_menu_actions = examples_menu(os.path.join(root_dir, fn), depth+1) submenu_actions.extend(sm_menu_actions) file_actions.extend(sm_file_actions) submenu_actions.append((action, label)) xml += dir_tmpl.format(name=fn, action=action, menu=sm_xml) elif os.path.splitext(path)[1] in ['.bot', '.py'] and not fn.startswith('_'): action = 'ShoebotExampleOpen {0}'.format(rel_path) label = ide_utils.make_readable_filename(fn) xml += file_tmpl.format(name=fn, action=action) file_actions.append((action, label)) return xml, file_actions, submenu_actions
def mk_examples_menu(text, root_dir=None, depth=0): """ :return: base_item, rel_paths """ # 3.12+ menus examples_dir = ide_utils.get_example_dir() if not examples_dir: return None, [] root_dir = root_dir or examples_dir file_actions = [] menu = Gio.Menu.new() base_item = Gio.MenuItem.new_submenu(text, menu) for fn in sorted(os.listdir(root_dir)): path = os.path.join(root_dir, fn) rel_path = path[len(examples_dir):] if os.path.isdir(path): label = fn.capitalize() item, sm_file_actions = mk_examples_menu( label, os.path.join(root_dir, fn)) menu.append_item(item) file_actions.extend(sm_file_actions) elif os.path.splitext(path)[1] in [".bot", ".py" ] and not fn.startswith("_"): label = ide_utils.make_readable_filename(fn) # the only way I could work out to attach the data to the menu item is in the name :/ action_name = "win.open_example__%s" % encode_relpath(rel_path) menu.append(label, action_name) file_actions.append(rel_path) return base_item, file_actions
def mk_examples_menu(text, root_dir=None, depth=0): """ :return: base_item, rel_paths """ # 3.12+ menus examples_dir = ide_utils.get_example_dir() if not examples_dir: return None, [] root_dir = root_dir or examples_dir file_actions = [] menu = Gio.Menu.new() base_item = Gio.MenuItem.new_submenu(text, menu) for fn in sorted(os.listdir(root_dir)): path = os.path.join(root_dir, fn) rel_path = path[len(examples_dir):] if os.path.isdir(path): label = fn.capitalize() item, sm_file_actions = mk_examples_menu(label, os.path.join(root_dir, fn)) menu.append_item(item) file_actions.extend(sm_file_actions) elif os.path.splitext(path)[1] in ['.bot', '.py'] and not fn.startswith('_'): label = ide_utils.make_readable_filename(fn) # the only way I could work out to attach the data to the menu item is in the name :/ action_name = "win.open_example__%s" % encode_relpath(rel_path) menu.append(label, action_name) file_actions.append(rel_path) return base_item, file_actions