def _do_import(self, filename=None): permsys = PermissionSystem(self.env) try: with file_or_std(filename, 'rb') as f: encoding = stream_encoding(f) linesep = os.linesep if filename else '\n' reader = csv.reader(f, lineterminator=linesep) for row in reader: if len(row) < 2: raise AdminCommandError( _( "Invalid row %(line)d. Expected <user>, " "<action>, [action], [...]", line=reader.line_num)) user = to_unicode(row[0], encoding) actions = [ to_unicode(action, encoding) for action in row[1:] ] if user.isupper(): raise AdminCommandError( _( "Invalid user %(user)s on line %(line)d: All " "upper-cased tokens are reserved for permission " "names.", user=user, line=reader.line_num)) old_actions = self.get_user_perms(user) for action in set(actions) - set(old_actions): permsys.grant_permission(user, action) except csv.Error, e: raise AdminCommandError( _("Cannot import from %(filename)s line %(line)d: %(error)s ", filename=path_to_unicode(filename or 'stdin'), line=reader.line_num, error=e))
def _do_import(self, filename=None): permsys = PermissionSystem(self.env) try: with file_or_std(filename, 'rb') as f: encoding = stream_encoding(f) linesep = os.linesep if filename else '\n' reader = csv.reader(f, lineterminator=linesep) for row in reader: if len(row) < 2: raise AdminCommandError( _("Invalid row %(line)d. Expected <user>, " "<action>, [action], [...]", line=reader.line_num)) user = to_unicode(row[0], encoding) actions = [to_unicode(action, encoding) for action in row[1:]] if user.isupper(): raise AdminCommandError( _("Invalid user %(user)s on line %(line)d: All " "upper-cased tokens are reserved for permission " "names.", user=user, line=reader.line_num)) old_actions = self.get_user_perms(user) for action in set(actions) - set(old_actions): permsys.grant_permission(user, action) except csv.Error as e: raise AdminCommandError( _("Cannot import from %(filename)s line %(line)d: %(error)s ", filename=path_to_unicode(filename or 'stdin'), line=reader.line_num, error=e)) except IOError as e: raise AdminCommandError( _("Cannot import from %(filename)s: %(error)s", filename=path_to_unicode(filename or 'stdin'), error=e.strerror))
def _do_export(self, filename=None): try: with file_or_std(filename, 'wb') as f: encoding = stream_encoding(f) linesep = os.linesep if filename else '\n' writer = csv.writer(f, lineterminator=linesep) users = self.get_user_list() for user in sorted(users): actions = sorted(self.get_user_perms(user)) writer.writerow([s.encode(encoding, 'replace') for s in [user] + actions]) except IOError as e: raise AdminCommandError( _("Cannot export to %(filename)s: %(error)s", filename=path_to_unicode(filename or 'stdout'), error=e.strerror))