def test_generated_files(self): due_date = datetime.datetime(2011, 10, 24) sale = self._create_sale(1666, due_date=due_date) sale.identifier = 1234 for p in sale.payments: p.identifier = 4321 generator = NFeGenerator(sale, self.store) # If we generate random cnf, the test will always fail _get_random_cnf = NFeIdentification._get_random_cnf NFeIdentification._get_random_cnf = lambda s: 10000001 # Mimic today behavior _get_today_date = NFeGenerator._get_today_date NFeGenerator._get_today_date = lambda s: due_date generator.generate() NFeIdentification._get_random_cnf = _get_random_cnf NFeGenerator._get_today_date = _get_today_date basedir = get_tests_datadir('plugins') expected = os.path.join(basedir, "nfe-expected.txt") output = os.path.join(basedir, "nfe-output.txt") if not os.path.isfile(expected): with open(expected, 'wb') as fp: fp.write(strip_accents(generator._as_txt())) return with open(output, 'wb') as fp: fp.write(strip_accents(generator._as_txt())) # Diff and compare diff = diff_files(expected, output) os.unlink(output) self.failIf(diff, '%s\n%s' % ("Files differ, output:", diff))
def apply_patch(store): store.execute(""" CREATE TABLE product_manufacturer ( id serial NOT NULL PRIMARY KEY, te_created_id bigint UNIQUE REFERENCES transaction_entry(id), te_modified_id bigint UNIQUE REFERENCES transaction_entry(id), name text UNIQUE ); ALTER TABLE product ADD COLUMN manufacturer_id bigint REFERENCES product_manufacturer(id); """) alikes = {} for name, in store.execute("""SELECT DISTINCT(manufacturer) FROM product ORDER BY manufacturer;""").get_all(): if not name or not name.strip(): continue # If a manufacturer with a similar name have already been created, use # that instead. key = strip_accents(name.strip().lower()) if key in alikes: m = alikes[key] else: m = ProductManufacturer(store=store, name=name.strip()) alikes[key] = m store.execute( """ UPDATE product set manufacturer_id = ? WHERE manufacturer = ? """, (m.id, name)) store.execute("""ALTER TABLE product DROP COLUMN manufacturer;""")
def _filter_results(self, text): text = strip_accents(text) query = text.lower() query = query.split() self._reset_results() if not query: # Nothing to look for return for param in self._parameters: group = strip_accents(param.group).lower() desc = strip_accents(param.short_desc).lower() group_matches = all(i in group for i in query) desc_matches = all(i in desc for i in query) if not group_matches and not desc_matches: self.results.remove(param)
def _test_generated_files(self, new_client=None): due_date = datetime.datetime(2011, 10, 24, 0, 0, 0, 0) sale = self._create_sale(1666, due_date=due_date) sale.identifier = 1234 if new_client: sale.client = new_client for p in sale.payments: p.identifier = 4321 generator = NFeGenerator(sale, self.store) # If we generate random cnf, the test will always fail _get_random_cnf = NFeIdentification._get_random_cnf NFeIdentification._get_random_cnf = lambda s: 10000001 # Mimic now_datetime behavior _get_now_datetime = NFeGenerator._get_now_datetime NFeGenerator._get_now_datetime = lambda s: due_date generator.generate() NFeIdentification._get_random_cnf = _get_random_cnf NFeGenerator._get_now_datetime = _get_now_datetime basedir = get_tests_datadir('plugins') if new_client is None: expected = os.path.join(basedir, "nfe-expected.txt") elif isinstance(sale.get_client_role(), Individual): expected = os.path.join(basedir, "individual-nfe-expected.txt") else: expected = os.path.join(basedir, "company-nfe-expected.txt") output = os.path.join(basedir, "nfe-output.txt") if not os.path.isfile(expected): with open(expected, 'wb') as fp: fp.write(strip_accents(generator._as_txt())) return with open(output, 'wb') as fp: fp.write(strip_accents(generator._as_txt())) # Diff and compare diff = diff_files(expected, output) os.unlink(output) self.failIf(diff, '%s\n%s' % ("Files differ, output:", diff))
def _test_generated_files(self, new_client=None): due_date = datetime.datetime(2011, 10, 24, 0, 0, 0, 0) sale = self._create_sale(1666, due_date=due_date) sale.identifier = 1234 if new_client: sale.client = new_client for p in sale.payments: p.identifier = 4321 generator = NFeGenerator(sale, self.store) # If we generate random cnf, the test will always fail _get_random_cnf = NFeIdentification._get_random_cnf NFeIdentification._get_random_cnf = lambda s: 10000001 # Mimic now_datetime behavior _get_now_datetime = NFeGenerator._get_now_datetime NFeGenerator._get_now_datetime = lambda s: due_date generator.generate() NFeIdentification._get_random_cnf = _get_random_cnf NFeGenerator._get_now_datetime = _get_now_datetime basedir = get_tests_datadir('plugins') if new_client is None: expected = os.path.join(basedir, "nfe-expected.txt") elif isinstance(sale.get_client_role(), Individual): expected = os.path.join(basedir, "individual-nfe-expected.txt") else: expected = os.path.join(basedir, "company-nfe-expected.txt") output = os.path.join(basedir, "nfe-output.txt") if not os.path.isfile(expected): with open(expected, 'wb') as fp: fp.write(strip_accents(generator._as_txt()).encode()) return with open(output, 'wb') as fp: fp.write(strip_accents(generator._as_txt()).encode()) # Diff and compare diff = diff_files(expected, output) os.unlink(output) self.assertFalse(diff, '%s\n%s' % ("Files differ, output:", diff))
def _completion_normal_match_func(self, completion, key, iter): model = completion.get_model() if not len(model): return False content = model[iter][COL_TEXT] if content is None: # FIXME: Find out why this happens some times return False # We need to do strip_accents() before lower(), since # lower() on win32 will corrupt the utf-8 encoded string if self.completion_ignore_accents: key = strip_accents(key) content = strip_accents(content) if self.completion_ignore_case: key = key.lower() content = content.lower() return key in content
def __init__(self, temp, models, skip=0, store=None): self.store = store self.models = models self.skip = skip self.temp = temp self.rows = [] for model in models: for i in range(model.quantity): # XXX: glabels is not working with unicode caracters desc = strip_accents(model.description) self.rows.append([model.code, model.barcode[:-1], desc, model.price])
def __init__(self, temp, models, skip=0, store=None): self.store = store self.models = models self.skip = skip self.temp = temp self.rows = [] for model in models: for i in range(model.quantity): # XXX: glabels is not working with unicode caracters desc = strip_accents(model.description) self.rows.append( [model.code, model.barcode[:-1], desc, model.price])
def _parse_row(sellable, columns): data = [] for col in columns: value = kgetattr(sellable, col) if value is None: value = '' elif isinstance(value, str): value = strip_accents(value) data.append(value) return data
def _parse_row(sellable, columns): data = [] for col in columns: value = kgetattr(sellable, col) if value is None: value = '' elif isinstance(value, str): # XXX: glabels is not working with unicode caracters value = strip_accents(value) data.append(value) return data
def _visible_function(self, model, iter, data=None): if not self._entry: return False if not self._key: return False if self._match_function: return self._match_function(self, self._key, iter) value = model[iter][0] if not value: return False entry_text = self._entry.get_text() if self._entry.completion_ignore_case: entry_text = entry_text.lower() value = value.lower() if self._entry.completion_ignore_accents: entry_text = strip_accents(entry_text) value = strip_accents(value) return value.startswith(entry_text)
def _get_key_for_completion(self, key): if key == self._last_key: return self._fixed_key self._last_key = key # We need to do strip_accents() before lower(), since # lower() on win32 will corrupt the utf-8 encoded string if self.completion_ignore_accents: key = strip_accents(key) if self.completion_ignore_case: key = key.lower() self._fixed_key = key return self._fixed_key
def _get_content_for_completion(self, model, iter): content = model[iter][COL_TEXT] hit = self._cache.get(content) if hit or content is None: return hit fixed = content # We need to do strip_accents() before lower(), since # lower() on win32 will corrupt the utf-8 encoded string if self.completion_ignore_accents: fixed = strip_accents(fixed) if self.completion_ignore_case: fixed = fixed.lower() self._cache[content] = fixed return fixed
def append_tag(self, tag, value, mandatory=True, cdata=False): if value in [None, ''] and not mandatory: # If the tag is not mandatory and the value is empty, # dont add the tag to the xml. return if cdata and value is not None: value = etree.CDATA(unicode(value)) elif value is not None: value = escape(strip_accents(unicode(value).strip())) if hasattr(self, 'NAMESPACE'): tag = etree.SubElement(self, '{%s}%s' % (self.NAMESPACE, tag)) else: tag = etree.SubElement(self, tag) tag.text = value
def append_tag(self, tag, value, mandatory=True, cdata=False): if value in [None, ''] and not mandatory: # If the tag is not mandatory and the value is empty, # dont add the tag to the xml. return if cdata and value is not None: value = etree.CDATA(str(value)) elif value is not None: value = escape(strip_accents(str(value).strip())) if hasattr(self, 'NAMESPACE'): tag = etree.SubElement(self, '{%s}%s' % (self.NAMESPACE, tag)) else: tag = etree.SubElement(self, tag) tag.text = value
def testStripAccents(self): for string, string_without_accentuation in [ # normal strings ('áâãäåāăąàÁÂÃÄÅĀĂĄÀ', 'aaaaaaaaaAAAAAAAAA'), ('èééêëēĕėęěĒĔĖĘĚ', 'eeeeeeeeeeEEEEE'), ('ìíîïìĩīĭÌÍÎÏÌĨĪĬ', 'iiiiiiiiIIIIIIII'), ('óôõöōŏőÒÓÔÕÖŌŎŐ', 'oooooooOOOOOOOO'), ('ùúûüũūŭůÙÚÛÜŨŪŬŮ', 'uuuuuuuuUUUUUUUU'), ('çÇ', 'cC'), # unicode strings (u'áâãäåāăąàÁÂÃÄÅĀĂĄÀ', u'aaaaaaaaaAAAAAAAAA'), (u'èééêëēĕėęěĒĔĖĘĚ', u'eeeeeeeeeeEEEEE'), (u'ìíîïìĩīĭÌÍÎÏÌĨĪĬ', u'iiiiiiiiIIIIIIII'), (u'óôõöōŏőÒÓÔÕÖŌŎŐ', u'oooooooOOOOOOOO'), (u'ùúûüũūŭůÙÚÛÜŨŪŬŮ', u'uuuuuuuuUUUUUUUU'), (u'çÇ', u'cC'), ]: self.assertEqual(strip_accents(string), string_without_accentuation)
def testStripAccents(self): for string, string_without_accentuation in [ # bytes ('áâãäåāăąàÁÂÃÄÅĀĂĄÀ'.encode(), b'aaaaaaaaaAAAAAAAAA'), ('èééêëēĕėęěĒĔĖĘĚ'.encode(), b'eeeeeeeeeeEEEEE'), ('ìíîïìĩīĭÌÍÎÏÌĨĪĬ'.encode(), b'iiiiiiiiIIIIIIII'), ('óôõöōŏőÒÓÔÕÖŌŎŐ'.encode(), b'oooooooOOOOOOOO'), ('ùúûüũūŭůÙÚÛÜŨŪŬŮ'.encode(), b'uuuuuuuuUUUUUUUU'), ('çÇ'.encode(), b'cC'), # strings ('áâãäåāăąàÁÂÃÄÅĀĂĄÀ', 'aaaaaaaaaAAAAAAAAA'), ('èééêëēĕėęěĒĔĖĘĚ', 'eeeeeeeeeeEEEEE'), ('ìíîïìĩīĭÌÍÎÏÌĨĪĬ', 'iiiiiiiiIIIIIIII'), ('óôõöōŏőÒÓÔÕÖŌŎŐ', 'oooooooOOOOOOOO'), ('ùúûüũūŭůÙÚÛÜŨŪŬŮ', 'uuuuuuuuUUUUUUUU'), ('çÇ', 'cC'), ]: self.assertEqual(strip_accents(string), string_without_accentuation)
def as_string(self): """Formats this field to its string representation""" value = self.get_value() # cnab fields are always None if self.name not in ['cnab', '_']: assert value is not None, self.name if self.type is str: value = strip_accents(str(value or '')).ljust(self.size)[:self.size] elif self.type is int: value = str(value or 0).rjust(self.size, '0') assert len(value) == self.size, (self.name, value, len(value), self.size) elif self.type is Decimal: value = value or 0 value = str(int(value * (10 ** self.decimals))) value = str(value).rjust(self.size, '0') assert len(value) == self.size, (value, len(value), self.size) return value or ''
def generate_filizola_file(store): content = [] content.append(diversos) for sellable in store.find(Sellable): try: # We can only send sellables whose code can be converted to integer code = int(sellable.code) except ValueError: continue if sellable.unit_description == 'Kg': unit = 'p' # peso else: unit = 'u' # unidade content.append("%06d%s%-22s%07d%03d%126s%04d" % ( code, unit, str(strip_accents(sellable.description))[:22], int(sellable.price * 100), 0, '', 0, )) if platform.system() == 'Windows': dest_dir = os.path.join('C:\\', 'Filizola') else: # The software filizola provides is for windows, so on linux, it will # probably be running through wine (even though it didn't work properly # under wine). dest_dir = os.path.join('~', '.wine', 'drive_c', 'Filizola') dest_dir = os.path.expanduser(dest_dir) if not os.path.exists(dest_dir): os.makedirs(dest_dir) dest = os.path.join(dest_dir, 'CADTXT.TXT') with open(dest, 'w') as fh: fh.write('\n'.join(content)) return dest
def __init__(self, temp, models, skip=0, store=None): self.store = store self.models = models self.skip = skip self.temp = temp self.rows = [] columns = sysparam.get_string('LABEL_COLUMNS') columns = columns.split(',') for model in models: for i in range(int(model.quantity)): if columns: from stoqlib.domain.sellable import Sellable if not isinstance(model, Sellable): model = model.sellable self.rows.append(_parse_row(model, columns)) else: # XXX: glabels is not working with unicode caracters desc = strip_accents(model.description) self.rows.append( [model.code, model.barcode, desc, model.price])
def apply_patch(store): store.execute( """ CREATE TABLE product_manufacturer ( id serial NOT NULL PRIMARY KEY, te_created_id bigint UNIQUE REFERENCES transaction_entry(id), te_modified_id bigint UNIQUE REFERENCES transaction_entry(id), name text UNIQUE ); ALTER TABLE product ADD COLUMN manufacturer_id bigint REFERENCES product_manufacturer(id); """ ) alikes = {} for (name,) in store.execute( """SELECT DISTINCT(manufacturer) FROM product ORDER BY manufacturer;""" ).get_all(): if not name or not name.strip(): continue # If a manufacturer with a similar name have already been created, use # that instead. key = strip_accents(name.strip().lower()) if key in alikes: m = alikes[key] else: m = ProductManufacturer(store=store, name=name.strip()) alikes[key] = m store.execute( """ UPDATE product set manufacturer_id = ? WHERE manufacturer = ? """, (m.id, name), ) store.execute("""ALTER TABLE product DROP COLUMN manufacturer;""")