83
74
def milliseconds_to_string(ms):
84
75
td = datetime.timedelta(0, 0, 0, ms)
85
76
return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
86
.format(days = "{}T".format(td.days) if td.days else "",
77
.format(days = "{0}T".format(td.days) if td.days else "",
87
78
hours = td.seconds // 3600,
88
79
minutes = (td.seconds % 3600) // 60,
89
80
seconds = td.seconds % 60,
93
def rfc3339_duration_to_delta(duration):
94
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
96
>>> rfc3339_duration_to_delta("P7D")
98
>>> rfc3339_duration_to_delta("PT60S")
99
datetime.timedelta(0, 60)
100
>>> rfc3339_duration_to_delta("PT60M")
101
datetime.timedelta(0, 3600)
102
>>> rfc3339_duration_to_delta("PT24H")
103
datetime.timedelta(1)
104
>>> rfc3339_duration_to_delta("P1W")
105
datetime.timedelta(7)
106
>>> rfc3339_duration_to_delta("PT5M30S")
107
datetime.timedelta(0, 330)
108
>>> rfc3339_duration_to_delta("P1DT3M20S")
109
datetime.timedelta(1, 200)
112
# Parsing an RFC 3339 duration with regular expressions is not
113
# possible - there would have to be multiple places for the same
114
# values, like seconds. The current code, while more esoteric, is
115
# cleaner without depending on a parsing library. If Python had a
116
# built-in library for parsing we would use it, but we'd like to
117
# avoid excessive use of external libraries.
119
# New type for defining tokens, syntax, and semantics all-in-one
120
Token = collections.namedtuple("Token",
121
("regexp", # To match token; if
122
# "value" is not None,
123
# must have a "group"
125
"value", # datetime.timedelta or
127
"followers")) # Tokens valid after
129
# RFC 3339 "duration" tokens, syntax, and semantics; taken from
130
# the "duration" ABNF definition in RFC 3339, Appendix A.
131
token_end = Token(re.compile(r"$"), None, frozenset())
132
token_second = Token(re.compile(r"(\d+)S"),
133
datetime.timedelta(seconds=1),
134
frozenset((token_end,)))
135
token_minute = Token(re.compile(r"(\d+)M"),
136
datetime.timedelta(minutes=1),
137
frozenset((token_second, token_end)))
138
token_hour = Token(re.compile(r"(\d+)H"),
139
datetime.timedelta(hours=1),
140
frozenset((token_minute, token_end)))
141
token_time = Token(re.compile(r"T"),
143
frozenset((token_hour, token_minute,
145
token_day = Token(re.compile(r"(\d+)D"),
146
datetime.timedelta(days=1),
147
frozenset((token_time, token_end)))
148
token_month = Token(re.compile(r"(\d+)M"),
149
datetime.timedelta(weeks=4),
150
frozenset((token_day, token_end)))
151
token_year = Token(re.compile(r"(\d+)Y"),
152
datetime.timedelta(weeks=52),
153
frozenset((token_month, token_end)))
154
token_week = Token(re.compile(r"(\d+)W"),
155
datetime.timedelta(weeks=1),
156
frozenset((token_end,)))
157
token_duration = Token(re.compile(r"P"), None,
158
frozenset((token_year, token_month,
159
token_day, token_time,
161
# Define starting values
162
value = datetime.timedelta() # Value so far
164
followers = frozenset((token_duration,)) # Following valid tokens
165
s = duration # String left to parse
166
# Loop until end token is found
167
while found_token is not token_end:
168
# Search for any currently valid tokens
169
for token in followers:
170
match = token.regexp.match(s)
171
if match is not None:
173
if token.value is not None:
174
# Value found, parse digits
175
factor = int(match.group(1), 10)
176
# Add to value so far
177
value += factor * token.value
178
# Strip token from string
179
s = token.regexp.sub("", s, 1)
182
# Set valid next tokens
183
followers = found_token.followers
186
# No currently valid tokens were found
187
raise ValueError("Invalid RFC 3339 duration")
192
83
def string_to_delta(interval):
193
84
"""Parse a string and return a datetime.timedelta
316
201
parser.add_argument("--approval-duration",
317
202
help="Set duration of one client approval")
318
203
parser.add_argument("-H", "--host", help="Set host for client")
319
parser.add_argument("-s", "--secret",
320
type=argparse.FileType(mode="rb"),
204
parser.add_argument("-s", "--secret", type=file,
321
205
help="Set password blob (file) for client")
322
206
parser.add_argument("-A", "--approve", action="store_true",
323
207
help="Approve any current client request")
324
208
parser.add_argument("-D", "--deny", action="store_true",
325
209
help="Deny any current client request")
326
parser.add_argument("--check", action="store_true",
327
help="Run self-test")
328
210
parser.add_argument("client", nargs="*", help="Client name")
329
211
options = parser.parse_args()
374
252
if options.all or not options.client:
375
clients = { bus.get_object(busname, path): properties
376
for path, properties in mandos_clients.items() }
253
clients = dict((bus.get_object(busname, path), properties)
254
for path, properties in
255
mandos_clients.iteritems())
378
257
for name in options.client:
379
for path, client in mandos_clients.items():
258
for path, client in mandos_clients.iteritems():
380
259
if client["Name"] == name:
381
260
client_objc = bus.get_object(busname, path)
382
261
clients[client_objc] = client
385
print("Client not found on server: {!r}"
264
print("Client not found on server: {0!r}"
386
265
.format(name), file=sys.stderr)