def test_box_list_from_json(self): alist = [{"item": 1}, {"CamelBad": 2}] json_list = json.dumps(alist) bl = BoxList.from_json(json_list, camel_killer_box=True) assert bl[0].item == 1 assert bl[1].camel_bad == 2 with pytest.raises(BoxError): BoxList.from_json(json.dumps({"a": 2}))
def _download_city_list(self): if not self.city_file_location.exists(): self.city_file_location.parent.mkdir(parents=True, exist_ok=True) reusables.download( 'http://bulk.openweathermap.org/sample/city.list.json.gz', filename=str(self.city_file_location)) return BoxList.from_json(gzip.open(self.city_file_location).read())
def test_from_multiline(self): content = '{"a": 2}\n{"b": 3}\r\n \n' with open(tmp_json_file, "w") as f: f.write(content) a = BoxList.from_json(filename=tmp_json_file, multiline=True) assert a[1].b == 3
def user_in_org(self, user, org): members = BoxList.from_json(filename=self.get_test_filename( 'github_org_{org}_public_members.json').format(org=org)) ret = self.user_in_members(user, members) return ret
def main(): url = "http://slackmojis.com/emojis.json" output_file = 'emojis.json' remove_file(output_file) download_file(url, output_file) # for downloaded emoji create_dirs(SLACKMOJI_DL_DIR) slackmoji_pack_dir = 'slackmoji-packs' create_dirs(slackmoji_pack_dir) slackmojis = BoxList.from_json(filename=output_file) categories = get_categories(slackmojis) data = {} for category in categories: data[category] = {'emojis': []} output_file_yaml = os.path.join(slackmoji_pack_dir, 'slackmojis-{}.yaml'.format(category)) remove_file(output_file_yaml) data_header = { 'title': 'slackmoji-{}'.format(category) } write_yaml_file(data_header, output_file_yaml) name_count = Box() for slackmoji in slackmojis: name = str(slackmoji['name']) category = 'uncategorized' if 'category' in slackmoji: category = str(slackmoji.category.name).lower().replace(' ', '-') output_file_yaml = os.path.join(slackmoji_pack_dir, 'slackmojis-{}.yaml'.format(category)) # Special cases - a.k.a stupid cases if name == 'yes2': # there are two 'yes' and one 'yes2' emojis already name = 'yes2-1' if name == 'no2': # there are two 'no' and one 'no2' emojis already name = 'no2-1' sports = ['mlb', 'nba', 'nfl', 'nhl'] if category in sports: # The NFL logo should not be :nfl-nfl: if name == 'nfl': pass else: name = '{}-{}'.format(category, name) if 'facebook' in category: name = 'fb-{}'.format(name) if 'scrabble' in category: name = 'scrabble-{}'.format(name) name_count[name] = name_count[name] + 1 if name in name_count else 1 if name_count[name] > 1: name = ''.join([name, str(name_count[name])]) src = str(slackmoji['image_url']).split('?')[0] if not valid_image(name, src): continue slackmoji_data = { 'name': name, 'src': src } data[category]['emojis'].append(slackmoji_data) for category in categories: output_file_yaml = os.path.join(slackmoji_pack_dir, 'slackmojis-{}.yaml'.format(category)) write_yaml_file(data[category], output_file_yaml)