python_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python_examples [2017/12/05 02:04] – added msgread.mpy example avon | python_examples [2019/03/09 21:40] (current) – [Simple File Lister] g00r00 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Mystic Python Examples ====== | ====== Mystic Python Examples ====== | ||
- | This page contains examples of code using Mystic Python. | + | This page contains examples of code using Mystic Python. Use |
+ | the table of contents to jump to the various examples below. | ||
- | ====msgread.mpy==== | + | ----- |
+ | ===== Simple Message Reader ===== | ||
- | Python functions for reading Message Bases are: | + | <file python msgread.mpy> |
- | msg_open, msg_seek, msg_found, msg_next, msg_prev, msg_gethdr, msg_gettxt, msg_close | + | ##################################################### |
- | + | # Simple Message Reader Example using Mystic Python # | |
- | This script is an example of how to use these functions. It implements a very basic message | + | ##################################################### |
- | reader using the user's current message base. | + | |
- | + | # Reads messages in the user's current message base | |
- | | + | # With a pause prompt |
- | # Simple Message Reader Example using Mystic Python # | + | |
- | ##################################################### | + | from mystic_bbs import * |
- | + | ||
- | # Reads messages in the user's current message base | + | # Load the current user and then load their current message base |
- | # With a pause prompt | + | # but fail if they have not selected a message base |
- | + | ||
- | from mystic_bbs import * | + | user = getuser(0) |
- | + | mbase = getmbaseid(user[" | |
- | # Load the current user and then load their current message base | + | |
- | # but fail if they have not selected a message base | + | if mbase is None: |
- | + | writeln (" | |
- | user = getuser(0) | + | quit() |
- | mbase = getmbaseid(user[" | + | |
- | + | # Open the message base then check to make sure its open before reading | |
- | if mbase is None: | + | # data from it |
- | writeln (" | + | |
- | quit() | + | msg = msg_open(mbase[" |
- | + | ||
- | # Open the message base then check to make sure its open before reading | + | if msg is None: |
- | # data from it | + | quit() |
- | + | ||
- | msg = msg_open(mbase[" | + | done = False |
- | + | ||
- | if msg is None: | + | # Seek to the first message in the base and loop while a message |
- | quit() | + | # is found, calling msg_next after each message so the next one |
- | + | # will be loaded. | |
- | + | # from the first message. | |
- | | + | |
- | + | msg_seek(msg, | |
- | # Seek to the first message in the base and loop while a message | + | |
- | # is found, calling msg_next after each message so the next one | + | while msg_found(msg) and not done and not shutdown(): |
- | # will be loaded. | + | |
- | # from the first message. | + | # Load the message header information into a dictionary |
- | + | # and the message text into a list | |
- | msg_seek(msg, | + | |
- | + | msghdr | |
- | while msg_found(msg) and not done and not shutdown(): | + | msgtext = msg_gettxt(msg) |
- | + | ||
- | # Load the message header information into a dictionary | + | # Show the message header, setting a line counter that can |
- | # and the message text into a list | + | # be used to pause the screen |
- | + | ||
- | msghdr | + | writeln(" |
- | msgtext = msg_gettxt(msg) | + | writeln(" |
- | + | writeln(" | |
- | # Show the message header, setting a line counter that can | + | writeln(" |
- | # be used to pause the screen | + | writeln(" |
- | + | ||
- | | + | pausecount = 4 |
- | + | ||
- | | + | # loop through each line in the message (list) and pause when |
- | writeln(" | + | # we get more than 23 lines |
- | writeln(" | + | |
- | writeln(" | + | for line in msgtext: |
- | + | ||
- | # loop through each line in the message (list) and pause when | + | # before printing a line check if we need to pause |
- | # we get more than 23 lines | + | |
- | + | if pausecount >= 23: | |
- | for line in msgtext: | + | pausecount = 1 |
- | | + | |
- | + | write(" | |
- | writeln(line) | + | |
- | + | ch = onekey(chr(13) + ' | |
- | if pausecount >= 23: | + | |
- | pausecount = 1 | + | # after getting input, erase the pause prompt then process the input |
- | + | ||
- | write(" | + | backspace(wherex(), |
- | + | ||
- | | + | if ch == 'Q': |
- | done = True | + | done = True |
- | break | + | break |
- | + | elif ch == ' | |
- | # Load the next message | + | break |
- | + | ||
- | | + | # increase pause counter and send a line of message text |
- | + | # unless it is a kludge line: | |
- | # Close the message base and report that we're done | + | |
- | + | if (line == "" | |
- | | + | pausecount += 1 |
- | + | writeln(line) | |
- | writeln(" | + | |
+ | # At end of message, so lets give a prompt if we didn't get a quit | ||
+ | # from the pause prompt: | ||
+ | |||
+ | if not done: | ||
+ | write(" | ||
+ | |||
+ | ch = onekey(chr(13) + 'APQ', True); | ||
+ | |||
+ | if ch == ' | ||
+ | # do nothing here so it redisplays the same msg | ||
+ | pass | ||
+ | |||
+ | elif ch == ' | ||
+ | if msghdr[" | ||
+ | msg_prev(msg) | ||
+ | |||
+ | elif ch == ' | ||
+ | done = True | ||
+ | break | ||
+ | else: | ||
+ | msg_next(msg); | ||
+ | |||
+ | # Close the message base and report that we're done | ||
+ | |||
+ | msg_close(msg) | ||
+ | |||
+ | writeln(" | ||
+ | |||
+ | </ | ||
+ | |||
+ | ----- | ||
+ | ===== MCI Helper Functions ===== | ||
+ | |||
+ | <file python mcihelp.mpy> | ||
+ | import mystic_bbs as bbs; | ||
+ | |||
+ | def stripmci (str): | ||
+ | pos = str.find(" | ||
+ | |||
+ | while pos != -1: | ||
+ | str = str[:pos] + str[pos+3: | ||
+ | pos = str.find(" | ||
+ | |||
+ | return str | ||
+ | |||
+ | def mcilen (str): | ||
+ | |||
+ | return len(stripmci(str)) | ||
+ | |||
+ | |||
+ | text = " | ||
+ | |||
+ | bbs.rwriteln(stripmci(text)); | ||
+ | bbs.rwriteln(str(mcilen(text))) | ||
+ | |||
+ | bbs.writeln(" | ||
+ | |||
+ | </ | ||
+ | |||
+ | ----- | ||
+ | ===== Simple File Lister ===== | ||
+ | |||
+ | <file python filelist.mpy> | ||
+ | ################################################### | ||
+ | # Simple File Listing Example using Mystic Python # | ||
+ | ################################################### | ||
+ | |||
+ | # List files in the user's current file base | ||
+ | |||
+ | from mystic_bbs import * | ||
+ | |||
+ | # filelist flags | ||
+ | |||
+ | file_offline = int(" | ||
+ | file_invalid = int(" | ||
+ | file_deleted = int(" | ||
+ | file_failed | ||
+ | file_free | ||
+ | file_hatched = int(" | ||
+ | |||
+ | # Load the current user and then load their current file base | ||
+ | # but fail if they have not selected a file base | ||
+ | |||
+ | user = getuser(0) | ||
+ | fbase = getfbaseid(user[" | ||
+ | |||
+ | if fbase is None: | ||
+ | writeln (" | ||
+ | quit() | ||
+ | |||
+ | # Open the file list then check to make sure its open before reading | ||
+ | # data from it | ||
+ | |||
+ | flist = fl_open(fbase[" | ||
+ | |||
+ | if flist is None: | ||
+ | writeln(" | ||
+ | quit() | ||
+ | |||
+ | done = False | ||
+ | |||
+ | # Seek to the first file in the list and loop while a file is found calling | ||
+ | # next after each file so the next one will be loaded. | ||
+ | # called before cycling through the list | ||
+ | |||
+ | fl_seek(flist, | ||
+ | |||
+ | while fl_found(flist) and not done and not shutdown(): | ||
+ | |||
+ | # Load the file information into a dictionary and then get the file | ||
+ | # description | ||
+ | |||
+ | fileinfo = fl_getfile(flist) | ||
+ | filedesc = fl_getdesc(flist) | ||
+ | |||
+ | # Show the file | ||
+ | |||
+ | writeln(" | ||
+ | writeln("" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | |||
+ | flags = "" | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + "FREE " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if flags == "": | ||
+ | flags = " | ||
+ | |||
+ | writeln(" | ||
+ | writeln("" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | |||
+ | # only print up to the first 10 lines so we can fit it on the screen | ||
+ | |||
+ | for line in range(min(fileinfo[" | ||
+ | writeln(filedesc[line]) | ||
+ | |||
+ | writeln("" | ||
+ | write(" | ||
+ | |||
+ | ch = onekey(chr(13) + ' | ||
+ | |||
+ | if ch == ' | ||
+ | # do nothing here so it redisplays the same file | ||
+ | pass | ||
+ | |||
+ | elif ch == ' | ||
+ | fl_prev(flist) | ||
+ | |||
+ | elif ch == ' | ||
+ | done = True | ||
+ | break | ||
+ | else: | ||
+ | fl_next(flist); | ||
+ | |||
+ | # Close the file list and report that we're done | ||
+ | |||
+ | fl_close(flist) | ||
+ | |||
+ | writeln(" | ||
+ | </ | ||
+ | |||
+ | ===== View Dictionaries and Lists ===== | ||
+ | <file python dump.mpy> | ||
+ | from mystic_bbs import * | ||
+ | |||
+ | # Dump contents of a dictionary or list to the screen with | ||
+ | # screen pause | ||
+ | |||
+ | def dump (obj, linecount): | ||
+ | | ||
+ | textcolor(7) | ||
+ | | ||
+ | for k, v in obj.items(): | ||
+ | if hasattr(v, ' | ||
+ | writeln(k) | ||
+ | dump(v, linecount) | ||
+ | else: | ||
+ | writeln ("{} : {}" | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write(" | ||
+ | | ||
+ | for v in obj: | ||
+ | if hasattr(v, ' | ||
+ | dump(v, linecount) | ||
+ | else: | ||
+ | | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write("|PA|07" | ||
+ | else: | ||
+ | writeln(" | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write(" | ||
+ | |||
+ | # grab the current user and dump the contents of their user dictionary | ||
+ | # This function can be used to dump anything, users, bases, groups, etc | ||
+ | |||
+ | writeln(" | ||
+ | user = getuser(0) | ||
+ | dump(user, 1) | ||
+ | </ |
python_examples.1512461047.txt.gz · Last modified: 2017/12/05 02:04 by avon