Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe24773

Browse files
committed
Move validation to it's own file.
1 parent7a1d5f6 commitbe24773

File tree

2 files changed

+151
-151
lines changed

2 files changed

+151
-151
lines changed

‎github3/structs.py

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
fromcollectionsimportIterator
22
fromgithub3.modelsimportGitHubCore,urlparse
3-
fromrequests.compatimportbasestring
4-
fromdatetimeimportdatetime
5-
importre
63

74

85
classGitHubIterator(GitHubCore,Iterator):
@@ -87,151 +84,3 @@ def refresh(self, conditional=False):
8784

8885
defnext(self):
8986
returnself.__next__()
90-
91-
92-
classParameterValidator(dict):
93-
"""This class is used to validate parameters sent to methods.
94-
95-
It will use a slightly strict validation method and be capable of being
96-
passed directly to requests or ``json.dumps``.
97-
98-
"""
99-
100-
def__init__(self,params,schema):
101-
self.params=params
102-
self.update(self.params)
103-
self.schema=schema
104-
self.validate()
105-
106-
defvalidate(self):
107-
params_copy=self.params.copy()
108-
forkeyinparams_copy:
109-
ifkeynotinself.schema:
110-
self.remove_key(key)
111-
# We can not pass extra information onto GitHub
112-
# If we let items pass through that we don't validate, this
113-
# would be a pointless exercise
114-
115-
forkey, (required,validator)inself.schema.items():
116-
ifkeynotinself.params:
117-
continue
118-
value=self.params[key]
119-
ifnotvalidator.is_valid(value):
120-
ifrequired:
121-
raiseValueError(
122-
'Key "{0}" is required but is invalid.'.format(key)
123-
)
124-
self.remove_key(key)
125-
else:
126-
self.set_key(key,validator.convert(value))
127-
128-
defremove_key(self,key):
129-
del(self.params[key],self[key])
130-
131-
defset_key(self,key,value):
132-
self.params[key]=self[key]=value
133-
134-
135-
classBaseValidator(object):
136-
def__init__(self,allow_none=False,sub_schema=None):
137-
self.allow_none=allow_none
138-
self.sub_schema=sub_schemaor {}
139-
140-
defnone(self,o):
141-
ifself.allow_noneandoisNone:
142-
returnTrue
143-
144-
defis_valid(self,obj):
145-
raiseNotImplementedError(
146-
"You can not use the BaseValidator's is_vaild method"
147-
)
148-
149-
150-
classDateValidator(BaseValidator):
151-
# with thanks to
152-
# https://code.google.com/p/jquery-localtime/issues/detail?id=4
153-
ISO_8601=re.compile(
154-
"^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0"
155-
"[1-9]|[1-2][0-9])(T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0"
156-
"-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5]["
157-
"0-9])?)?$"
158-
)
159-
160-
defis_valid(self,timestamp):
161-
ifself.none(timestamp):
162-
returnTrue
163-
164-
return (isinstance(timestamp,datetime)or
165-
(isinstance(timestamp,basestring)and
166-
DateValidator.ISO_8601.match(timestamp)))
167-
168-
defconvert(self,timestamp):
169-
iftimestampisNone:
170-
returnNone
171-
172-
ifisinstance(timestamp,datetime):
173-
returntimestamp.isoformat()
174-
175-
ifisinstance(timestamp,basestring):
176-
ifnotDateValidator.ISO_8601.match(timestamp):
177-
raiseValueError(
178-
("Invalid timestamp: %s is not a valid ISO-8601"
179-
" formatted date")%timestamp)
180-
returntimestamp
181-
182-
raiseValueError(
183-
"Cannot accept type %s for timestamp"%type(timestamp))
184-
185-
186-
classStringValidator(BaseValidator):
187-
defis_valid(self,string):
188-
ifisinstance(string,basestring):
189-
returnTrue
190-
191-
ifnotself.allow_noneandstringisNone:
192-
returnFalse
193-
194-
try:
195-
str(string)
196-
except:
197-
returnFalse
198-
199-
returnTrue
200-
201-
defconvert(self,string):
202-
returnstringifisinstance(string,basestring)elsestr(string)
203-
204-
205-
classDictValidator(BaseValidator):
206-
defis_valid(self,dictionary):
207-
try:
208-
dictionary=dict(dictionary)
209-
exceptValueError:
210-
returnFalse
211-
212-
schema_items=self.sub_schema.items()
213-
returnany(
214-
[v.is_valid(dictionary[k])for (k,v)inschema_items]
215-
)
216-
217-
defconvert(self,dictionary):
218-
dictionary=dict(dictionary)
219-
fork,vinself.sub_schema.items():
220-
ifdictionary.get(k):
221-
dictionary[k]=v.convert(dictionary[k])
222-
returndictionary
223-
224-
225-
classListValidator(BaseValidator):
226-
defis_valid(self,lyst):
227-
try:
228-
lyst=list(lyst)
229-
exceptTypeError:
230-
returnFalse
231-
232-
is_valid=self.sub_schema.is_valid
233-
returnall([is_valid(i)foriinlyst])
234-
235-
defconvert(self,lyst):
236-
convert=self.sub_schema.convert
237-
return [convert(i)foriinlyst]

‎github3/validation.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
fromrequests.compatimportbasestring
2+
fromdatetimeimportdatetime
3+
importre
4+
5+
6+
classParameterValidator(dict):
7+
"""This class is used to validate parameters sent to methods.
8+
9+
It will use a slightly strict validation method and be capable of being
10+
passed directly to requests or ``json.dumps``.
11+
12+
"""
13+
14+
def__init__(self,params,schema):
15+
self.params=params
16+
self.update(self.params)
17+
self.schema=schema
18+
self.validate()
19+
20+
defvalidate(self):
21+
params_copy=self.params.copy()
22+
forkeyinparams_copy:
23+
ifkeynotinself.schema:
24+
self.remove_key(key)
25+
# We can not pass extra information onto GitHub
26+
# If we let items pass through that we don't validate, this
27+
# would be a pointless exercise
28+
29+
forkey, (required,validator)inself.schema.items():
30+
ifkeynotinself.params:
31+
continue
32+
value=self.params[key]
33+
ifnotvalidator.is_valid(value):
34+
ifrequired:
35+
raiseValueError(
36+
'Key "{0}" is required but is invalid.'.format(key)
37+
)
38+
self.remove_key(key)
39+
else:
40+
self.set_key(key,validator.convert(value))
41+
42+
defremove_key(self,key):
43+
del(self.params[key],self[key])
44+
45+
defset_key(self,key,value):
46+
self.params[key]=self[key]=value
47+
48+
49+
classBaseValidator(object):
50+
def__init__(self,allow_none=False,sub_schema=None):
51+
self.allow_none=allow_none
52+
self.sub_schema=sub_schemaor {}
53+
54+
defnone(self,o):
55+
ifself.allow_noneandoisNone:
56+
returnTrue
57+
58+
defis_valid(self,obj):
59+
raiseNotImplementedError(
60+
"You can not use the BaseValidator's is_vaild method"
61+
)
62+
63+
64+
classDateValidator(BaseValidator):
65+
# with thanks to
66+
# https://code.google.com/p/jquery-localtime/issues/detail?id=4
67+
ISO_8601=re.compile(
68+
"^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0"
69+
"[1-9]|[1-2][0-9])(T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0"
70+
"-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5]["
71+
"0-9])?)?$"
72+
)
73+
74+
defis_valid(self,timestamp):
75+
ifself.none(timestamp):
76+
returnTrue
77+
78+
return (isinstance(timestamp,datetime)or
79+
(isinstance(timestamp,basestring)and
80+
DateValidator.ISO_8601.match(timestamp)))
81+
82+
defconvert(self,timestamp):
83+
iftimestampisNone:
84+
returnNone
85+
86+
ifisinstance(timestamp,datetime):
87+
returntimestamp.isoformat()
88+
89+
ifisinstance(timestamp,basestring):
90+
ifnotDateValidator.ISO_8601.match(timestamp):
91+
raiseValueError(
92+
("Invalid timestamp: %s is not a valid ISO-8601"
93+
" formatted date")%timestamp)
94+
returntimestamp
95+
96+
raiseValueError(
97+
"Cannot accept type %s for timestamp"%type(timestamp))
98+
99+
100+
classStringValidator(BaseValidator):
101+
defis_valid(self,string):
102+
ifisinstance(string,basestring):
103+
returnTrue
104+
105+
ifnotself.allow_noneandstringisNone:
106+
returnFalse
107+
108+
try:
109+
str(string)
110+
except:
111+
returnFalse
112+
113+
returnTrue
114+
115+
defconvert(self,string):
116+
returnstringifisinstance(string,basestring)elsestr(string)
117+
118+
119+
classDictValidator(BaseValidator):
120+
defis_valid(self,dictionary):
121+
try:
122+
dictionary=dict(dictionary)
123+
exceptValueError:
124+
returnFalse
125+
126+
schema_items=self.sub_schema.items()
127+
returnany(
128+
[v.is_valid(dictionary[k])for (k,v)inschema_items]
129+
)
130+
131+
defconvert(self,dictionary):
132+
dictionary=dict(dictionary)
133+
fork,vinself.sub_schema.items():
134+
ifdictionary.get(k):
135+
dictionary[k]=v.convert(dictionary[k])
136+
returndictionary
137+
138+
139+
classListValidator(BaseValidator):
140+
defis_valid(self,lyst):
141+
try:
142+
lyst=list(lyst)
143+
exceptTypeError:
144+
returnFalse
145+
146+
is_valid=self.sub_schema.is_valid
147+
returnall([is_valid(i)foriinlyst])
148+
149+
defconvert(self,lyst):
150+
convert=self.sub_schema.convert
151+
return [convert(i)foriinlyst]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp