forum.vmspython.org Forum Index forum.vmspython.org
Forum system
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading COBOL files with VMS Python

 
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS
View previous topic :: View next topic  
Author Message
sampsacom



Joined: 04 May 2015
Posts: 4

PostPosted: Mon May 04, 2015 7:18 pm    Post subject: Reading COBOL files with VMS Python Reply with quote

Hi guys,

I've looked through the docs and examples but can't find anything on dealing RMS files (specifically ones produced by COBOL).

DIR/FULL says the file has these attributes:



Code:
UserS.DAT;3            File ID:   (36035,6,0)
Size:          8KB/8KB         Owner:   [USER,CBLBRD]
Created:    11-SEP-2012 18:50:37.74
Revised:     1-MAY-2015 18:36:21.51 (177)
Expires:    <None specified>
Backup:     11-FEB-2014 17:36:10.87
Effective:  <None specified>
Recording:  <None specified>
Accessed:   <None specified>
Attributes: <None specified>
Modified:   <None specified>
Linkcount:  1
File organization:  Indexed, Prolog: 3, Using 1 key
Shelved state:       Online
Caching attribute:  Writethrough
File attributes:    Allocation: 16, Extend: 0, Maximum bucket size: 2, Global bu
          Version limit: 5
Record format:       Fixed length 170 byte records
Record attributes:  Carriage return carriage control
RMS attributes:     None


And the COBOL definition of the records are:

Code:

FD UserFile.
01 UserDetails.
   02  UserId          PIC X(10).
   02    Name               PIC X(30).
   02    PWHash       PIC X(30).
   02    Email           PIC X(50).
   02    URL             PIC X(50).


Where can I start looking for docs / examples to work with RMS in Python?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jescab



Joined: 28 Jan 2008
Posts: 252

PostPosted: Mon May 04, 2015 9:29 pm    Post subject: Reply with quote

Hi.
I have used the builtin RMS tools and functions to read RMS files.
The fact that it is Cobol code that writes them is totaly irrelevant, of course.
RMS doesn't know that the file was written from Cobol... Smile

In your case the file structure is very simple with only char fields.

The example below should print all records in your file.
I'm not sure that all the imports are needed, just copied that from someware...

Regards, Jan-Erik.
Code:

$ python

import struct
from vms.rmsdef import RMS__SUC, RMS__RNF, RMS__OK_DUP
from vms.fabdef import FAB_M_PUT, FAB_M_GET, FAB_M_DEL, FAB_M_UPD, \
       FAB_M_SHRPUT, FAB_M_SHRGET, FAB_M_SHRDEL, FAB_M_SHRUPD
from vms.rms.IndexedFile import IndexedFile
from construct import *
import sys, rdb, os


UsersRec = Struct("UsersRec",
                   String("USERID", 10),
                   String("NAME", 30),
                   String("PWHASH", 30),
                   String("EMAIL", 50),
                   String("URL", 50))

class USERS(IndexedFile):
    Name = 'USERS.DAT'
    def __init__(self):
        IndexedFile.__init__(self, USERS.Name, UsersRec)

    f = USERS()

    for rec in f:
      print rec.USERID
      print rec.NAME
      print rec.PWHASH
      print rec.EMAIL
      print rec.URL

sys.exit()

$!
$ exit
Back to top
View user's profile Send private message
sampsacom



Joined: 04 May 2015
Posts: 4

PostPosted: Mon May 04, 2015 11:13 pm    Post subject: NotImplementedError? Reply with quote

When I run that piece of code I get:
Code:

Traceback (most recent call last):
  File "readusers.py", line 21, in <module>

  File "readusers.py", line 19, in __init__

  File "/python_root/local/vms/rms/IndexedFile.py", line 26, in __init__
    self._iterkeynum = self.primary_keynum()
  File "/python_root/local/vms/rms/IndexedFile.py", line 33, in primary_keynum
    raise NotImplementedError('IndexedFile.primary_keynum')
NotImplementedError: IndexedFile.primary_keynum
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sampsacom



Joined: 04 May 2015
Posts: 4

PostPosted: Mon May 04, 2015 11:17 pm    Post subject: Re: Reading COBOL files with VMS Python Reply with quote

BTW, of course I did sort of know that it didn't matter what language produced the file, but I had some idea in my head that different languages would use RMS to structure their output in different ways, but wasn't sure (i.e. this file is indexed with a prolog of 3 using 1 key, without me specifying it) so thought I'd mention how the file was created...

I figured a Pascal record might be stored another way and a C struct yet another..
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sampsacom



Joined: 04 May 2015
Posts: 4

PostPosted: Mon May 04, 2015 11:34 pm    Post subject: Re: Reading COBOL files with VMS Python Reply with quote

Found the answer with some googling, the subclass needed to define a primary_keynum(self) method, here's the working code for anyone who comes across the thread later:

Code:

import struct
from vms.rmsdef import RMS__SUC, RMS__RNF, RMS__OK_DUP
from vms.fabdef import FAB_M_PUT, FAB_M_GET, FAB_M_DEL, FAB_M_UPD, FAB_M_SHRPUT, FAB_M_SHRGET, FAB_M_SHR◆
from vms.rms.IndexedFile import IndexedFile
from construct import *
import sys, rdb, os


UsersRec = Struct("UsersRec",
                   String("USERID", 10),
                   String("NAME", 30),
                   String("PWHASH", 30),
                   String("EMAIL", 50),
                   String("URL", 50))

key0_struct = Struct('key0', String('USERID',10))
class USERS(IndexedFile):
    Name = 'USERS.DAT'
    def __init__(self):
        IndexedFile.__init__(self, USERS.Name, UsersRec)

    def primary_keynum(self):
        return 0

f = USERS()

for rec in f:
      print rec.USERID
      print rec.NAME
      print rec.PWHASH
      print rec.EMAIL
      print rec.URL

sys.exit()
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jescab



Joined: 28 Jan 2008
Posts: 252

PostPosted: Tue May 05, 2015 9:11 am    Post subject: Reply with quote

He he... Smile
I actualy took the primary_keynum() away from my working example.
I was not sure that it was needed... Smile

Even if you didn't sound that happy that I replied in the first place,
I still hope that my example was to some help for you...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group