From: Lionel Date: Fri, 24 Oct 2014 13:12:18 +0000 (+0200) Subject: Code cleaning X-Git-Url: http://gitweb.enneade.fdn.org/?a=commitdiff_plain;h=e41aba013d8e6d61543aa3de45c6ee9e6f8196f2;p=pyowl.git Code cleaning --- diff --git a/cm160Server.py b/cm160Server.py index 347047e..c255dbe 100755 --- a/cm160Server.py +++ b/cm160Server.py @@ -26,9 +26,6 @@ class OWLError(Exception): pass VERSION = 1.0 -CM160SERVER_CONFIG_FILE = '.cm160server.cfg' -PREVIOUS_DB_FILE = 'PREVIOUS_DB_DFILE' -NON_COMPAT_MODE_TEXT_FILE_HEADER = "TIME DATE AMPS COST PER KWH" #*********************** UserOutput *************************** class UserOutput: @@ -879,124 +876,13 @@ class MySQLDataStore(object): # self.__uo.info(text) - -######################## Presistent dict config ########################## - -def getUserHome(): - """Get the user home path as this will be used to store config files""" - try: - return os.environ["HOME"] - except KeyError: - #If we don't have the HOME env variable set then attempt to use - #the default windows path - homeDrive=os.environ["HOMEDRIVE"] - homePath=os.environ["HOMEPATH"] - return "%s%s" % (homeDrive, homePath) - -def getConfigFileHeader(): - lines=[] - lines.append("#cm160Server.py config file.\n" ) - lines.append("\n") - return "".join(lines) - -def saveDict(config, cfgFile): - """Save the destClientConfig to a file""" - #Save the resultant dict - fd = open(cfgFile,'w') - fd.write(getConfigFileHeader() ) - keys=config.keys() - keys.sort() - for k in keys: - t="%s=%s\n" % (k, config[k]) - fd.write(t) - fd.close() - -def getLinesFromFile(f): - """Get Lines from file""" - fd = open(f,"r") - lines = fd.readlines() - fd.close() - return lines - -def _removeInvalidChars(line): - """Return a copy of line with each ASCII control character (0-31), - and each double quote, removed. (Vaguely private)""" - output = '' - for c in line: - if c >= ' ' and c != '"': - output=output+c - return output - -def _addEntry(line,dict): - """Parse line into a key and value, adding the result to dict, as in - getDict.""" - #check for a parameter - fields=line.split('=') - #if at least 2 fields exist - if len(fields) > 1: - #add the key,value pair to the dictionary - key=_removeInvalidChars(fields[0]) - value=_removeInvalidChars(fields[1]) - dict[key]=value - -def getDict(filename): - """Load dict from file - - Lines containing a hash sign as the first non-whitespace character are - ignored. Leading and trailing whitespace is ignored. - - Lines not containing an equals sign are also silently ignored, for the - moment. - - Lines not ignored are assumed to be in the form key=value, where key - does not contain an equals sign; value is assigned to dict[key]. - Control characters and double quotes in both key and value are silently - discarded. value is also truncated just before the first whitespace or - equals sign it contains. - """ - d = {} - lines = getLinesFromFile(filename) - for line in lines: - #strip leading and trailing whitespaces - line = line.strip() - #if a comment line then ignore - if line.find('#') == 0: - continue - #add an entry to the dict - _addEntry(line, d) - return d - -def getConfigFile(): - """Get the absolute path of the dest client config file""" - return os.path.join(getUserHome(), CM160SERVER_CONFIG_FILE) - ######################## Command line interface ########################## - - def usage(uo): - uo.info("-p : Followed by the server port (default=%d)." % (CM160.DEFAULT_SERVER_PORT) ) uo.info("-s : Followed by the serial port to check for the") uo.info(" %s device." % (CM160.DEVICE_NAME) ) uo.info(" If not supplied then a search for a connected") uo.info(" %s device is performed." % (CM160.DEVICE_NAME)) - uo.info("-r: Read data from the database and display on stdout. If -i/-l") - uo.info(" arguments are not supplied then the entire database is displayed.") - uo.info("-i: Followed by the inital date/time. Only used if the -r argument") - uo.info(" is provided.") - uo.info(" Format of date time may be") - uo.info(" dd/mm/yyyy or") - uo.info(" hh:mi:dd/mm/yyyy") - uo.info("-l: Followed by the last date/time. Only used if the -r argument") - uo.info(" is provided (same format as used for the -i argument).") - uo.info("-t: Forward the data from the sqlite database to a text file ") - uo.info(" named as per the sqlite DB, but with .txt extension") - uo.info(" added. This is provided to allow user scripts to parse the") - uo.info(" text file.") - uo.info("-o: Display data (when -r or -t arguments are used) in old compatible") - uo.info(" mode format. This was the original output format for CM160") - uo.info(" data and is retained only because some users have developed") - uo.info(" scripts that parse data in this format.") uo.info("-q : Quiet mode. No text is sent to stdout in this mode.") uo.info("-d: Debug on. Send extra debug text to stdout if not in quiet mode.") uo.info("-h : Display this help text.") @@ -1041,14 +927,8 @@ if __name__=='__main__': #Not all platforms allow locale setting. uo.warn("Failed to set locale.") - #Load the saved config if present - cfgDict=None - cfgFilename = getConfigFile() - if os.path.isfile(cfgFilename): - cfgDict = getDict(cfgFilename) - cm160 = None - debug = False + debug = False try: #Parse command line opts, args = getopt.getopt(sys.argv[1:], 'toi:l:rf:p:qds:h', ["help"])