def cache_gpx(self, guid): printable_url = self.page_url("/seek/cdpf.aspx?guid=" + guid) sendtogps_url = self.page_url("/seek/sendtogps.aspx?guid=" + guid) info_plain = self.send(printable_url) info = html.parse(StringIO(info_plain)) print >> sys.stderr, "Parsing printable version with BeautifulSoup" info_soup = BeautifulSoup(info_plain) s2gps = self.send(sendtogps_url) s2gps = s2gps[s2gps.index("<?xml") : s2gps.index("</gpx>") + 6] gpx = etree.parse(StringIO(s2gps)) cache = tools.cache(gpx) if cache is None: return None short_desc = find_or_create(cache, "short_description") long_desc = find_or_create(cache, "long_description") hints = find_or_create(cache, "encoded_hints") tmp = lookup_by_header(info_soup, "Short Description") if tmp is not None: short_desc.text = re.sub(r"\s+", " ", tmp.text.strip()) else: short_desc.text = "" tmp = lookup_by_header(info_soup, "Long Description") if tmp is not None: long_desc.text = tmp.renderContents().decode("utf-8") else: long_desc.text = "" long_desc.set("html", "True") try: h = lookup_by_header(info_soup, "Additional Hints") if h is not None: decr = h.find("div", {"class": "hint-encrypted"}) hints.text = decr.text.strip() # hints_text = re.sub(r'\s+', ' ', info_soup.find("div", {"id":"div_hint"}).div.renderContents().decode('utf-8')).strip() # hints_text = re.sub(r'<br ?/?>', "\n", hints_text).decode("rot13") # hints.text = hints_text except AttributeError: hints.text = "No hint" attrs = find_or_create(cache, "attributes") for path in info.xpath('//img[contains(@src, "/attributes/")]/@src'): name, yesno = path.split("/")[-1].strip(".gif").split("-") if name in attribute_map: attr = find_or_create(attrs, "attribute") attr.set("id", str(attribute_map[name][0])) attr.text = attribute_map[name][1] if yesno == "yes": attr.set("inc", "1") else: attr.set("inc", "0") attrs.append(attr) return gpx
def cache_gpx(self, guid): printable_url = self.page_url("/seek/cdpf.aspx?guid=" + guid) sendtogps_url = self.page_url("/seek/sendtogps.aspx?guid=" + guid) info_plain = self.send(printable_url) info = html.parse(StringIO(info_plain)) print >> sys.stderr, "Parsing printable version with BeautifulSoup" info_soup = BeautifulSoup(info_plain) s2gps = self.send(sendtogps_url) s2gps = s2gps[s2gps.index("<?xml"):s2gps.index("</gpx>")+6] gpx = etree.parse(StringIO(s2gps)) cache = tools.cache(gpx) if cache is None: return None short_desc = find_or_create(cache, "short_description") long_desc = find_or_create(cache, "long_description") hints = find_or_create(cache, "encoded_hints") tmp = lookup_by_header(info_soup, "Short Description") if tmp is not None: short_desc.text = re.sub(r'\s+', ' ', tmp.text.strip()) else: short_desc.text = "" tmp = lookup_by_header(info_soup, "Long Description") if tmp is not None: long_desc.text = tmp.renderContents().decode('utf-8') else: long_desc.text = "" long_desc.set("html", "True") try: h = lookup_by_header(info_soup, "Additional Hints") if h is not None: decr = h.find("div", {"class": "hint-encrypted"}) hints.text = decr.text.strip() #hints_text = re.sub(r'\s+', ' ', info_soup.find("div", {"id":"div_hint"}).div.renderContents().decode('utf-8')).strip() #hints_text = re.sub(r'<br ?/?>', "\n", hints_text).decode("rot13") #hints.text = hints_text except AttributeError: hints.text = "No hint" attrs = find_or_create(cache, "attributes") for path in info.xpath('//img[contains(@src, "/attributes/")]/@src'): name, yesno = path.split("/")[-1].strip(".gif").split("-") if name in attribute_map: attr = find_or_create(attrs, "attribute") attr.set("id", str(attribute_map[name][0])) attr.text = attribute_map[name][1] if yesno == "yes": attr.set("inc", "1") else: attr.set("inc", "0") attrs.append(attr) return gpx
def __init__(self, lang="en"): self.cache = cache("/tmp/LinksToDAG_linkparses_cache.p") self.language = lang
class ir_model_access(osv.osv): _name = 'ir.model.access' _columns = { 'name': fields.char('Name', size=64, required=True, select=True), 'model_id': fields.many2one('ir.model', 'Object', required=True, domain=[('osv_memory','=', False)], select=True, ondelete='cascade'), 'group_id': fields.many2one('res.groups', 'Group', ondelete='cascade', select=True), 'perm_read': fields.boolean('Read Access'), 'perm_write': fields.boolean('Write Access'), 'perm_create': fields.boolean('Create Access'), 'perm_unlink': fields.boolean('Delete Access'), } def check_groups(self, cr, uid, group): """ check if uid belongs in 'group' @param group Group specification. Can be: - string like 'model.name' for ir.model.data - list of ['model.name',...] strings - gid ? (TODO) @return True or False """ if isinstance(group, basestring): return self._check_groups2(cr, uid, group) elif isinstance(group, list): for g in group: # crude way, may be saved by cache.. if self._check_groups2(cr, uid, g): return True return False else: raise NotImplementedError() @tools.cache(timeout=10.0) # TODO find a way to clear the cache def _check_groups2(self, cr, uid, group): grouparr = group.split('.') if not grouparr: return False cr.execute_prepared('ima_check_groups2', "SELECT EXISTS (SELECT 1 FROM res_groups_users_rel AS ur, ir_model_data AS imd " \ " WHERE ur.uid=%s AND ur.gid = imd.res_id " \ " AND imd.model = 'res.groups' " \ " AND imd.module=%s AND imd.name=%s )", \ (uid, grouparr[0], grouparr[1],), debug=self._debug) return bool(cr.fetchone()[0]) def check_group(self, cr, uid, model, mode, group_ids): """ Check if a specific group has the access mode to the specified model""" assert mode in ['read','write','create','unlink'], 'Invalid access mode' if isinstance(model, browse_record): assert model._table_name == 'ir.model', 'Invalid model object' model_name = model.name else: model_name = model if isinstance(group_ids, (int, long)): group_ids = [group_ids] for group_id in group_ids: cr.execute_prepared('ima_check_group1_'+mode, "SELECT perm_" + mode + " " " FROM ir_model_access a " " JOIN ir_model m ON (m.id = a.model_id) " " WHERE m.model = %s AND (a.group_id = %s OR a.group_id IS NULL)" " ORDER BY a.group_id ASC", (model_name, group_id), debug=self._debug ) # note: ORDER BY .. ASC puts NULLS LAST (settable in pg >=8.3) r = cr.fetchone() access = bool(r and r[0]) if access: return True # pass no groups -> no access return False def check(self, cr, uid, model, mode='read', raise_exception=True, context=None): if uid==1: # User root have all accesses # TODO: exclude xml-rpc requests return True # TODO: can we let this fn use multiple models at each time? # or even, write a new one, which will also share the same cache? assert mode in ['read','write','create','unlink'], 'Invalid access mode' if isinstance(model, browse_record): assert model._table_name == 'ir.model', 'Invalid model object' model_name = model.model model_obj = model._table assert model_obj, "No ORM object for %r" % model else: model_name = model model_obj = self.pool.get(model_name) # osv_memory objects can be read by everyone, as they only return # results that belong to the current user (except for superuser) if isinstance(model_obj, osv.osv_memory): return True # We check if a specific rule exists cr.execute_prepared('ima_group_check_' + mode, 'SELECT BOOL_OR(perm_' + mode + ') ' ' FROM ir_model_access a ' ' JOIN ir_model m ON (m.id = a.model_id) ' ' LEFT JOIN res_groups_users_rel gu ON (gu.gid = a.group_id) ' ' WHERE m.model = %s ' ' AND (gu.uid = %s OR gu.uid IS NULL) ' ' GROUP BY (gu.uid IS NULL) ORDER BY MIN(gu.uid) ' , (model_name, uid,), debug= (model_obj and model_obj._debug) or self._debug ) # GROUP BY makes sure we separate specific group rules from # generic ones, and those groups are OR-ed together. # ORDER tells the query to put the specific first. r = cr.fetchone() if r: r = r[0] if not r and raise_exception: cr.execute('''select g.name from ir_model_access a left join ir_model m on (a.model_id=m.id) left join res_groups g on (a.group_id=g.id) where m.model=%s and a.group_id is not null and perm_''' + mode, (model_name, )) groups = ', '.join(map(lambda x: x[0], cr.fetchall())) or '/' msgs = { 'read': _("You can not read this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'write': _("You can not write in this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'create': _("You can not create this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'unlink': _("You can not delete this document (%s) ! Be sure your user belongs to one of these groups: %s."), } raise except_orm(_('AccessError'), msgs[mode] % (model_name, groups) ) return r or False check = tools.cache()(check) __cache_clearing_methods = [] def register_cache_clearing_method(self, model, method): self.__cache_clearing_methods.append((model, method)) def unregister_cache_clearing_method(self, model, method): try: i = self.__cache_clearing_methods.index((model, method)) del self.__cache_clearing_methods[i] except ValueError: pass def call_cache_clearing_methods(self, cr): self.check.clear_cache(cr.dbname) # clear the cache of check function for model, method in self.__cache_clearing_methods: object_ = self.pool.get(model) if object_: getattr(object_, method)() # # Check rights on actions # def write(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).write(cr, uid, *args, **argv) return res def create(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).create(cr, uid, *args, **argv) return res def unlink(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).unlink(cr, uid, *args, **argv) return res
class ir_model_access(osv.osv): _name = 'ir.model.access' _columns = { 'name': fields.char('Name', size=64, required=True, select=True), 'model_id': fields.many2one('ir.model', 'Object', required=True, domain=[('osv_memory','=', False)], select=True, ondelete='cascade'), 'group_id': fields.many2one('res.groups', 'Group', ondelete='cascade', select=True), 'perm_read': fields.boolean('Read Access'), 'perm_write': fields.boolean('Write Access'), 'perm_create': fields.boolean('Create Access'), 'perm_unlink': fields.boolean('Delete Access'), } def check_groups(self, cr, uid, group): grouparr = group.split('.') if not grouparr: return False cr.execute("select 1 from res_groups_users_rel where uid=%s and gid IN (select res_id from ir_model_data where module=%s and name=%s)", (uid, grouparr[0], grouparr[1],)) return bool(cr.fetchone()) def check_group(self, cr, uid, model, mode, group_ids): """ Check if a specific group has the access mode to the specified model""" assert mode in ['read','write','create','unlink'], 'Invalid access mode' if isinstance(model, browse_record): assert model._table_name == 'ir.model', 'Invalid model object' model_name = model.name else: model_name = model if isinstance(group_ids, (int, long)): group_ids = [group_ids] for group_id in group_ids: cr.execute("SELECT perm_" + mode + " " " FROM ir_model_access a " " JOIN ir_model m ON (m.id = a.model_id) " " WHERE m.model = %s AND a.group_id = %s", (model_name, group_id) ) r = cr.fetchone() if r is None: cr.execute("SELECT perm_" + mode + " " " FROM ir_model_access a " " JOIN ir_model m ON (m.id = a.model_id) " " WHERE m.model = %s AND a.group_id IS NULL", (model_name, ) ) r = cr.fetchone() access = bool(r and r[0]) if access: return True # pass no groups -> no access return False def check(self, cr, uid, model, mode='read', raise_exception=True, context=None): if uid==1: # User root have all accesses # TODO: exclude xml-rpc requests return True assert mode in ['read','write','create','unlink'], 'Invalid access mode' if isinstance(model, browse_record): assert model._table_name == 'ir.model', 'Invalid model object' model_name = model.name else: model_name = model # osv_memory objects can be read by everyone, as they only return # results that belong to the current user (except for superuser) model_obj = self.pool.get(model_name) if isinstance(model_obj, osv.osv_memory): return True # We check if a specific rule exists cr.execute('SELECT MAX(CASE WHEN perm_' + mode + ' THEN 1 ELSE 0 END) ' ' FROM ir_model_access a ' ' JOIN ir_model m ON (m.id = a.model_id) ' ' JOIN res_groups_users_rel gu ON (gu.gid = a.group_id) ' ' WHERE m.model = %s ' ' AND gu.uid = %s ' , (model_name, uid,) ) r = cr.fetchone()[0] if r is None: # there is no specific rule. We check the generic rule cr.execute('SELECT MAX(CASE WHEN perm_' + mode + ' THEN 1 ELSE 0 END) ' ' FROM ir_model_access a ' ' JOIN ir_model m ON (m.id = a.model_id) ' ' WHERE a.group_id IS NULL ' ' AND m.model = %s ' , (model_name,) ) r = cr.fetchone()[0] if not r and raise_exception: cr.execute('''select g.name from ir_model_access a left join ir_model m on (a.model_id=m.id) left join res_groups g on (a.group_id=g.id) where m.model=%s and a.group_id is not null and perm_''' + mode, (model_name, )) groups = ', '.join(map(lambda x: x[0], cr.fetchall())) or '/' msgs = { 'read': _("You can not read this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'write': _("You can not write in this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'create': _("You can not create this document (%s) ! Be sure your user belongs to one of these groups: %s."), 'unlink': _("You can not delete this document (%s) ! Be sure your user belongs to one of these groups: %s."), } raise except_orm(_('AccessError'), msgs[mode] % (model_name, groups) ) return r check = tools.cache()(check) __cache_clearing_methods = [] def register_cache_clearing_method(self, model, method): self.__cache_clearing_methods.append((model, method)) def unregister_cache_clearing_method(self, model, method): try: i = self.__cache_clearing_methods.index((model, method)) del self.__cache_clearing_methods[i] except ValueError: pass def call_cache_clearing_methods(self, cr): self.check.clear_cache(cr.dbname) # clear the cache of check function for model, method in self.__cache_clearing_methods: object_ = self.pool.get(model) if object_: getattr(object_, method)() # # Check rights on actions # def write(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).write(cr, uid, *args, **argv) return res def create(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).create(cr, uid, *args, **argv) return res def unlink(self, cr, uid, *args, **argv): self.call_cache_clearing_methods(cr) res = super(ir_model_access, self).unlink(cr, uid, *args, **argv) return res
#!/usr/bin/env python import argparse from pprint import pprint import tools CACHE = tools.cache('progress') def controller(): global VERBOSE parser = argparse.ArgumentParser(description='A simple script for any loading progress follow up', prog='progress.py', version='%prog 0.5', ) parser.add_argument('-e', '--env', action='store_true', dest='env', help='Displays the script\'s variables') args = parser.parse_args() if args.env: pprint(CACHE.get_vars()) else: data = CACHE.read() if data is None: data = { 'unit': 'M', 'size': 23000, 'records': [] } CACHE.write('yakuza5', data) pprint(CACHE.read())
def __init__(self, lang = "en"): self.cache = cache("/tmp/LinksToDAG_linkparses_cache.p") self.language = lang
#!/usr/bin/env python from encoder import * from solver import * from decoder import * from tools.cache import * import argparse, sys, subprocess if __name__=="__main__": cache = cache("/tmp/LinksToDAG_SCIP_cache.p") linksFile = "/tmp/LinksToDAG_links.txt" zplFile = "/tmp/LinksToDAG_links.zpl" solutionFile = "/tmp/LinksToDAG_solutions.txt" solutionsDirectory = "sol/" if not os.path.exists(solutionsDirectory): os.makedirs(solutionsDirectory) linksConllFile = solutionsDirectory+"links.conll" allowedLinksFile = solutionsDirectory+"allowedLinks.txt" open(linksFile, 'w+').close() open(zplFile, 'w+').close() open(solutionFile, 'w+').close() # Argument parser. To support command line flags parser = argparse.ArgumentParser(description="Runs the LinksToDAG project")
#!/usr/bin/env python from encoder import * from solver import * from decoder import * from tools.cache import * import argparse, sys, subprocess if __name__ == "__main__": cache = cache("/tmp/LinksToDAG_SCIP_cache.p") linksFile = "/tmp/LinksToDAG_links.txt" zplFile = "/tmp/LinksToDAG_links.zpl" solutionFile = "/tmp/LinksToDAG_solutions.txt" solutionsDirectory = "sol/" if not os.path.exists(solutionsDirectory): os.makedirs(solutionsDirectory) linksConllFile = solutionsDirectory + "links.conll" allowedLinksFile = solutionsDirectory + "allowedLinks.txt" open(linksFile, 'w+').close() open(zplFile, 'w+').close() open(solutionFile, 'w+').close() # Argument parser. To support command line flags parser = argparse.ArgumentParser(description="Runs the LinksToDAG project") parser.add_argument("-id", "--ID", dest="ID",