pysmug

Package pysmug |
[hide private]
[frames] | no frames]

# Source Code for Package pysmug

     1  # Copyright (c) 2008 Brian Zimmer <bzimmer@ziclix.com>
     2  #
     3  # Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  # this software and associated documentation files (the "Software"), to deal in
     5  # the Software without restriction, including without limitation the rights to
     6  # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     7  # of the Software, and to permit persons to whom the Software is furnished to do
     8  # so, subject to the following conditions:
     9  #
    10  # The above copyright notice and this permission notice shall be included in all
    11  # copies or substantial portions of the Software.
    12  #
    13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    19  # SOFTWARE.
    20
    21  r"""A high-performance client to the SmugMug API.
    22
    23   This client supports the entire set of methods available through smugmug
    24   both serially and in batch.
    25
    26   References:
    27     - U{pysmug <http://code.google.com/p/pysmug>}
    28     - U{SmugMug API <http://smugmug.jot.com/WikiHome/API/Versions/1.2.1>}
    29  """
    30
    31  __version__ = "0.3"
    32
    33  from pysmug.smugmug import SmugMug, SmugBatch, SmugMugException, HTTPException
    34
    35 -def login(conf=None, klass=SmugMug, proxy=None):
    36    """Login to smugmug using the contents of the configuration file.
    37
    38    If no configuration file specified then a file named C{.pysmugrc} in
    39    the user's home directory is used if it exists.
    40
    41    The following order determines the C{login} method used:
    42
    43      - B{In all cases C{APIKey} is required.}
    44
    45      1. If C{PasswordHash} is in configuration, then C{login_withHash} is used.
    46        - C{UserID} is additionally required.
    47      2. If C{Password} is in configuration, then C{login_withPassword} is used.
    48        - C{EmailAddress} is additionally required.
    49      3. Else C{login_anonymously} is used.
    50
    51    @param conf: path to a configuration file
    52    @type klass: C{SmugMug} class
    53    @param klass: class to instantiate
    54    @param proxy: address of proxy server if one is required (http[s]://localhost[:8080])
    55    @raise ValueError: if no configuration file is found
    56    """
    57
    58    import os
    59    if not conf:
    60      home = os.environ.get("HOME", None)
    61      if not home:
    62        raise ValueError("unknown home directory")
    63      conf = os.path.join(home, ".pysmugrc")
    64      if not os.path.exists(conf):
    65        raise ValueError("'%s' not found" % (conf))
    66    config = eval(open(conf).read())
    67    m = klass(proxy=proxy)
    68    keys = set(x.lower() for x in config.keys())
    69    if "passwordhash" in keys:
    70      return m.login_withHash(**config)
    71    elif "password" in keys:
    72      return m.login_withPassword(**config)
    73    return m.login_anonymously(**config)
    74