emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BABEL,PATCH] call awk with no input
@ 2011-07-24 10:39 Litvinov Sergey
  2011-07-24 20:07 ` Eric Schulte
  0 siblings, 1 reply; 2+ messages in thread
From: Litvinov Sergey @ 2011-07-24 10:39 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 156 bytes --]

Please consider this tiny fix to execute awk program with no input.
Example with gawk:
gawk --posix 'BEGIN {print 42}'

I also added some tests for ob-awk.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: awk-ob-patch --]
[-- Type: text/x-diff, Size: 4350 bytes --]

From 868fa910254c48752801cdc7849c3eb4db63f811 Mon Sep 17 00:00:00 2001
From: Litvinov Sergey <slitvinov@gmail.com>
Date: Sun, 24 Jul 2011 12:28:35 +0200
Subject: [PATCH] Awk can be called with no in-file: and no :stdin

---
 lisp/ob-awk.el                   |    6 +---
 testing/README.org               |    1 +
 testing/examples/ob-awk-test.in  |    2 +
 testing/examples/ob-awk-test.org |   39 ++++++++++++++++++++++++++++++++++++++
 testing/lisp/test-ob-awk.el      |   19 ++++++++++++++++++
 5 files changed, 63 insertions(+), 4 deletions(-)
 create mode 100644 testing/examples/ob-awk-test.in
 create mode 100644 testing/examples/ob-awk-test.org
 create mode 100644 testing/lisp/test-ob-awk.el

diff --git a/lisp/ob-awk.el b/lisp/ob-awk.el
index d5098bc..cfed04a 100644
--- a/lisp/ob-awk.el
+++ b/lisp/ob-awk.el
@@ -26,8 +26,7 @@
 
 ;;; Commentary:
 
-;; Babel's awk support relies on two special header argument one of
-;; which is required to pass data to the awk process.
+;; Babel's awk can use special header argument:
 ;; 
 ;; - :in-file takes a path to a file of data to be processed by awk
 ;;   
@@ -89,11 +88,10 @@ called by `org-babel-execute-src-block'"
 	      (with-temp-file tmp (insert results))
 	      (org-babel-import-elisp-from-file tmp)))))
       (cond
-       (in-file (org-babel-eval cmd ""))
        (stdin (with-temp-buffer
 		(call-process-shell-command cmd stdin (current-buffer))
 		(buffer-string)))
-       (t (error "ob-awk: must specify either :in-file or :stdin"))))
+       (t (org-babel-eval cmd ""))))
      (org-babel-pick-name
       (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
      (org-babel-pick-name
diff --git a/testing/README.org b/testing/README.org
index 3cc0487..2f16a55 100644
--- a/testing/README.org
+++ b/testing/README.org
@@ -78,6 +78,7 @@ First tangle this file out to your desktop.
   (org-id-update-id-locations
    (list (concat org-dir "/testing/examples/babel.org")
          (concat org-dir "/testing/examples/normal.org")
+         (concat org-dir "/testing/examples/ob-awk-test.org")
          (concat org-dir "/testing/examples/ob-fortran-test.org")
          (concat org-dir "/testing/examples/link-in-heading.org")
          (concat org-dir "/testing/examples/links.org")))
diff --git a/testing/examples/ob-awk-test.in b/testing/examples/ob-awk-test.in
new file mode 100644
index 0000000..c38382d
--- /dev/null
+++ b/testing/examples/ob-awk-test.in
@@ -0,0 +1,2 @@
+ # an input file for awk test
+15
\ No newline at end of file
diff --git a/testing/examples/ob-awk-test.org b/testing/examples/ob-awk-test.org
new file mode 100644
index 0000000..7f51772
--- /dev/null
+++ b/testing/examples/ob-awk-test.org
@@ -0,0 +1,39 @@
+#+Title: a collection of examples for ob-awk tests
+#+OPTIONS: ^:nil
+
+* Simple tests
+  :PROPERTIES:
+  :ID:       9e998b2a-3581-43fe-b26d-07d3c507b86a
+  :END:
+Run without input stream
+#+begin_src awk :ouput silent :results silent
+  BEGIN {
+      print 42
+  }
+#+end_src
+
+Use a code block ouput as an input
+#+begin_src awk  :stdin genseq :results silent
+  {
+      print 42+$1
+  }
+#+end_src
+
+Use input file
+#+srcname: genfile
+#+begin_src awk  :in-file ob-awk-test.in :results silent
+    $0~/[\t]*#/{
+        # skip comments 
+        next
+    }
+    { 
+        print $1*10
+    }
+#+end_src
+
+* Input data generators
+A code block to generate input stream
+#+srcname: genseq
+#+begin_src emacs-lisp :results silent
+(print "1")
+#+end_src
diff --git a/testing/lisp/test-ob-awk.el b/testing/lisp/test-ob-awk.el
new file mode 100644
index 0000000..018dec4
--- /dev/null
+++ b/testing/lisp/test-ob-awk.el
@@ -0,0 +1,19 @@
+(require 'ob-awk)
+
+(ert-deftest ob-awk/input-none ()
+  "Test with no input file"
+  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
+    (org-babel-next-src-block)
+    (should (= 42 (org-babel-execute-src-block)))))
+
+(ert-deftest ob-awk/input-src-block ()
+  "Test a code block as an input"
+  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
+    (org-babel-next-src-block 2)
+    (should (= 43 (org-babel-execute-src-block)))))
+
+(ert-deftest ob-awk/input-src-block ()
+  "Test a code block as an input"
+  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
+    (org-babel-next-src-block 3)
+    (should (= 150 (org-babel-execute-src-block)))))
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [BABEL,PATCH] call awk with no input
  2011-07-24 10:39 [BABEL,PATCH] call awk with no input Litvinov Sergey
@ 2011-07-24 20:07 ` Eric Schulte
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Schulte @ 2011-07-24 20:07 UTC (permalink / raw)
  To: Litvinov Sergey; +Cc: emacs-orgmode

Hi Sergey,

Thanks for another useful patch.  This has been applied.

Also, good to see you have ERT'd the testing script.  The contribution
of tests along with patches is *greatly* appreciated.

Cheers -- Eric

Litvinov Sergey <slitvinov@gmail.com> writes:

> Please consider this tiny fix to execute awk program with no input.
> Example with gawk:
> gawk --posix 'BEGIN {print 42}'
>
> I also added some tests for ob-awk.
>
> From 868fa910254c48752801cdc7849c3eb4db63f811 Mon Sep 17 00:00:00 2001
> From: Litvinov Sergey <slitvinov@gmail.com>
> Date: Sun, 24 Jul 2011 12:28:35 +0200
> Subject: [PATCH] Awk can be called with no in-file: and no :stdin
>
> ---
>  lisp/ob-awk.el                   |    6 +---
>  testing/README.org               |    1 +
>  testing/examples/ob-awk-test.in  |    2 +
>  testing/examples/ob-awk-test.org |   39 ++++++++++++++++++++++++++++++++++++++
>  testing/lisp/test-ob-awk.el      |   19 ++++++++++++++++++
>  5 files changed, 63 insertions(+), 4 deletions(-)
>  create mode 100644 testing/examples/ob-awk-test.in
>  create mode 100644 testing/examples/ob-awk-test.org
>  create mode 100644 testing/lisp/test-ob-awk.el
>
> diff --git a/lisp/ob-awk.el b/lisp/ob-awk.el
> index d5098bc..cfed04a 100644
> --- a/lisp/ob-awk.el
> +++ b/lisp/ob-awk.el
> @@ -26,8 +26,7 @@
>  
>  ;;; Commentary:
>  
> -;; Babel's awk support relies on two special header argument one of
> -;; which is required to pass data to the awk process.
> +;; Babel's awk can use special header argument:
>  ;; 
>  ;; - :in-file takes a path to a file of data to be processed by awk
>  ;;   
> @@ -89,11 +88,10 @@ called by `org-babel-execute-src-block'"
>  	      (with-temp-file tmp (insert results))
>  	      (org-babel-import-elisp-from-file tmp)))))
>        (cond
> -       (in-file (org-babel-eval cmd ""))
>         (stdin (with-temp-buffer
>  		(call-process-shell-command cmd stdin (current-buffer))
>  		(buffer-string)))
> -       (t (error "ob-awk: must specify either :in-file or :stdin"))))
> +       (t (org-babel-eval cmd ""))))
>       (org-babel-pick-name
>        (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
>       (org-babel-pick-name
> diff --git a/testing/README.org b/testing/README.org
> index 3cc0487..2f16a55 100644
> --- a/testing/README.org
> +++ b/testing/README.org
> @@ -78,6 +78,7 @@ First tangle this file out to your desktop.
>    (org-id-update-id-locations
>     (list (concat org-dir "/testing/examples/babel.org")
>           (concat org-dir "/testing/examples/normal.org")
> +         (concat org-dir "/testing/examples/ob-awk-test.org")
>           (concat org-dir "/testing/examples/ob-fortran-test.org")
>           (concat org-dir "/testing/examples/link-in-heading.org")
>           (concat org-dir "/testing/examples/links.org")))
> diff --git a/testing/examples/ob-awk-test.in b/testing/examples/ob-awk-test.in
> new file mode 100644
> index 0000000..c38382d
> --- /dev/null
> +++ b/testing/examples/ob-awk-test.in
> @@ -0,0 +1,2 @@
> + # an input file for awk test
> +15
> \ No newline at end of file
> diff --git a/testing/examples/ob-awk-test.org b/testing/examples/ob-awk-test.org
> new file mode 100644
> index 0000000..7f51772
> --- /dev/null
> +++ b/testing/examples/ob-awk-test.org
> @@ -0,0 +1,39 @@
> +#+Title: a collection of examples for ob-awk tests
> +#+OPTIONS: ^:nil
> +
> +* Simple tests
> +  :PROPERTIES:
> +  :ID:       9e998b2a-3581-43fe-b26d-07d3c507b86a
> +  :END:
> +Run without input stream
> +#+begin_src awk :ouput silent :results silent
> +  BEGIN {
> +      print 42
> +  }
> +#+end_src
> +
> +Use a code block ouput as an input
> +#+begin_src awk  :stdin genseq :results silent
> +  {
> +      print 42+$1
> +  }
> +#+end_src
> +
> +Use input file
> +#+srcname: genfile
> +#+begin_src awk  :in-file ob-awk-test.in :results silent
> +    $0~/[\t]*#/{
> +        # skip comments 
> +        next
> +    }
> +    { 
> +        print $1*10
> +    }
> +#+end_src
> +
> +* Input data generators
> +A code block to generate input stream
> +#+srcname: genseq
> +#+begin_src emacs-lisp :results silent
> +(print "1")
> +#+end_src
> diff --git a/testing/lisp/test-ob-awk.el b/testing/lisp/test-ob-awk.el
> new file mode 100644
> index 0000000..018dec4
> --- /dev/null
> +++ b/testing/lisp/test-ob-awk.el
> @@ -0,0 +1,19 @@
> +(require 'ob-awk)
> +
> +(ert-deftest ob-awk/input-none ()
> +  "Test with no input file"
> +  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
> +    (org-babel-next-src-block)
> +    (should (= 42 (org-babel-execute-src-block)))))
> +
> +(ert-deftest ob-awk/input-src-block ()
> +  "Test a code block as an input"
> +  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
> +    (org-babel-next-src-block 2)
> +    (should (= 43 (org-babel-execute-src-block)))))
> +
> +(ert-deftest ob-awk/input-src-block ()
> +  "Test a code block as an input"
> +  (org-test-at-id "9e998b2a-3581-43fe-b26d-07d3c507b86a"
> +    (org-babel-next-src-block 3)
> +    (should (= 150 (org-babel-execute-src-block)))))

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-07-24 20:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-24 10:39 [BABEL,PATCH] call awk with no input Litvinov Sergey
2011-07-24 20:07 ` Eric Schulte

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).