josh-gmail-backup 0.104

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+ # Copyright (C) 2008 Jan Svec and Filip Jurcicek
2
+ #
3
+ # YOU USE THIS TOOL ON YOUR OWN RISK!
4
+ #
5
+ # email: info@gmail-backup.com
6
+ #
7
+ #
8
+ # Disclaimer of Warranty
9
+ # ----------------------
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, licensor provides
12
+ # this tool (and each contributor provides its contributions) on an "AS IS"
13
+ # BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
+ # implied, including, without limitation, any warranties or conditions of
15
+ # TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
16
+ # PURPOSE. You are solely responsible for determining the appropriateness of
17
+ # using this work and assume any risks associated with your exercise of
18
+ # permissions under this license.
19
+
20
+ def Flag(arg):
21
+ """Flag conversion function
22
+ """
23
+ argl = arg.lower()
24
+ if argl in ['1', 'on', 'true']:
25
+ return True
26
+ elif argl in ['0', 'off', 'false']:
27
+ return False
28
+ else:
29
+ raise ValueError("Not boolean argument: %r" % argl)
30
+
31
+ Bool = Flag
32
+
33
+ def Integer(arg):
34
+ """Integer conversion function
35
+
36
+ :Parameters:
37
+ - `arg`: string to convert
38
+
39
+ :Returns: converted object
40
+ :RType: int
41
+ """
42
+ return int(arg)
43
+
44
+ def Float(arg):
45
+ """Float conversion function
46
+
47
+ :Parameters:
48
+ - `arg`: string to convert
49
+
50
+ :Returns: converted object
51
+ :RType: float
52
+ """
53
+ return float(arg)
54
+
55
+ def String(arg):
56
+ """String conversion function (ie. no conversion)
57
+
58
+ :Parameters:
59
+ - `arg`: string to convert
60
+
61
+ :Returns: converted object
62
+ :RType: str
63
+ """
64
+ return arg
65
+
66
+ def ListOf(arg, type, separator=','):
67
+ """List conversion function
68
+
69
+ String ``arg`` is splited by ``separator``. On each resulting item is
70
+ applied conversion function ``type``.
71
+
72
+ :Parameters:
73
+ - `arg`: string to convert
74
+ - `type`: type of each item in list
75
+ - `separator`: separator of items
76
+ :Returns: converted objects
77
+ :RType: list
78
+ """
79
+ arg = arg.split(separator)
80
+ return [type(i) for i in arg]
81
+
82
+ def DictOf(d, template, separators=':,'):
83
+ """Dictionary conversion function
84
+
85
+ Converts string of key:value pairs into dictionary. Values of dictionary
86
+ are converted using conversion functions in ``template``.
87
+
88
+ :Parameters:
89
+ - `template`: dictionary describing resulting dictionary
90
+ - `separators`: 2-char string, 1st char specifies separator between
91
+ keys and values, 2nd char is separator between keys:values pairs
92
+ :Returns: converted dictionary corresponding to ``template``
93
+ :RType: dict
94
+ """
95
+ sep_key, sep_item = separators
96
+ d = [tuple(i.split(sep_key)) for i in d.split(sep_item)]
97
+ d = dict(d)
98
+ ret = {}
99
+ for key, val in d.iteritems():
100
+ try:
101
+ conversion = template[key]
102
+ except KeyError:
103
+ raise ValueError, 'Unknown key %r' % key
104
+ ret[key] = conversion(val)
105
+ return ret
106
+
107
+ def FileInput(f, mode='', type=file):
108
+ """Input file conversion function
109
+
110
+ This function opens file with name ``f`` for reading. Additional file modes
111
+ can be specified using ``mode`` parameter.
112
+
113
+ :Parameters:
114
+ - `f`: File name (option value)
115
+ - `mode`: Additional file mode (eg. ``'b'`` for binary access). ``'r'``
116
+ is automatically added to this mode.
117
+ - `type`: Type of opened file. You can for example use another file
118
+ type as StringIO.
119
+
120
+ :Returns: Created file object
121
+ :RType: file
122
+ """
123
+ mode = 'r' + mode
124
+ return type(f, mode)
125
+
126
+ def FileOutput(f, mode='', type=file):
127
+ """Output file conversion function
128
+
129
+ This function opens file with name ``f`` for writing. Additional file modes
130
+ can be specified using ``mode`` parameter. It is useful for writing return
131
+ values of main function.
132
+
133
+ :Parameters:
134
+ - `f`: File name (option value)
135
+ - `mode`: Additional file mode (eg. ``'b'`` for binary access). ``'w'``
136
+ is automatically added to this mode.
137
+ - `type`: Type of opened file. You can for example use another file
138
+ type as StringIO.
139
+
140
+ :Returns: Created file object
141
+ :RType: file
142
+ """
143
+ mode = 'w' + mode
144
+ return type(f, mode)
145
+
146
+ def ScriptInput(f, mode='', type=file):
147
+ if f == '-':
148
+ return sys.stdin
149
+ else:
150
+ return FileInput(f, mode, type)
151
+
152
+ def ScriptOutput(f, mode='', type=file):
153
+ if f == '-':
154
+ return sys.stdout
155
+ else:
156
+ return FileOutput(f, mode, type)
157
+