class Config(DotSection): """Validation and encapsulation for the DXR config file Examples:: # Settings from the [DXR] section: >>> Config(...).default_tree # Settings from individual trees: >>> Config(...).trees['some-tree'].build_command # Settings from plugin-specific sections of trees: >>> Config(...).trees['some-tree'].buglink.url """ # Design decisions: # * Keep the explicit [DXR] section name because, otherwise, global options # can collide with tree names. # * Keep tree.config shortcut. It makes a lot of things shorter and doesn't # hurt anything, since it's all read-only anyway. # * Crosswire __getattr__ with __getitem__. It makes callers more readable. # * Keep whitespace-delimited lists in config file. I prefer them to # commas, people will have to change less, and it wasn't a big deal to # customize. # * Use configobj to parse the ini file and then schema to validate it. # configobj's favored "validate" module makes it very hard to have # whitespace-delimited lists and doesn't compose well since its # validators are strings. The downside is we only report 1 error at a # time, but this can and should be fixed in the schema lib. def __init__(self, input, relative_to=None): """Pull in and validate a config file. :arg input: A string or dict from which to populate the config :arg relative_to: The dir relative to which to interpret relative paths Raise ConfigError if the configuration is invalid. """ schema = Schema({ 'DXR': { Optional('temp_folder', default=abspath('dxr-temp-{tree}')): AbsPath, Optional('default_tree', default=None): basestring, Optional('disabled_plugins', default=plugin_list('')): Plugins, Optional('enabled_plugins', default=plugin_list('*')): Plugins, Optional('generated_date', default=datetime.utcnow() .strftime("%a, %d %b %Y %H:%M:%S +0000")): basestring, Optional('log_folder', default=abspath('dxr-logs-{tree}')): AbsPath, Optional('workers', default=if_raises(NotImplementedError, cpu_count, 1)): WORKERS_VALIDATOR, Optional('skip_stages', default=[]): WhitespaceList, Optional('www_root', default=''): Use(lambda v: v.rstrip('/')), Optional('google_analytics_key', default=''): basestring, Optional('es_hosts', default='http://127.0.0.1:9200/'): WhitespaceList, # A semi-random name, having the tree name and format version in it. Optional('es_index', default='dxr_{format}_{tree}_{unique}'): basestring, Optional('es_alias', default='dxr_{format}_{tree}'): basestring, Optional('es_catalog_index', default='dxr_catalog'): basestring, Optional('es_catalog_replicas', default=1): Use(int, error='"es_catalog_replicas" must be an integer.'), Optional('max_thumbnail_size', default=20000): And(Use(int), lambda v: v >= 0, error='"max_thumbnail_size" must be a non-negative ' 'integer.'), Optional('es_indexing_timeout', default=60): And(Use(int), lambda v: v >= 0, error='"es_indexing_timeout" must be a non-negative ' 'integer.'), Optional('es_indexing_retries', default=0): And(Use(int), lambda v: v >= 0, error='"es_indexing_retries" must be a non-negative ' 'integer.'), Optional('es_refresh_interval', default=60): Use(int, error='"es_refresh_interval" must be an integer.') }, basestring: dict }) # Parse the ini into nested dicts: config_obj = ConfigObj(input.splitlines() if isinstance(input, basestring) else input, list_values=False) if not relative_to: relative_to = getcwd() with cd(relative_to): try: config = schema.validate(config_obj.dict()) except SchemaError as exc: raise ConfigError(exc.code, ['DXR']) self._section = config['DXR'] # Normalize enabled_plugins: if self.enabled_plugins.is_all: # Then explicitly enable anything that isn't explicitly # disabled: self._section['enabled_plugins'] = [ p for p in all_plugins_but_core().values() if p not in self.disabled_plugins] # Now that enabled_plugins and the other keys that TreeConfig # depends on are filled out, make some TreeConfigs: self.trees = OrderedDict() # name -> TreeConfig for section in config_obj.sections: if section != 'DXR': try: self.trees[section] = TreeConfig(section, config[section], config_obj[section].sections, self) except SchemaError as exc: raise ConfigError(exc.code, [section]) # Make sure default_tree is defined: if not self.default_tree: self._section['default_tree'] = first(self.trees.iterkeys()) # These aren't intended for actual use; they're just to influence # enabled_plugins of trees, and now we're done with them: del self._section['enabled_plugins'] del self._section['disabled_plugins']
class HybridView(newcls, JSONResponseMixin): """Middleware class which add the JSONResponseMixin in the view to handle ajax requests""" def __init__(self, *args, **kwargs): """Our newcls can be an instance of several mixins working with the get and post functions. e.g : CreateView is instance of BaseCreateView and ProcessFormView Let's add our custom mixins that implement get and post without returning a render_to_response, and call """ newcls.__init__(self, **kwargs) # The order matters for the get/post calls. self.mixins = OrderedDict() self.mixins[BaseListView] = BaseListViewMixin self.mixins[BaseCreateView] = BaseCreateViewMixin self.mixins[BaseUpdateView] = BaseUpdateViewMixin self.mixins[BaseDetailView] = BaseDetailViewMixin self.mixins[BaseDeleteView] = BaseDeleteViewMixin self.mixins[ProcessFormView] = ProcessFormViewMixin [self.mixins.pop(baseView) for baseView in self.mixins.iterkeys() if not isinstance(self, baseView)] @property def is_ajax(self): """Check the META HTTP_X_REQUESTED_WITH and CONTENT_TYPE""" meta = self.request.META return meta.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'\ or "json" in meta.get("CONTENT_TYPE") def render_to_response(self, context): cls = type(self).__bases__[self.is_ajax] return cls.render_to_response(self, context) def get(self, request, **kwargs): if not self.is_ajax: "If it's not ajax, return the inherited get" return super(HybridView, self).get(self, **kwargs) for mixin in self.mixins.itervalues(): self, kwargs = mixin().get(self, **kwargs) context = kwargs context.update(self.get_json_context(**kwargs)) context.pop("form", None) context.pop("object", None) context.pop("object_list", None) return self.render_to_response(context) def post(self, request, **kwargs): """Hybrid post to handle all parents post actions""" if not self.is_ajax: "If it's not ajax, return the inherited get" return super(HybridView, self).post(self, **kwargs) for mixin in self.mixins.itervalues(): try: self, kwargs = mixin().post(self, **kwargs) except AttributeError: """The inherited view doesn't handle post""" pass context = kwargs context.update(self.get_json_context(**kwargs)) context.pop("form", None) return self.render_to_response(context)
class Config(DotSection): """Validation and encapsulation for the DXR config file Examples:: # Settings from the [DXR] section: >>> Config(...).default_tree # Settings from individual trees: >>> Config(...).trees['some-tree'].build_command # Settings from plugin-specific sections of trees: >>> Config(...).trees['some-tree'].buglink.url """ # Design decisions: # * Keep the explicit [DXR] section name because, otherwise, global options # can collide with tree names. # * Keep tree.config shortcut. It makes a lot of things shorter and doesn't # hurt anything, since it's all read-only anyway. # * Crosswire __getattr__ with __getitem__. It makes callers more readable. # * Keep whitespace-delimited lists in config file. I prefer them to # commas, people will have to change less, and it wasn't a big deal to # customize. # * Use configobj to parse the ini file and then schema to validate it. # configobj's favored "validate" module makes it very hard to have # whitespace-delimited lists and doesn't compose well since its # validators are strings. The downside is we only report 1 error at a # time, but this can and should be fixed in the schema lib. def __init__(self, input, relative_to=None): """Pull in and validate a config file. :arg input: A string or dict from which to populate the config :arg relative_to: The dir relative to which to interpret relative paths Raise ConfigError if the configuration is invalid. """ schema = Schema({ 'DXR': { Optional('temp_folder', default=abspath('dxr-temp-{tree}')): AbsPath, Optional('default_tree', default=None): basestring, Optional('disabled_plugins', default=plugin_list('')): Plugins, Optional('enabled_plugins', default=plugin_list('*')): Plugins, Optional('generated_date', default=datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000")): basestring, Optional('log_folder', default=abspath('dxr-logs-{tree}')): AbsPath, Optional('workers', default=if_raises(NotImplementedError, cpu_count, 1)): And(Use(int), lambda v: v >= 0, error='"workers" must be a non-negative integer.'), Optional('skip_stages', default=[]): WhitespaceList, Optional('www_root', default=''): Use(lambda v: v.rstrip('/')), Optional('google_analytics_key', default=''): basestring, Optional('es_hosts', default='http://127.0.0.1:9200/'): WhitespaceList, # A semi-random name, having the tree name and format version in it. Optional('es_index', default='dxr_{format}_{tree}_{unique}'): basestring, Optional('es_alias', default='dxr_{format}_{tree}'): basestring, Optional('es_catalog_index', default='dxr_catalog'): basestring, Optional('es_catalog_replicas', default=1): Use(int, error='"es_catalog_replicas" must be an integer.'), Optional('max_thumbnail_size', default=20000): And(Use(int), lambda v: v >= 0, error='"max_thumbnail_size" must be a non-negative ' 'integer.'), Optional('es_indexing_timeout', default=60): And(Use(int), lambda v: v >= 0, error='"es_indexing_timeout" must be a non-negative ' 'integer.'), Optional('es_indexing_retries', default=0): And(Use(int), lambda v: v >= 0, error='"es_indexing_retries" must be a non-negative ' 'integer.'), Optional('es_refresh_interval', default=60): Use(int, error='"es_refresh_interval" must be an integer.') }, basestring: dict }) # Parse the ini into nested dicts: config_obj = ConfigObj( input.splitlines() if isinstance(input, basestring) else input, list_values=False) if not relative_to: relative_to = getcwd() with cd(relative_to): try: config = schema.validate(config_obj.dict()) except SchemaError as exc: raise ConfigError(exc.code, ['DXR']) self._section = config['DXR'] # Normalize enabled_plugins: if self.enabled_plugins.is_all: # Then explicitly enable anything that isn't explicitly # disabled: self._section['enabled_plugins'] = [ p for p in all_plugins_but_core().values() if p not in self.disabled_plugins ] # Now that enabled_plugins and the other keys that TreeConfig # depends on are filled out, make some TreeConfigs: self.trees = OrderedDict() # name -> TreeConfig for section in config_obj.sections: if section != 'DXR': try: self.trees[section] = TreeConfig( section, config[section], config_obj[section].sections, self) except SchemaError as exc: raise ConfigError(exc.code, [section]) # Make sure default_tree is defined: if not self.default_tree: self._section['default_tree'] = first(self.trees.iterkeys()) # These aren't intended for actual use; they're just to influence # enabled_plugins of trees, and now we're done with them: del self._section['enabled_plugins'] del self._section['disabled_plugins']
while line != '': # mientras la linea no sea la "última", o sea, el fin del archivo. if line[:2] == '//': # si la nueva linea es un separador de PTMs "//" hacer un INSERT # output.write(str(record.items())) sql_insert_values = '\'' + '\', \''.join(record.itervalues()) + '\'' # unir los elementos devalues con comas tgs = (((sql_insert_values.replace("'", "").replace(".", "")).split(", "))[3]) tgs = tgs.split("-") #print(len(tgs)) #output.write(("INSERT INTO ptm_table VALUES (%r);" # % sql_insert_values + '\n').replace("\"", '').replace('.', '')) cur.execute(("INSERT INTO ptm_table VALUES (%r);" % sql_insert_values + '\n').replace("\"", '').replace('.', '')) con.commit() # con esto logro que se graben los inserts, sino no anda... pero lo hace re lenteja! record = empty_record.copy() line = ptmlist.readline() # y cambiar de linea. for cat in categories.iterkeys(): # toma cada elemento de categoria (en orden) if cat == "TG" and cat == line[:2]: record[cat] = line[5:-1] line = ptmlist.readline() elif line[:2] == cat: # y si la linea corresponde a la categoria record[cat] = line[5:-1].replace("'", "''") # agrega su contenido al registro para esa categoria line = ptmlist.readline() # y cambia a una nueva linea while line[:2] == cat: # mientras la linea nueva sea de la misma id que la anterior record[cat] += ' --- ' + line[5:-1] # agrega su contenido con un separador line = ptmlist.readline() # y cambia a una nueva linea # si la linea está vacía, es porque llegó al final del archivo y el while termina #Cerrar el kiosko cur.execute("SHOW WARNINGS") print(cur.fetchall()) if con: