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  """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://www.smugmug.com>} 
 29  """ 
 30   
 31  __version__ = "0.4" 
 32   
 33  from pysmug.methods import apikeys 
 34   
35 -def smugmug_keywords(fn):
36 """Prepare the keywords for sending to SmugMug. 37 38 The following operations are performed:: 39 1. If the key is "method", continue. 40 2. If the key starts with an upper case letter, continue. 41 3. If the key is in {methods.apikeys}, replace the key. 42 4. If the key ends with {id}, upper case the first letter 43 and {ID} and replace the key. 44 5. Else, upper case the first letter only and replace the 45 key. 46 47 @param fn: the decorated function 48 """ 49 def mg(*args, **kwargs): 50 items = kwargs.items() 51 for k, v, in items: 52 if k == "method": 53 continue 54 if k[0].isupper(): 55 continue 56 lk = k.lower() 57 if lk in apikeys: 58 key, func = apikeys[lk] 59 del kwargs[k] 60 kwargs[key] = func(v) if func else v 61 else: 62 del kwargs[k] 63 if lk.endswith("id"): 64 kwargs[lk[:-2].title() + "ID"] = v 65 else: 66 kwargs[lk.title()] = v 67 return fn(*args, **kwargs)
68 return mg 69 70 # the imports are here because the smugmug_keywords decorator needs to be 71 # available prior to import Smug* 72 from pysmug.smugmug import SmugMug, SmugBatch, SmugMugException, HTTPException 73 from pysmug.smugtool import SmugTool 74
75 -def login(conf=None, klass=None, proxy=None):
76 """Login to smugmug using the contents of the configuration file. 77 78 If no configuration file is specified then a file named C{.pysmugrc} in 79 the user's home directory is used if it exists. 80 81 The format is a standard configuration parseable by C{ConfigParser}. The main 82 section C{pysmug} is required. The key C{login} references which section to use 83 for authentication with SmugMug. The key C{smugmug} is optional and can specify 84 an alternate C{SmugMug} class to instantiate. This is an example file:: 85 86 [pysmug] 87 login=login_withHash 88 smugmug=pysmug.SmugTool 89 90 [login_withHash] 91 APIKey = <my api key> 92 userId = <my user id> 93 passwordHash = <my password hash> 94 95 [login_anonymously] 96 APIKey = <my api key> 97 98 @type conf: string 99 @param conf: path to a configuration file 100 @type klass: C{SmugMug} class 101 @param klass: class to instantiate 102 @param proxy: address of proxy server if one is required (http[s]://localhost[:8080]) 103 @raise ValueError: if no configuration file is found 104 """ 105 import os 106 from ConfigParser import ConfigParser 107 108 if not conf: 109 home = os.environ.get("HOME", None) 110 if not home: 111 raise ValueError("unknown home directory") 112 conf = os.path.join(home, ".pysmugrc") 113 if not os.path.exists(conf): 114 raise ValueError("'%s' not found" % (conf)) 115 116 config = ConfigParser() 117 config.optionxform = str 118 config.read(conf) 119 120 if not klass: 121 if config.has_option("pysmug", "smugmug"): 122 path = config.get("pysmug", "smugmug") 123 i = path.rfind(".") 124 module, attr = path[:i], path[i+1:] 125 mod = __import__(module, globals(), locals(), [attr]) 126 klass = getattr(mod, attr) 127 else: 128 klass = SmugMug 129 m = klass(proxy=proxy) 130 131 auth = config.get("pysmug", "login") 132 keys = dict(config.items(auth)) 133 return getattr(m, auth)(**keys)
134