def _construct_provided_module(name, data, url, commit): # At this point the @commmit part should be removed from url so: # either url should not have an @, # or the @ should be for [email protected] assert "@" not in url or url.rindex(".") > url.rindex("@") module = OrderedDict() module["name"] = name if "description" not in data: user_error( "missing required key 'description' in module definition: %s" % pretty(data)) module["description"] = data["description"] module["url"] = url module["commit"] = commit subdirectory = data.get("subdirectory") if subdirectory: module["subdirectory"] = subdirectory dependencies = data.get("dependencies") if dependencies: module["dependencies"] = dependencies if "steps" not in data: user_error("missing required key 'steps' in module definition: %s" % pretty(data)) module["steps"] = data["steps"] module["added_by"] = "cfbs add" return module
def get_provides(self): modules = OrderedDict() if "provides" not in self._data: user_error( "missing required key 'provides' in module definition: %s" % pretty(self._data)) for k, v in self._data["provides"].items(): module = _construct_provided_module(k, v, self.url, self.url_commit) modules[k] = module return modules
def put_definition(data: dict): global definition definition = data with open(cfbs_filename(), "w") as f: f.write(pretty(data))
def save(self): data = pretty(self._data) + "\n" with open(self.path, "w") as f: f.write(data)
def test_pretty(): # Test primitives assert pretty(None) == "null" assert pretty(True) == "true" assert pretty(False) == "false" assert pretty(123) == "123" assert pretty(123.456) == "123.456" assert pretty("Hello World!") == '"Hello World!"' # Test collections assert pretty([]) == "[]" assert pretty(()) == "[]" assert pretty({}) == "{}" test = OrderedDict() test["a"] = [] test["b"] = () expected = '{ "a": [], "b": [] }' assert pretty(test) == expected test = [None, True, False, 1, 1.2, "Hello!"] expected = '[null, true, false, 1, 1.2, "Hello!"]' assert pretty(test) == expected test = (None, True, False, 1, 1.2, "Hello!") expected = '[null, true, false, 1, 1.2, "Hello!"]' assert pretty(test) == expected test = OrderedDict() test["a"] = None test["b"] = True test["c"] = False test["d"] = 1 test["e"] = 1.2 test["f"] = "Hello!" expected = '{ "a": null, "b": true, "c": false, "d": 1, "e": 1.2, "f": "Hello!" }' assert pretty(test) == expected # Test that strings are escaped correctly: test = "" # Empty string expected = '""' # is represented as "" in JSON, same as python assert pretty(test) == expected test = r'""' # Putting double quotes inside the string expected = r'"\"\""' # means they have to be escaped with backslashes assert pretty(test) == expected test = '\n' # A newline character expected = r'"\n"' # is encoded as \n, same as python assert pretty(test) == expected test = r'\ ' # A backslash character expected = r'"\\ "' # represented by two backslashes in JSON assert pretty(test) == expected
def __str__(self) -> str: return pretty(self.to_dict())
def write_json(path, data): data = pretty(data) + "\n" return save_file(path, data)