cubicweb/server/utils.py
changeset 12567 26744ad37953
parent 12508 a8c1ea390400
child 12574 6ccf8fda063f
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """Some utilities for the CubicWeb server."""
    18 """Some utilities for the CubicWeb server."""
    19 from __future__ import print_function
       
    20 
       
    21 
    19 
    22 from functools import wraps
    20 from functools import wraps
    23 import sched
       
    24 import sys
       
    25 import logging
    21 import logging
    26 from threading import Thread
    22 from threading import Thread
    27 from getpass import getpass
    23 from getpass import getpass
    28 
       
    29 from six import PY2, text_type
       
    30 from six.moves import input
       
    31 
    24 
    32 from passlib.utils import handlers as uh, to_hash_str
    25 from passlib.utils import handlers as uh, to_hash_str
    33 from passlib.context import CryptContext
    26 from passlib.context import CryptContext
    34 
    27 
    35 from cubicweb.md5crypt import crypt as md5crypt
    28 from cubicweb.md5crypt import crypt as md5crypt
    90     if not user:
    83     if not user:
    91         if msg:
    84         if msg:
    92             print(msg)
    85             print(msg)
    93         while not user:
    86         while not user:
    94             user = input('login: ')
    87             user = input('login: ')
    95         if PY2:
       
    96             user = text_type(user, sys.stdin.encoding)
       
    97     passwd = getpass('%s: ' % passwdmsg)
    88     passwd = getpass('%s: ' % passwdmsg)
    98     if confirm:
    89     if confirm:
    99         while True:
    90         while True:
   100             passwd2 = getpass('confirm password: ')
    91             passwd2 = getpass('confirm password: ')
   101             if passwd == passwd2:
    92             if passwd == passwd2:
   102                 break
    93                 break
   103             print('password doesn\'t match')
    94             print('password doesn\'t match')
   104             passwd = getpass('password: ')
    95             passwd = getpass('password: ')
   105     # XXX decode password using stdin encoding then encode it using appl'encoding
    96     # XXX decode password using stdin encoding then encode it using appl'encoding
   106     return user, passwd
    97     return user, passwd
   107 
       
   108 
       
   109 if PY2:
       
   110     import time  # noqa
       
   111 
       
   112     class scheduler(sched.scheduler):
       
   113         """Python2 version of sched.scheduler that matches Python3 API."""
       
   114 
       
   115         def __init__(self, **kwargs):
       
   116             kwargs.setdefault('timefunc', time.time)
       
   117             kwargs.setdefault('delayfunc', time.sleep)
       
   118             # sched.scheduler is an old-style class.
       
   119             sched.scheduler.__init__(self, **kwargs)
       
   120 
       
   121 else:
       
   122     scheduler = sched.scheduler
       
   123 
    98 
   124 
    99 
   125 def schedule_periodic_task(scheduler, interval, func, *args):
   100 def schedule_periodic_task(scheduler, interval, func, *args):
   126     """Enter a task with `func(*args)` as a periodic event in `scheduler`
   101     """Enter a task with `func(*args)` as a periodic event in `scheduler`
   127     executing at `interval` seconds. Once executed, the task would re-schedule
   102     executing at `interval` seconds. Once executed, the task would re-schedule