Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active July 18, 2018 03:32
Show Gist options
  • Select an option

  • Save BlogBlocks/ee1c229f7eb93139eef52f981d26a871 to your computer and use it in GitHub Desktop.

Select an option

Save BlogBlocks/ee1c229f7eb93139eef52f981d26a871 to your computer and use it in GitHub Desktop.
"""
A lot to look over here.
This file sets in a directory contain
./cfdg_files ( your database and the binary cfdg file ALSO a list of CFDG files on your computer)
./cfdg_files/cfdgs/ ( here is where your *.cfdg files ate generated )
./cfdg_files/images/ ( where you image files are generated )
Using the CFDG grammar/script inside the triple apostophies:
It generates a file ( filename = "cfdg_files/cfdgs/learning005.cfgd" ) rename this to save new files.
Then it saves the cfdg script in a SQLITE database. Then runs the the cfdg script saves and
opens the generated image.
"""
import sqlite3
import base64
import re
import time
import os
import hashlib
import subprocess
import sys
filename = "cfdg_files/cfdgs/learning005.cfgd"
if os.path.exists(filename):
print "Filename Exists"
question ="Replace File"
Join = raw_input(question)
if Join.lower() == 'y':
init = open(filename,"w");init.close()
print "Over writing ",filename
else:
print "Enter New Filename and Retry"
sys.exit()
else:
init = open(filename,"w");init.close()
database = "cfdg_files/cfdg_files2.db"
conn = sqlite3.connect(database)
conn.text_factory = lambda x: unicode(x, "utf-8", "ignore")
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS editor
(file text, content text, description text, hash text, UNIQUE(content, hash));"""
)
c.execute("CREATE VIRTUAL TABLE IF NOT EXISTS data USING FTS4(file, content, description, hash)");
conn.commit()
ablob = """
startshape ab
rule ab
{
CIRCLE{}
cd{x 1 size .5}
}
rule cd
{
SQUARE{}
ef{x 1 size .5}
eg{x .15 y .5 size .5}
eg{x .25 y .35 size .25}
eg{x .5 y .5 size .5}
eg{x .35 y .35 size .25}
eg{x .45 y .5 size .5}
}
rule ef
{
CIRCLE{}
ef{x 1 size .5}
eg{x .15 y .5 size .5}
eg{x .25 y .65 size .25}
eg{x .5 y .5 size .5}
eg{x .35 y .85 size .25}
eg{x .45 y .5 size .5}
}
rule eg
{
CIRCLE{}
}
"""
description = "Date :"+dt+filename
if os.path.isfile(filename):
content = sqlite3.Binary(ablob)
m = hashlib.md5()
m.update(content)
Hash = m.hexdigest()
c.execute("INSERT or IGNORE INTO editor (file, content, description, hash) VALUES(?, ?, ?, ?)",
(filename, content, description, Hash))
c.execute("INSERT or IGNORE INTO data (file, content, description, hash) VALUES(?, ?, ?, ?)",
(filename, content, description, Hash))
conn.commit()
for row in c.execute('SELECT rowid, * FROM editor ORDER BY rowid DESC LIMIT 1'):
print row[0],"\n",row[1],"\n",row[2],row[3]
with open(filename, 'a') as outfile:
for row in c.execute('SELECT rowid, * FROM editor ORDER BY rowid DESC LIMIT 1'):
#for row in c.execute('SELECT * FROM ipynb WHERE rowid = 100 LIMIT 10 OFFSET 0'):
row2 = row[2]
row2 =str(row2)
outfile.write(row2)
conn.close()
#f.close()
#prints the filename of the file just retrieved
print filename
conn.close()
#imageout = "cfdg_files/"+base_cfdg[:-5]+count+".png"
imageout = filename.replace("/cfdgs/","/images/")[:-5]+".png"
text ="cfdg_files/cfdg -s 640 "+filename+" "+imageout
print text.split()
subprocess.call(text, shell=True)
from IPython.display import Image
Image(imageout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment