From ad3c926d180f5d843b77db44c2b2449cdffeec34 Mon Sep 17 00:00:00 2001 From: Agnibho Mondal Date: Sun, 13 Dec 2015 20:07:45 +0530 Subject: [PATCH] Added a new function to compile module --- compile.py | 84 +++++++++++++++++++++++++++++++++++------------------- conf.py | 74 +++++++++++++++++++++++------------------------ ddstorm.py | 2 +- 3 files changed, 93 insertions(+), 67 deletions(-) diff --git a/compile.py b/compile.py index a94e730..acb5ec7 100644 --- a/compile.py +++ b/compile.py @@ -1,56 +1,82 @@ #! /usr/bin/python3 -# DDStorm -# ------- -# Copyright (c) 2015 Agnibho Mondal -# All rights reserved +''' +This module converts text differetial diagnosis files +to a simplified modular file format that can be used +by the program. +''' +''' +Copyright (c) 2015 Agnibho Mondal +All rights reserved -# This file is part of DDStorm. +This file is part of DDStorm. -# DDStorm is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +DDStorm is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. -# DDStorm is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +DDStorm is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. -# You should have received a copy of the GNU General Public License -# along with DDStorm. If not, see . +You should have received a copy of the GNU General Public License +along with DDStorm. If not, see . +''' -import os, logging, re +import os +import logging +import re from operator import itemgetter from fnmatch import fnmatch + from conf import Conf from const import * from alias import Alias + logging.basicConfig(filename=LOG_FILE) class Compile: + ''' + This class creates a compiler for the DDStorm + that compiles the text files containing list of + differential diagnosis to simplified modular + data files usable by the program. + ''' + def __init__(self, conf=False): - self.source=set() - self.custom=set() + ''' + The constructor optionally accepts a configuration. + If none is provided it creates a default configuration. + + Parameters: + conf - A dictionary containing configuration options + ''' if(conf): - self.conf=conf + self._conf=conf else: - self.conf=Conf() - self.alias=Alias(conf) + self._conf=Conf() + + def compile(self): + ''' Compile the text files to DDStorm modules. ''' + self.source=set() + self.custom=set() + self.alias=Alias(self._conf) self.clean=True - for path, subdirs, files in os.walk(self.conf.get("library_path")): + for path, subdirs, files in os.walk(self._conf.get("library_path")): for name in files: if(fnmatch(name, "*.txt")): self.source.add(os.path.join(path, name)) - for path, subdirs, files in os.walk(self.conf.get("custom_path")): + for path, subdirs, files in os.walk(self._conf.get("custom_path")): for name in files: if(fnmatch(name, "*.txt")): self.custom.add(os.path.join(path, name)) - if(not os.path.isdir(self.conf.get("module_path"))): - os.makedirs(self.conf.get("module_path")) - for f in os.listdir(self.conf.get("module_path")): + if(not os.path.isdir(self._conf.get("module_path"))): + os.makedirs(self._conf.get("module_path")) + for f in os.listdir(self._conf.get("module_path")): if(fnmatch(f, "*.module")): - os.unlink(self.conf.get("module_path")+f) + os.unlink(self._conf.get("module_path")+f) self.priorityRegex=re.compile("(?<=\.)\d+$") for src in self._sortPriority(self.source): self._makeModule(src) @@ -77,7 +103,7 @@ class Compile: m=re.search(self.priorityRegex, module) if(m): module=module.replace("."+m.group(), "") - modFile=self.conf.get("module_path")+module+".module" + modFile=self._conf.get("module_path")+module+".module" modFlag=False with open(src, "r") as sf, open(modFile, "a") as tf: for line in sf: @@ -116,7 +142,7 @@ class Compile: print(text, file=fn) def main(): - c=Compile() + c=Compile().compile() if(__name__=="__main__"): main() diff --git a/conf.py b/conf.py index c8a73c2..5b84851 100644 --- a/conf.py +++ b/conf.py @@ -40,7 +40,7 @@ class Conf: function calls. ''' - conf={} #Initiates configuration dictionary + _conf={} #Initiates configuration dictionary def __init__(self, filename=CONF_FILE): ''' @@ -54,14 +54,14 @@ class Conf: def default(self): ''' Set the default values ''' - self.conf["library_path"]="./library/" - self.conf["custom_path"]="./custom/" - self.conf["index_path"]="./index/" - self.conf["alias_path"]="./alias/" - self.conf["module_path"]="./modules/" - self.conf["splash_screen"]="yes" - self.conf["clean_log"]="no" - self.conf["status_message"]="on" + self._conf["library_path"]="./library/" + self._conf["custom_path"]="./custom/" + self._conf["index_path"]="./index/" + self._conf["alias_path"]="./alias/" + self._conf["module_path"]="./modules/" + self._conf["splash_screen"]="yes" + self._conf["clean_log"]="no" + self._conf["status_message"]="on" def read(self): ''' Read the configuration file and collect the values ''' @@ -73,36 +73,36 @@ class Conf: if(line.startswith("#")): # Ignores comments pass elif(line.startswith("library_path=")): # Library files path - self.conf["library_path"]=line[13:] - if(os.path.isdir(self.conf["library_path"])): - if(not self.conf["library_path"].endswith("/")): - self.conf["library_path"]+="/" + self._conf["library_path"]=line[13:] + if(os.path.isdir(self._conf["library_path"])): + if(not self._conf["library_path"].endswith("/")): + self._conf["library_path"]+="/" elif(line.startswith("custom_path=")): # Custom files path - self.conf["custom_path"]=line[12:] - if(os.path.isdir(self.conf["custom_path"])): - if(not self.conf["custom_path"].endswith("/")): - self.conf["custom_path"]+="/" + self._conf["custom_path"]=line[12:] + if(os.path.isdir(self._conf["custom_path"])): + if(not self._conf["custom_path"].endswith("/")): + self._conf["custom_path"]+="/" elif(line.startswith("index_path=")): # Index files path - self.conf["index_path"]=line[11:] - if(os.path.isdir(self.conf["index_path"])): - if(not self.conf["index_path"].endswith("/")): - self.conf["index_path"]+="/" + self._conf["index_path"]=line[11:] + if(os.path.isdir(self._conf["index_path"])): + if(not self._conf["index_path"].endswith("/")): + self._conf["index_path"]+="/" elif(line.startswith("alias_path=")): # Alias files path - self.conf["alias_path"]=line[11:] - if(os.path.isdir(self.conf["alias_path"])): - if(not self.conf["alias_path"].endswith("/")): - self.conf["alias_path"]+="/" + self._conf["alias_path"]=line[11:] + if(os.path.isdir(self._conf["alias_path"])): + if(not self._conf["alias_path"].endswith("/")): + self._conf["alias_path"]+="/" elif(line.startswith("module_path=")): # Path to save compiled modules - self.conf["module_path"]=line[12:] - if(os.path.isdir(self.conf["module_path"])): - if(not self.conf["module_path"].endswith("/")): - self.conf["module_path"]+="/" + self._conf["module_path"]=line[12:] + if(os.path.isdir(self._conf["module_path"])): + if(not self._conf["module_path"].endswith("/")): + self._conf["module_path"]+="/" elif(line.startswith("splash_screen=")): # Whether to show a splash screen - self.conf["splash_screen"]=line[14:] + self._conf["splash_screen"]=line[14:] elif(line.startswith("clean_log=")): # Whether to clean logs before exit - self.conf["clean_log"]=line[10:] + self._conf["clean_log"]=line[10:] elif(line.startswith("status_message=")): # Whether to show status messages - self.conf["status_message"]=line[15:] + self._conf["status_message"]=line[15:] else: logging.warning("Unrecognized configuration: "+line) # Log a warning if unrecognized option found except: # Go with default if file could not be read and log an error @@ -121,9 +121,9 @@ class Conf: Dictionary containing all options if key not specified ''' if(key): - return self.conf[key] + return self._conf[key] else: - return self.conf + return self._conf def set(self, key, value): ''' @@ -133,13 +133,13 @@ class Conf: key - configuration item name value - the value to set the configuration item to ''' - self.conf[key]=value + self._conf[key]=value def write(self): ''' Write configurations to file ''' with open(CONF_FILE, "w") as f: - for k in self.conf: - print(k+"="+self.conf[k], file=f) + for k in self._conf: + print(k+"="+self._conf[k], file=f) #If invoked directly, prints a list of available configurations if(__name__=="__main__"): diff --git a/ddstorm.py b/ddstorm.py index 0acf7b2..7fb300a 100644 --- a/ddstorm.py +++ b/ddstorm.py @@ -33,7 +33,7 @@ class DDStorm: else: self.conf=Conf() if(comp): - self.compiler=Compile(conf) + self.compiler=Compile(conf).compile() self.index=Index(conf) def dd(self, symptoms): -- 2.39.2