Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73317

Re: line to argv transformation

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '+++': 0.03; 'else:': 0.03; 'diff': 0.07; 'none:': 0.07; '-0500': 0.09; 'file)': 0.09; 'filename': 0.09; 'inclusion': 0.09; 'iterate': 0.09; 'spec': 0.09; 'subject:skip:t 10': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '+244,7': 0.16; '-tkc': 0.16; 'clone': 0.16; 'emit': 0.16; 'eof': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lexer': 0.16; 'popping': 0.16; 'quoted': 0.16; 'tt:': 0.16; 'tweak': 0.16; 'wrote:': 0.18; 'not,': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'accommodate': 0.24; 'file.': 0.24; '---': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'file': 0.32; 'probably': 0.32; 'raw': 0.33; 'maybe': 0.34; "i'd": 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'false': 0.36; 'charset:us-ascii': 0.36; 'project': 0.37; 'handle': 0.38; 'skip:p 20': 0.39; 'called': 0.40; 'new': 0.61; 'name': 0.63; 'below.': 0.71; 'pardon': 0.84; 'received:50.22': 0.84; 'tok': 0.84
Date Mon, 16 Jun 2014 07:44:20 -0500
From Tim Chase <python.list@tim.thechases.com>
To Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Subject Re: line to argv transformation
In-Reply-To <539EDA28.8090505@rece.vub.ac.be>
References <539EBF61.4050106@rece.vub.ac.be> <lnmir3$r6t$1@ger.gmane.org> <539EDA28.8090505@rece.vub.ac.be>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.com
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11088.1402922709.18130.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402922709 news.xs4all.nl 2940 [2001:888:2000:d::a6]:50387
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73317

Show key headers only | View raw


On 2014-06-16 13:51, Antoon Pardon wrote:
> >>> shlex.split("ls *.py")  
> ['ls', '*.py']
> >>> shlex.split("ls '*.py'")  
> ['ls', '*.py']

To accommodate this, I'd probably just clone the shlib.py to my local
project under a new name and then tweak the source to emit whether a
token was quoted or not, something like the diff below.

You can then iterate over your string/token-stream and know whether
it was quoted or not, allowing you to do any post-processing/globbing
on that file.

-tkc

--- /usr/lib/python2.7/shlex.py	2014-03-13 05:54:53.000000000 -0500
+++ /home/tim/tmp/myshlex.py	2014-06-16 07:39:34.130645270 -0500
@@ -93,29 +93,30 @@
                 print "shlex: popping token " + repr(tok)
             return tok
         # No pushback.  Get a token.
-        raw = self.read_token()
+        was_quoted, raw = self.read_token()
         # Handle inclusions
         if self.source is not None:
             while raw == self.source:
-                spec = self.sourcehook(self.read_token())
+                was_quoted, token = self.read_token()
+                spec = self.sourcehook(roken)
                 if spec:
                     (newfile, newstream) = spec
                     self.push_source(newstream, newfile)
-                raw = self.get_token()
+                was_quoted, raw = self.get_token()
         # Maybe we got EOF instead?
         while raw == self.eof:
             if not self.filestack:
                 return self.eof
             else:
                 self.pop_source()
-                raw = self.get_token()
+                was_quoted, raw = self.get_token()
         # Neither inclusion nor EOF
         if self.debug >= 1:
             if raw != self.eof:
                 print "shlex: token=" + repr(raw)
             else:
                 print "shlex: token=EOF"
-        return raw
+        return was_quoted, raw
 
     def read_token(self):
         quoted = False
@@ -243,7 +244,7 @@
                 print "shlex: raw token=" + repr(result)
             else:
                 print "shlex: raw token=EOF"
-        return result
+        return quoted, result
 
     def sourcehook(self, newfile):
         "Hook called on a filename to be sourced."
@@ -266,10 +267,10 @@
         return self
 
     def next(self):
-        token = self.get_token()
+        was_quoted, token = self.get_token()
         if token == self.eof:
             raise StopIteration
-        return token
+        return was_quoted, token
 
 def split(s, comments=False, posix=True):
     lex = shlex(s, posix=posix)
@@ -285,7 +286,7 @@
         file = sys.argv[1]
         lexer = shlex(open(file), file)
     while 1:
-        tt = lexer.get_token()
+        was_quoted, tt = lexer.get_token()
         if tt:
             print "Token: " + repr(tt)
         else:

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: line to argv transformation Tim Chase <python.list@tim.thechases.com> - 2014-06-16 07:44 -0500

csiph-web