Example #1
0
 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))
Example #2
0
 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))
Example #3
0
 def _do_export(self, resource, name, destination=None):
     realm, id_ = self.split_resource(resource)
     attachment = Attachment(self.env, realm, id_, name)
     if destination is not None:
         if os.path.isdir(destination):
             destination = os.path.join(destination, name)
         if os.path.isfile(destination):
             raise AdminCommandError(_("File '%(name)s' exists",
                                       name=path_to_unicode(destination)))
     with attachment.open() as input:
         with file_or_std(destination, 'wb') as output:
             shutil.copyfileobj(input, output)
Example #4
0
 def _do_export(self, filename=None):
     try:
         with file_or_std(filename, 'w') as 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 for s in [user] + actions])
             f.flush()
     except IOError as e:
         raise AdminCommandError(
             _("Cannot export to %(filename)s: %(error)s",
               filename=path_to_unicode(filename or 'stdout'),
               error=e.strerror))
Example #5
0
 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))