1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
71
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