diff --git a/PowerDiagram.py b/PowerDiagram.py index bad1ae9..9cf8bc7 100644 --- a/PowerDiagram.py +++ b/PowerDiagram.py @@ -13,7 +13,7 @@ import os import re import sys import pygraphviz - +from mysql2sqlite import mysql_to_sqlite def add_capacity_constraints(capacity1, capacity2): """ @@ -31,52 +31,6 @@ def add_capacity_constraints(capacity1, capacity2): else: return min(capacity1, capacity2) -def mysql_to_sqlite( mysql_sql_code ): - """ - converts a mysql-compatible sql code into a sqlite-ompatible sql code - - note: the original code was found on internet, then tweaked - """ - content = mysql_sql_code - - # unused commands - COMMAND_RE = re.compile(r'^(SET).*?;\n$', re.I | re.M | re.S) - content = COMMAND_RE.sub('', content) - - # table constraints - TCONS_RE = re.compile(r'\)(\s*(CHARSET|DEFAULT|ENGINE)(=.*?)?\s*)+;', re.I | re.M | re.S) - content = TCONS_RE.sub(');', content) - - # remove comments - # content = re.sub(r'^-- Tab[.]', 'toto', content, flags=re.I | re.M | re.S) - - # sqlite doesn't like 'unsigned' as in `ip_address_3` tinyint(3) unsigned NOT NULL default '27', - content = re.sub(r' unsigned ', ' ', content) - - # sqlite doesn't like 'enum' as in `type` enum('normal','light_out_management') NOT NULL default 'normal',, - content = re.sub(r' enum\([^\)]*\) ', ' varchar(255) ', content) - - # insert multiple values - # INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\*;', re.I | re.M | re.S) - INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*([^;]*);', re.I | re.M | re.S) - #INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*((\[^\)](\)));$', re.I | re.M | re.S) - INSERTVALS_SPLIT_RE = re.compile(r'\)\s*,\s*\(', re.I | re.M | re.S) - - def insertvals_replacer(match): - insert, values = match.groups() - # print("insert=%s"%insert) - # print("values=%s"%values) - values = re.sub('^\s*\(' ,'', values) - values = re.sub('\)\s*$' ,'', values) - replacement = '' - for vals in INSERTVALS_SPLIT_RE.split(values): - #print("vals=%s"%vals) - replacement = '%s\n%s (%s);' % (replacement, insert, vals) - return replacement - - content = INSERTVALS_RE.sub(insertvals_replacer, content) - return content - class Machine(object): """ represents a device with input and output power plugs. It could represent a power consumer such as a server (in which case, it has no output plug), or a power distrubtion unit (in which case, it has one input plug and more than one output plugs), or even something else... diff --git a/mysql2sqlite.py b/mysql2sqlite.py new file mode 100644 index 0000000..03ac7eb --- /dev/null +++ b/mysql2sqlite.py @@ -0,0 +1,47 @@ +import re + +def mysql_to_sqlite( mysql_sql_code ): + """ + converts a mysql-compatible sql code into a sqlite-ompatible sql code + + note: the original code was found on internet, then tweaked + """ + content = mysql_sql_code + + # unused commands + COMMAND_RE = re.compile(r'^(SET).*?;\n$', re.I | re.M | re.S) + content = COMMAND_RE.sub('', content) + + # table constraints + TCONS_RE = re.compile(r'\)(\s*(CHARSET|DEFAULT|ENGINE)(=.*?)?\s*)+;', re.I | re.M | re.S) + content = TCONS_RE.sub(');', content) + + # remove comments + # content = re.sub(r'^-- Tab[.]', 'toto', content, flags=re.I | re.M | re.S) + + # sqlite doesn't like 'unsigned' as in `ip_address_3` tinyint(3) unsigned NOT NULL default '27', + content = re.sub(r' unsigned ', ' ', content) + + # sqlite doesn't like 'enum' as in `type` enum('normal','light_out_management') NOT NULL default 'normal',, + content = re.sub(r' enum\([^\)]*\) ', ' varchar(255) ', content) + + # insert multiple values + # INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*\((.*)\*;', re.I | re.M | re.S) + INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*([^;]*);', re.I | re.M | re.S) + #INSERTVALS_RE = re.compile(r'^(INSERT INTO.*?VALUES)\s*((\[^\)](\)));$', re.I | re.M | re.S) + INSERTVALS_SPLIT_RE = re.compile(r'\)\s*,\s*\(', re.I | re.M | re.S) + + def insertvals_replacer(match): + insert, values = match.groups() + # print("insert=%s"%insert) + # print("values=%s"%values) + values = re.sub('^\s*\(' ,'', values) + values = re.sub('\)\s*$' ,'', values) + replacement = '' + for vals in INSERTVALS_SPLIT_RE.split(values): + #print("vals=%s"%vals) + replacement = '%s\n%s (%s);' % (replacement, insert, vals) + return replacement + + content = INSERTVALS_RE.sub(insertvals_replacer, content) + return content