From 1207f47bd2340922268a1340601d45e6c336c11a Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 7 Nov 2014 18:51:23 -0500 Subject: Introduce initialization logic and hooks --- sx-question.el | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'sx-question.el') diff --git a/sx-question.el b/sx-question.el index e9634f7..0ce5413 100644 --- a/sx-question.el +++ b/sx-question.el @@ -30,11 +30,10 @@ (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 - ;; (stack-filter-compile - ;; nil - ;; '(user.profile_image shallow_user.profile_image)) - ) +(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) -- cgit v1.2.3 From 7ed29c4dc940a871562aaa802ac53ddee4c66a27 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 8 Nov 2014 14:12:42 -0500 Subject: 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 --- sx-auth.el | 2 +- sx-cache.el | 9 ++++++++- sx-filter.el | 54 +++++++++++++++++------------------------------------- sx-question.el | 9 +++------ sx-request.el | 8 ++++++-- test/tests.el | 21 +-------------------- 6 files changed, 36 insertions(+), 67 deletions(-) (limited to 'sx-question.el') diff --git a/sx-auth.el b/sx-auth.el index b23b448..049827d 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -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 -- cgit v1.2.3 From 6aa21b85ace92b01676c6da66372b409fe639920 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 8 Nov 2014 15:49:17 -0500 Subject: Abstract out method calls Keeping method calls within `sx-request.el' was causing circular requirements. This commit sorts through all of the requirements for each of the files and ensures that this does not happen. Much of the content removed was for `sx-request-default-keyword-arguments-alist' and related items. It was unused, so it was pruned. If it is deemed necessary in the future, it should be included in `sx-method.el'. --- sx-auth.el | 19 +++++------ sx-filter.el | 4 +-- sx-method.el | 48 ++++++++++++++++++++++++++ sx-question-list.el | 6 ++-- sx-question.el | 6 ++-- sx-request.el | 98 +++++++++++++++++------------------------------------ sx.el | 3 +- test/tests.el | 1 + 8 files changed, 99 insertions(+), 86 deletions(-) create mode 100644 sx-method.el (limited to 'sx-question.el') diff --git a/sx-auth.el b/sx-auth.el index 049827d..7912508 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -50,16 +50,15 @@ questions)." (interactive) (setq sx-auth-access-token - (let* ((sx-request-api-root sx-auth-root) - (url (sx-request--build - "dialog" - `((client_id . ,sx-auth-client-id) - (scope . (read_inbox - no_expiry - write_access)) - (redirect_uri . ,(url-hexify-string - sx-auth-redirect-uri))) - ","))) + (let ((url (sx-request-build + "dialog" + `((client_id . ,sx-auth-client-id) + (scope . (read_inbox + no_expiry + write_access)) + (redirect_uri . ,(url-hexify-string + sx-auth-redirect-uri))) + "," sx-auth-root))) (browse-url url) (read-string "Enter the access token displayed on the webpage: "))) (if (string-equal "" sx-auth-access-token) diff --git a/sx-filter.el b/sx-filter.el index 0ba8186..4ed3dbe 100644 --- a/sx-filter.el +++ b/sx-filter.el @@ -28,6 +28,7 @@ (require 'sx) (require 'sx-cache) +(require 'sx-request) ;;; Customizations @@ -53,8 +54,7 @@ or string." "filter/create" keyword-arguments))) (url-hexify-string - (cdr (assoc 'filter - (elt response 0))))))) + (cdr (assoc 'filter (elt response 0))))))) ;;; Storage and Retrieval diff --git a/sx-method.el b/sx-method.el new file mode 100644 index 0000000..8dc5a65 --- /dev/null +++ b/sx-method.el @@ -0,0 +1,48 @@ +;;; sx-request.el --- requests for stack-mode + +;; Copyright (C) 2014 Sean Allred + +;; Author: Sean Allred +;; Keywords: + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; + +;;; Code: +(require 'json) +(require 'url) +(require 'sx) +(require 'sx-request) +(require 'sx-filter) + +(defun sx-method-call + (method &optional keyword-arguments filter silent) + "Call METHOD with KEYWORD-ARGUMENTS using FILTER. + +If SILENT is non-nil, no messages will be printed. + +Return the entire response as a complex alist." + (sx-request-make + method + (cons (cons 'filter + (sx-filter-get-var + (cond (filter filter) + ((boundp 'stack-filter) stack-filter)))) + keyword-arguments))) + +(provide 'sx-method) +;;; sx-request.el ends here diff --git a/sx-question-list.el b/sx-question-list.el index 86e9194..caf24b1 100644 --- a/sx-question-list.el +++ b/sx-question-list.el @@ -20,11 +20,13 @@ ;;; Commentary: ;;; Code: -(require 'sx-question) -(require 'sx-time) (require 'tabulated-list) (require 'cl-lib) +(require 'sx) +(require 'sx-time) +(require 'sx-question) + ;;; Customization (defcustom sx-question-list-height 12 diff --git a/sx-question.el b/sx-question.el index 8957e6f..20a71cc 100644 --- a/sx-question.el +++ b/sx-question.el @@ -27,16 +27,14 @@ (require 'sx) (require 'sx-filter) (require 'sx-lto) -(require 'sx-request) +(require 'sx-method) -;; I don't know why this is here, but it was causing an API request on require. (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." - (sx-request-make + (sx-method-call "questions" `((site . ,site) (page . ,page)) diff --git a/sx-request.el b/sx-request.el index 3656b94..f987d2c 100644 --- a/sx-request.el +++ b/sx-request.el @@ -1,9 +1,9 @@ -;;; sx-request.el --- requests for stack-mode +;;; sx-request.el --- requests and url manipulation -*- lexical-binding: t; -*- ;; Copyright (C) 2014 Sean Allred -;; Author: Sean Allred -;; Keywords: +;; Author: Sean Allred +;; Keywords: help ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -23,10 +23,26 @@ ;; ;;; Code: -(require 'json) + (require 'url) +(require 'json) + (require 'sx) -(require 'sx-filter) + + +;;; Variables + +(defconst sx-request-api-key + "0TE6s1tveCpP9K5r5JNDNQ((" + "When passed, this key provides a higher request quota.") + +(defconst sx-request-api-version + "2.2" + "The current version of the API.") + +(defconst sx-request-api-root + (format "http://api.stackexchange.com/%s/" sx-request-api-version) + "The base URL to make requests from.") (defcustom sx-request-silent-p t @@ -51,45 +67,15 @@ recent call. Set by `sx-request-make'.") number, `sx-request-make' will begin printing out the number of requests left every time it finishes a call.") -(defcustom sx-request-default-keyword-arguments-alist - '(("filters/create") - ("sites") - ("questions" (site . emacs)) - (t nil)) - "Keywords to use as the default for a given method. - -The first element of each list is the method call the keywords -apply to. The remaining cons cells (and they must be conses) are -the values for each keyword. - -For each list, if no keywords are provided, the method's -arguments are forced to the default as determined by the API. - -For each cons cell, if the cdr is `nil', then the keyword will be -forced to the default as determined by the API. + +;;; Making Requests -See `sx-request-get-default-keyword-arguments' and -`sx-request-build-keyword-arguments'. -") - -(defconst sx-request-api-version - "2.2" - "The current version of the API.") - -(defconst sx-request-api-root - (format "http://api.stackexchange.com/%s/" sx-request-api-version) - "The base URL to make requests from.") - -(defconst sx-request-api-key - "0TE6s1tveCpP9K5r5JNDNQ((" - "When passed, this key provides a higher request quota.") - -(defun sx-request--make +(defun sx-request-make (method &optional args silent) (let ((url-automatic-caching sx-request-cache-p) (url-inhibit-uncompression t) (silent (or silent sx-request-silent-p)) - (call (sx-request--build + (call (sx-request-build method (cons (cons 'key sx-request-api-key) args)))) @@ -133,30 +119,19 @@ See `sx-request-get-default-keyword-arguments' and (cdr (assoc 'error_name response)) (cdr (assoc 'error_message response)))) (when (< (setq sx-request-remaining-api-requests - (cdr (assoc 'quote_remaining response))) + (cdr (assoc 'quota_remaining response))) sx-request-remaining-api-requests-message-threshold) (sx-message "%d API requests reamining" sx-request-remaining-api-requests)) (cdr (assoc 'items response)))))))) -(defun sx-request-make - (method &optional keyword-arguments filter silent) - "Make a request to the StackExchange API using METHOD and -optional KEYWORD-ARGUMENTS. If no KEYWORD-ARGUMENTS are given, -`sx-default-keyword-arguments-alist' is used. Return the -entire response as a complex alist." - (sx-request--make - method - (cons (cons 'filter - (sx-filter-get-var - (cond (filter filter) - ((boundp 'stack-filter) stack-filter)))) - keyword-arguments))) - -(defun sx-request--build (method keyword-arguments &optional kv-value-sep) + +;;; Support Functions + +(defun sx-request-build (method keyword-arguments &optional kv-value-sep root) "Build the request string that will be used to process REQUEST with the given KEYWORD-ARGUMENTS." - (let ((base (concat sx-request-api-root method)) + (let ((base (concat (or root sx-request-api-root) method)) (args (sx-request--build-keyword-arguments keyword-arguments kv-value-sep))) (if (string-equal "" args) @@ -181,16 +156,5 @@ false, use the symbol `false'. Each element is processed with alist)) "&")) -(defun sx-request--get-default-keyword-arguments (method) - "Gets the correct keyword arguments for METHOD." - (let ((entry (assoc method sx-request-default-keyword-arguments-alist))) - (cdr (or entry (assoc t sx-request-default-keyword-arguments-alist))))) - -;;; @todo sx-request-change-default-keyword-arguments -;;; (method new-keyword-arguments) -;;; @todo sx-request-change-default-keyword-arguments-for-key -;;; (method key new-value) - - (provide 'sx-request) ;;; sx-request.el ends here diff --git a/sx.el b/sx.el index 54ad8d0..2f7b6a7 100644 --- a/sx.el +++ b/sx.el @@ -25,7 +25,8 @@ ;;; Code: -;;; Requirements +;;; Utility Functions + (defun sx-message (format-string &rest args) "Display a message" (message "[stack] %s" (apply #'format format-string args))) diff --git a/test/tests.el b/test/tests.el index 7915ac0..a66394c 100644 --- a/test/tests.el +++ b/test/tests.el @@ -35,6 +35,7 @@ (expand-file-name (format "../../.cask/%s/elpa" emacs-version) sx-test-data-dir)) (package-initialize) + (require 'cl-lib) (require 'sx) (require 'sx-question) -- cgit v1.2.3