def _get_fp(self, filename=None, mode='r'): if not filename: filename = self.filename path = get_root_prefix() + filename if not os.path.exists(path): open(path, 'a').close() return open(path, mode)
def save(filename, kvps): path = path_utils.get_root_prefix() + '/' + filename if os.path.exists(filename): with open(path, 'r') as fp: org_kvps = json.load(fp) for key, value in kvps.items(): org_kvps[key] = value kvps = org_kvps with open(path, 'w') as fp: json.dump(kvps, fp)
def create_service(api_name, api_version, scopes): prefix = get_root_prefix() store = file.Storage(prefix + '{}_{}_token.json'.format(api_name, api_version)) creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets(prefix + 'credentials.json', scopes) creds = tools.run_flow(flow, store) while True: try: return build(api_name, api_version, http=creds.authorize(Http())) except (ServerNotFoundError, TimeoutError): time.sleep(2)
def __init__(self, config_file_paths=None, dict=None): dict.__init__(self) prefix = path_utils.get_root_prefix() if config_file_paths: for config_file_path in config_file_paths: path = prefix + config_file_path if os.path.exists(path): log.info('Reading config: ' + path) with open(path, 'r') as fp: kvps = json.load(fp) for key, value in kvps.items(): self[key] = value if dict: for key, value in dict.items(): self[key] = value
def set_status(self, video_id, status): new_filename = 'tmp' + self.filename found_id = False with self._get_fp() as fp: with self._get_fp(new_filename, 'w') as fp_new: for line in fp: if line == '': continue s = line.split(',') if s[0] == video_id: found_id = True line = s[0] + ',' + status fp_new.write(line) if not found_id: fp_new.write('{},{}\n'.format(video_id, status)) prefix = get_root_prefix() os.rename(prefix + new_filename, prefix + self.filename)
def get_resource_contents(self, path, open_mode='r'): prefix = get_root_prefix() with open(prefix + 'tests/resources/' + path.strip('/'), open_mode) as fp: return fp.read()
def tearDown(self): path = get_root_prefix() + k_FILENAME if os.path.exists(path): os.remove(path)