def cson2json(cson): if has_cson_lib and not has_cson_cmd: return cson_lib.loads(cson) elif has_cson_cmd and not has_cson_lib: return _cson2json_cmd(cson) else: try: return cson_lib.loads(cson) except Exception: return _cson2json_cmd(cson)
def cson2json(cson): if cson is None: return None if has_cson_lib and not has_cson_cmd: result = cson_lib.loads(cson) elif has_cson_cmd and not has_cson_lib: result = _cson2json_cmd(cson) else: try: result = cson_lib.loads(cson) except Exception: result = _cson2json_cmd(cson) return result
def run(self, edit): scope = self.view.scope_name(self.view.sel()[0].a) if "source.json" in scope \ or scope.startswith('source.sublime'): print("Converter: No action required") return # read input from view selection = self.view.substr(sublime.Region(0, self.view.size())) # interprete and validate input try: input = cson.loads(selection) except: sublime.error_message("Converter\n\nInvalid CSON, aborting conversion") return sort_keys = loadConfig().get("jsonSortKeys") or True indent = loadConfig().get("jsonIndent") or 2 # write converted input to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, json.dumps(input, sort_keys=sort_keys, indent=indent, separators=(',', ': '))) # set syntax to JSON if sublime.version() >= "3103": self.view.set_syntax_file('Packages/JavaScript/JSON.sublime-syntax') else: self.view.set_syntax_file('Packages/JavaScript/JSON.tmLanguage')
def run(self, edit): scope = self.view.scope_name(self.view.sel()[0].a) if "source.json" in scope \ or scope.startswith('source.sublime'): print("Converter: No action required") return # read input from view selection = self.view.substr(sublime.Region(0, self.view.size())) # interprete and validate input try: input = cson.loads(selection) except: sublime.error_message("Converter\n\nInvalid CSON, aborting conversion") return # write converted input to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, dicttoxml.dicttoxml(input)) # set syntax to XML if sublime.version() >= "3103": self.view.set_syntax_file('Packages/XML/XML.sublime-syntax') else: self.view.set_syntax_file('Packages/XML/XML.tmLanguage')
def run(self, edit): import cson, json # read data from view selection = self.view.substr(sublime.Region(0, self.view.size())) # interprete and validate data try: data = cson.loads(selection) except BaseException as e: sublime.message_dialog("Atomizr\n\nInvalid CSON, aborting conversion. See console for details.") print(e) sort_keys = sublime.load_settings('Atomizr.sublime-settings').get("json_sort_keys") or False indent = sublime.load_settings('Atomizr.sublime-settings').get("json_indentation") or 2 # write converted data to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, json.dumps(data, sort_keys=sort_keys, indent=indent, separators=(',', ': '))) # set syntax to JSON Helpers.set_json(self) Helpers.rename_file(self, "json") # Reset selection Helpers.reset_selection(self)
def run(self, edit): import cson, json # read data from view input = self.view.substr(sublime.Region(0, self.view.size())) try: data = cson.loads(input) except BaseException as e: sublime.message_dialog("Atomizr\n\nInvalid CSON, aborting conversion. See console for details.") print(e) for key in data.keys(): if key[0] != ".": sublime.message_dialog("Atomizr\n\nNot an Atom snippet file") return output = data[key] sort_keys = sublime.load_settings('Atomizr.sublime-settings').get("json_sort_keys") or True indent = sublime.load_settings('Atomizr.sublime-settings').get("json_indentation") or 2 selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, json.dumps(output, sort_keys=sort_keys, indent=indent, separators=(',', ': '))) # set syntax to JSON Helpers.set_json(self) Helpers.rename_file(self, "json") # Reset selection Helpers.reset_selection(self)
def load(self): fileName = self.settingsFileLocation() if not os.path.exists(fileName): # make folder thedir = os.path.dirname(fileName) if not os.path.exists(thedir): os.makedirs(thedir) if not os.path.isdir(thedir): raise Exception( "Configuration folder path does not point to a folder") self.setDefaults() f = io.open(fileName, 'r') try: ext = os.path.splitext(fileName)[1] newOptions = cson.loads(f.read()) except (IOError, ModuleNotFoundError): self.setDefaults() else: for key in self.options.keys(): if key in newOptions: assert isinstance(newOptions[key], type(self.options[key])) else: newOptions[key] = self.options[key] self.options = newOptions finally: f.close() self.overrideSettings()
def note_link_replacement(match): title = 'not found' file = Path(sys.argv[1]) / 'notes' / (match.group(1) + '.cson') if file.is_file(): doc = cson.loads(file.read_text(encoding='utf-8')) title = doc['title'] return f'(@note/{filename_from_title(title)})'
def test_create_queries_return_safe_config(client): """Post a config file to /queries. Verify if the new config file is safe (no query description). """ testdir = os.path.dirname(os.path.realpath(__file__)) res = client.post("/queries", data=open(os.path.join(testdir, "cson/config.cson"))) # request returns an HTTP 200 assert res.status_code == 200 # the response is a correct CSON file try: safe_config = cson.loads(res.data) except Exception as ex: assert False, "The response is not a correct CSON file (%s)" % str(ex) # each query has been replaced by its md5 checksum assert safe_config["settings"][0][ "query"] == 'dbee8e5efef34bed71d5db632c3938b1' assert safe_config["others"]["dict"][ "query"] == '120f098643762d0bfa38f2c9816dcc7b' assert safe_config["others"]["aggregate"][ "query"] == 'e83dde3367d06bee18e9e6d123ea9fe1'
def cson2dict(content): try: return cson.loads(content) except cson.ParseError as e: msg_sche = 'your content {} is not in the right cson ' \ 'style and details are {}' error(__file__, msg_sche.format(content, e))
def test_DeepCopyRef(): # test that a reference is a copy of the data and not a python pointer to the original input = '{a : 1, c : @b, b : { c: @a }}' expected = json.loads('{"a":1, "c": { "c" : 2 }, "b":{ "c" : 1}}') actual = cson.loads(input) actual['c']['c']=2 if not compare(json.dumps(expected),json.dumps(actual)): pytest.fail("expected("+ str(expected) + ") actual("+ str(actual) +")")
def defaultOptions(self): if self._defaultOptions is None: f = io.open(self._defaultOptLocation) try: opt = cson.loads(f.read()) finally: f.close() self._defaultOptions = opt return self._defaultOptions
def installConfig(self): print("install atom config") merged = {} current_config = j.sal.fs.fileGetContents( os.path.expanduser("~/.atom/config.cson")) merged = cson.loads(current_config) new_config_path = os.path.join( os.path.dirname(inspect.getfile(self.__class__)), "config.cson") new_config_content = j.sal.fs.fileGetContents(new_config_path) new_config = cson.loads(new_config_content) for k, v in new_config.items(): if k in merged: merged[k].update(new_config[k]) else: merged[k] = v content = cson.dumps(merged, indent=4, sort_keys=True) j.sal.fs.writeFile(os.path.expanduser("~/.atom/config.cson"), content)
def updateProjects(): global mtime try: new_mtime = os.path.getmtime(projects_file) except Exception as e: warning("Could not get mtime of file: " + projects_file + str(e)) if mtime != new_mtime: mtime = new_mtime with open(projects_file) as projects_cson: global projects projects = cson.loads(projects_cson.read())
def text_to_dict(text): """convert json or cson to dict""" try: return cson.loads(text) except: pass try: return json.loads(text) except: pass raise Exception("text should be in cson or json format")
def read_cson(input): """Reads Atom snippets (CSON)""" import cson # interprete and validate data try: data = cson.loads(input) except BaseException as e: sublime.message_dialog("Atomizr\n\nInvalid CSON, aborting conversion. See console for details.") print(e) return False completions = [] scope_replacements = sublime.load_settings('Atomizr.sublime-settings').get("scope_replacements") or True # but is it an Atom snippet? try: for key in data.keys(): # get scope, convert if necessary for subl, atom in scope_replacements: if key == atom: scope = subl break else: scope = key.lstrip(".") # add description, if available for item in (data[key]): trigger = data[key][item]["prefix"] description = None if "description" in data[key][item]: description = data[key][item]["description"] else: if item != trigger: description = item contents = Helpers.remove_trailing_tabstop(data[key][item]["body"]) if description is None: completions.append( {"trigger": trigger, "contents": contents} ) else: completions.append( {"trigger": trigger, "contents": contents, "description": description} ) except: sublime.message_dialog("Atomizr\n\nNot an Atom snippet file") return False output = { "scope": scope, "completions": completions } return output
def parseTest(input, expected, message=""): __tracebackhide__ = True #message = typeof message == 'undefined' ? # input : message.toUpperCase() + ':'; if len(message): print("") print('\x1B[1m\x1B[33m' + message + '\x1B[22m') actual = cson.loads(input) if not compare(expected, actual): pytest.fail('\x1B[1m\x1B[31mFAILED\x1B[39m\x1B[22m', ': expected(', expected, ") actual(", actual, ")") #pytest.fail("not configured: %s" %(x,)) print('\x1B[1m\x1B[32mPASS\x1B[39m\x1B[22m', ': expected(', expected, ") actual(", actual, ")")
def files_boostnote(dir): import cson ret = [] files = os.listdir(dir) for file in files: path = os.path.join(dir, file) with open(path) as f: cdict = cson.loads(f.read()) cdict['name'] = file ret.append(cdict) return ret
def test_writer(): srcdir = os.path.join(os.path.split(__file__)[0], 'writer') for name in os.listdir(srcdir): if not name.endswith('.json'): continue json_fname = os.path.join(srcdir, name) with open(json_fname, 'rb') as fin: j = json.loads(fin.read().decode('utf-8')) c = cson.dumps(j, indent=4, sort_keys=True, ensure_ascii=False) cson_name = name[:-5] + '.cson' with open(os.path.join(srcdir, cson_name), 'rb') as fin: cc = fin.read().decode('utf-8').replace('\r\n', '\n') assert c == cc c = cson.loads(c) assert c == j
def main(): if len(sys.argv) != 3: usage() sys.exit(1) boostnote_dir = Path(sys.argv[1]) output_dir = Path(sys.argv[2]) notes_dir = output_dir / 'notes' notes_dir.mkdir(parents=True, exist_ok=True) attach_dir = output_dir / 'attachments' attach_dir.mkdir(parents=True, exist_ok=True) boostnote_json = json.loads((boostnote_dir / 'boostnote.json').read_text()) folders = {f['key']: f['name'] for f in boostnote_json['folders']} boostnote_notes_dir = boostnote_dir / 'notes' for file in boostnote_notes_dir.iterdir(): print(file, '...', sep='', end='') doc = cson.loads(file.read_text(encoding='utf-8')) if doc['isTrashed']: print('skipped (trash)') continue if doc['type'] != "MARKDOWN_NOTE": print('skipped ', doc['type']) continue output = metadata(doc, folders) output += content(doc) path = notes_dir / filename_from_title(doc['title']) path.write_text(output, encoding='utf-8') print('done') boostnote_attach_dir = boostnote_dir / 'attachments' for subdir in boostnote_attach_dir.iterdir(): if subdir.is_dir(): for file in subdir.iterdir(): print(file, '...', sep='', end='') shutil.copy2(file, attach_dir) print('done')
def installSnippets(self): """Adds Jumpscale snippets to your atom snippets file.""" # Note : to add more snippets you they need to be on the same 'key' # so we will do a snippets merge based on keys. print("install snippets") merged = {} snippets_existing_path = os.path.expanduser("~/.atom/snippets.cson") snippetspath = os.path.join( os.path.dirname(inspect.getfile(self.__class__)), "snippets.cson") if j.sal.fs.exists(snippets_existing_path, followlinks=True): snippets_existing = j.sal.fs.fileGetContents( snippets_existing_path) snippets_existing2 = "" for line in snippets_existing.split("\n"): if line.startswith("#") or line.strip == "": continue snippets_existing2 += line if snippets_existing2.strip == "": merged = cson.loads(snippets_existing2) with open(snippetspath) as jssnippets: snippets = cson.load(jssnippets) for k, v in snippets.items(): if k in merged: merged[k].update(snippets[k]) content = cson.dumps(merged, indent=4, sort_keys=True) else: content = j.sal.fs.fileGetContents(snippetspath) j.sal.fs.writeFile(os.path.expanduser("~/.atom/snippets.cson"), content) else: nc = j.sal.fs.fileGetContents(snippetspath) j.sal.fs.writeFile(filename=snippets_existing_path, contents=nc, append=False)
def create_queries(cson_content): """Create the queries from the CSON configuration file Args: cson_content: The CSON configuration file content. Returns: The CSON configuration file content with key instead of description for the queries. Raises: ValueError: The CSON if malformed. """ try: config = cson.loads(cson_content) except Exception as ex: # no specific exception seems to be raised in case of malformed CSON raise ValueError( "An error occured during the CSON parsing: {0}".format(ex)) queries = list( search_and_replace_queries(config)) # walks through config recursively # create or update all the queries using a bulk command for better performance. # bulk_write with update is not available with pymodm (for insert or update) # Therefore, we first retrieve all existing query id in order to bulk_create # only the new ones. query_ids = get_all_query_ids() new_queries = [] new_query_ids = [] for query in queries: if (query.id not in query_ids) and (query.id not in new_query_ids): new_queries.append(query) new_query_ids.append(query.id) if len(new_queries) > 0: Query.objects.bulk_create([new_query for new_query in new_queries]) return cson.dumps(config, indent=True, ensure_ascii=False)
def parse_cson(stream): import cson return cson.loads(stream)
c = cson.dumps(j, indent=4, sort_keys=True, ensure_ascii=False) cson_name = name[:-5] + '.cson' with open(os.path.join(srcdir, cson_name), 'rb') as fin: cc = fin.read().decode('utf-8').replace('\r\n', '\n') if c != cc: print('error: {}'.format(name)) print(repr(c)) print(repr(cc)) errors.append(name) continue try: c = cson.loads(c) except cson.ParseError as e: print('writer/{}({},{}): error: {}'.format(name, e.line, e.col, e.msg)) errors.append(name) continue if c != j: print('error: {}'.format(name)) print(repr(c)) print(repr(j)) errors.append(name) try: o = [] o.append({'a': o}) cson.dumps(o, indent=4)
import cson import os cfg_file = os.path.expanduser('~/.atom/config.cson') with open(cfg_file, 'r') as f: obj = cson.loads(f.read()) obj['*']['latex'] = {'useDicy': True} obj['*']['markdown-image-assistant'] = {'preserveFileNameInAssetsFolder': True} with open(cfg_file, 'w') as f: f.write(cson.dumps(obj, indent=2))
def main2(argv): if len(argv) != 2: print "usage: %s url" % argv[0] sys.exit(1) # Load config file, if available cfgfile = ".knxmonitor.cson" try: print "Trying: %s" % cfgfile cfg = cson.loads(open("%s" % cfgfile).read()) except IOError: try: print "Trying: ~/%s" % cfgfile cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read()) except IOError: print "No .knxmonitor.cson file found, using default values for config" cfg = { 'unitfile': 'enheter.xml', 'groupfile': 'groupaddresses.csv' } #loadGroupAddrs(cfg['groupfile']) #loadDeviceAddrs(cfg['unitfile']) if argv[1] != "simul": try: con = EIBConnection() except: print "Could not instanciate EIBConnection" sys.exit(1) tries = 1 connected = False while (not connected) and (tries < 5): try: if con.EIBSocketURL(argv[1]) != 0: print "Could not connect to: %s" % argv[1] sys.exit(1) else: connected = True except socket.error: print "failed to connect, retrying in 5 sec..." time.sleep(5) tries += 1 if not connected: print "Unable to connect, tried %d times, giving up." % tries sys.exit(1) if con.EIBOpenVBusmonitorText() != 0: # For some reason this always "fails" with EBUSY, # hence just ignore that particular error if con.errno != errno.EBUSY: print "Could not open bus monitor" sys.exit(1) log = KnxLogFileHandler() buf = EIBBuffer() while 1: length = con.EIBGetBusmonitorPacket(buf) if length == 0: print "Read failed" sys.exit(1) ts = time.localtime() b = "" for x in buf.buffer: b += chr(x) print time.asctime(ts) + ":" + b outfile = log.getFileToUse() outfile.write(time.asctime(ts) + ":" + b + "\n") outfile.flush() con.EIBClose()
def cson_load(): with open("./dumped/cson.cson") as file: return cson.loads(file.read())
tagOrder = ["author", "date", "title", "images", "video", "content"] DIR_PATH = "./data/blog" for fileName in os.listdir(DIR_PATH): if fileName.endswith(".xml"): filePath = os.path.join(DIR_PATH, fileName) os.remove(filePath) for fileName in os.listdir(DIR_PATH): filePath = os.path.join(DIR_PATH, fileName) if filePath.endswith(".cson"): with open(filePath, "r") as file: contents = file.read() parsed = cson.loads(contents) xmlStr = "" xmlStr += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<root>\n\n" dateStr = "" for tag in tagOrder: if tag in parsed: tagData = parsed[tag] if tag == "images": if len(tagData) == 0: continue else: if tag == "date": dateStr = tagData if len(tagData.strip()) == 0: continue
def snippetsInFileForScope(file_name, scope=HTML_SCOPE): "Returns Snippets in the file which are in the specified scope." with open(file_name, "r") as f: data = cson.loads(f.read()) return data.get(HTML_SCOPE, {})
def main2(argv=sys.argv): groupAddrs = [] types = {} def groupAddr_callback(option, opt_str, value, parser): assert value is None value = [] def floatable(str): try: float(str) return True except ValueError: return False for arg in parser.rargs: # stop on --foo like options if arg[:2] == "--" and len(arg) > 2: break # stop on -a, but not on -3 or -3.0 if arg[:1] == "-" and len(arg) > 1 and not floatable(arg): break value.append(arg) del parser.rargs[:len(value)] if len(value) == 0: raise OptionValueError("-g option requires one or two arguments") groupAddrs.append(value[0]) if len(value) > 1: t = value[1] if t not in ["onoff", "temp", "time", "%"]: # We also allow %<scaling factor> if t[0] != "%" or not str.isdigit(t[1:]): raise OptionValueError("type argument for group " "addresses must " "be either 'onoff', 'temp', " "'time', or '%%[<x>]', not: %s" %t) types[value[0]] = t op = OptionParser() op.add_option("-d", "--dump-group-addresses", dest="dumpGAtable", action="store_true", help="dump group address table and exit") op.add_option("-i", "--input", dest="infilenames", action="append", help="read log from FILE", metavar="<FILE>") op.add_option("-g", "--group-address", action="callback", callback=groupAddr_callback, help=("Specify which group address(es) to print, " "and optionally what type to convert the value to")) op.add_option("-f", "--flanks-only", dest="flanksOnly", action="store_true", help=("only print telegrams when values has " "changed since last telegram")) op.add_option("-p", "--plot", dest="plot", action="store_true", help="plot chart of data") op.add_option("-j", "--json", dest="JSON", action="store_true", help="produce JSON output") op.add_option("-v", "--verbose", dest="verbose", action="store_true", help="print more information, warnings, and error messages") op.add_option("--tail", dest="tail", type="int", metavar="<NUM>", default=0, help="print only the last NUM number of telegrams") op.add_option("--hourly-avg", dest="hourly_avg", action="store_true", help="Average temperatures within the hour") options, args = op.parse_args(args=argv) #print options #print groupAddrs #print types #sys.exit(1) verbose = options.verbose stream_setVerbose(verbose) # All output variants will likly support utf-8... sys.stdout = codecs.getwriter('utf-8')(sys.stdout) try: infiles = [ open(f, "rb") for f in options.infilenames] except Exception as e: print "Error: %s" %e op.print_help() sys.exit(1) # Load config file, if available cfgfile = ".knxmonitor.cson" try: print "Trying: %s" %cfgfile cfg = cson.loads(open("%s" %cfgfile).read()) except IOError: try: print "Trying: ~/%s" %cfgfile cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read()) except IOError: print "No .knxmonitor.cson file found, using default values for config" cfg = { 'unitfile' : 'enheter.xml', 'groupfile' : 'groupaddresses.csv' } knx = KnxLogViewer(cfg['unitfile'], cfg['groupfile'], infiles, options.dumpGAtable, types, options.flanksOnly, options.tail, None, options.hourly_avg) if options.plot: knx.plotLog(groupAddrs, "") elif options.JSON: knx.printJSON(groupAddrs) else: knx.printLog(groupAddrs)
# if outputfile.is_file(): # print(f'output file "{outputfile}" exists, won’t overwrite.') # # TODO: read in, add information # sys.exit(1) print(f'reading {inputfile}') with open(inputfile, 'r') as input: text = '\n'.join(input.readlines()) # cson parser can’t handle continued strings m = reContinue.search(text) while m: text = text.replace(m.group(0), '') m = reContinue.search(text) data = cson.loads(text) for cmd in data['.text.tex.context']: try: desc = data['.text.tex.context'][cmd]['description'] except KeyError: desc = '' try: url = data['.text.tex.context'][cmd]['descriptionMoreURL'] except KeyError: url = 'https://wiki.contextgarden.net/Command/' + cmd.replace('\\', '') COMMANDS[cmd] = { 'description': desc, 'body': data['.text.tex.context'][cmd]['body'], 'url': url }
#pip install --target=/Users/taravser/anaconda3/envs/r-reticulate/lib/python3.7/ cson import cson # Opening file f = open('snippets.cson') obj = cson.load(f) obj['.source.r']['BSPO_0000105'] type(obj) obj{1} cson.loads('a: 1') with open('snippets.cson', 'rb') as fin: obj = cson.load(fin) obj
def main2(argv): if len(argv) != 2: print "usage: %s url" % argv[0]; sys.exit(1); # Load config file, if available cfgfile = ".knxmonitor.cson" try: print "Trying: %s" %cfgfile cfg = cson.loads(open("%s" %cfgfile).read()) except IOError: try: print "Trying: ~/%s" %cfgfile cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read()) except IOError: print "No .knxmonitor.cson file found, using default values for config" cfg = { 'unitfile' : 'enheter.xml', 'groupfile' : 'groupaddresses.csv' } #loadGroupAddrs(cfg['groupfile']) #loadDeviceAddrs(cfg['unitfile']) if argv[1] != "simul": try: con = EIBConnection() except: print "Could not instanciate EIBConnection"; sys.exit(1); tries = 1 connected = False while (not connected) and (tries < 5): try: if con.EIBSocketURL(argv[1]) != 0: print "Could not connect to: %s" %argv[1] sys.exit(1) else: connected = True except socket.error: print "failed to connect, retrying in 5 sec..." time.sleep(5) tries += 1 if not connected: print "Unable to connect, tried %d times, giving up." % tries sys.exit(1) if con.EIBOpenVBusmonitorText() != 0: # For some reason this always "fails" with EBUSY, # hence just ignore that particular error if con.errno != errno.EBUSY: print "Could not open bus monitor"; sys.exit(1) log = KnxLogFileHandler() buf = EIBBuffer() while 1: length = con.EIBGetBusmonitorPacket (buf) if length == 0: print "Read failed" sys.exit(1) ts = time.localtime() b = "" for x in buf.buffer: b += chr(x) print time.asctime(ts) + ":" + b outfile = log.getFileToUse() outfile.write(time.asctime(ts) + ":" + b + "\n") outfile.flush() con.EIBClose()
def cson2json(cson): if cson is None: return None result = cson_lib.loads(cson) return result
def loads(self, s): self._data = cson.loads(s) self.is_updated = False return self
def parseTest(input, expected, message=""): actual = cson.loads(input) if not compare(json.dumps(expected),json.dumps(actual)): pytest.fail("expected("+ str(expected) + ") actual("+ str(actual) +")")
def main2(argv=sys.argv): groupAddrs = [] types = {} def groupAddr_callback(option, opt_str, value, parser): assert value is None value = [] def floatable(str): try: float(str) return True except ValueError: return False for arg in parser.rargs: # stop on --foo like options if arg[:2] == "--" and len(arg) > 2: break # stop on -a, but not on -3 or -3.0 if arg[:1] == "-" and len(arg) > 1 and not floatable(arg): break value.append(arg) del parser.rargs[:len(value)] if len(value) == 0: raise OptionValueError("-g option requires one or two arguments") groupAddrs.append(value[0]) if len(value) > 1: t = value[1] if t not in ["onoff", "temp", "time", "%"]: # We also allow %<scaling factor> if t[0] != "%" or not str.isdigit(t[1:]): raise OptionValueError("type argument for group " "addresses must " "be either 'onoff', 'temp', " "'time', or '%%[<x>]', not: %s" % t) types[value[0]] = t op = OptionParser() op.add_option("-d", "--dump-group-addresses", dest="dumpGAtable", action="store_true", help="dump group address table and exit") op.add_option("-i", "--input", dest="infilenames", action="append", help="read log from FILE", metavar="<FILE>") op.add_option("-g", "--group-address", action="callback", callback=groupAddr_callback, help=("Specify which group address(es) to print, " "and optionally what type to convert the value to")) op.add_option("-f", "--flanks-only", dest="flanksOnly", action="store_true", help=("only print telegrams when values has " "changed since last telegram")) op.add_option("-p", "--plot", dest="plot", action="store_true", help="plot chart of data") op.add_option("-v", "--verbose", dest="verbose", action="store_true", help="print more information, warnings, and error messages") op.add_option("--tail", dest="tail", type="int", metavar="<NUM>", default=0, help="print only the last NUM number of telegrams") op.add_option("--hourly-avg", dest="hourly_avg", action="store_true", help="Average temperatures within the hour") options, args = op.parse_args() #print options #print groupAddrs #print types #sys.exit(1) verbose = options.verbose stream_setVerbose(verbose) # All output variants will likly support utf-8... sys.stdout = codecs.getwriter('utf-8')(sys.stdout) try: infiles = [open(f, "rb") for f in options.infilenames] except Exception as e: print "Error: %s" % e op.print_help() sys.exit(1) # Load config file, if available cfgfile = ".knxmonitor.cson" try: print "Trying: %s" % cfgfile cfg = cson.loads(open("%s" % cfgfile).read()) except IOError: try: print "Trying: ~/%s" % cfgfile cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read()) except IOError: print "No .knxmonitor.cson file found, using default values for config" cfg = { 'unitfile': 'enheter.xml', 'groupfile': 'groupaddresses.csv' } knx = KnxLogViewer(cfg['unitfile'], cfg['groupfile'], infiles, options.dumpGAtable, types, options.flanksOnly, options.tail, None, options.hourly_avg) if options.plot: knx.plotLog(groupAddrs, "") else: knx.printLog(groupAddrs)
def test_DeadRef(): with pytest.raises(Exception): cson.loads('{c: @k}')
import os import cson # moneytracker/mt BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # moneytracker/ ROOT = os.path.realpath(os.path.abspath(os.path.join(BASE_DIR, '../'))) # moneytracker/ext_settings.cson CONFIG_FILE = os.path.join( ROOT, os.environ.get('ext_settings_file', 'ext_settings.cson')) try: with open(CONFIG_FILE) as f: ext = cson.loads(f.read()) except IOError: ext = {} SECRET_KEY = ext.get('secret_key') DEBUG = ext.get('debug', False) ALLOWED_HOSTS = ext.get('allowed_hosts', []) # May add back at later time: 'django.contrib.gis' INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'whitenoise.runserver_nostatic', 'rest_framework', 'spending',
def test_RefRecurseSelf(): with pytest.raises(Exception): cson.loads('{c: null, b: { c: @b }}')
def main2(argv): if len(argv) != 2: print "usage: %s url" % argv[0]; sys.exit(1); # Load config file, if available cfgfile = ".knxmonitor.cson" try: print "Trying: %s" %cfgfile cfg = cson.loads(open("%s" %cfgfile).read()) print "Loaded: %s" %cfgfile except IOError: try: print "Trying: ~/%s" %cfgfile cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read()) print "Loaded: ~/%s" %cfgfile except IOError: print "No .knxmonitor.cson file found, using default values for config" cfg = { 'unitfile' : 'enheter.xml', 'groupfile' : 'groupaddresses.csv' } #loadGroupAddrs(cfg['groupfile']) #loadDeviceAddrs(cfg['unitfile']) devDict = KnxAddressCollection() groupDict = KnxAddressCollection() dptDict = KnxAddressCollection() # Load device and address info groupDict.loadGroupAddrs(open(cfg['groupfile'])) devDict.loadDeviceAddrs(open(cfg['unitfile'])) if 'dptfile' in cfg.keys(): dptDict.loadDptTable(open(cfg['dptfile'])) # Should we push to an InfluxDB instance? if 'push2influx' in cfg.keys(): host, port = cfg['push2influx'].split(":") print "Pushing to InfluxDB: %s:%d" %(host,int(port)) if argv[1] != "simul": try: con = EIBConnection() except: print "Could not instantiate EIBConnection"; sys.exit(1); tries = 1 connected = False while (not connected) and (tries < 5): try: if con.EIBSocketURL(argv[1]) != 0: print "Could not connect to: %s" %argv[1] sys.exit(1) else: connected = True except socket.error: print "failed to connect, retrying in 5 sec..." time.sleep(5) tries += 1 if not connected: print "Unable to connect, tried %d times, giving up." % tries sys.exit(1) if con.EIBOpenVBusmonitorText() != 0: # For some reason this always "fails" with EBUSY, # hence just ignore that particular error if con.errno != errno.EBUSY: print "Could not open bus monitor"; sys.exit(1) log = KnxLogFileHandler() buf = EIBBuffer() while 1: length = con.EIBGetBusmonitorPacket (buf) if length == 0: print "Read failed" sys.exit(1) ts = time.localtime() b = "" for x in buf.buffer: b += chr(x) print time.asctime(ts) + ":" + b outfile = log.getFileToUse() outfile.write(time.asctime(ts) + ":" + b + "\n") outfile.flush() if 'push2influx' in cfg.keys(): # Best effort decode... try: pdu = KnxPdu(devDict, groupDict, b) tim = time.mktime(ts) to = pdu.getTo() info,typ = dptDict[to] val = float(pdu.getValue(typ)) json_line = json.dumps( { "name" : "KNX", info : val, "tim" : tim } ) print json_line #continue try: nc = Netcat(host, int(port)) nc.write(json_line) nc.close() except Exception as e: print "Failed to netcat: %s" %e except: # Ignore problems for now... #print "failed to decode: %s" %b pass con.EIBClose()
def test_RefRecurse(): with pytest.raises(Exception): cson.loads('{c: @b, b: @c}')