Пример #1
0
    def __init__(self):
        """
        Initialize the object.
        """

        if init.__ACCESS_TOKEN__ == None:
            raise pytxInitError("Must init() before instantiating")
Пример #2
0
    def __init__(self):
        """
        Initialize the object.
        """

        if init.__ACCESS_TOKEN__ == None:
            raise pytxInitError("Must init() before instantiating")
Пример #3
0
def init(app_id=None, app_secret=None, token_file=None):
    """
    Use the app_id and app_secret to store the access_token globally for all
    instantiated objects to leverage.

    If you use token_file, you can either:
        - put the app_id and app_token on separate lines
        - combine them with a "|" in between on a single line

    If no options are passed into this function, it will check for the following
    environment variables:
        - TX_APP_ID
        - TX_APP_SECRET
        - TX_ACCESS_TOKEN

    The latter is the combined app_id and app_secret with a "|" in between.

    :param app_id: The APP-ID to use.
    :type app_id: str
    :param app_secret: The APP-SECRET to use.
    :type app_secret: str
    :param token_file: The full path and filename where to find the access token.
    :type token_file: str
    :raises: :class:`errors.pytxIniterror`
    """

    global __ACCESS_TOKEN__
    global Broker

    if app_id and app_secret:
        try:
            __ACCESS_TOKEN__ = app_id + "|" + app_secret
        except Exception, e:
            raise pytxInitError("Error generating access token: %s" % str(e))
    def __init__(self, **kwargs):
        """
        Initialize the object. Set the _access_token and any attributes that
        were provided.
        """

        self._access_token = init.__ACCESS_TOKEN__
        if self._access_token == None:
            raise pytxInitError("Must init() before instantiating")
        for name, value in kwargs.items():
            self.__setattr__(name, value)
Пример #5
0
    def __init__(self, **kwargs):
        """
        Initialize the object. Set the _access_token and any attributes that
        were provided.
        """

        self._access_token = init.__ACCESS_TOKEN__
        if self._access_token == None:
            raise pytxInitError("Must init() before instantiating")
        for name, value in kwargs.items():
            self.__setattr__(name, value)
Пример #6
0
def init(app_id=None, app_secret=None, token_file=None):
    """
    Use the app_id and app_secret to store the access_token globally for all
    instantiated objects to leverage.

    There are many ways to specify the app_id and app_secret. In order, init will try:
     1. Use the value of the 'TX_ACCESS_TOKEN' environment variable.
     2. Use the concatenation of the 'TX_APP_ID' and 'TX_APP_SECRET' environment variables.
     3. Use the first line of the file '$PWD/.pytx' or ~/.pytx'
     4. Use the concatenation of the app_id and app_secret parameters
     5. Use the first line of the file 'token_file'

    :param app_id: The APP-ID to use.
    :type app_id: str
    :param app_secret: The APP-SECRET to use.
    :type app_secret: str
    :param token_file: The full path and filename where to find the access token.
    :type token_file: str
    :raises: :class:`errors.pytxIniterror`
    """
    global __ACCESS_TOKEN

    # 1. Use the value of the 'TX_ACCESS_TOKEN' environment variable.
    __ACCESS_TOKEN = os.environ.get(te.TX_ACCESS_TOKEN, None)
    if __ACCESS_TOKEN:
        return

    # 2. Use the concatenation of the 'TX_APP_ID' and 'TX_APP_SECRET' environment variables.
    env_app_id = os.environ.get(te.TX_APP_ID, None)
    env_app_secret = os.environ.get(te.TX_APP_SECRET, None)
    if env_app_id and env_app_secret:
        __ACCESS_TOKEN = env_app_id + '|' + env_app_secret
        return

    # 3. Use the first line of the file '$PWD/.pytx' or ~/.pytx'
    filepath = _find_token_file()
    if filepath:
        __ACCESS_TOKEN = _read_token_file(filepath)
        return

    # 4. Use the concatenation of the app_id and app_secret parameters
    if app_id and app_secret:
        __ACCESS_TOKEN = app_id + '|' + app_secret
        return

    # 5. Use the first line of the file 'token_file'
    if token_file:
        __ACCESS_TOKEN = _read_token_file(token_file)
        return

    raise pytxInitError('Unable to set access token.')
Пример #7
0
def init(app_id=None, app_secret=None, token_file=None):
    """
    Use the app_id and app_secret to store the access_token globally for all
    instantiated objects to leverage.

    There are many ways to specify the app_id and app_secret. In order, init will try:
     1. Use the value of the 'TX_ACCESS_TOKEN' environment variable.
     2. Use the concatenation of the 'TX_APP_ID' and 'TX_APP_SECRET' environment variables.
     3. Use the first line of the file '$PWD/.pytx' or ~/.pytx'
     4. Use the concatenation of the app_id and app_secret parameters
     5. Use the first line of the file 'token_file'

    :param app_id: The APP-ID to use.
    :type app_id: str
    :param app_secret: The APP-SECRET to use.
    :type app_secret: str
    :param token_file: The full path and filename where to find the access token.
    :type token_file: str
    :raises: :class:`errors.pytxIniterror`
    """
    global __ACCESS_TOKEN

    # 1. Use the value of the 'TX_ACCESS_TOKEN' environment variable.
    __ACCESS_TOKEN = os.environ.get(te.TX_ACCESS_TOKEN, None)
    if __ACCESS_TOKEN:
        return

    # 2. Use the concatenation of the 'TX_APP_ID' and 'TX_APP_SECRET' environment variables.
    env_app_id = os.environ.get(te.TX_APP_ID, None)
    env_app_secret = os.environ.get(te.TX_APP_SECRET, None)
    if env_app_id and env_app_secret:
        __ACCESS_TOKEN = env_app_id + '|' + env_app_secret
        return

    # 3. Use the first line of the file '$PWD/.pytx' or ~/.pytx'
    filepath = _find_token_file()
    if filepath:
        __ACCESS_TOKEN = _read_token_file(filepath)
        return

    # 4. Use the concatenation of the app_id and app_secret parameters
    if app_id and app_secret:
        __ACCESS_TOKEN = app_id + '|' + app_secret
        return

    # 5. Use the first line of the file 'token_file'
    if token_file:
        __ACCESS_TOKEN = _read_token_file(token_file)
        return

    raise pytxInitError('Unable to set access token.')
Пример #8
0
def _read_token_file(token_file):
    """
    Read a token file. Separated out for easy mocking during unit testing.

    :param token_file: The full path and filename where to find the access token.
    :type token_file: str
    :returns: str
    :raises: :class:`errors.pytxIniterror`    
    """
    try:
        with open(token_file, 'r') as infile:
            return infile.readline().strip()
    except IOError as e:
        raise pytxInitError(str(e))
Пример #9
0
def _read_token_file(token_file):
    """
    Read a token file. Separated out for easy mocking during unit testing.

    :param token_file: The full path and filename where to find the access token.
    :type token_file: str
    :returns: str
    :raises: :class:`errors.pytxIniterror`
    """
    try:
        with open(token_file, 'r') as infile:
            return infile.readline().strip()
    except IOError as e:
        raise pytxInitError(str(e))
Пример #10
0
def get_access_token():
    """
    Returns the existing access token if init() has been called.
    Will attempt to init() in the case that there is no access token.

    :raises: :class:`errors.pytxIniterror` if there is no access token.
    """
    global __ACCESS_TOKEN

    if not __ACCESS_TOKEN:
        init()

    if not __ACCESS_TOKEN:
        raise pytxInitError('Must init() before instantiating')

    return __ACCESS_TOKEN
Пример #11
0
def get_access_token():
    """
    Returns the existing access token if init() has been called.
    Will attempt to init() in the case that there is no access token.

    :raises: :class:`errors.pytxIniterror` if there is no access token.
    """
    global __ACCESS_TOKEN

    if not __ACCESS_TOKEN:
        init()

    if not __ACCESS_TOKEN:
        raise pytxInitError('Must init() before instantiating')

    return __ACCESS_TOKEN
Пример #12
0
def init(app_id, app_secret):
    """
    Use the app_id and app_secret to store the access_token globally for all
    instantiated objects to leverage.

    :param app_id: The APP-ID to use.
    :type app_id: str
    :param app_secret: The APP-SECRET to use.
    :type app_secret: str
    :raises: :class:`errors.pytxIniterror`
    """

    global __ACCESS_TOKEN__
    global Broker
    try:
        __ACCESS_TOKEN__ = app_id + "|" + app_secret
        Broker = b()
    except Exception, e:
        raise pytxInitError("Error generating access token: %s" % str(e))
Пример #13
0
def init(app_id, app_secret):
    """
    Use the app_id and app_secret to store the access_token globally for all
    instantiated objects to leverage.

    :param app_id: The APP-ID to use.
    :type app_id: str
    :param app_secret: The APP-SECRET to use.
    :type app_secret: str
    :raises: :class:`errors.pytxIniterror`
    """

    global __ACCESS_TOKEN__
    global Broker
    try:
        __ACCESS_TOKEN__ = app_id + "|" + app_secret
        Broker = b()
    except Exception, e:
        raise pytxInitError("Error generating access token: %s" % str(e))
Пример #14
0
         __ACCESS_TOKEN__ = app_id + "|" + app_secret
     except Exception, e:
         raise pytxInitError("Error generating access token: %s" % str(e))
 elif token_file:
     try:
         with open(token_file, 'r') as infile:
             token_list = []
             for line in infile:
                 token_list.append(line.strip())
             if len(token_list) == 1:
                 __ACCESS_TOKEN__ = token_list[0]
             elif len(token_list) == 2:
                 __ACCESS_TOKEN__ = token_list[0] + "|" + token_list[1]
             else:
                 raise pytxInitError(
                     "Error generating access token from file: %s" % token_file
                 )
     except Exception, e:
         raise pytxInitError(str(e))
 else:
     access_token = os.environ.get(te.TX_ACCESS_TOKEN, None)
     if access_token is None:
         app_id = os.environ.get(te.TX_APP_ID, None)
         app_secret = os.environ.get(te.TX_APP_SECRET, None)
         if app_id is None or app_secret is None:
             raise pytxInitError(
                 "Environment variables not set."
             )
         else:
             access_token = app_id.strip() + "|" + app_secret.strip()
     __ACCESS_TOKEN__ = access_token.strip()