def rename(subject_id, new_subject_id): with commit_wrap(): subj = p.load(subject_id) if subj: try: os.rename(p.subject_dir(subject_id), p.subject_dir(new_subject_id)) except OSError: nl.notify('Error: filesystem reported error moving %s to %s' % (subject_id, new_subject_id), level=nl.level.error) else: subj._subject_id = new_subject_id subj.save() if os.path.exists(p.subject_json(subj)): try: os.remove( os.path.join( p.subject_dir(subj), os.path.basename(p.subject_json(subject_id)))) except OSError: pass try: del (p.subject._all_subjects[str(subject_id)]) except KeyError: pass p.subject._index_one_subject(new_subject_id)
def save(self,json_file=None): ''' save current state to JSON file ''' if json_file==None: json_file = p.subject_json(self._subject_id) save_dict = self.__dict__() with open(json_file,'w') as f: f.write(json.dumps(save_dict, sort_keys=True, indent=2))
def _index_all_subjects(load_all=False): global _all_subjects if os.path.exists(p.data_dir): for subject_id in os.listdir(p.data_dir): if os.path.exists(p.subject_json(subject_id)): if subject_id not in _all_subjects: _all_subjects[subject_id] = None if load_all and _all_subjects[subject_id]==None: _index_one_subject(subject_id) if load_all: _indexed_and_loaded_all_subjects = True
def rename(subject_id,new_subject_id): with commit_wrap(): subj = p.load(subject_id) if subj: try: os.rename(p.subject_dir(subject_id),p.subject_dir(new_subject_id)) except OSError: nl.notify('Error: filesystem reported error moving %s to %s' % (subject_id,new_subject_id),level=nl.level.error) else: subj._subject_id = new_subject_id subj.save() if os.path.exists(p.subject_json(subj)): try: os.remove(os.path.join(p.subject_dir(subj),os.path.basename(p.subject_json(subject_id)))) except OSError: pass try: del(p.subject._all_subjects[str(subject_id)]) except KeyError: pass p.subject._index_one_subject(new_subject_id)
def create_subject(subject_id): ''' creates a new subject (loads old JSON if present and valid) ''' with commit_wrap(): subj = None if os.path.exists(p.subject_json(subject_id)): subj = p.Subject.load(subject_id) if subj == None: subj = p.Subject(subject_id) else: subj = p.Subject(subject_id) subj.init_directories() subj.save() return subj
def create_subject(subject_id): ''' creates a new subject (loads old JSON if present and valid) ''' with commit_wrap(): subj = None if os.path.exists(p.subject_json(subject_id)): subj = p.Subject.load(subject_id) if subj==None: subj = p.Subject(subject_id) else: subj = p.Subject(subject_id) subj.init_directories() subj.save() return subj
def load(cls,subject_id): ''' returns a subject object initialized using JSON file If no subject can be loaded, will print an error and return ``None`` This method is imported to the root-level, and can be called as :meth:``padre.load``''' subject_id = str(subject_id).rstrip('/') json_file = p.subject_json(subject_id) try: with open(json_file) as f: return Subject(subject_id,json.loads(f.read())) except (ValueError,IOError): p.error('Could not load valid JSON file for subject %s' % subject_id) return None
def subject_exists(subj_id): return os.path.exists(p.subject_json(subj_id))