[PATCH] devel: make printmimestructure py3 compatible

classic Classic list List threaded Threaded
7 messages Options
Daniel Kahn Gillmor Daniel Kahn Gillmor
Reply | Threaded
Open this post in threaded view
|

[PATCH] devel: make printmimestructure py3 compatible

Make printmimestructure work in python3 as well as python2.

PEP 394 suggests that python scripts that work with both python2 and
python3 should have a #!/usr/bin/python command line, so do that too.
---
 devel/printmimestructure | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/devel/printmimestructure b/devel/printmimestructure
index 34d12930..afa0590e 100755
--- a/devel/printmimestructure
+++ b/devel/printmimestructure
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 # Author: Daniel Kahn Gillmor <[hidden email]>
@@ -19,6 +19,8 @@
 # If you want to number the parts, i suggest piping the output through
 # something like "cat -n"
 
+from __future__ import print_function
+
 import email
 import sys
 
@@ -34,7 +36,7 @@ def test(z, prefix=''):
             if d[0] in [ 'attachment', 'inline' ]:
                 disposition = ' ' + d[0]
     if (z.is_multipart()):
-        print prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes'
+        print(prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes')
         if prefix.endswith('└'):
             prefix = prefix.rpartition('└')[0] + ' '
         if prefix.endswith('├'):
@@ -47,6 +49,6 @@ def test(z, prefix=''):
         test(parts[i], prefix + '└')
         # FIXME: show epilogue?
     else:
-        print prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes'
+        print(prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes')
 
 test(email.message_from_file(sys.stdin), '└')
--
2.17.1

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Jameson Graef Rollins Jameson Graef Rollins
Reply | Threaded
Open this post in threaded view
|

[PATCH] minor cleanup to printmimestructure

make the source slightly easier to read.  no functional change.
---
 devel/printmimestructure | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/devel/printmimestructure b/devel/printmimestructure
index afa0590e..ab2a6673 100755
--- a/devel/printmimestructure
+++ b/devel/printmimestructure
@@ -24,7 +24,7 @@ from __future__ import print_function
 import email
 import sys
 
-def test(z, prefix=''):
+def print_part(z, prefix):
     fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']'
     cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')'
     disp = z.get_params(None, header='Content-Disposition')
@@ -35,8 +35,23 @@ def test(z, prefix=''):
         for d in disp:
             if d[0] in [ 'attachment', 'inline' ]:
                 disposition = ' ' + d[0]
+    if z.is_multipart():
+        nbytes = len(z.as_string())
+    else:
+        nbytes = len(z.get_payload())
+
+    print('{}{}{}{}{} {:d} bytes'.format(
+        prefix,
+        z.get_content_type(),
+        cset,
+        disposition,
+        fname,
+        nbytes,
+    ))
+
+def test(z, prefix=''):
     if (z.is_multipart()):
-        print(prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes')
+        print_part(z, prefix+'┬╴')
         if prefix.endswith('└'):
             prefix = prefix.rpartition('└')[0] + ' '
         if prefix.endswith('├'):
@@ -49,6 +64,6 @@ def test(z, prefix=''):
         test(parts[i], prefix + '└')
         # FIXME: show epilogue?
     else:
-        print(prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes')
+        print_part(z, prefix+'─╴')
 
 test(email.message_from_file(sys.stdin), '└')
--
2.17.1

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Tomi Ollila-2 Tomi Ollila-2
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] devel: make printmimestructure py3 compatible

In reply to this post by Daniel Kahn Gillmor
On Mon, Jun 11 2018, Daniel Kahn Gillmor wrote:

> Make printmimestructure work in python3 as well as python2.
>
> PEP 394 suggests that python scripts that work with both python2 and
> python3 should have a #!/usr/bin/python command line, so do that too.

I did not see PEP 394 suggesting to change

-#!/usr/bin/env python
+#!/usr/bin/python

just that 'python' should be able to run both python2 and python3 code.

after the above change the code will not run on (those stupid) systems
that install python to e.g. /usr/local/bin/.

While I am not against this change, the commit message is misleading.

Tomi


> ---
>  devel/printmimestructure | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/devel/printmimestructure b/devel/printmimestructure
> index 34d12930..afa0590e 100755
> --- a/devel/printmimestructure
> +++ b/devel/printmimestructure
> @@ -1,4 +1,4 @@
> -#!/usr/bin/env python
> +#!/usr/bin/python
>  # -*- coding: utf-8 -*-
>  
>  # Author: Daniel Kahn Gillmor <[hidden email]>
> @@ -19,6 +19,8 @@
>  # If you want to number the parts, i suggest piping the output through
>  # something like "cat -n"
>  
> +from __future__ import print_function
> +
>  import email
>  import sys
>  
> @@ -34,7 +36,7 @@ def test(z, prefix=''):
>              if d[0] in [ 'attachment', 'inline' ]:
>                  disposition = ' ' + d[0]
>      if (z.is_multipart()):
> -        print prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes'
> +        print(prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes')
>          if prefix.endswith('└'):
>              prefix = prefix.rpartition('└')[0] + ' '
>          if prefix.endswith('├'):
> @@ -47,6 +49,6 @@ def test(z, prefix=''):
>          test(parts[i], prefix + '└')
>          # FIXME: show epilogue?
>      else:
> -        print prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes'
> +        print(prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes')
>  
>  test(email.message_from_file(sys.stdin), '└')
> --
> 2.17.1
>
> _______________________________________________
> notmuch mailing list
> [hidden email]
> https://notmuchmail.org/mailman/listinfo/notmuch
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Reto Brunner Reto Brunner
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] devel: make printmimestructure py3 compatible

On Tue, Jun 12, 2018 at 11:35:09AM +0300, Tomi Ollila wrote:
> -#!/usr/bin/env python
> +#!/usr/bin/python
>
> just that 'python' should be able to run both python2 and python3 code.
>
> after the above change the code will not run on (those stupid) systems
> that install python to e.g. /usr/local/bin/.
>
> While I am not against this change, the commit message is misleading.

Remind me again why putting stuff in /usr/local is "stupid"?

/usr/bin belongs to my package manager...

If I manually compile python for whatever reason I put it in
/usr/local/bin, as this is what the directory is for.

So why would you switch from a shebang which works always to a shebang
which only works on some very simple systems? This doesn't make sense for
me.
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Thomas Schneider Thomas Schneider
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] devel: make printmimestructure py3 compatible

In reply to this post by Tomi Ollila-2
Tomi Ollila <[hidden email]> writes:

> On Mon, Jun 11 2018, Daniel Kahn Gillmor wrote:
>
>> Make printmimestructure work in python3 as well as python2.
>>
>> PEP 394 suggests that python scripts that work with both python2 and
>> python3 should have a #!/usr/bin/python command line, so do that too.
>
> I did not see PEP 394 suggesting to change
>
> -#!/usr/bin/env python
> +#!/usr/bin/python
>
> just that 'python' should be able to run both python2 and python3 code.
>
> after the above change the code will not run on (those stupid) systems
> that install python to e.g. /usr/local/bin/.

For example all BSD systems, where the package manager installs to
/usr/local (and I do not think this is stupid).

> While I am not against this change, the commit message is misleading.

For the above reason, I am against changing the shebang at all.

        --qsx
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Daniel Kahn Gillmor Daniel Kahn Gillmor
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] devel: make printmimestructure py3 compatible

On Tue 2018-06-12 12:47:58 +0200, Thomas Schneider wrote:

> Tomi Ollila <[hidden email]> writes:
>
>> On Mon, Jun 11 2018, Daniel Kahn Gillmor wrote:
>>
>>> Make printmimestructure work in python3 as well as python2.
>>>
>>> PEP 394 suggests that python scripts that work with both python2 and
>>> python3 should have a #!/usr/bin/python command line, so do that too.
>>
>> I did not see PEP 394 suggesting to change
>>
>> -#!/usr/bin/env python
>> +#!/usr/bin/python
>>
>> just that 'python' should be able to run both python2 and python3 code.
>>
>> after the above change the code will not run on (those stupid) systems
>> that install python to e.g. /usr/local/bin/.
>
> For example all BSD systems, where the package manager installs to
> /usr/local (and I do not think this is stupid).
>
>> While I am not against this change, the commit message is misleading.
>
> For the above reason, I am against changing the shebang at all.
ok, but devel/nmbug/notmuch-report uses #!/usr/bin/python, and i was
leaning toward making them consistent.  if we care about using the
/usr/bin/env trick, we should use it consistently.

i'm fine either way, my main goal with this series was to make
printmimestructure compatible with both py2 and py3, and i have no
interest in fighting about shebang lines.

             --dkg

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch

signature.asc (233 bytes) Download Attachment
Daniel Kahn Gillmor Daniel Kahn Gillmor
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] devel: make printmimestructure py3 compatible

On Tue 2018-06-12 09:57:52 -0400, Daniel Kahn Gillmor wrote:

> On Tue 2018-06-12 12:47:58 +0200, Thomas Schneider wrote:
>> Tomi Ollila <[hidden email]> writes:
>>
>>> On Mon, Jun 11 2018, Daniel Kahn Gillmor wrote:
>>>
>>>> Make printmimestructure work in python3 as well as python2.
>>>>
>>>> PEP 394 suggests that python scripts that work with both python2 and
>>>> python3 should have a #!/usr/bin/python command line, so do that too.
>>>
>>> I did not see PEP 394 suggesting to change
>>>
>>> -#!/usr/bin/env python
>>> +#!/usr/bin/python
>>>
>>> just that 'python' should be able to run both python2 and python3 code.
>>>
>>> after the above change the code will not run on (those stupid) systems
>>> that install python to e.g. /usr/local/bin/.
>>
>> For example all BSD systems, where the package manager installs to
>> /usr/local (and I do not think this is stupid).
>>
>>> While I am not against this change, the commit message is misleading.
>>
>> For the above reason, I am against changing the shebang at all.
>
> ok, but devel/nmbug/notmuch-report uses #!/usr/bin/python, and i was
> leaning toward making them consistent.  if we care about using the
> /usr/bin/env trick, we should use it consistently.
>
> i'm fine either way, my main goal with this series was to make
> printmimestructure compatible with both py2 and py3, and i have no
> interest in fighting about shebang lines.

the thread starting at id:[hidden email]
has a revised series that normalizes shebang lines on

    #!/usr/bin/env python

while still incorporating the fixes from myself and jamie.

      --dkg
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch