"Fossies" - the Fresh Open Source Software Archive

Member "mailman-3.3.7/src/mailman/core/api.py" (10 Nov 2022, 2416 Bytes) of package /linux/misc/mailman-3.3.7.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "api.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3.5_vs_3.3.6.

    1 # Copyright (C) 2016-2022 by the Free Software Foundation, Inc.
    2 #
    3 # This file is part of GNU Mailman.
    4 #
    5 # GNU Mailman is free software: you can redistribute it and/or modify it under
    6 # the terms of the GNU General Public License as published by the Free
    7 # Software Foundation, either version 3 of the License, or (at your option)
    8 # any later version.
    9 #
   10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
   11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   13 # more details.
   14 #
   15 # You should have received a copy of the GNU General Public License along with
   16 # GNU Mailman.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 """REST web service API contexts."""
   19 
   20 from lazr.config import as_boolean
   21 from mailman.config import config
   22 from mailman.interfaces.api import IAPI
   23 from public import public
   24 from uuid import UUID
   25 from zope.interface import implementer
   26 
   27 
   28 @public
   29 @implementer(IAPI)
   30 class API30:
   31     version = '3.0'
   32     version_info = (3, 0)
   33 
   34     @classmethod
   35     def path_to(cls, resource):
   36         """See `IAPI`."""
   37         return '{}://{}:{}/{}/{}'.format(
   38             ('https' if as_boolean(config.webservice.use_https) else 'http'),
   39             config.webservice.hostname,
   40             config.webservice.port,
   41             cls.version,
   42             (resource[1:] if resource.startswith('/') else resource),
   43             )
   44 
   45     @staticmethod
   46     def from_uuid(uuid):
   47         """See `IAPI`."""
   48         return uuid.int
   49 
   50     @staticmethod
   51     def to_uuid(uuid):
   52         """See `IAPI`."""
   53         if isinstance(uuid, UUID):
   54             return uuid
   55         return UUID(int=int(uuid))
   56 
   57 
   58 @public
   59 @implementer(IAPI)
   60 class API31:
   61     version = '3.1'
   62     version_info = (3, 1)
   63 
   64     @classmethod
   65     def path_to(cls, resource):
   66         """See `IAPI`."""
   67         return '{}://{}:{}/{}/{}'.format(
   68             ('https' if as_boolean(config.webservice.use_https) else 'http'),
   69             config.webservice.hostname,
   70             config.webservice.port,
   71             cls.version,
   72             (resource[1:] if resource.startswith('/') else resource),
   73             )
   74 
   75     @staticmethod
   76     def from_uuid(uuid):
   77         """See `IAPI`."""
   78         return uuid.hex
   79 
   80     @staticmethod
   81     def to_uuid(uuid):
   82         """See `IAPI`."""
   83         if isinstance(uuid, UUID):
   84             return uuid
   85         return UUID(hex=uuid)