# -*- coding: utf-8 -*-
"""
Tällä ohjelmalla on tuotettu tämän sivuston python-ohjelmien esittelyt.
"""
# shd 0-0:m
'''
Yhdestä python-ohjelmasta eri ohjelmaversioita ja niiden dokumentaatioita
'''
# txt 0-0:m
'''
Halusin esitellä miten kehittelin peliohjelmia vaihe vaiheelta.
Esimerkiksi hyttysjahtipelin ensimmäinen versio
ei tee muuta kuin luo näytölle ikkunan.
Seuraavassa vaiheessa pelissä on nuolinäppäimillä
siirreltävä hyttyslätkä jne.
Peliä kehittäessä huomasin usein tarpeen muuttaa aikaisempien
vaiheiden koodia. Opin esimerkiksi vasta pelin valmistuttua,
miten avata näyttölaitteen ruudun kokoinen peli-ikkuna.
Jos olisin tehnyt oikeasti kuusi eri versioita pelistä,
olisin joutunut muuttamaan niitä kaikkia.
Niinpä tein tämän ohjelman, jolla voin "poimia" lopullisesta versiosta
ohjelman eri kehitysvaiheita vastaavat ohjelmaversiot.
Samalla teen ohjelmasta xml-tiedoston, josta luon tämänkaltaisia dokumentteja.
Generoin yhdestä python-ohjelmasta dokumentin ja
eri versioita itse ohjelmasta.
Lisään python ohjelmaan seuraavanlaisia kommentteja:
#shd n1-n1:v
shd-tagia seuraava kommentti näkyy dokumenttitiedostossa otsikkona.
#cod n1-n2:v n3-n4:m n5-100:z
# tähän tulee ohjelmakoodia, joka on ohjelmaversioissa n1:stä ylöspäin.
# Lisäksi tämä koodi näkyy dokumentissa vaiheissa n1-n2 ja n3-n4
# (Käytän kommenttirivejä, koska tämä teksti on siis suoritettavassa
# ohjelmassa.)
#txt n1-n2:v n3-n4:m
Tähän tulevat rivit eivät näy ohjelmassa,
mutta näkyvät vaiheiden n1-n2 ja n3-n4 dokumenteissa.
ylempi tarkoittaa, että sitä seuraavat rivit kirjoitetaan sekä
dokumenttitiedostoon että ohjelmatiedostoon.
Vaiheissa n1-n2 kirjoitetaan dokumenttitiedostoon vihreällä ja
vastaavan vaiheen ohjelmatiedostoon.
Vaiheissa n3-n4 rivit kirjoitetaan mustalla ja vaiheissa n5-100
ne kirjoitetaan pelkästään ohjelmatiedostoon.
Vaiheissa ennen n1:tä, seuraavia rivejä ei kirjoiteta dokumenttiin
eikä itse ohjelmaan.
'''
# cod 0-0:v 1-100:z
# -*- coding: utf-8 -*-
# = = = = = = = = = = = = = = = = = = = = = = = = = =
# import sys
# print "This is the name of the script: ", sys.argv[0]
# print "Number of arguments: ", len(sys.argv)
# print "The arguments are: " , str(sys.argv)
# = = = = = = = = = = = = = = = = = = = = = = = = = =
# txt 0-0:m
'''
-
'''
# cod 0-0:v 1-100:z
import re
import os
import sys
import ast
import time
import random
from pathlib import Path, PurePath
def tagline(tag, vari, vaihe, ln):
if tag == 'shd':
unique_id = projName + str(vaihe) + str(ln)
tglin = '\n\n'
else:
tglin = '\n<' + tag + ' mod = "' + vari + '">\n'
return tglin
def tagline_s(tag, vari):
tglin = '\n<' + tag + ' mod = "' + vari + '">\n'
return tglin
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def tarkistaVaihe_s(line, tag, vaihe):
if not tag.group(1) == 'shd':
attrs = re.findall(r"([0-9]*)-([0-9]*):([a-z])", line)
for attr in attrs:
if int(attr[0]) <= vaihe <= int(attr[1]):
if attr[2] == 'z':
txt = False
code = True
vari = 'm'
elif attr[2] == 'p':
txt = True
code = False
vari = 'p'
else:
txt = True
code = True
vari = str(attr[2])
tglin = tagline_s(tag.group(1), vari)
return (txt, code, tglin)
return (False, False, '')
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def tarkistaVaihe_c(line, tag, vaihe, ln):
if not tag.group(1) == 'txt':
attrs = re.findall(r"([0-9]*)-([0-9]*):([a-z])", line)
for attr in attrs:
if int(attr[0]) <= vaihe <= int(attr[1]):
if attr[2] == 'z':
code = True
vari = 'm'
elif attr[2] == 'p':
code = True
vari = 'p'
else:
code = True
vari = str(attr[2])
tglin = tagline(tag.group(1), vari, vaihe, ln)
return (code, tglin)
return (False, '')
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def tarkistaVaihe_py(line, tag, vaihe, ln):
if tag.group(1) == 'cod':
attrs = re.findall(r"([0-9]*)-([0-9]*):([a-z])", line)
for attr in attrs:
if int(attr[0]) <= vaihe <= int(attr[1]):
if attr[2] == 'z':
code = True
vari = 'm'
elif attr[2] == 'p':
code = False
vari = 'p'
else:
code = True
vari = str(attr[2])
tglin = tagline(tag.group(1), vari, vaihe, ln)
return (code, tglin)
return (False, '')
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def endtag(tag, fout):
if tag == 'shd':
fout.write('\n')
elif not tag == '':
fout.write('\n' + tag + '>')
return ''
def endtag_s(tag, fout):
if not tag == '':
fout.write('\n' + tag + '>')
return ''
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def ilman_selityksia(fout, vaihe):
with open(infil, 'r') as fin:
ln = 0
current = ''
pr_cod = False
for line in fin:
ln = ln + 1
tag = re.search(r"(?<=\# )(txt|cod|shd)", line)
if tag:
if pr_cod:
current = endtag(current, fout)
(pr_cod, tagLine) = tarkistaVaihe_c(
line, tag, vaihe, ln)
if pr_cod:
current = tag.group(1)
fout.write(tagLine)
elif pr_cod:
if (not re.search(r"'''|\"\"\"", line) and len(line)) > 1:
fout.write(repl(line))
current = endtag(current, fout)
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def selitykset(fout, vaihe):
with open(infil, 'r') as fin:
ln = 0
current = ''
pr_txt = False
for line in fin:
ln = ln + 1
tag = re.search(r"(?<=\# )(txt|cod|shd)", line)
if tag:
if pr_txt:
current = endtag_s(current, fout)
(pr_txt, pr_cod, tagLine) = tarkistaVaihe_s(
line, tag, vaihe)
if pr_txt:
current = tag.group(1)
fout.write(tagLine)
elif pr_txt:
if not re.search(r"'''|\"\"\"", line):
if current == 'txt':
fout.write(line)
elif current == 'cod':
alkuloppu = re.split(r"^#| #", line, maxsplit=1)
fout.write(repl(alkuloppu[0]))
if len(alkuloppu) > 1:
fout.write('<orange> # ' + repl(alkuloppu[1]) +
'</orange>')
current = endtag_s(current, fout)
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def vain_koodi(progfil, vaihe):
with open(infil, 'r') as fin:
ln = 0
pr_cod = False
for line in fin:
ln = ln + 1
tag = re.search(r"(?<=\# )(txt|cod|shd)", line)
if tag:
(pr_cod, tagLine) = tarkistaVaihe_py(line, tag, vaihe, ln)
elif pr_cod:
if not re.search(r"'''|\"\"\"", line): # and len(line)) > 1:
progfil.write(line)
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def repl(line):
return re.sub('<', '<', re.sub('>', '>', line))
def summary():
with open(infil) as fd:
file_contents = fd.read()
module = ast.parse(file_contents)
return ast.get_docstring(module)
def lisafileet(fout, curdir):
narg = len(sys.argv)
if narg > 3:
fout.write(
'\nLataa myös seuraavat, ellet ole vielä ladannut')
for i in range(3, narg):
webref = PurePath('/webPy/', curdir, str(sys.argv[i]))
fout.write(
'\nlisätiedosto: ' + str(sys.argv[i]) +
'
')
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
def main():
with open(xmlf, 'w') as fout:
fout.write('\n' +
projName + '.py\n\n')
fout.write('\n')
fout.write(summary())
fout.write('\n')
fout.write(time.asctime(time.localtime(time.time())))
fout.write('\n')
fout.write('\n')
for vaihe in range(maxVaihe):
ilman_selityksia(fout, vaihe)
fout.write('Selityksiä ylläolevaan')
selitykset(fout, vaihe)
pfname = projName+'_'+str(vaihe)+'.py'
with open(pfname, 'w') as progfil:
vain_koodi(progfil, vaihe)
webroot = Path(os.path.expandvars("$WEBPY"))
curdir = Path.cwd().relative_to(webroot)
webref = PurePath('/webPy/', curdir, pfname).as_posix()
fout.write(
'Lataa tästä ohjelman tämän hetkinen versio: ' +
pfname + '')
lisafileet(fout, curdir)
fout.write('\n\n')
fout.write('\n')
# txt 0-0:m
'''
a
'''
# cod 0-0:v 1-100:z
if len(sys.argv) > 1:
projName = str(sys.argv[1])
print(projName)
else:
projName = 'ProjektiX'
if len(sys.argv) > 2:
maxVaihe = int(sys.argv[2])
else:
maxVaihe = 6
xmlf = PurePath(projName + '_code.xml')
infil = Path(projName + '.py')
summary()
if __name__ == "__main__":
main()
# txt 0-100:z
# PurePath.relative_to(*other)
# classmethod Path.cwd()
# PurePath.name
# PurePath.as_posix()
# os.path.expandvars(path)