def test_circular_in_default_indent(): class X: pass xx = [1] xx.append(xx) def x_def(x): return xx with pytest.raises(ValueError): cson.dumps(X(), indent=4, default=x_def)
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 = json.loads(selection) except BaseException as e: sublime.message_dialog("Atomizr\n\nInvalid JSON, aborting conversion. See console for details.") print(e) sort_keys = sublime.load_settings('Atomizr.sublime-settings').get("cson_sort_keys") or True indent = sublime.load_settings('Atomizr.sublime-settings').get("cson_indentation") or 2 # write converted data to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, ATOM_GENERATOR + cson.dumps(data, sort_keys=sort_keys, indent=indent)) # set syntax to CSON, requires supported CoffeeScript package if Helpers.get_coffee(self) is True: Helpers.rename_file(self, "cson") # Reset selection Helpers.reset_selection(self)
def save(self, filename): open(filename, "w").write(cson.dumps({ "message": self.message.raw_dict, "index": self.index, "output": self.out, "target": self.target, }))
def gen_file(): final_data = {scope: snippets} pre_format_cson = cson.dumps(final_data, indent=2) formatted_cson = perform_formatting(pre_format_cson) return formatted_cson
def save_atom_config(atom_config): # Write atom config try: with open(atom_config_uri, 'w') as f: f.write(cson.dumps(atom_config, indent=4)) except: print("Unable to write atom config, exiting.") sys.exit()
def save(self, filename): open(filename, "w").write( cson.dumps({ "message": self.message.raw_dict, "index": self.index, "output": self.out, "target": self.target, }))
def test_default_indent(): class X: pass xx = [1] def x_def(x): return xx c = cson.dumps(X(), indent=4, default=x_def) assert c == '[\n 1\n]\n'
def test_default_terse(): class X: pass xx = [1] def x_def(x): return xx c = cson.dumps(X(), default=x_def) assert c == '[1]'
def renderExceptions(snippets): for snip in snippets: # If the element snippet is for a family of Polymer elements, # replace its body with just the name of the snippet. This will # let you auto-complete long element names in CSS and JavaScript. if snip.get("prefix", "").split("-")[0] in ELEMENT_FAMILIES: snip["body"] = snip["prefix"] else: del snip["body"] return cson.dumps( {".text.html .embedded": dict((s["prefix"], s) for s in snippets)}, indent=2)
def load_atom_config(): # Load atom config returns atom_config dict try: with open(atom_config_uri, 'rb') as f: atom_config = cson.load(f) # Lets back up the config file just in case config_backup = os.path.join(theme_historydir, 'config.cson.bak') with open(config_backup, 'w') as f: f.write(cson.dumps(atom_config, indent=4)) return atom_config except Exception as e: print("Unable to read atom config file, exiting.") print('{}'.format(e)) sys.exit()
def renderExceptions(snippets): for snip in snippets: # If the element snippet is for a family of Polymer elements, # replace its body with just the name of the snippet. This will # let you auto-complete long element names in CSS and JavaScript. if snip.get("prefix", "").split("-")[0] in ELEMENT_FAMILIES: snip["body"] = snip["prefix"] else: del snip["body"] return cson.dumps({ ".text.html .embedded": dict((s["prefix"], s) for s in snippets) }, indent=2)
def run(self, edit): import json, cson, sublime # read data from view input = self.view.substr(sublime.Region(0, self.view.size())) try: data = json.loads(input) except BaseException as e: sublime.message_dialog("Atomizr\n\nInvalid JSON, aborting conversion. See console for details.") print(e) for key in data.keys(): if key[0] == "." or "completions" in data or "scope" in data: sublime.message_dialog("Atomizr\n\nNot a Visual Studio Code snippet file") return output = { ".source": data } json_snippets = sublime.load_settings('Atomizr.sublime-settings').get("atom_json_snippets") or False if json_snippets is True: # Get JSON settings 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") else: # Get CSON settings sort_keys = sublime.load_settings('Atomizr.sublime-settings').get("cson_sort_keys") or True indent = sublime.load_settings('Atomizr.sublime-settings').get("cson_indentation") or 2 # write converted data to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, ATOM_GENERATOR + cson.dumps(output, sort_keys=sort_keys, indent=indent)) # set syntax to CSON, requires supported CoffeeScript package if Helpers.get_coffee(self) is True: Helpers.rename_file(self, "cson") Helpers.select_scope(self, "atom", ATOM_GENERATOR)
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 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 run(self, edit): # read input from view selection = self.view.substr(sublime.Region(0, self.view.size())) # interprete and validate input try: input = json.loads(selection) except: sublime.error_message("Converter\n\nInvalid JSON, aborting conversion") return sort_keys = loadConfig().get("csonSortKeys") or True indent = loadConfig().get("csonIndent") or 2 # write converted input to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, cson.dumps(input, sort_keys=sort_keys, indent=indent)) # set syntax to CSON, requires supported CoffeeScript package get_package(self)
def run(self, edit): import cson, json # read data from view input = self.view.substr(sublime.Region(0, self.view.size())) data = SublimeText.read_json(input) if data is False: return output = Atom.write_cson(data) json_snippets = sublime.load_settings('Atomizr.sublime-settings').get("atom_json_snippets") or False if json_snippets is True: 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") else: # Get CSON settings sort_keys = sublime.load_settings('Atomizr.sublime-settings').get("cson_sort_keys") or True indent = sublime.load_settings('Atomizr.sublime-settings').get("cson_indentation") or 2 # write converted data to view selection = sublime.Region(0, self.view.size()) self.view.replace(edit, selection, ATOM_GENERATOR + cson.dumps(output, sort_keys=sort_keys, indent=indent)) # set syntax to CSON, requires supported CoffeeScript package if Helpers.get_coffee(self) is True: Helpers.rename_file(self, "cson") # Reset selection Helpers.reset_selection(self)
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 test_circular(): with pytest.raises(ValueError): o = [] o.append({'a': o}) cson.dumps(o, indent=4)
def test_unskipped_keys(): class X: pass with pytest.raises(TypeError): cson.dumps({X(): 1}, indent=4)
def show_object(ast, title=None): if title: print('%s%s %s %s' % (os.linesep, '-' * 50, title, os.linesep)) _ast = ast.cson() if hasattr(ast, 'cson') else cson.dumps(ast, indent=4) print(' ' + _ast.replace('\n', '\n '))
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 cson_dump(): return cson.dumps(Benchmark.obj,indent=4)
print(repr(c)) print(repr(j)) errors.append(name) srcdir = os.path.join(os.path.split(__file__)[0], 'test', 'writer') for name in os.listdir(srcdir): if not name.endswith('.json'): continue if not matches(name): continue total += 1 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') 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:
def test_nonstring_keys(): c = cson.dumps({1: 1}, indent=4) assert c == "'1': 1\n"
def main(): print_output = True output_file_name = 'asymptote.cson' base_grammar = { 'scopeName': 'source.asymptote', 'name': 'Asymptote', 'fileTypes': ['asy'] } # basic semantics not covered by asy -l base_pattern = generate_base_pattern() matched_words = set() try: asy_list_file = io.open('asy.list') asy_list_raw = asy_list_file.read() asy_list_file.close() except (FileNotFoundError, IOError): return 2 operator_list = [] for asy_def in asy_list_raw.split(';'): asy_def = asy_def.strip() if not asy_def: continue asy_type, asy_signature = asy_def.split(' ', 1) if '(' in asy_signature: if 'operator' in asy_signature: if 'init()' in asy_signature: # type match_word = str.format('\\b({0})\\b', asy_type) match_type = 'storage.type' elif 'cast(' not in asy_signature: # operator operator_signature = asy_signature.split(' ', 1)[1] operator_symbol = operator_signature.split('(')[0] parsed_operator = [] for character in operator_symbol: if character in {'|', '+', '*', '$', '.', '\\', '^'}: parsed_operator.append('\\' + character) else: parsed_operator.append(character) parsed_op_text = ''.join(parsed_operator) if parsed_op_text.isalpha(): match_word = str.format('\\b({0})\\b', parsed_op_text) else: if parsed_op_text not in matched_words and ' ' not in parsed_op_text: matched_words.add(parsed_op_text) operator_list.append(parsed_op_text) continue match_type = 'keyword.operator' else: # function function_name = asy_signature.split('(')[0] match_word = str.format('\\b({0})\\b', function_name) match_type = 'support.function' else: # constant match_word = str.format('\\b({0})\\b', asy_signature) match_type = 'constant.language' if match_word not in matched_words: base_pattern.append({'match': match_word, 'name': match_type}) matched_words.add(match_word) base_pattern.append({ 'match': '|'.join(operator_list), 'name': 'keyword.operator' }) base_grammar['patterns'] = base_pattern final_output = cson.dumps(base_grammar, indent=True) if print_output: print(generate_preamble() + final_output) else: output_file = io.open(output_file_name, 'w') output_file.write(generate_preamble()) output_file.write(final_output) output_file.close() print('Done!')
def test_skipkeys(): class X: pass c = cson.dumps({X(): 1}, indent=4, skipkeys=True) assert c == "{}\n"