Use Path object
This commit is contained in:
parent
f815a9c2c2
commit
82afb34f7a
2 changed files with 81 additions and 32 deletions
|
|
@ -31,6 +31,7 @@ from weboob.tools.application.formatters.iformatter import IFormatter
|
|||
|
||||
from weboob.capabilities.collection import Collection, ICapCollection, CollectionNotFound
|
||||
|
||||
from weboob.tools.path import Path
|
||||
|
||||
__all__ = ['Videoob']
|
||||
|
||||
|
|
@ -71,12 +72,12 @@ class Videoob(ReplApplication):
|
|||
|
||||
nsfw = True
|
||||
videos = []
|
||||
|
||||
working_dir = []
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ReplApplication.__init__(self, *args, **kwargs)
|
||||
self.player = MediaPlayer(self.logger)
|
||||
self.working_path = Path()
|
||||
|
||||
def main(self, argv):
|
||||
self.load_config()
|
||||
|
|
@ -245,15 +246,16 @@ class Videoob(ReplApplication):
|
|||
|
||||
def do_ls(self, line):
|
||||
self.videos = []
|
||||
if len(self.working_dir) == 0:
|
||||
path = self.working_path.get()
|
||||
if len(path) == 0:
|
||||
for name in [b.NAME for b in self.weboob.iter_backends(caps=ICapCollection)]:
|
||||
print name
|
||||
return 0
|
||||
|
||||
def do(backend):
|
||||
return backend.iter_resources(self.working_dir[1:])
|
||||
return backend.iter_resources(path[1:])
|
||||
|
||||
for backend, rep in self.do(do, backends=self.working_dir[0]):
|
||||
for backend, rep in self.do(do, backends=path[0]):
|
||||
if isinstance(rep, BaseVideo):
|
||||
self.videos.append(rep)
|
||||
self.format(rep)
|
||||
|
|
@ -265,21 +267,16 @@ class Videoob(ReplApplication):
|
|||
def do_cd(self, line):
|
||||
line = line.encode('utf-8')
|
||||
|
||||
import urllib
|
||||
line = urllib.quote_plus(line)
|
||||
import posixpath
|
||||
path = posixpath.normpath(line)
|
||||
abspath = posixpath.normpath(posixpath.join('/' + '/'.join(self.working_dir), path))
|
||||
final_parse = abspath.split('/')[1:]
|
||||
while len(final_parse) > 0 and final_parse[0] == u'': del final_parse[0]
|
||||
if len(final_parse) == 0:
|
||||
self.working_dir = []
|
||||
self.working_path.extend(line)
|
||||
|
||||
req_path = self.working_path.get()
|
||||
|
||||
if len(req_path) == 0:
|
||||
self.prompt = '%s> ' % self.APPNAME
|
||||
return 0
|
||||
|
||||
final_parse = map(urllib.unquote_plus, final_parse)
|
||||
working_backend = final_parse[0]
|
||||
path = final_parse [1:]
|
||||
|
||||
working_backend = req_path[0]
|
||||
path = req_path[1:]
|
||||
|
||||
if working_backend in [b.NAME for b in self.enabled_backends]:
|
||||
if working_backend in [b.NAME for b in self.weboob.iter_backends(caps=ICapCollection)]:
|
||||
|
|
@ -291,33 +288,29 @@ class Videoob(ReplApplication):
|
|||
print >>sys.stderr, "Error backend %s unknow" % working_backend
|
||||
return 1
|
||||
|
||||
|
||||
if not path:
|
||||
self.working_dir = [working_backend,]
|
||||
backend.working_coll = []
|
||||
self.prompt = '%s:%s> ' % (self.APPNAME, '/' + '/'.join(self.working_dir))
|
||||
return 0
|
||||
try:
|
||||
path = backend.change_working_collection(path)
|
||||
except NotImplementedError:
|
||||
print >>sys.stderr, "Error backend %s not implement collection" % working_backend
|
||||
self.working_path.restore()
|
||||
return 1
|
||||
except CollectionNotFound:
|
||||
print >>sys.stderr, "Path: %s not found" % ('/'+'/'.join(final_parse))
|
||||
print >>sys.stderr, "Path: %s not found" % ('/'+'/'.join(req_path))
|
||||
self.working_path.restore()
|
||||
return 1
|
||||
|
||||
escape = lambda s: s.replace('/', '%2F')
|
||||
path = map(escape, path)
|
||||
self.working_dir = [working_backend,]+path
|
||||
self.prompt = '%s:%s> ' % (self.APPNAME, '/' + '/'.join(self.working_dir))
|
||||
self.prompt = '%s:%s> ' % (self.APPNAME, '/' + '/'.join(req_path))
|
||||
|
||||
def complete_cd(self, text, line, begidx, endidx):
|
||||
mline = line.partition(' ')[2]
|
||||
offs = len(mline) - len(text)
|
||||
|
||||
if len(self.working_dir) == 0:
|
||||
path = self.working_path.get()
|
||||
|
||||
if len(path) == 0:
|
||||
tmp = [b.NAME for b in self.weboob.iter_backends(caps=ICapCollection)]
|
||||
else:
|
||||
backend = [b for b in self.enabled_backends if b.NAME == self.working_dir[0]][0]
|
||||
tmp = [rep for rep in backend.iter_resources(self.working_dir[1:])]
|
||||
backend = [b for b in self.enabled_backends if b.NAME == path[0]][0]
|
||||
tmp = [rep for rep in backend.iter_resources(path[1:])]
|
||||
|
||||
return [s[offs:] for s in tmp if s.startswith(mline)]
|
||||
|
|
|
|||
56
weboob/tools/path.py
Normal file
56
weboob/tools/path.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 Nicolas Duhamel
|
||||
#
|
||||
# This file is part of weboob.
|
||||
#
|
||||
# weboob is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# weboob is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||
import urllib
|
||||
import posixpath
|
||||
import copy
|
||||
|
||||
class Path(object):
|
||||
def __init__(self):
|
||||
self._working_path = []
|
||||
self._previous = self._working_path
|
||||
|
||||
|
||||
def extend(self, user_input):
|
||||
|
||||
user_input = urllib.quote_plus(user_input)
|
||||
user_input = posixpath.normpath(user_input)
|
||||
|
||||
escape = lambda s: s.replace('/', '%2F')
|
||||
current_path = map(escape, self._working_path)
|
||||
|
||||
abspath = posixpath.normpath(posixpath.join('/' + '/'.join(current_path), user_input))
|
||||
|
||||
abspath = abspath.split('/')[1:]
|
||||
while len(abspath) > 0 and abspath[0] == u'': del abspath[0]
|
||||
|
||||
final_parse = map(urllib.unquote_plus, abspath)
|
||||
|
||||
self._previous = self._working_path
|
||||
|
||||
if len(final_parse) == 0:
|
||||
self._working_path = []
|
||||
|
||||
self._working_path = final_parse
|
||||
|
||||
def restore(self):
|
||||
self._working_path = self._previous
|
||||
|
||||
|
||||
def get(self):
|
||||
return copy.copy(self._working_path)
|
||||
Loading…
Add table
Add a link
Reference in a new issue