This was my quick test of using setccsid to set the IFS file attribute:

import subprocess

#Probably something in codecs or somewhere which already has these mappings.
ccxlate = {}
ccxlate['cp819'] = '819'
ccxlate['utf-8'] = '1208'
ccxlate['cp1252'] = '1252'
ccxlate['cp037'] = '37'

def writeFile(fileName, data, ccsid='cp819'):
f = open(fileName, 'w', encoding=ccsid)
f.write(data)
f.close()
subprocess.run(['setccsid', ccxlate[ccsid], fileName])

def main():
data = '$£[]'
writeFile( 'f1208.txt', data, 'utf-8' )
writeFile( 'f1252.txt', data, 'cp1252' )
writeFile( 'f37.txt', data, 'cp037' )
writeFile( 'f819.txt', data )

if __name__ == '__main__':
main()



On Tue, 2 Oct 2018 at 14:34, Craig Richards <craig@xxxxxxxxxxxxxxxx> wrote:

Thanks Kevin and Jeremy.

Yes that does work for me.

Thanks for the info. I'll have to read up a bit on some of this.
Nice one. I appreciate you taking the time to post.
best regards,
Craig

On Tue, 2 Oct 2018 at 14:27, Kevin Bucknum <Kevin@xxxxxxxxxxxxxxxxxxx>
wrote:

This works.

import ibm_db_dbi as db2
from itoolkit import *
from itoolkit.db2.idb2call import *

itool = iToolKit()
itransport = iDB2Call()
itool.add(iCmd5250('dspsyssts', 'dspsyssts'))
itool.call(itransport)
dspsyssts = itool.dict_out('dspsyssts')
if 'error' in dspsyssts:
print (dspsyssts['error'])
exit()
else:
print (dspsyssts['dspsyssts'])





________________________________
From: OpenSource <opensource-bounces@xxxxxxxxxxxx> on behalf of Craig
Richards <craig@xxxxxxxxxxxxxxxx>
Sent: Tuesday, October 2, 2018 7:46 AM
To: IBMi Open Source Roundtable
Subject: Re: [IBMiOSS] Creating Stream Files with Python

So, to solve my particular problem of setting the ccsid of a streamfile (
not the data, the ccsid tag on the file ) I can run:

import subprocess
subprocess.run(['setccsid', '1208', 'myfile'])

I guess this seems a reasonable solution?

I can assume that Kevin's comment about 32 vs 64 bit architecture might
explain the original question.

I'd still like to know what other transports I could use if I needed to
call a program or run a command in 64 bit and also I'd be interested to
find out if the config.itransport is an error in the itoolkit
documentation
or something that has changed.

On Tue, 2 Oct 2018 at 13:22, Craig Richards <craig@xxxxxxxxxxxxxxxx>
wrote:

Hi Bryan,

Thanks for your post.
Well, that is certainly a difference between the two:

Python 3.4.6 (default, Mar 21 2017, 20:59:04)
[GCC 4.8.2] on aix6

platform.architecture(), sys.maxsize
(('32bit', ''), 2147483647)

==================================

Python 3.6.6 (default, Aug 3 2018, 19:02:01)
[GCC 6.3.0] on aix6

platform.architecture(), sys.maxsize
(('64bit', 'COFF'), 9223372036854775807)

I guess Kevin's comment of July this year is still applicable:


*"ibCall does not work when run 64-bit. For the time being you will
needto
use a different transport."*

Although I have no clue what other transports are available.
I'd even say I don't really know what a transport is - I'd guess it's
some
kind of linkage between python and the IBMi OS.

thanks for the info.
best regards,
Craig


On Tue, 2 Oct 2018 at 12:37, Bryan Dietz <bdietz400@xxxxxxxxx> wrote:

I recall reading this:
https://archive.midrange.com/opensource/201807/msg00030.html

I do not know if it applies, but wanted to re-pass along

Bryan


Craig Richards wrote on 10/2/2018 6:56 AM:
Hi Kevin,

Thanks for taking the time to reply.

I don't know if you had a particular command line program in mind for
setting the CCSID but it seems to me that the easiest way to achieve
it
might be to use the CHGATR command.
It's certainly simpler from a 'user' perspective than coding up a
call
to
qp0lSetAttr()

Seeking about for information on how to call AS400 Commands /
Programs
from
Python lead me to a post which I noticed you had contributed to:



https://stackoverflow.com/questions/50576341/how-to-call-an-rpg-program-from-python

And so that lead me to try a test of:

from itoolkit import *
from itoolkit.lib.ilibcall import *
itransport = iLibCall()
itool = iToolKit()
itool.add(iCmd('sndmsg', 'SNDMSG "WIBBLE" RICHARC'))
itool.call(itransport)
output = itool.dict_out('sndmsg')
print(output)

This works from Python 3.4.6 and prints:
{'success': '+++ success SNDMSG "WIBBLE" RICHARC'}

However if I run it from Python 3.6.6 ( /QopenSys/pkgs/bin/python3 )
it
fails and prints:
{'error': {'error': '*NODATA', 'sndmsg': {...}}}

If my memory serves me correctly, 3.4.6 is what came with the system
and I
installed 3.6.6 via IBMi ACS Open Source Package Management.

I'm not sure what the problem is with this, forgive my ignorance.

Another thing which also puzzled me is that the documentation for
itoolkit
which you kindly linked in your post:

https://python-itoolkit.readthedocs.io/en/latest/examples.html

is different to the code you supplied in the original post and
doesn't
work
for me with either version of python.
For example the code for DSPSYSSTS:
import config
from itoolkit import *
itool = iToolKit()
itool.add(iCmd5250('dspsyssts', dspsyssts'))
itool.call(config.itransport)
...

This codes fails from both versions of python with:
AttributeError: 'module' object has no attribute 'itransport'

So that leaves me with the following questions:

1. Is CHGATR from python a reasonable way to set the "tag" of the
IFS
file. I guess there might be something I can run more directly
from
QSH via
subprocess() that might let me set the CCSID.
2. Is itoolkit a reasonable way to make the call to CHGATR. I'm a
little
confused about it being called an XMLSERVICE collector but is
that
just the
way it is communicating with the OS?
3. Why doesn't my example work with Python 3.6.6 - is it running
a
different version of itoolkit? using help() for iCmd, the syntax
looked
unchanged.
4. Are the code examples for itoolkit wrong or is it to do with
change
code versions or assumed imports or something?

Sorry for my ignorance and the many questions.
What can I say, I'm a curious man...

thanks and best regards,
Craig

On Mon, 1 Oct 2018 at 17:29, Kevin Adler <kadler@xxxxxxxxxx> wrote:

Python has its own built-in codecs and does not use the system
codecs.
You
can see the list (which does not include cp285) here:
[1]
https://docs.python.org/3/library/codecs.html#standard-encodings

As Python runs in PASE it will open the file tagged with
whatever
the
PASE
ccsid is (which defaults to 819 except in rare cases). There is
currently
no mechanism in Python to match the CCSID tag with the encoding
of
the
data, though there are various command line programs you could
call
manually from within Python to do so.


----- Original message -----
From: Craig Richards <craig@xxxxxxxxxxxxxxxx>
Sent by: "OpenSource" <opensource-bounces@xxxxxxxxxxxx>
To: IBMi Open Source Roundtable <opensource@xxxxxxxxxxxx>
Cc:
Subject: [IBMiOSS] Creating Stream Files with Python
Date: Mon, Oct 1, 2018 11:13 AM

Hi All,

I can create streamfiles with different encodings in the IFS
using
python
using the encoding parameter and this works fine e.g:

f = open('asci.txt', 'w', encoding='cp819')

This is good. And fine too. Some might say dandy.
However if I look at the attributes of the stream file it is
always
CCSID
819 if I am creating a new file.

This means that using DSPF to look at the data on the IBMi
will
display
the
data incorrectly if that data is not actually written with
encoding
819.

I can modify the file manually using EDTF to change the file's
CCSID
and
then it will be displayed correctly ( within the limitations
of
the
5250
Client ) however I assume there is a way to set this attribute
correctly
for new files using python?
I understand that the IBMi is fairly unique in that it has
additional
attributes on stream files that store information such as the
(intended)
ccsid but I know it is possible to set these correctly when
using the
unix
open API to create stream files ( for example )

Additionally I'm using an IBMi in the UK and QCCSID is set to
285, I
get
an
error in the above code if I specify encoding='cp285' although
it is
quite
happy to use encoding='cp037'.

Not that I have a requirement to encoding in the IFS in 285
but
I was
a
little surprised that 37 was supported but not 285 -
especially
given
that
it is a UK not US box.

However my main question is - should I be using a different
method to
create my streamfiles if I want the endcoding of the
streamfile
( the
file
header, not the actual data ) to be other than 819?

I've tried using the codecs.open method but that seems to work
the
same
-
it creates the data correctly but the CCSID for the file
itself
is
always
819 for NEW files.

Thanks kindly,
Craig
--
This is the IBMi Open Source Roundtable (OpenSource) mailing
list
To post a message email: OpenSource@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: [2]
https://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at [3]https://archive.midrange.com/opensource.




References

Visible links
1.
https://docs.python.org/3/library/codecs.html#standard-encodings
2. https://lists.midrange.com/mailman/listinfo/opensource
3. https://archive.midrange.com/opensource
--
This is the IBMi Open Source Roundtable (OpenSource) mailing list
To post a message email: OpenSource@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/opensource.

--
This is the IBMi Open Source Roundtable (OpenSource) mailing list
To post a message email: OpenSource@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/opensource.


--
This is the IBMi Open Source Roundtable (OpenSource) mailing list
To post a message email: OpenSource@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/opensource.



[https://www.medtronsoftware.com/img/MedtronMinilogo.bmp] Kevin Bucknum
Senior Programmer Analyst
MEDDATA / MEDTRON
120 Innwood Drive
Covington LA 70433
Local: 985-893-2550
Toll Free: 877-893-2550
https://www.medtronsoftware.com



CONFIDENTIALITY NOTICE

This document and any accompanying this email transmission contain
confidential information, belonging to the sender that is legally
privileged. This information is intended only for the use of the
individual or entity named above. The authorized recipient of this
information is prohibited from disclosing this information to any other
party and is required to destroy the information after its stated need has
been fulfilled. If you are not the intended recipient, or the employee of
agent responsible to deliver it to the intended recipient, you are hereby
notified that any disclosure, copying, distribution or action taken in
reliance on the contents of these documents is STRICTLY PROHIBITED. If you
have received this email in error, please notify the sender immediately to
arrange for return or destruction of these documents.
--
This is the IBMi Open Source Roundtable (OpenSource) mailing list
To post a message email: OpenSource@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/opensource.



This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2020 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].