@@ -18,12 +18,14 @@ class Codelet(object): | |||
code was last modified. | |||
:ivar rank: (float) A quanitification of the source code's quality, as | |||
per available ratings (stars, forks, upvotes, etc.). | |||
:ivar symbols: (dict) Dictionary containing dictionaries of functions, classes, | |||
variable definitions, etc. | |||
:ivar symbols: (dict) Dictionary containing dictionaries of functions, | |||
classes, variable definitions, etc. | |||
:ivar origin: (tuple) 3-tuple of (site_name, site_url, image_blob), as | |||
added by the database. | |||
""" | |||
def __init__(self, name, code, filename, language, authors, code_url, | |||
date_created, date_modified, rank): | |||
date_created, date_modified, rank, symbols=None, origin=None): | |||
""" | |||
Create a Codelet instance. | |||
@@ -36,6 +38,8 @@ class Codelet(object): | |||
:param date_created: see :attr:`self.date_created` | |||
:param date_modified: see :attr:`self.date_modified` | |||
:param rank: see :attr:`self.rank` | |||
:param symbols: see :attr:`self.symbols` | |||
:param origin: see :attr:`self.origin` | |||
:type name: see :attr:`self.name` | |||
:type code: see :attr:`self.code` | |||
@@ -46,6 +50,8 @@ class Codelet(object): | |||
:type date_created: see :attr:`self.date_created` | |||
:type date_modified: see :attr:`self.date_modified` | |||
:type rank: see :attr:`self.rank` | |||
:type symbols: see :attr:`self.symbols` | |||
:type origin: see :attr:`self.origin` | |||
""" | |||
self.name = name | |||
@@ -57,3 +63,5 @@ class Codelet(object): | |||
self.date_created = date_created | |||
self.date_modified = date_modified | |||
self.rank = rank | |||
self.symbols = symbols or {} | |||
self.origin = origin or (None, None, None) |
@@ -65,10 +65,30 @@ class Database(object): | |||
num_results = 0 # TODO: NotImplemented | |||
return ids, num_results | |||
def _get_codelets_from_ids(self, cursor, ids): | |||
def _get_codelets_from_ids(self, ids): | |||
"""Return a list of Codelet objects given a list of codelet IDs.""" | |||
# TODO: remember that codelets need an origin field | |||
raise NotImplementedError() # TODO | |||
query = """SELECT * | |||
FROM codelets | |||
INNER JOIN code ON codelet_code_id = code_id | |||
INNER JOIN origins ON codelet_origin = origin_id | |||
WHERE codelet_id = ?""" | |||
with self._conn.cursor(oursql.DictCursor) as cursor: | |||
cursor.executemany(query, [(id,) for id in ids]) | |||
for row in cursor.fetchone(): | |||
if row["origin_url_base"]: | |||
url = row["codelet_url"] | |||
else: | |||
url = row["origin_url_base"] + row["codelet_url"] | |||
origin = (row["origin_name"], row["origin_url"], | |||
row["origin_image"]) | |||
authors = NotImplemented # TODO: list of 3-tuples (author_name, author_url or None) | |||
symbols = NotImplemented # TODO: dict of {sym_type: (name, decls, uses)} | |||
yield Codelet( | |||
row["codelet_name"], row["code_code"], None, | |||
row["code_lang"], authors, url, | |||
row["codelet_date_created"], row["codelet_date_modified"], | |||
row["codelet_rank"], symbols, origin) | |||
def _decompose_url(self, cursor, url): | |||
"""Break up a URL into an origin (with a URL base) and a suffix.""" | |||
@@ -133,7 +153,8 @@ class Database(object): | |||
num_mnt = num_results / (10 ** num_exp) | |||
cursor.execute(query2, (cache_id, num_mnt, num_exp)) | |||
cursor.executemany(query3, [(cache_id, c_id) for c_id in ids]) | |||
return (num_results, self._get_codelets_from_ids(cursor, ids)) | |||
codelet_gen = self._get_codelets_from_ids(ids) | |||
return (num_results, list(codelet_gen)) | |||
def insert(self, codelet): | |||
""" | |||
@@ -33,15 +33,13 @@ class Tree(object): | |||
""" | |||
return repr(self) | |||
def build_query(self, page=1, page_size=10, pretty=False): | |||
def build_query(self, page=1, page_size=10): | |||
"""Convert the query tree into a parameterized SQL SELECT statement. | |||
:param page: The page number to get results for. | |||
:type page: int | |||
:param page_size: The number of results per page. | |||
:type page_size: int | |||
:param pretty: Whether to pretty-print the SQL query or not. | |||
:type pretty: bool | |||
:return: SQL query data. | |||
:rtype: 2-tuple of (SQL statement string, query parameter tuple) | |||
@@ -67,6 +65,5 @@ class Tree(object): | |||
joins = " ".join(get_table_joins(tables)) | |||
offset = (page - 1) * page_size | |||
## TODO: handle pretty | |||
query = QUERY_TEMPLATE % (score, joins, cond, page_size, offset) | |||
return query, tuple(arglist * 2 if need_ranks else arglist) |