-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgjsonhandler.py
More file actions
executable file
·54 lines (45 loc) · 2.2 KB
/
imgjsonhandler.py
File metadata and controls
executable file
·54 lines (45 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env python
import time
import json
import logging.handlers
import logging
from imgprocessor import imgprocessor
class imgjsonhandler(object):
'''the class is used for processing of the image dynamically and accepts the name of the original image
the screensize and the bandwidth. This makes a call to class:imgprocessor and the returned file name is converted
to json format the output format given is application/json which the consuming application can set if doing an inline
rendering into browser etc
'''
'''This can be used in Django or CGI gateway for making calls and getting JSON while generating the image locally'''
'''Initialize the logger to log information'''
mylogger = logging.getLogger('jsonimagehandler')
handler = logging.handlers.RotatingFileHandler('jsonimagehandler.log', 'a', maxBytes=10000000, backupCount=5)
mylogger.addHandler(handler)
def outputjson(self,filename,size,bandwidth,returnfullpath):
try:
imgprocess = imgprocessor()
jsondata = json.dumps({})
self.mylogger.info('processing for {}, {}, {}'.format(filename,size,bandwidth))
imgname = imgprocess.generate(filename=filename,size=size,bandwidth=bandwidth,returnFullpath=returnfullpath)
self.mylogger.info('processed')
data = {"path": imgname, "key": "{}_{}_{}".format(filename,size,bandwidth)}
jsondata = json.dumps(data)
except Exception as ex:
self.mylogger.exception(ex)
finally:
return jsondata
if __name__ == '__main__':
imgjson = imgjsonhandler()
'''Handle or get the values here from the request handler and set the same'''
'''Replace this with input from handler'''
filename = "nasa.jpeg"
size= 720
band='2g'
returnfullpath = False
'''the output automatically will be of json with content type and values set
the output would have 2 keys.
path is imagename & key is unique key for the image & screen, size combination to handle in edge servers like varnish
'''
print('content-type:application/json')
output = imgjson.outputjson(filename,size=size,bandwidth=band, returnfullpath=returnfullpath)
print(output)