Example #1
0
    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find('dialog')).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find('dialog').find('unpack')
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify('info', _('Unpacked'))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info('[fm] unpacking %s' % self.item.fullpath)

        unpack_btn.on('click', lambda: unpack())

        # Edit
        edit_btn = self.find('dialog').find('edit')
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch('notepad', path=self.item.fullpath)

        edit_btn.on('click', lambda: edit())
Example #2
0
File: fm.py Project: mmihalev/kiosk
    def edit(self, path):
        self.find("dialog").visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find("dialog")).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find("dialog").find("unpack")
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify("info", _("Unpacked"))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info("[fm] unpacking %s" % self.item.fullpath)

        unpack_btn.on("click", lambda: unpack())

        # Edit
        edit_btn = self.find("dialog").find("edit")
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch("notepad", path=self.item.fullpath)

        edit_btn.on("click", lambda: edit())
Example #3
0
    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find('dialog')).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find('dialog').find('unpack')
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify('info', _('Unpacked'))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info('[fm] unpacking %s' % self.item.fullpath)

        unpack_btn.on('click', lambda: unpack())

        # Edit
        edit_btn = self.find('dialog').find('edit')
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch('notepad', path=self.item.fullpath)

        edit_btn.on('click', lambda: edit())
Example #4
0
File: fm.py Project: inone/ajenti
    def navigate(self, path):
        if not os.path.isdir(path):
            return
        self.path = path
        self.shortpath = os.path.split(path)[1] or '/'
        self.items = []
        for item in os.listdir(self.path):
            self.items.append(Item(os.path.join(self.path, item)))
        self.items = sorted(self.items, key=lambda x: (not x.isdir, x.name))

        self.breadcrumbs = []
        p = path
        while len(p) > 1:
            p = os.path.split(p)[0]
            self.breadcrumbs.insert(0, Breadcrumb(p))
Example #5
0
class FileManager (SectionPlugin):
    default_classconfig = {'root': '/'}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _('File Manager')
        self.category = _('Tools')
        self.icon = 'folder-open'

        self.backend = FMBackend().get()

        self.append(self.ui.inflate('fm:main'))
        self.controller = Controller()

        def post_item_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_item_click, object, item)
            ui.find('edit').on('click', self.edit, item.fullpath)
        self.find('items').post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_bc_click, object, item)
        self.find('breadcrumbs').post_item_bind = post_bc_bind

        self.clipboard = []
        self.tabs = self.find('tabs')
        self.find('dialog').buttons = [
            {'id': 'save', 'text': _('Save')},
            {'id': 'cancel', 'text': _('Cancel')},
        ]

    def on_first_page_load(self):
        self.controller.new_tab(self.classconfig['root'])
        self.binder = Binder(self.controller, self.find('filemanager')).autodiscover().populate()
        self.binder_c = Binder(self, self.find('bind-clipboard')).autodiscover().populate()


    def refresh_clipboard(self):
        self.binder_c.reset().autodiscover().populate()

    @on('tabs', 'switch')
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.controller.new_tab(self.classconfig['root'])
        self.refresh()

    @on('close', 'click')
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on('new-file', 'click')
    def on_new_file(self):
        open(os.path.join(self.controller.tabs[self.tabs.active].path, 'new file'), 'w').close()
        self.refresh()

    def upload(self, name, file):
        open(os.path.join(self.controller.tabs[self.tabs.active].path, name), 'w').write(file.read())
        self.refresh()

    @on('new-dir', 'click')
    def on_new_directory(self):
        os.mkdir(os.path.join(self.controller.tabs[self.tabs.active].path, 'new directory'))
        self.refresh()

    @on('mass-cut', 'click')
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = 'cut'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-copy', 'click')
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = 'copy'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-delete', 'click')
    def on_delete(self):
        self.backend.remove(self._get_checked())
        self.refresh()

    @on('paste', 'click')
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == 'cut':
                for_move.append(i)
            else:
                for_copy.append(i)

        try:
            if for_move:
                self.backend.move(for_move, tab.path)
            if for_copy:
                self.backend.copy(for_copy, tab.path)
            self.clipboard = []
        except Exception as e:
            self.context.notify('error', str(e))
        self.refresh_clipboard()
        self.refresh()

    @on('select-all', 'click')
    def on_select_all(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        for item in tab.items:
            item.checked = True
        self.binder.populate()
        self.context.notify('info', _('Selected %i items') % len(tab.items)) 

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on('clear-clipboard', 'click')
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig['root']):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find('dialog')).autodiscover().populate()

    @on('dialog', 'button')
    def on_close_dialog(self, button):
        self.find('dialog').visible = False
        if button == 'save':
            self.binder_d.update()
            self.item.write()
            self.refresh()

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig['root']):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()
Example #6
0
 def edit(self, path):
     self.find('dialog').visible = True
     self.item = Item(path)
     self.item.read()
     self.binder_d = Binder(self.item, self.find('dialog')).autodiscover().populate()
Example #7
0
class FileManager(SectionPlugin):
    default_classconfig = {'root': '/'}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _('File Manager')
        self.category = _('Tools')
        self.icon = 'folder-open'

        self.backend = FMBackend().get()

        self.append(self.ui.inflate('fm:main'))
        self.controller = Controller()

        def post_item_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_item_click, object, item)
            ui.find('edit').on('click', self.edit, item.fullpath)

        self.find('items').post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_bc_click, object, item)

        self.find('breadcrumbs').post_item_bind = post_bc_bind

        self.clipboard = []
        self.tabs = self.find('tabs')
        self.find('dialog').buttons = [
            {
                'id': 'save',
                'text': _('Save')
            },
            {
                'id': 'cancel',
                'text': _('Cancel')
            },
        ]

    def on_first_page_load(self):
        self.controller.new_tab(self.classconfig['root'])
        self.binder = Binder(
            self.controller,
            self.find('filemanager')).autodiscover().populate()
        self.binder_c = Binder(
            self, self.find('bind-clipboard')).autodiscover().populate()

    def refresh_clipboard(self):
        self.binder_c.reset().autodiscover().populate()

    @on('tabs', 'switch')
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.controller.new_tab(self.classconfig['root'])
        self.refresh()

    @on('close', 'click')
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on('new-file', 'click')
    def on_new_file(self):
        open(
            os.path.join(self.controller.tabs[self.tabs.active].path,
                         'new file'), 'w').close()
        self.refresh()

    def upload(self, name, file):
        open(os.path.join(self.controller.tabs[self.tabs.active].path, name),
             'w').write(file.read())
        self.refresh()

    @on('new-dir', 'click')
    def on_new_directory(self):
        os.mkdir(
            os.path.join(self.controller.tabs[self.tabs.active].path,
                         'new directory'))
        self.refresh()

    @on('mass-cut', 'click')
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = 'cut'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-copy', 'click')
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = 'copy'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-delete', 'click')
    def on_delete(self):
        self.backend.remove(self._get_checked())
        self.refresh()

    @on('paste', 'click')
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == 'cut':
                for_move.append(i)
            else:
                for_copy.append(i)

        if for_move:
            self.backend.move(for_move, tab.path)
        if for_copy:
            self.backend.copy(for_copy, tab.path)
        self.clipboard = []
        self.refresh_clipboard()
        self.refresh()

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on('clear-clipboard', 'click')
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig['root']):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item,
                               self.find('dialog')).autodiscover().populate()

    @on('dialog', 'button')
    def on_close_dialog(self, button):
        self.find('dialog').visible = False
        if button == 'save':
            self.binder_d.update()
            self.item.write()
            self.refresh()

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig['root']):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()
Example #8
0
 def edit(self, path):
     self.find('dialog').visible = True
     self.item = Item(path)
     self.item.read()
     self.binder_d = Binder(self.item,
                            self.find('dialog')).autodiscover().populate()
Example #9
0
File: fm.py Project: mmihalev/kiosk
class FileManager(SectionPlugin):
    default_classconfig = {"root": "/", "start": "/"}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _("Файлов мениджър")
        self.category = _("Tools")
        self.icon = "folder-open"

        self.backend = FMBackend().get()

        self.append(self.ui.inflate("fm:main"))
        self.controller = Controller()

        def post_item_bind(object, collection, item, ui):
            ui.find("name").on("click", self.on_item_click, object, item)
            ui.find("edit").on("click", self.edit, item.fullpath)

        self.find("items").post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find("name").on("click", self.on_bc_click, object, item)

        self.find("breadcrumbs").post_item_bind = post_bc_bind

        self.clipboard = []
        self.tabs = self.find("tabs")

    def on_first_page_load(self):
        self.new_tab()
        self.binder = Binder(self.controller, self.find("filemanager")).populate()
        self.binder_c = Binder(self, self.find("bind-clipboard")).populate()

    def on_page_load(self):
        self.refresh()

    def refresh_clipboard(self):
        self.binder_c.setup().populate()

    @on("tabs", "switch")
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.new_tab()
        self.refresh()

    @intent("fm:browse")
    def new_tab(self, path=None):
        dir = path or self.classconfig.get("start", None) or "/"
        if not dir.startswith(self.classconfig["root"]):
            dir = self.classconfig["root"]
        self.controller.new_tab(dir)
        if not self.active:
            self.activate()

    @on("close", "click")
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on("new-file", "click")
    def on_new_file(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info("[fm] new file in %s" % destination)
        path = os.path.join(destination, "new file")
        try:
            open(path, "w").close()
            self._chown_new(path)
        except OSError as e:
            self.context.notify("error", str(e))
        self.refresh()

    @on("new-dir", "click")
    def on_new_directory(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info("[fm] new directory in %s" % destination)
        path = os.path.join(destination, "new directory")
        if not os.path.exists(path):
            try:
                os.mkdir(path)
                os.chmod(path, 0755)
                self._chown_new(path)
            except OSError as e:
                self.context.notify("error", str(e))
        self.refresh()

    def _chown_new(self, path):
        uid = self.classconfig.get("new_owner", "root") or "root"
        gid = self.classconfig.get("new_group", "root") or "root"
        try:
            uid = int(uid)
        except:
            uid = pwd.getpwnam(uid)[2]
        try:
            gid = int(gid)
        except:
            gid = grp.getgrnam(gid)[2]
        os.chown(path, uid, gid)

    def upload(self, name, file):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info("[fm] uploading %s to %s" % (name, destination))
        try:
            output = open(os.path.join(destination, name), "w")
            while True:
                data = file.read(1024 * 1024)
                if not data:
                    break
                gevent.sleep(0)
                output.write(data)
            output.close()
        except OSError as e:
            self.context.notify("error", str(e))
        self.refresh()

    @on("mass-cut", "click")
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = "cut"
        self.clipboard += l
        self.refresh_clipboard()

    @on("mass-copy", "click")
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = "copy"
        self.clipboard += l
        self.refresh_clipboard()

    @on("mass-delete", "click")
    def on_delete(self):
        def callback(task):
            self.context.notify("info", _("Files deleted"))
            self.refresh()

        self.backend.remove(self._get_checked(), cb=callback)

    @on("paste", "click")
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == "cut":
                for_move.append(i)
            else:
                for_copy.append(i)

        try:
            if for_move:

                def callback(task):
                    self.context.notify("info", _("Files moved"))
                    self.refresh()

                self.backend.move(for_move, tab.path, callback)
            if for_copy:

                def callback(task):
                    self.context.notify("info", _("Files copied"))
                    self.refresh()

                self.backend.copy(for_copy, tab.path, callback)
            self.clipboard = []
        except Exception as e:
            self.context.notify("error", str(e))
        self.refresh_clipboard()

    @on("select-all", "click")
    def on_select_all(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        for item in tab.items:
            item.checked = not item.checked
        self.binder.populate()
        self.context.notify("info", _("Selected %i items") % len(tab.items))

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on("clear-clipboard", "click")
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig["root"]):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find("dialog").visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find("dialog")).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find("dialog").find("unpack")
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify("info", _("Unpacked"))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info("[fm] unpacking %s" % self.item.fullpath)

        unpack_btn.on("click", lambda: unpack())

        # Edit
        edit_btn = self.find("dialog").find("edit")
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch("notepad", path=self.item.fullpath)

        edit_btn.on("click", lambda: edit())

    @on("dialog", "button")
    def on_close_dialog(self, button):
        self.find("dialog").visible = False
        if button == "save":
            self.binder_d.update()
            try:
                self.item.write()
            except Exception as e:
                self.context.notify("error", str(e))
            self.refresh()

            if self.find("chmod-recursive").value:
                cmd = 'chown -Rv "%s:%s" "%s"; chmod -Rv %o "%s"' % (
                    self.item.owner,
                    self.item.group,
                    self.item.fullpath,
                    self.item.mode,
                    self.item.fullpath,
                )
                self.context.launch("terminal", command=cmd)

            logging.info(
                "[fm] modifying %s: %o %s:%s" % (self.item.fullpath, self.item.mode, self.item.owner, self.item.group)
            )

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig["root"]):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self, _=None):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()
Example #10
0
class FileManager (SectionPlugin):
    default_classconfig = {'root': '/', 'start': '/'}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _('Файлов мениджър')
        self.category = _('Tools')
        self.icon = 'folder-open'

        self.backend = FMBackend().get()

        self.append(self.ui.inflate('fm:main'))
        self.controller = Controller()

        def post_item_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_item_click, object, item)
            ui.find('edit').on('click', self.edit, item.fullpath)
        self.find('items').post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_bc_click, object, item)
        self.find('breadcrumbs').post_item_bind = post_bc_bind

        self.clipboard = []
        self.tabs = self.find('tabs')

    def on_first_page_load(self):
        self.new_tab()
        self.binder = Binder(self.controller, self.find('filemanager')).populate()
        self.binder_c = Binder(self, self.find('bind-clipboard')).populate()

    def on_page_load(self):
        self.refresh()

    def refresh_clipboard(self):
        self.binder_c.setup().populate()

    @on('tabs', 'switch')
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.new_tab()
        self.refresh()

    @intent('fm:browse')
    def new_tab(self, path=None):
        dir = path or self.classconfig.get('start', None) or '/'
        if not dir.startswith(self.classconfig['root']):
            dir = self.classconfig['root']
        self.controller.new_tab(dir)
        if not self.active:
            self.activate()

    @on('close', 'click')
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on('new-file', 'click')
    def on_new_file(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] new file in %s' % destination)
        path = os.path.join(destination, 'new file')
        try:
            open(path, 'w').close()
            self._chown_new(path)
        except OSError as e:
            self.context.notify('error', str(e))
        self.refresh()

    @on('new-dir', 'click')
    def on_new_directory(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] new directory in %s' % destination)
        path = os.path.join(destination, 'new directory')
        if not os.path.exists(path):
            try:
                os.mkdir(path)
                os.chmod(path, 0755)
                self._chown_new(path)
            except OSError as e:
                self.context.notify('error', str(e))
        self.refresh()

    def _chown_new(self, path):
        uid = self.classconfig.get('new_owner', 'root') or 'root'
        gid = self.classconfig.get('new_group', 'root') or 'root'
        try:
            uid = int(uid)
        except:
            uid = pwd.getpwnam(uid)[2]
        try:
            gid = int(gid)
        except:
            gid = grp.getgrnam(gid)[2]
        os.chown(path, uid, gid)

    def upload(self, name, file):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] uploading %s to %s' % (name, destination))
        try:
            output = open(os.path.join(destination, name), 'w')
            while True:
                data = file.read(1024 * 1024)
                if not data:
                    break
                gevent.sleep(0)
                output.write(data)
            output.close()
        except OSError as e:
            self.context.notify('error', str(e))
        self.refresh()

    @on('mass-cut', 'click')
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = 'cut'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-copy', 'click')
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = 'copy'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-delete', 'click')
    def on_delete(self):
        def callback(task):
            self.context.notify('info', _('Files deleted'))
            self.refresh()
        self.backend.remove(self._get_checked(), cb=callback)

    @on('paste', 'click')
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == 'cut':
                for_move.append(i)
            else:
                for_copy.append(i)

        try:
            if for_move:
                def callback(task):
                    self.context.notify('info', _('Files moved'))
                    self.refresh()
                self.backend.move(for_move, tab.path, callback)
            if for_copy:
                def callback(task):
                    self.context.notify('info', _('Files copied'))
                    self.refresh()
                self.backend.copy(for_copy, tab.path, callback)
            self.clipboard = []
        except Exception as e:
            self.context.notify('error', str(e))
        self.refresh_clipboard()

    @on('select-all', 'click')
    def on_select_all(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        for item in tab.items:
            item.checked = not item.checked
        self.binder.populate()
        self.context.notify('info', _('Selected %i items') % len(tab.items)) 

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on('clear-clipboard', 'click')
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig['root']):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find('dialog')).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find('dialog').find('unpack')
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify('info', _('Unpacked'))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info('[fm] unpacking %s' % self.item.fullpath)

        unpack_btn.on('click', lambda: unpack())

        # Edit
        edit_btn = self.find('dialog').find('edit')
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch('notepad', path=self.item.fullpath)

        edit_btn.on('click', lambda: edit())

    @on('dialog', 'button')
    def on_close_dialog(self, button):
        self.find('dialog').visible = False
        if button == 'save':
            self.binder_d.update()
            try:
                self.item.write()
            except Exception as e:
                self.context.notify('error', str(e))
            self.refresh()

            if self.find('chmod-recursive').value:
                cmd = 'chown -Rv "%s:%s" "%s"; chmod -Rv %o "%s"' % (
                    self.item.owner, self.item.group,
                    self.item.fullpath,
                    self.item.mode,
                    self.item.fullpath,
                )
                self.context.launch('terminal', command=cmd)

            logging.info('[fm] modifying %s: %o %s:%s' % (self.item.fullpath, self.item.mode, self.item.owner, self.item.group))

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig['root']):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self, _=None):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()
Example #11
0
File: fm.py Project: nnpro/ajenti
class FileManager(SectionPlugin):
    default_classconfig = {"root": "/"}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _("File Manager")
        self.category = _("Tools")
        self.icon = "folder-open"

        self.backend = FMBackend().get()

        self.append(self.ui.inflate("fm:main"))
        self.controller = Controller()
        self.controller.new_tab(self.classconfig["root"])

        def post_item_bind(object, collection, item, ui):
            ui.find("name").on("click", self.on_item_click, object, item)
            ui.find("edit").on("click", self.edit, item.fullpath)

        self.find("items").post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find("name").on("click", self.on_bc_click, object, item)

        self.find("breadcrumbs").post_item_bind = post_bc_bind

        self.binder = Binder(self.controller, self.find("filemanager")).autodiscover().populate()
        self.clipboard = []
        self.binder_c = Binder(self, self.find("filemanager")).autodiscover().populate()
        self.tabs = self.find("tabs")

        self.find("dialog").buttons = [{"id": "save", "text": _("Save")}, {"id": "cancel", "text": _("Cancel")}]

    def refresh_clipboard(self):
        self.binder_c.reset().autodiscover().populate()

    @on("tabs", "switch")
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.controller.new_tab(self.classconfig["root"])
        self.refresh()

    @on("close", "click")
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on("new-file", "click")
    def on_new_file(self):
        open(os.path.join(self.controller.tabs[self.tabs.active].path, "new file"), "w").close()
        self.refresh()

    def upload(self, name, file):
        open(os.path.join(self.controller.tabs[self.tabs.active].path, name), "w").write(file.read())
        self.refresh()

    @on("new-dir", "click")
    def on_new_directory(self):
        os.mkdir(os.path.join(self.controller.tabs[self.tabs.active].path, "new directory"))
        self.refresh()

    @on("mass-cut", "click")
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = "cut"
        self.clipboard += l
        self.refresh_clipboard()

    @on("mass-copy", "click")
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = "copy"
        self.clipboard += l
        self.refresh_clipboard()

    @on("mass-delete", "click")
    def on_delete(self):
        self.backend.remove(self._get_checked())
        self.refresh()

    @on("paste", "click")
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == "cut":
                for_move.append(i)
            else:
                for_copy.append(i)

        if for_move:
            self.backend.move(for_move, tab.path)
        if for_copy:
            self.backend.copy(for_copy, tab.path)
        self.clipboard = []
        self.refresh_clipboard()
        self.refresh()

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on("clear-clipboard", "click")
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig["root"]):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find("dialog").visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find("dialog")).autodiscover().populate()

    @on("dialog", "button")
    def on_close_dialog(self, button):
        self.find("dialog").visible = False
        if button == "save":
            self.binder_d.update()
            self.item.write()
            self.refresh()

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig["root"]):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()
Example #12
0
    except HTTPError, e:
      return e.error_response()

    if not item_list.user_can_update(auth_user):
      message = 'List cannot be modified by the user'
      logging.warn(message)
      return error_response(403, message)

    # Validate data for new item.
    params = web.input(data=None)
    try:
      if params.data:
        item_data = decode_json(params.data)
      else:
        item_data = decode_json(web.ctx.data)
      Item.validate(item_data)
    except ValueError, e:
      logging.warn(e)
      return error_response(400, 'Could not parse JSON')
    except ValidationError, e:
      logging.warn(e)
      return error_response(400, e.error)

    try:
      item = Item(item_data)
      item.set_creator(auth_user)
      item.save()
      item_added = item_list.add_item(item)
      if item_added:
        item_list.save()
        formatted_dict = item.formatted_dict()
Example #13
0
class FileManager (SectionPlugin):
    default_classconfig = {'root': '/', 'start': '/'}
    classconfig_editor = FileManagerConfigEditor
    classconfig_root = True

    def init(self):
        self.title = _('File Manager')
        self.category = _('Tools')
        self.icon = 'folder-open'

        self.backend = FMBackend().get()

        self.append(self.ui.inflate('fm:main'))
        self.controller = Controller()

        def post_item_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_item_click, object, item)
            ui.find('edit').on('click', self.edit, item.fullpath)
        self.find('items').post_item_bind = post_item_bind

        def post_bc_bind(object, collection, item, ui):
            ui.find('name').on('click', self.on_bc_click, object, item)
        self.find('breadcrumbs').post_item_bind = post_bc_bind

        self.clipboard = []
        self.tabs = self.find('tabs')

    def on_first_page_load(self):
        self.new_tab()
        self.binder = Binder(self.controller, self.find('filemanager')).populate()
        self.binder_c = Binder(self, self.find('bind-clipboard')).populate()

    def on_page_load(self):
        self.refresh()

    def refresh_clipboard(self):
        self.binder_c.setup().populate()

    @on('tabs', 'switch')
    def on_tab_switch(self):
        if self.tabs.active == (len(self.controller.tabs) - 1):
            self.new_tab()
        self.refresh()

    @intent('fm:browse')
    def new_tab(self, path=None):
        dir = path or self.classconfig.get('start', None) or '/'
        if not dir.startswith(self.classconfig['root']):
            dir = self.classconfig['root']
        self.controller.new_tab(dir)
        if not self.active:
            self.activate()

    @on('close', 'click')
    def on_tab_close(self):
        if len(self.controller.tabs) > 2:
            self.controller.tabs.pop(self.tabs.active)
        self.tabs.active = 0
        self.refresh()

    @on('new-file', 'click')
    def on_new_file(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] new file in %s' % destination)
        path = os.path.join(destination, 'new file')
        try:
            open(path, 'w').close()
            self._chown_new(path)
        except OSError as e:
            self.context.notify('error', str(e))
        self.refresh()

    @on('new-dir', 'click')
    def on_new_directory(self):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] new directory in %s' % destination)
        path = os.path.join(destination, 'new directory')
        if not os.path.exists(path):
            try:
                os.mkdir(path)
                os.chmod(path, 0755)
                self._chown_new(path)
            except OSError as e:
                self.context.notify('error', str(e))
        self.refresh()

    def _chown_new(self, path):
        uid = self.classconfig.get('new_owner', 'root') or 'root'
        gid = self.classconfig.get('new_group', 'root') or 'root'
        try:
            uid = int(uid)
        except:
            uid = pwd.getpwnam(uid)[2]
        try:
            gid = int(gid)
        except:
            gid = grp.getgrnam(gid)[2]
        os.chown(path, uid, gid)

    def upload(self, name, file):
        destination = self.controller.tabs[self.tabs.active].path
        logging.info('[fm] uploading %s to %s' % (name, destination))
        try:
            output = open(os.path.join(destination, name), 'w')
            while True:
                data = file.read(1024 * 1024)
                if not data:
                    break
                gevent.sleep(0)
                output.write(data)
            output.close()
        except OSError as e:
            self.context.notify('error', str(e))
        self.refresh()

    @on('mass-cut', 'click')
    def on_cut(self):
        l = self._get_checked()
        for i in l:
            i.action = 'cut'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-copy', 'click')
    def on_copy(self):
        l = self._get_checked()
        for i in l:
            i.action = 'copy'
        self.clipboard += l
        self.refresh_clipboard()

    @on('mass-delete', 'click')
    def on_delete(self):
        def callback(task):
            self.context.notify('info', _('Files deleted'))
            self.refresh()
        self.backend.remove(self._get_checked(), cb=callback)

    @on('paste', 'click')
    def on_paste(self):
        tab = self.controller.tabs[self.tabs.active]
        for_move = []
        for_copy = []
        for i in self.clipboard:
            if i.action == 'cut':
                for_move.append(i)
            else:
                for_copy.append(i)

        try:
            if for_move:
                def callback(task):
                    self.context.notify('info', _('Files moved'))
                    self.refresh()
                self.backend.move(for_move, tab.path, callback)
            if for_copy:
                def callback(task):
                    self.context.notify('info', _('Files copied'))
                    self.refresh()
                self.backend.copy(for_copy, tab.path, callback)
            self.clipboard = []
        except Exception as e:
            self.context.notify('error', str(e))
        self.refresh_clipboard()

    @on('select-all', 'click')
    def on_select_all(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        for item in tab.items:
            item.checked = not item.checked
        self.binder.populate()
        self.context.notify('info', _('Selected %i items') % len(tab.items)) 

    def _get_checked(self):
        self.binder.update()
        tab = self.controller.tabs[self.tabs.active]
        r = []
        for item in tab.items:
            if item.checked:
                r.append(item)
                item.checked = False
        self.refresh()
        return r

    @on('clear-clipboard', 'click')
    def on_clear_clipboard(self):
        self.clipboard = []
        self.refresh_clipboard()

    def on_item_click(self, tab, item):
        path = os.path.join(tab.path, item.name)
        if not os.path.isdir(path):
            self.edit(path)
        if not path.startswith(self.classconfig['root']):
            return
        tab.navigate(path)
        self.refresh()

    def edit(self, path):
        self.find('dialog').visible = True
        self.item = Item(path)
        self.item.read()
        self.binder_d = Binder(self.item, self.find('dialog')).populate()

        # Unpack
        u = Unpacker.find(self.item.fullpath.lower())
        unpack_btn = self.find('dialog').find('unpack')
        unpack_btn.visible = u is not None

        def cb():
            self.context.notify('info', _('Unpacked'))
            self.refresh()

        def unpack():
            u.unpack(self.item.fullpath, cb=cb)
            logging.info('[fm] unpacking %s' % self.item.fullpath)

        unpack_btn.on('click', lambda: unpack())

        # Edit
        edit_btn = self.find('dialog').find('edit')
        if self.item.size > 1024 * 1024 * 5:
            edit_btn.visible = False

        def edit():
            self.context.launch('notepad', path=self.item.fullpath)

        edit_btn.on('click', lambda: edit())

    @on('dialog', 'button')
    def on_close_dialog(self, button):
        self.find('dialog').visible = False
        if button == 'save':
            self.binder_d.update()
            try:
                self.item.write()
            except Exception as e:
                self.context.notify('error', str(e))
            self.refresh()

            if self.find('chmod-recursive').value:
                cmd = 'chown -Rv "%s:%s" "%s"; chmod -Rv %o "%s"' % (
                    self.item.owner, self.item.group,
                    self.item.fullpath,
                    self.item.mode,
                    self.item.fullpath,
                )
                self.context.launch('terminal', command=cmd)

            logging.info('[fm] modifying %s: %o %s:%s' % (self.item.fullpath, self.item.mode, self.item.owner, self.item.group))

    def on_bc_click(self, tab, item):
        if not item.path.startswith(self.classconfig['root']):
            return
        tab.navigate(item.path)
        self.refresh()

    def refresh(self, _=None):
        for tab in self.controller.tabs:
            tab.refresh()
        self.binder.populate()