Skip to content

Commit

Permalink
Merge pull request #3 from sarg/fix-defmethod
Browse files Browse the repository at this point in the history
Fix defmethod
  • Loading branch information
ukaszg committed Mar 10, 2023
2 parents cc16615 + c86e2c1 commit 55a2317
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions aria2.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Author: Łukasz Gruner <lukasz@gruner.lu>
;; Maintainer: Łukasz Gruner <lukasz@gruner.lu>
;; Version: 2
;; Package-Requires: ((emacs "24.4"))
;; Package-Requires: ((emacs "25.1"))
;; URL: https://bitbucket.org/ukaszg/aria2-mode
;; Created: 19/10/2014
;; Keywords: download bittorrent aria2
Expand Down Expand Up @@ -177,10 +177,11 @@ See aria2c manual for supported options."
"Return base64-encoded string with contents of file on PATH."
(unless (file-exists-p path)
(signal 'aria2-err-file-doesnt-exist '(path)))
(with-current-buffer (find-file-noselect path)
(unwind-protect
(base64-encode-string (buffer-string) t)
(kill-buffer))))
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally path)
(base64-encode-region (point-min) (point-max) t)
(buffer-string)))

(defun aria2--is-aria-process-p (pid)
"Returns t if `process-attributes' of PID belongs to aria."
Expand Down Expand Up @@ -261,13 +262,13 @@ See aria2c manual for supported options."

;;; Internal methods start here.

(defmethod get-next-id ((this aria2-controller))
(cl-defmethod get-next-id ((this aria2-controller))
"Return next request id json form. Resets back to 1 upon reaching `most-positive-fixnum'"
(let ((id (1+ (oref this request-id))))
(oset this request-id (if (equal id most-positive-fixnum) 0 id))
id))

(defmethod is-process-running ((this aria2-controller))
(cl-defmethod is-process-running ((this aria2-controller))
"Returns status of aria2c process."
(with-slots (pid) this
(when aria2--debug (message "aria2 pid %d" pid))
Expand All @@ -276,7 +277,7 @@ See aria2c manual for supported options."
(aria2--is-aria-process-p pid))
t)))

(defmethod run-process ((this aria2-controller))
(cl-defmethod run-process ((this aria2-controller))
"Starts aria2c process, if not already running."
(unless (is-process-running this)
(let ((options (delq nil
Expand All @@ -300,7 +301,7 @@ See aria2c manual for supported options."
(unless (is-process-running this)
(signal 'aria2-err-failed-to-start (concat aria2-executable " " (string-join options " ")))))))

(defmethod make-request ((this aria2-controller) method &rest params)
(cl-defmethod make-request ((this aria2-controller) method &rest params)
"Calls a remote METHOD with PARAMS. Returns response alist."
(when aria2-start-rpc-server
(run-process this))
Expand Down Expand Up @@ -332,64 +333,64 @@ See aria2c manual for supported options."

;;; Api implementation starts here

(defmethod addUri ((this aria2-controller) urls)
(cl-defmethod addUri ((this aria2-controller) urls)
"Add a list of http/ftp/bittorrent URLS, pointing at the same file.
When sending magnet link, URLS must have only one element."
(make-request this "aria2.addUri" (vconcat urls)))

(defmethod addTorrent ((this aria2-controller) path)
(cl-defmethod addTorrent ((this aria2-controller) path)
"Add PATH pointing at a torrent file to download list."
(unless (file-exists-p path)
(signal 'aria2-err-file-doesnt-exist '(path)))
(unless (string-match-p "\\.torrent$" path)
(signal 'aria2-err-not-a-torrent-file nil))
(make-request this "aria2.addTorrent" (aria2--base64-encode-file path)))

(defmethod addMetalink ((this aria2-controller) path)
(cl-defmethod addMetalink ((this aria2-controller) path)
"Add local .metalink PATH to download list."
(unless (file-exists-p path)
(signal 'aria2-err-file-doesnt-exist '(path)))
(unless (string-match-p "\\.meta\\(?:4\\|link\\)$" path)
(signal 'aria2-err-not-a-metalink-file nil))
(make-request this "aria2.addMetalink" (aria2--base64-encode-file path)))

(defmethod remove-download ((this aria2-controller) gid &optional force)
(cl-defmethod remove-download ((this aria2-controller) gid &optional force)
"Remove download identified by GID. If FORCE don't unregister download at bittorrent tracker."
(make-request this (if force "aria2.forceRemove" "aria2.remove") gid))

(defmethod pause ((this aria2-controller) gid &optional force)
(cl-defmethod pause ((this aria2-controller) gid &optional force)
"Pause download identified by GID. If FORCE don't unregister download at bittorrent tracker."
(make-request this (if force "aria2.forcePause" "aria2.pause") gid))

(defmethod pauseAll ((this aria2-controller) &optional force)
(cl-defmethod pauseAll ((this aria2-controller) &optional force)
"Pause all downloads. If FORCE don't unregister download at bittorrent tracker."
(make-request this (if force "aria2.forcePauseAll" "aria2.pauseAll")))

(defmethod unpause ((this aria2-controller) gid)
(cl-defmethod unpause ((this aria2-controller) gid)
"Unpause download identified by GID."
(make-request this "aria2.unpause" gid))

(defmethod unpauseAll ((this aria2-controller))
(cl-defmethod unpauseAll ((this aria2-controller))
"Unpause all downloads."
(make-request this "aria2.unpauseAll"))

(defmethod tellStatus ((this aria2-controller) gid &optional keys)
(cl-defmethod tellStatus ((this aria2-controller) gid &optional keys)
"Return status of a download identified by GID."
(make-request this "aria2.tellStatus" gid keys))

(defmethod tellActive ((this aria2-controller) &optional keys)
(cl-defmethod tellActive ((this aria2-controller) &optional keys)
"Return statuses of active downloads."
(make-request this "aria2.tellActive" keys))

(defmethod tellWaiting ((this aria2-controller) &optional offset num keys)
(cl-defmethod tellWaiting ((this aria2-controller) &optional offset num keys)
"Return statuses of waiting downloads."
(make-request this "aria2.tellWaiting" (or offset 0) (or num most-positive-fixnum) keys))

(defmethod tellStopped ((this aria2-controller) &optional offset num keys)
(cl-defmethod tellStopped ((this aria2-controller) &optional offset num keys)
"Return statuses of stopped downloads."
(make-request this "aria2.tellStopped" (or offset 0) (or num most-positive-fixnum) keys))

(defmethod changePosition ((this aria2-controller) gid pos &optional how)
(cl-defmethod changePosition ((this aria2-controller) gid pos &optional how)
"Change position of a download denoted by GID. POS is a number. HOW is one of:
\"POS_SET\" - sets file to POS position from the beginning of a list (first element is 0),
\"POS_CUR\" - moves file by POS places relative to it's current position,
Expand All @@ -399,64 +400,64 @@ If nil defaults to \"POS_CUR\"."
(signal 'aria2-err-no-such-position-type (list how)))
(make-request this "aria2.changePosition" gid pos (or how "POS_CUR")))

(defmethod changeUri ((this aria2-controller) gid file-index del-uris add-uris &optional position)
(cl-defmethod changeUri ((this aria2-controller) gid file-index del-uris add-uris &optional position)
"This method removes the URIs in DEL-URIS list and appends the URIs in ADD-URIS list to download denoted by GID.
FILE-INDEX is 1-based position, identifying a file in a download.
POSITION is a 0-based index specifying where URIs are inserted in waiting list.
Returns a pair of numbers denoting amount of files deleted and files inserted."
(make-request this "aria2.changeUri" gid file-index del-uris add-uris (or position 0)))

(defmethod getOption ((this aria2-controller) gid)
(cl-defmethod getOption ((this aria2-controller) gid)
"This method returns options of the download denoted by GID."
(make-request this "aria2.getOption" gid))

(defmethod changeOption ((this aria2-controller) gid options)
(cl-defmethod changeOption ((this aria2-controller) gid options)
"Set OPTIONS for file denoted by GID. OPTIONS is an alist."
(make-request this "aria2.changeOption" gid options))

(defmethod getGlobalOption ((this aria2-controller))
(cl-defmethod getGlobalOption ((this aria2-controller))
"Return an alist of global options. Global options are used as defaults for newly added files."
(make-request this "aria2.getGlobalOptions"))

(defmethod changeGlobalOption ((this aria2-controller) options)
(cl-defmethod changeGlobalOption ((this aria2-controller) options)
"Changes default global opts to OPTIONS. OPTIONS is an alist of opt-name and value."
(make-request this "aria2.changeGlobalOption" options))

(defmethod getGlobalStat ((this aria2-controller))
(cl-defmethod getGlobalStat ((this aria2-controller))
"Returns global statistics such as the overall download and upload speeds."
(make-request this "aria2.getGlobalStat"))

(defmethod purgeDownloadResult ((this aria2-controller))
(cl-defmethod purgeDownloadResult ((this aria2-controller))
"This method purges completed/error/removed downloads to free memory."
(make-request this "aria2.purgeDownloadResult"))

(defmethod removeDownloadResult ((this aria2-controller) gid)
(cl-defmethod removeDownloadResult ((this aria2-controller) gid)
"Removes a completed/error/removed download denoted by GID from memory."
(make-request this "aria2.removeDownloadResult" gid))

(defmethod saveSession ((this aria2-controller))
(cl-defmethod saveSession ((this aria2-controller))
"Saves the current session to a `aria2-session-file' file."
(make-request this "aria2.saveSession"))

(defmethod shutdown ((this aria2-controller) &optional force)
(cl-defmethod shutdown ((this aria2-controller) &optional force)
"Shut down aria2c process. If FORCE don't wait for unregistering torrents."
(when (is-process-running this)
(make-request this (if force "aria2.forceShutdown" "aria2.shutdown"))
(oset this pid -1)))

(defmethod getUris ((this aria2-controller) gid)
(cl-defmethod getUris ((this aria2-controller) gid)
"Return a list of uris used in download identified by GID."
(make-request this "aria2.getUris" gid))

(defmethod getFiles ((this aria2-controller) gid)
(cl-defmethod getFiles ((this aria2-controller) gid)
"Return a file list of a download identified by GID."
(make-request this "aria2.getFiles" gid))

(defmethod getPeers ((this aria2-controller) gid)
(cl-defmethod getPeers ((this aria2-controller) gid)
"Return a list peers of the download denoted by GID."
(make-request this "aria2.getPeers" gid))

(defmethod getServers ((this aria2-controller) gid)
(cl-defmethod getServers ((this aria2-controller) gid)
"Return currently connected HTTP(S)/FTP servers of the download denoted by GID."
(make-request this "aria2.getServers" gid))

Expand Down Expand Up @@ -898,6 +899,7 @@ With prefix remove all applicable downloads."
;; Local Variables:
;; coding: utf-8-unix
;; indent-tabs-mode: nil
;; lisp-body-indent: 4
;; End:

;;; aria2.el ends here

0 comments on commit 55a2317

Please sign in to comment.