aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVikraman Choudhury <vikraman.choudhury@gmail.com>2011-07-25 18:04:52 +0530
committerVikraman Choudhury <vikraman.choudhury@gmail.com>2011-07-25 18:04:52 +0530
commit3c459b75e6d5c193fda884068fb749c1b99c6e32 (patch)
tree87dbdac2920c38cecf7b64cccf4bf0d5c217b828
parentadd package search feature (diff)
downloadgentoostats-3c459b75e6d5c193fda884068fb749c1b99c6e32.tar.gz
gentoostats-3c459b75e6d5c193fda884068fb749c1b99c6e32.tar.bz2
gentoostats-3c459b75e6d5c193fda884068fb749c1b99c6e32.zip
export json from server
-rw-r--r--server/arch.py6
-rw-r--r--server/feature.py6
-rw-r--r--server/helpers.py12
-rw-r--r--server/host.py5
-rw-r--r--server/kwd.py6
-rw-r--r--server/lang.py6
-rw-r--r--server/mirror.py6
-rw-r--r--server/package.py20
-rw-r--r--server/profile.py6
-rw-r--r--server/repo.py6
-rw-r--r--server/search.py6
11 files changed, 72 insertions, 13 deletions
diff --git a/server/arch.py b/server/arch.py
index 8b9ef99..20dde69 100644
--- a/server/arch.py
+++ b/server/arch.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Arch(object):
@@ -7,4 +8,7 @@ class Arch(object):
arch_data = dict()
for t in arch_count:
arch_data[t['ARCH']] = {'HOSTS':t['HOSTS']}
- return render.arch(arch_data)
+ if helpers.is_json_request():
+ return helpers.serialize(arch_data)
+ else:
+ return render.arch(arch_data)
diff --git a/server/feature.py b/server/feature.py
index d7674f7..1ea1c36 100644
--- a/server/feature.py
+++ b/server/feature.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Feature(object):
@@ -7,4 +8,7 @@ class Feature(object):
feature_data = dict()
for t in feature_count:
feature_data[t['FEATURE']] = {'HOSTS':t['HOSTS']}
- return render.feature(feature_data)
+ if helpers.is_json_request():
+ return helpers.serialize(feature_data)
+ else:
+ return render.feature(feature_data)
diff --git a/server/helpers.py b/server/helpers.py
index 28d15a2..a5977be 100644
--- a/server/helpers.py
+++ b/server/helpers.py
@@ -1,4 +1,6 @@
+import web
+import json
import uuid
import re
from portage.versions import catpkgsplit
@@ -106,3 +108,13 @@ def get_rkey(db, repo):
else:
rkey = db.insert('REPOSITORIES', REPO=repo)
return rkey
+
+def is_json_request():
+ return web.ctx.environ['HTTP_ACCEPT'].find('json') != -1
+
+def serialize(object, human=True):
+ if human:
+ indent = 2
+ else:
+ indent = None
+ return json.JSONEncoder(indent=indent).encode(object)
diff --git a/server/host.py b/server/host.py
index 6d1f6f3..fab706c 100644
--- a/server/host.py
+++ b/server/host.py
@@ -55,7 +55,10 @@ class Host(object):
cpv = p['CAT'] + '/' + p['PKG'] + '-' + p['VER']
host_data['PACKAGES'][cpv] = dict()
- return render.host(host_data)
+ if helpers.is_json_request():
+ return helpers.serialize(host_data)
+ else:
+ return render.host(host_data)
def POST(self, str_uuid):
post_data = json.JSONDecoder().decode(web.data())
diff --git a/server/kwd.py b/server/kwd.py
index 6cef893..b8c50cd 100644
--- a/server/kwd.py
+++ b/server/kwd.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Keyword(object):
@@ -7,4 +8,7 @@ class Keyword(object):
keyword_data = dict()
for t in keyword_count:
keyword_data[t['KEYWORD']] = {'HOSTS':t['HOSTS'], 'PACKAGES':t['PACKAGES']}
- return render.keyword(keyword_data)
+ if helpers.is_json_request():
+ return helpers.serialize(keyword_data)
+ else:
+ return render.keyword(keyword_data)
diff --git a/server/lang.py b/server/lang.py
index e8c2d90..de6c625 100644
--- a/server/lang.py
+++ b/server/lang.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Lang(object):
@@ -7,4 +8,7 @@ class Lang(object):
lang_data = dict()
for t in lang_count:
lang_data[t['LANG']] = {'HOSTS':t['HOSTS']}
- return render.lang(lang_data)
+ if helpers.is_json_request():
+ return helpers.serialize(lang_data)
+ else:
+ return render.lang(lang_data)
diff --git a/server/mirror.py b/server/mirror.py
index c8b44c2..e701404 100644
--- a/server/mirror.py
+++ b/server/mirror.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Mirror(object):
@@ -7,4 +8,7 @@ class Mirror(object):
mirror_data = dict()
for t in mirror_count:
mirror_data[t['MIRROR']] = {'HOSTS':t['HOSTS']}
- return render.mirror(mirror_data)
+ if helpers.is_json_request():
+ return helpers.serialize(mirror_data)
+ else:
+ return render.mirror(mirror_data)
diff --git a/server/package.py b/server/package.py
index adc2d94..aa037e5 100644
--- a/server/package.py
+++ b/server/package.py
@@ -38,7 +38,10 @@ class Package(object):
'CPV_COUNT':p_tuple['CPV_COUNT'],
'TOP_C':self.__top(top)
}
- return render.package(p_data)
+ if helpers.is_json_request():
+ return helpers.serialize(p_data)
+ else:
+ return render.package(p_data)
def __GET_C(self, top, cat):
p_query = db.query('SELECT COUNT(DISTINCT UUID) AS HOST_COUNT, \
@@ -54,7 +57,10 @@ class Package(object):
'CPV_COUNT':p_tuple['CPV_COUNT'],
'TOP_CP':self.__top(top, cat)
}
- return render.package_c(cat, p_data)
+ if helpers.is_json_request():
+ return helpers.serialize(p_data)
+ else:
+ return render.package_c(cat, p_data)
def __GET_CP(self, top, cat, pkg):
p_query = db.query('SELECT COUNT(DISTINCT UUID) AS HOST_COUNT, \
@@ -68,7 +74,10 @@ class Package(object):
'CPV_COUNT':p_tuple['CPV_COUNT'],
'TOP_CPV':self.__top(top, cat, pkg)
}
- return render.package_cp(cat, pkg, p_data)
+ if helpers.is_json_request():
+ return helpers.serialize(p_data)
+ else:
+ return render.package_cp(cat, pkg, p_data)
def __GET_CPV(self, cat, pkg, ver):
p_query = db.query('SELECT COUNT(DISTINCT UUID) AS HOST_COUNT\
@@ -79,7 +88,10 @@ class Package(object):
p_data = {
'HOST_COUNT':p_tuple['HOST_COUNT'],
}
- return render.package_cpv(cat, pkg, ver, p_data)
+ if helpers.is_json_request():
+ return helpers.serialize(p_data)
+ else:
+ return render.package_cpv(cat, pkg, ver, p_data)
def __top(self, count, *args):
t_list = list()
diff --git a/server/profile.py b/server/profile.py
index c0a0fc5..798c90d 100644
--- a/server/profile.py
+++ b/server/profile.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Profile(object):
@@ -7,4 +8,7 @@ class Profile(object):
profile_data = dict()
for t in profile_count:
profile_data[t['PROFILE']] = {'HOSTS':t['HOSTS']}
- return render.profile(profile_data)
+ if helpers.is_json_request():
+ helpers.serialize(profile_data)
+ else:
+ return render.profile(profile_data)
diff --git a/server/repo.py b/server/repo.py
index 5db221a..7be99ac 100644
--- a/server/repo.py
+++ b/server/repo.py
@@ -1,4 +1,5 @@
+import helpers
from config import render, db
class Repo(object):
@@ -7,4 +8,7 @@ class Repo(object):
repo_data = dict()
for t in repo_count:
repo_data[t['REPO']] = {'HOSTS':t['HOSTS'], 'PACKAGES':t['PACKAGES']}
- return render.repo(repo_data)
+ if helpers.is_json_request():
+ return helpers.serialize(repo_data)
+ else:
+ return render.repo(repo_data)
diff --git a/server/search.py b/server/search.py
index 69170ac..7ddcded 100644
--- a/server/search.py
+++ b/server/search.py
@@ -1,6 +1,7 @@
import web
import string
+import helpers
from config import render, db
what = ['CAT', 'PKG', 'VER', 'REPO', 'COUNT(DISTINCT UUID) AS HOSTS']
@@ -33,7 +34,10 @@ class Search(object):
'repo':self.args.repo,
'min_hosts':self.min_hosts,
'max_hosts':self.max_hosts})
- return render.search(search_tuples)
+ if helpers.is_json_request():
+ return helpers.serialize(search_tuples)
+ else:
+ return render.search(search_tuples)
def _build_query(self, where, having):
sep = ' '