diff options
author | Sean Allred <code@seanallred.com> | 2014-11-08 14:12:42 -0500 |
---|---|---|
committer | Sean Allred <code@seanallred.com> | 2014-11-08 14:12:42 -0500 |
commit | 7ed29c4dc940a871562aaa802ac53ddee4c66a27 (patch) | |
tree | 4656459efc90a16ad70a5f857630066a85805e11 | |
parent | b6043f63e5b3437633a53bcc191214bb8a7f936b (diff) |
Re-work filtering and caching
* sx-auth.el
- Use new symbolic cache access
* sx-cache.el
- Implement symbolic cache access
* sx-filter.el
- Use symbolic cache access
- Compile and save filters on-demand
(more work to be done to this end)
* sx-question.el
- Symbolic filters
* sx-request.el
- Protection against infinitely recursing when compiling a filter
This will be re-worked into requests (a front-end function) and
'raw' requests (a back-end function). The front-end will add
convenience to the back-end.
* test/tests.el
Remove outdated tests
-rw-r--r-- | sx-auth.el | 2 | ||||
-rw-r--r-- | sx-cache.el | 9 | ||||
-rw-r--r-- | sx-filter.el | 54 | ||||
-rw-r--r-- | sx-question.el | 9 | ||||
-rw-r--r-- | sx-request.el | 8 | ||||
-rw-r--r-- | test/tests.el | 21 |
6 files changed, 36 insertions, 67 deletions
@@ -65,7 +65,7 @@ questions)." (if (string-equal "" sx-auth-access-token) (progn (setq sx-auth-access-token nil) (error "You must enter this code to use this client fully")) - (sx-cache-set "auth.el" `((access-token . ,sx-auth-access-token))))) + (sx-cache-set 'auth `((access-token . ,sx-auth-access-token))))) (provide 'sx-auth) ;;; sx-auth.el ends here diff --git a/sx-cache.el b/sx-cache.el index a090982..54ae94f 100644 --- a/sx-cache.el +++ b/sx-cache.el @@ -20,7 +20,12 @@ ;;; Commentary: +;; All caches are retrieved and set using symbols. The symbol should +;; be the sub-subpackage that is using the cache. For example, +;; `sx-pkg' would use `(sx-cache-get 'pkg)'. ;; +;; This symbol is then converted into a filename within +;; `sx-cache-directory'. ;;; Code: @@ -30,7 +35,9 @@ (defun sx-cache-get-file-name (filename) "Expands FILENAME in the context of `sx-cache-directory'." - (expand-file-name filename sx-cache-directory)) + (expand-file-name + (concat (symbol-name filename) ".el") + sx-cache-directory)) (defun sx-cache-get (cache) "Return the data within CACHE. diff --git a/sx-filter.el b/sx-filter.el index 7178259..0ba8186 100644 --- a/sx-filter.el +++ b/sx-filter.el @@ -32,17 +32,9 @@ ;;; Customizations -(defconst sx-filter-cache-file - "filters.el") - -(defvar sx-filter - 'default - "The current filter. -To customize the filter for the next call to `sx-request-make', -let-bind this variable to the output of a call to -`sx-filter-compile'. Be careful! If you're going to be using -this new filter a lot, create a variable for it. Creation -requests count against `sx-request-remaining-api-requests'!") +(defvar sx--filter-alist + (sx-cache-get 'filter) + "") ;;; Compilation @@ -67,32 +59,20 @@ or string." ;;; Storage and Retrieval -(defun sx-filter-get (filter) - "Retrieve named FILTER from `sx-filter-cache-file'." - (cdr (assoc filter (sx-cache-get sx-filter-cache-file)))) - -(defun sx-filter-store (name &optional filter) - "Store NAME as FILTER in `sx-filter-cache-file'. - -NAME should be a symbol and FILTER is a string as compiled by -`sx-filter-compile'. - -If NAME is a cons cell, (car NAME) is taken to be the actual NAME -and (cdr NAME) is taken to be the actual FILTER. In this case, -the second argument is simply ignored." - (let ((name (if (consp name) (car name) name)) - (filter (if (consp name) (cdr name) filter))) - (unless (symbolp name) - (error "Name must be a symbol: %S" name)) - (let* ((dict (sx-cache-get sx-filter-cache-file)) - (entry (assoc name dict))) - (if entry (setcdr entry filter) - (setq dict (cons (cons name filter) dict))) - - (sx-cache-set sx-filter-cache-file dict)))) - -(defun sx-filter-store-all (name-filter-alist) - (mapc #'sx-filter-store name-filter-alist)) +(defun sx-filter-get-var (filter-variable) + "Return the string representation of FILTER-VARIABLE." + (apply #'sx-filter-get filter-variable)) + +(defun sx-filter-get (&optional include exclude base) + "Return the string representation of the given filter." + ;; Maybe we alreay have this filter + (or (cdr (assoc (list include exclude base) sx--filter-alist)) + ;; If we don't, build it, save it, and return it. + (let ((filter (sx-filter-compile include exclude base))) + (when filter + (push (cons (list include exclude base) filter) sx--filter-alist) + (sx-cache-set 'filter sx--filter-alist) + filter)))) (provide 'sx-filter) ;;; sx-filter.el ends here diff --git a/sx-question.el b/sx-question.el index 0ce5413..8957e6f 100644 --- a/sx-question.el +++ b/sx-question.el @@ -30,12 +30,9 @@ (require 'sx-request) ;; I don't know why this is here, but it was causing an API request on require. -(defvar sx-question-browse-filter nil) -(sx-init-variable - sx-question-browse-filter - (sx-filter-compile nil '(user.profile_image shallow_user.profile_image))) - -;; (stack-filter-store 'question-browse sx-question-browse-filter) +(defvar sx-question-browse-filter + ;; Remember: INCLUDE EXCLUDE BASE + '(nil (user.profile_image shallow_user.profile_image))) (defun sx-question-get-questions (site &optional page) "Get the page PAGE of questions from SITE." diff --git a/sx-request.el b/sx-request.el index a62ee0e..8ca0314 100644 --- a/sx-request.el +++ b/sx-request.el @@ -26,6 +26,7 @@ (require 'json) (require 'url) (require 'sx) +(require 'sx-filter) (defcustom sx-request-silent-p t @@ -95,8 +96,11 @@ entire response as a complex alist." (call (sx-request--build method - (append `((filter . ,(cond (filter filter) - ((boundp 'stack-filter) stack-filter))) + (append `((filter . ,(unless (string-equal method "filter/create") + (sx-filter-get-var + (cond (filter filter) + ((boundp 'stack-filter) + stack-filter))))) (key . ,sx-request-api-key)) (if keyword-arguments keyword-arguments (sx-request--get-default-keyword-arguments method)))))) diff --git a/test/tests.el b/test/tests.el index c7861d8..7915ac0 100644 --- a/test/tests.el +++ b/test/tests.el @@ -9,8 +9,7 @@ (defvar sx-test-data-dir (expand-file-name "data-samples/" - (or (file-name-directory load-file-name) "./")) - "") + (or (file-name-directory load-file-name) "./"))) (defun sx-test-sample-data (method &optional directory) (let ((file (concat (when directory (concat directory "/")) @@ -89,24 +88,6 @@ ((1 . alpha) (2 . beta))] '(1 2 3))))) -(ert-deftest test-filters () - (let ((stack-cache-directory (make-temp-file "stack-test" t))) - (should-error (sx-filter-store "names must be symbols" - "this is a filter")) - ;; basic use - (should (equal '((test . "filter")) - (sx-filter-store 'test "filter"))) - ;; aggregation - (should (equal '((test2 . "filter2") (test . "filter")) - (sx-filter-store 'test2 "filter2"))) - ;; mutation - (should (equal '((test2 . "filter2") (test . "filter-test")) - (sx-filter-store 'test "filter-test"))) - ;; clean up (note: the file should exist) - (delete-file - (sx-cache-get-file-name - sx-filter-cache-file)))) - (defmacro line-should-match (regexp) "" `(let ((line (buffer-substring-no-properties |