Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 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()
Exemplo n.º 7
0
 def tearDown(self):
     path = get_root_prefix() + k_FILENAME
     if os.path.exists(path):
         os.remove(path)