Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chenhao Ma
core
Commits
bb1f25d1
Commit
bb1f25d1
authored
8 years ago
by
Nathaniel Kofalt
Browse files
Options
Downloads
Patches
Plain Diff
Fix import issue
parent
f24a195b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/api.py
+3
-11
3 additions, 11 deletions
api/api.py
api/encoder.py
+42
-0
42 additions, 0 deletions
api/encoder.py
api/placer.py
+3
-2
3 additions, 2 deletions
api/placer.py
api/util.py
+0
-26
0 additions, 26 deletions
api/util.py
with
48 additions
and
39 deletions
api/api.py
+
3
−
11
View file @
bb1f25d1
...
...
@@ -11,6 +11,7 @@ from . import base
from
.jobs.jobs
import
Job
from
.jobs.handlers
import
JobsHandler
,
JobHandler
from
.dao.containerutil
import
FileReference
,
ContainerReference
from
.
import
encoder
from
.
import
root
from
.
import
util
from
.
import
config
...
...
@@ -106,7 +107,7 @@ class Config(base.RequestHandler):
self
.
response
.
write
(
'
config =
'
+
json
.
dumps
(
self
.
get
(),
sort_keys
=
True
,
indent
=
4
,
separators
=
(
'
,
'
,
'
:
'
),
default
=
util
.
custom_json_serializer
,)
+
json
.
dumps
(
self
.
get
(),
sort_keys
=
True
,
indent
=
4
,
separators
=
(
'
,
'
,
'
:
'
),
default
=
encoder
.
custom_json_serializer
,)
+
'
;
'
)
...
...
@@ -261,20 +262,11 @@ routes = [
]
def
custom_json_serializer
(
obj
):
if
isinstance
(
obj
,
bson
.
objectid
.
ObjectId
):
return
str
(
obj
)
elif
isinstance
(
obj
,
datetime
.
datetime
):
return
pytz
.
timezone
(
'
UTC
'
).
localize
(
obj
).
isoformat
()
elif
isinstance
(
obj
,
Job
):
return
obj
.
map
()
raise
TypeError
(
repr
(
obj
)
+
"
is not JSON serializable
"
)
def
dispatcher
(
router
,
request
,
response
):
try
:
rv
=
router
.
default_dispatcher
(
request
,
response
)
if
rv
is
not
None
:
response
.
write
(
json
.
dumps
(
rv
,
default
=
custom_json_serializer
))
response
.
write
(
json
.
dumps
(
rv
,
default
=
encoder
.
custom_json_serializer
))
response
.
headers
[
'
Content-Type
'
]
=
'
application/json; charset=utf-8
'
except
webapp2
.
exc
.
HTTPException
as
e
:
util
.
send_json_http_exception
(
response
,
str
(
e
),
e
.
code
)
...
...
This diff is collapsed.
Click to expand it.
api/encoder.py
0 → 100644
+
42
−
0
View file @
bb1f25d1
import
bson.objectid
import
datetime
import
json
import
pytz
from
.jobs.jobs
import
Job
def
custom_json_serializer
(
obj
):
if
isinstance
(
obj
,
bson
.
objectid
.
ObjectId
):
return
str
(
obj
)
elif
isinstance
(
obj
,
datetime
.
datetime
):
return
pytz
.
timezone
(
'
UTC
'
).
localize
(
obj
).
isoformat
()
elif
isinstance
(
obj
,
Job
):
return
obj
.
map
()
raise
TypeError
(
repr
(
obj
)
+
"
is not JSON serializable
"
)
def
sse_pack
(
d
):
"""
Format a map with Server-Sent-Event-meaningful keys into a string for transport.
Happily borrowed from: http://taoofmac.com/space/blog/2014/11/16/1940
For reading on web usage: http://www.html5rocks.com/en/tutorials/eventsource/basics
For reading on the format: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
"""
buffer
=
''
for
k
in
[
'
retry
'
,
'
id
'
,
'
event
'
,
'
data
'
]:
if
k
in
d
.
keys
():
buffer
+=
'
%s: %s
\n
'
%
(
k
,
d
[
k
])
return
buffer
+
'
\n
'
def
json_sse_pack
(
d
):
"""
Variant of sse_pack that will json-encode your data blob.
"""
d
[
'
data
'
]
=
json
.
dumps
(
d
[
'
data
'
],
default
=
custom_json_serializer
)
return
sse_pack
(
d
)
This diff is collapsed.
Click to expand it.
api/placer.py
+
3
−
2
View file @
bb1f25d1
...
...
@@ -9,6 +9,7 @@ import zipfile
from
.
import
base
from
.
import
config
from
.
import
encoder
from
.
import
files
from
.jobs
import
rules
from
.
import
tempdir
as
tempfile
...
...
@@ -355,7 +356,7 @@ class PackfilePlacer(Placer):
# Report progress
complete
+=
1
yield
util
.
json_sse_pack
({
yield
encoder
.
json_sse_pack
({
'
event
'
:
'
progress
'
,
'
data
'
:
{
'
done
'
:
complete
,
'
total
'
:
max
,
'
percent
'
:
(
complete
/
float
(
max
))
*
100
},
})
...
...
@@ -470,7 +471,7 @@ class PackfilePlacer(Placer):
}
# Report result
yield
util
.
json_sse_pack
({
yield
encoder
.
json_sse_pack
({
'
event
'
:
'
result
'
,
'
data
'
:
result
,
})
This diff is collapsed.
Click to expand it.
api/util.py
+
0
−
26
View file @
bb1f25d1
...
...
@@ -198,29 +198,3 @@ def mkdir_p(path):
pass
else
:
raise
def
sse_pack
(
d
):
"""
Format a map with Server-Sent-Event-meaningful keys into a string for transport.
Happily borrowed from: http://taoofmac.com/space/blog/2014/11/16/1940
For reading on web usage: http://www.html5rocks.com/en/tutorials/eventsource/basics
For reading on the format: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
"""
buffer
=
''
for
k
in
[
'
retry
'
,
'
id
'
,
'
event
'
,
'
data
'
]:
if
k
in
d
.
keys
():
buffer
+=
'
%s: %s
\n
'
%
(
k
,
d
[
k
])
return
buffer
+
'
\n
'
def
json_sse_pack
(
d
):
"""
Variant of sse_pack that will json-encode your data blob.
"""
d
[
'
data
'
]
=
json
.
dumps
(
d
[
'
data
'
],
default
=
custom_json_serializer
)
return
sse_pack
(
d
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment