add non tty stdin support to edit command in webcontentedit

Signed-off-by: Laurent Bachelier <laurent@bachelier.name>
This commit is contained in:
Goffi 2013-03-09 14:47:40 +01:00 committed by Laurent Bachelier
commit 19a89c9a9e

View file

@ -24,6 +24,7 @@ from __future__ import with_statement
import os
import sys
import tempfile
import locale
from weboob.core.bcall import CallErrors
from weboob.capabilities.content import ICapContent
@ -58,6 +59,7 @@ class WebContentEdit(ReplApplication):
print >>sys.stderr, 'No contents found'
return 3
if sys.stdin.isatty():
paths = {}
for content in contents:
tmpdir = os.path.join(tempfile.gettempdir(), "weboob")
@ -116,6 +118,28 @@ class WebContentEdit(ReplApplication):
else:
sys.stdout.write(' done\n')
os.unlink(path)
else:
# stdin is not a tty
if len(contents) != 1:
print >>sys.stderr, "Multiple ids not supported with pipe"
return 2
message, minor = '', False
data = sys.stdin.read()
contents[0].content = data.decode(sys.stdin.encoding or locale.getpreferredencoding())
errors = CallErrors([])
for content in contents:
sys.stdout.write('Pushing %s...' % content.id.encode('utf-8'))
sys.stdout.flush()
try:
self.do('push_content', content, message, minor=minor, backends=[content.backend]).wait()
except CallErrors, e:
errors.errors += e.errors
sys.stdout.write(' error\n')
else:
sys.stdout.write(' done\n')
if len(errors.errors) > 0:
raise errors