Re: Printing UTF-8 mail to terminal

2024-11-04 Thread Loris Bennett via Python-list
Cameron Simpson  writes:

> On 01Nov2024 10:10, Loris Bennett  wrote:
>>as expected.  The non-UTF-8 text occurs when I do
>>
>>  mail = EmailMessage()
>>  mail.set_content(body, cte="quoted-printable")
>>  ...
>>
>>  if args.verbose:
>>  print(mail)
>>
>>which is presumably also correct.
>>
>>The question is: What conversion is necessary in order to print the
>>EmailMessage object to the terminal, such that the quoted-printable
>>parts are turned (back) into UTF-8?
>
> Do you still have access to `body` ? That would be the original
> message text? Otherwise maybe:
>
> print(mail.get_content())
>
> The objective is to obtain the message body Unicode text (i.e. a
> regular Python string with the original text, unencoded). And to print
> that.

With the following:

##

import email.message

m = email.message.EmailMessage()

m['Subject'] = 'Übung'

m.set_content('Dies ist eine Übung')
print('== cte: default == \n')
print(m)

print('-- full mail ---')
print(m)
print('-- just content--')
print(m.get_content())

m.set_content('Dies ist eine Übung', cte='quoted-printable')
print('== cte: quoted-printable ==\n')
print('-- full mail --')
print(m)
print('-- just content --')
print(m.get_content())

##

I get the following output:

##

== cte: default == 

Subject: Übung
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0

RGllcyBpc3QgZWluZSDDnGJ1bmcK

-- full mail ---
Subject: Übung
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0

RGllcyBpc3QgZWluZSDDnGJ1bmcK

-- just content--
Dies ist eine Übung

== cte: quoted-printable ==

-- full mail --
Subject: Übung
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Dies ist eine =C3=9Cbung

-- just content --
Dies ist eine Übung

##

So in both cases the subject is fine, but it is unclear to me how to
print the body.  Or rather, I know how to print the body OK, but I don't
know how to print the headers separately - there seems to be nothing
like 'get_headers()'.  I can use 'get('Subject) etc. and reconstruct the
headers, but that seems a little clunky.  

Cheers,

Loris

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman/listinfo/python-list


TkInter Scrolled Listbox class?

2024-11-04 Thread Ulrich Goebel via Python-list
Hi,

I would like to build a class ScrolledListbox, which can be packed somewhere in 
ttk.Frames. What I did is to build not really a scrolled Listbox but a Frame 
containing a Listbox and a Scrollbar:

class FrameScrolledListbox(ttk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
#
# build Listbox and Scrollbar
self.Listbox = tk.Listbox(self)
self.Scrollbar = ttk.Scrollbar(self)
#
# configure these two
self.Listbox.config(yscrollcommand=self.Scrollbar.set)
self.Scrollbar.config(command=self.Listbox.yview)
#
# pack them in Frame
self.Listbox.pack(side=tk.LEFT, fill=tk.BOTH)
self.Scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)

That works, so instances of FrameScrolledListbox can be packed and the 
tk.Listbox itself is accessible via an attribute:

frmScrolledListbox = FrameScrolledListbox(main)
frmScrolledListbox.Listbox.config(...)

But it would be a bit nicer to get a class like

class ScrolledListbox(tk.Listbox):
...

So it would be used that way:

scrolledListbox = ScrolledListbox(main)
scrolledListbox.config(...)

Is that possible? The problem which I can't handle is to handle the Frame which 
seems to be needed to place the Scrollbar somewhere.

Best regards
Ulrich


-- 
Ulrich Goebel 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Printing UTF-8 mail to terminal

2024-11-04 Thread Loris Bennett via Python-list
Inada Naoki  writes:

> 2024年11月2日(土) 0:36 Loris Bennett via Python-list :
>
>> Left Right  writes:
>>
>> > There's quite a lot of misuse of terminology around terminal / console
>> > / shell.  Please, correct me if I'm wrong, but it looks like you are
>> > printing that on MS Windows, right?  MS Windows doesn't have or use
>> > terminals (that's more of a Unix-related concept). And, by "terminal"
>> > I mean terminal emulator (i.e. a program that emulates the behavior of
>> > a physical terminal). You can, of course, find some terminal programs
>> > for windows (eg. mintty), but I doubt that that's what you are dealing
>> > with.
>> >
>> > What MS Windows users usually end up using is the console.  If you
>> > run, eg. cmd.exe, it will create a process that displays a graphical
>> > console.  The console uses an encoding scheme to represent the text
>> > output.  I believe that the default on MS Windows is to use some
>> > single-byte encoding. This answer from SE family site tells you how to
>> > set the console encoding to UTF-8 permanently:
>> >
>> https://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8
>> > , which, I believe, will solve your problem with how the text is
>> > displayed.
>>
>> I'm not using MS Windows.  I am using a Gnome terminal on Debian 12
>> locally and connecting via SSH to a AlmaLinux 8 server, where I start a
>> tmux session.
>>
>> > On Thu, Oct 31, 2024 at 5:19 PM Loris Bennett via Python-list
>> >  wrote:
>> >>
>> >> Hi,
>> >>
>> >> I have a command-line program which creates an email containing German
>> >> umlauts.  On receiving the mail, my mail client displays the subject and
>> >> body correctly:
>> >>
>> >>   Subject: Übung
>> >>
>> >>   Sehr geehrter Herr Dr. Bennett,
>> >>
>> >>   Dies ist eine Übung.
>> >>
>> >> So far, so good.  However, when I use the --verbose option to print
>> >> the mail to the terminal via
>> >>
>> >>   if args.verbose:
>> >>   print(mail)
>> >>
>> >> I get:
>> >>
>> >>   Subject: Übungsbetreff
>> >>
>> >>   Sehr geehrter Herr Dr. Bennett,
>> >>
>> >>   Dies ist eine =C3=9Cbung.
>> >>
>> >> What do I need to do to prevent the body from getting mangled?
>> >>
>> >> I seem to remember that I had issues in the past with a Perl version of
>> >> a similar program.  As far as I recall there was an issue with fact the
>> >> greeting is generated by querying a server, whereas the body is being
>> >> read from a file, which lead to oddities when the two bits were
>> >> concatenated.  But that might just have been a Perl thing.
>> >>
>
> Try PYTHONUTF8=1 envver.
>

This does not seem to affect the way the email body is printed.

Cheers,

Loris

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TkInter Scrolled Listbox class?

2024-11-04 Thread Alan Gauld via Python-list
On 04/11/2024 15:32, Ulrich Goebel via Python-list wrote:

> I would like to build a class ScrolledListbox, 

I assume like the one that used to be available via the Tix module?
It's a great shame that Tix is gone, it had a lot of these useful
widgets, but they were all wrappers around Tcl/Tk's Tix module and
that is the thing that has gone.

If you really want to replace it I'd suggest looking at the IDLE
code and see what they did because the IDLE settings dialogues
have scrolled lists and the IDLE guys have probably done a decent
job in building their widgets. (Based on the fact that IDLE has
had a tone of work done in recent years using ttk)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TkInter Scrolled Listbox class?

2024-11-04 Thread Cameron Simpson via Python-list

On 04Nov2024 16:32, Ulrich Goebel  wrote:
I would like to build a class ScrolledListbox, which can be packed 
somewhere in ttk.Frames. What I did is to build not really a scrolled 
Listbox but a Frame containing a Listbox and a Scrollbar:


That's what I would build too.


class FrameScrolledListbox(ttk.Frame):
   def __init__(self, *args, **kwargs):
   super().__init__(*args, **kwargs)
   # build Listbox and Scrollbar
   self.Listbox = tk.Listbox(self)
   self.Scrollbar = ttk.Scrollbar(self)

[...]

But it would be a bit nicer to get a class like

class ScrolledListbox(tk.Listbox):
   ...

So it would be used that way:

scrolledListbox = ScrolledListbox(main)
scrolledListbox.config(...)


Probably you want to proxy various methods to the enclosed widgets.  
Possibly you want to do that with parameters in `__init__` also.


Example:

class FrameScrolledListbox(ttk.Frame):
def __init__(self, *frame_args, *, height=None, jump=None, **frame_kw):
super().__init__(*frame_args, **frame_kw)
self.Listbox = tk.Listbox(self, height=height)
self.Scrollbar = ttk.Scrollbar(self, jump=jump)


def config(self, *a, **kw):
return self.Listbox.config(*a, **kw)

and so forth for the various listbox methods you want to proxy to the 
listbox itself. You could pass scroll specific methods to the scrollbar 
as well.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Printing UTF-8 mail to terminal

2024-11-04 Thread Loris Bennett via Python-list
"Loris Bennett"  writes:

> "Loris Bennett"  writes:
>
>> Cameron Simpson  writes:
>>
>>> On 01Nov2024 10:10, Loris Bennett  wrote:
as expected.  The non-UTF-8 text occurs when I do

  mail = EmailMessage()
  mail.set_content(body, cte="quoted-printable")
  ...

  if args.verbose:
  print(mail)

which is presumably also correct.

The question is: What conversion is necessary in order to print the
EmailMessage object to the terminal, such that the quoted-printable
parts are turned (back) into UTF-8?
>>>
>>> Do you still have access to `body` ? That would be the original
>>> message text? Otherwise maybe:
>>>
>>> print(mail.get_content())
>>>
>>> The objective is to obtain the message body Unicode text (i.e. a
>>> regular Python string with the original text, unencoded). And to print
>>> that.
>>
>> With the following:
>>
>> ##
>>
>> import email.message
>>
>> m = email.message.EmailMessage()
>>
>> m['Subject'] = 'Übung'
>>
>> m.set_content('Dies ist eine Übung')
>> print('== cte: default == \n')
>> print(m)
>>
>> print('-- full mail ---')
>> print(m)
>> print('-- just content--')
>> print(m.get_content())
>>
>> m.set_content('Dies ist eine Übung', cte='quoted-printable')
>> print('== cte: quoted-printable ==\n')
>> print('-- full mail --')
>> print(m)
>> print('-- just content --')
>> print(m.get_content())
>>
>> ##
>>
>> I get the following output:
>>
>> ##
>>
>> == cte: default == 
>>
>> Subject: Übung
>> Content-Type: text/plain; charset="utf-8"
>> Content-Transfer-Encoding: base64
>> MIME-Version: 1.0
>>
>> RGllcyBpc3QgZWluZSDDnGJ1bmcK
>>
>> -- full mail ---
>> Subject: Übung
>> Content-Type: text/plain; charset="utf-8"
>> Content-Transfer-Encoding: base64
>> MIME-Version: 1.0
>>
>> RGllcyBpc3QgZWluZSDDnGJ1bmcK
>>
>> -- just content--
>> Dies ist eine Übung
>>
>> == cte: quoted-printable ==
>>
>> -- full mail --
>> Subject: Übung
>> MIME-Version: 1.0
>> Content-Type: text/plain; charset="utf-8"
>> Content-Transfer-Encoding: quoted-printable
>>
>> Dies ist eine =C3=9Cbung
>>
>> -- just content --
>> Dies ist eine Übung
>>
>> ##
>>
>> So in both cases the subject is fine, but it is unclear to me how to
>> print the body.  Or rather, I know how to print the body OK, but I don't
>> know how to print the headers separately - there seems to be nothing
>> like 'get_headers()'.  I can use 'get('Subject) etc. and reconstruct the
>> headers, but that seems a little clunky.  
>
> Sorry, I am confusing the terminology here.  The 'body' seems to be the
> headers plus the 'content'.  So I can print the *content* without the
> headers OK, but I can't easily print all the headers separately.  If
> just print the body, i.e. headers plus content, the umlauts in the
> content are not resolved.

OK, so I can do:

##
if args.verbose:
for k in mail.keys():
print(f"{k}: {mail.get(k)}")
print('')
print(mail.get_content())
##

prints what I want and is not wildly clunky, but I am a little surprised
that I can't get a string representation of the whole email in one go.

Cheers,

Loris


-- 
Dr. Loris Bennett (Herr/Mr)
FUB-IT, Freie Universität Berlin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Printing UTF-8 mail to terminal

2024-11-04 Thread Loris Bennett via Python-list
"Loris Bennett"  writes:

> Cameron Simpson  writes:
>
>> On 01Nov2024 10:10, Loris Bennett  wrote:
>>>as expected.  The non-UTF-8 text occurs when I do
>>>
>>>  mail = EmailMessage()
>>>  mail.set_content(body, cte="quoted-printable")
>>>  ...
>>>
>>>  if args.verbose:
>>>  print(mail)
>>>
>>>which is presumably also correct.
>>>
>>>The question is: What conversion is necessary in order to print the
>>>EmailMessage object to the terminal, such that the quoted-printable
>>>parts are turned (back) into UTF-8?
>>
>> Do you still have access to `body` ? That would be the original
>> message text? Otherwise maybe:
>>
>> print(mail.get_content())
>>
>> The objective is to obtain the message body Unicode text (i.e. a
>> regular Python string with the original text, unencoded). And to print
>> that.
>
> With the following:
>
> ##
>
> import email.message
>
> m = email.message.EmailMessage()
>
> m['Subject'] = 'Übung'
>
> m.set_content('Dies ist eine Übung')
> print('== cte: default == \n')
> print(m)
>
> print('-- full mail ---')
> print(m)
> print('-- just content--')
> print(m.get_content())
>
> m.set_content('Dies ist eine Übung', cte='quoted-printable')
> print('== cte: quoted-printable ==\n')
> print('-- full mail --')
> print(m)
> print('-- just content --')
> print(m.get_content())
>
> ##
>
> I get the following output:
>
> ##
>
> == cte: default == 
>
> Subject: Übung
> Content-Type: text/plain; charset="utf-8"
> Content-Transfer-Encoding: base64
> MIME-Version: 1.0
>
> RGllcyBpc3QgZWluZSDDnGJ1bmcK
>
> -- full mail ---
> Subject: Übung
> Content-Type: text/plain; charset="utf-8"
> Content-Transfer-Encoding: base64
> MIME-Version: 1.0
>
> RGllcyBpc3QgZWluZSDDnGJ1bmcK
>
> -- just content--
> Dies ist eine Übung
>
> == cte: quoted-printable ==
>
> -- full mail --
> Subject: Übung
> MIME-Version: 1.0
> Content-Type: text/plain; charset="utf-8"
> Content-Transfer-Encoding: quoted-printable
>
> Dies ist eine =C3=9Cbung
>
> -- just content --
> Dies ist eine Übung
>
> ##
>
> So in both cases the subject is fine, but it is unclear to me how to
> print the body.  Or rather, I know how to print the body OK, but I don't
> know how to print the headers separately - there seems to be nothing
> like 'get_headers()'.  I can use 'get('Subject) etc. and reconstruct the
> headers, but that seems a little clunky.  

Sorry, I am confusing the terminology here.  The 'body' seems to be the
headers plus the 'content'.  So I can print the *content* without the
headers OK, but I can't easily print all the headers separately.  If
just print the body, i.e. headers plus content, the umlauts in the
content are not resolved.

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman/listinfo/python-list