Lines 488-503
class _use_dep(object):
Link Here
|
488 |
|
488 |
|
489 |
return _use_dep(tokens) |
489 |
return _use_dep(tokens) |
490 |
|
490 |
|
491 |
class Atom(object): |
491 |
class Atom(str): |
492 |
|
492 |
|
493 |
""" |
493 |
""" |
494 |
For compatibility with existing atom string manipulation code, this |
494 |
For compatibility with existing atom string manipulation code, this |
495 |
class emulates most of the str methods that are useful with atoms. |
495 |
class emulates most of the str methods that are useful with atoms. |
496 |
""" |
496 |
""" |
497 |
|
497 |
|
498 |
__slots__ = ("__weakref__", "blocker", "cp", "cpv", "operator", |
|
|
499 |
"slot", "use", "without_use", "_str",) |
500 |
|
501 |
class _blocker(object): |
498 |
class _blocker(object): |
502 |
__slots__ = ("overlap",) |
499 |
__slots__ = ("overlap",) |
503 |
|
500 |
|
Lines 510-519
class Atom(object):
Link Here
|
510 |
def __init__(self, forbid_overlap=False): |
507 |
def __init__(self, forbid_overlap=False): |
511 |
self.overlap = self._overlap(forbid=forbid_overlap) |
508 |
self.overlap = self._overlap(forbid=forbid_overlap) |
512 |
|
509 |
|
513 |
def __init__(self, mypkg): |
510 |
def __new__(cls, s): |
514 |
s = mypkg = str(mypkg) |
511 |
self = str.__new__(cls, s) |
515 |
obj_setattr = object.__setattr__ |
|
|
516 |
obj_setattr(self, '_str', s) |
517 |
|
512 |
|
518 |
if "!" == s[:1]: |
513 |
if "!" == s[:1]: |
519 |
blocker = self._blocker(forbid_overlap=("!" == s[1:2])) |
514 |
blocker = self._blocker(forbid_overlap=("!" == s[1:2])) |
Lines 523-532
class Atom(object):
Link Here
|
523 |
s = s[1:] |
518 |
s = s[1:] |
524 |
else: |
519 |
else: |
525 |
blocker = False |
520 |
blocker = False |
526 |
obj_setattr(self, "blocker", blocker) |
521 |
self.__dict__['blocker'] = blocker |
527 |
m = _atom_re.match(s) |
522 |
m = _atom_re.match(s) |
528 |
if m is None: |
523 |
if m is None: |
529 |
raise InvalidAtom(mypkg) |
524 |
raise InvalidAtom(self) |
530 |
|
525 |
|
531 |
if m.group('op') is not None: |
526 |
if m.group('op') is not None: |
532 |
base = _atom_re.groupindex['op'] |
527 |
base = _atom_re.groupindex['op'] |
Lines 534-558
class Atom(object):
Link Here
|
534 |
cpv = m.group(base + 2) |
529 |
cpv = m.group(base + 2) |
535 |
cp = m.group(base + 3) |
530 |
cp = m.group(base + 3) |
536 |
if m.group(base + 4) is not None: |
531 |
if m.group(base + 4) is not None: |
537 |
raise InvalidAtom(mypkg) |
532 |
raise InvalidAtom(self) |
538 |
elif m.group('star') is not None: |
533 |
elif m.group('star') is not None: |
539 |
base = _atom_re.groupindex['star'] |
534 |
base = _atom_re.groupindex['star'] |
540 |
op = '=*' |
535 |
op = '=*' |
541 |
cpv = m.group(base + 1) |
536 |
cpv = m.group(base + 1) |
542 |
cp = m.group(base + 2) |
537 |
cp = m.group(base + 2) |
543 |
if m.group(base + 3) is not None: |
538 |
if m.group(base + 3) is not None: |
544 |
raise InvalidAtom(mypkg) |
539 |
raise InvalidAtom(self) |
545 |
elif m.group('simple') is not None: |
540 |
elif m.group('simple') is not None: |
546 |
op = None |
541 |
op = None |
547 |
cpv = cp = m.group(_atom_re.groupindex['simple'] + 1) |
542 |
cpv = cp = m.group(_atom_re.groupindex['simple'] + 1) |
548 |
if m.group(_atom_re.groupindex['simple'] + 2) is not None: |
543 |
if m.group(_atom_re.groupindex['simple'] + 2) is not None: |
549 |
raise InvalidAtom(mypkg) |
544 |
raise InvalidAtom(self) |
550 |
else: |
545 |
else: |
551 |
raise AssertionError(_("required group not found in atom: '%s'") % s) |
546 |
raise AssertionError(_("required group not found in atom: '%s'") % self) |
552 |
obj_setattr(self, "cp", cp) |
547 |
self.__dict__['cp'] = cp |
553 |
obj_setattr(self, "cpv", cpv) |
548 |
self.__dict__['cpv'] = cpv |
554 |
obj_setattr(self, "slot", m.group(_atom_re.groups - 1)) |
549 |
self.__dict__['slot'] = m.group(_atom_re.groups - 1) |
555 |
obj_setattr(self, "operator", op) |
550 |
self.__dict__['operator'] = op |
556 |
|
551 |
|
557 |
use_str = m.group(_atom_re.groups) |
552 |
use_str = m.group(_atom_re.groups) |
558 |
if use_str is not None: |
553 |
if use_str is not None: |
Lines 562-569
class Atom(object):
Link Here
|
562 |
use = None |
557 |
use = None |
563 |
without_use = self |
558 |
without_use = self |
564 |
|
559 |
|
565 |
obj_setattr(self, "use", use) |
560 |
self.__dict__['use'] = use |
566 |
obj_setattr(self, "without_use", without_use) |
561 |
self.__dict__['without_use'] = without_use |
|
|
562 |
return self |
567 |
|
563 |
|
568 |
def __setattr__(self, name, value): |
564 |
def __setattr__(self, name, value): |
569 |
raise AttributeError("Atom instances are immutable", |
565 |
raise AttributeError("Atom instances are immutable", |
Lines 600-664
class Atom(object):
Link Here
|
600 |
|
596 |
|
601 |
return False |
597 |
return False |
602 |
|
598 |
|
603 |
# Implement some common str methods. |
|
|
604 |
|
605 |
def __eq__(self, other): |
606 |
return self._str == other |
607 |
|
608 |
def __getitem__(self, key): |
609 |
return self._str[key] |
610 |
|
611 |
def __hash__(self): |
612 |
return hash(self._str) |
613 |
|
614 |
def __len__(self): |
615 |
return len(self._str) |
616 |
|
617 |
def __lt__(self, other): |
618 |
return self._str < other |
619 |
|
620 |
def __ne__(self, other): |
621 |
return self._str != other |
622 |
|
623 |
def __repr__(self): |
624 |
return repr(self._str) |
625 |
|
626 |
def __str__(self): |
627 |
return self._str |
628 |
|
629 |
def endswith(self, *pargs, **kargs): |
630 |
return self._str.endswith(*pargs, **kargs) |
631 |
|
632 |
def find(self, *pargs, **kargs): |
633 |
return self._str.find(*pargs, **kargs) |
634 |
|
635 |
def index(self, *pargs, **kargs): |
636 |
return self._str.index(*pargs, **kargs) |
637 |
|
638 |
def lstrip(self, *pargs, **kargs): |
639 |
return self._str.lstrip(*pargs, **kargs) |
640 |
|
641 |
def replace(self, *pargs, **kargs): |
642 |
return self._str.replace(*pargs, **kargs) |
643 |
|
644 |
def startswith(self, *pargs, **kargs): |
645 |
return self._str.startswith(*pargs, **kargs) |
646 |
|
647 |
def split(self, *pargs, **kargs): |
648 |
return self._str.split(*pargs, **kargs) |
649 |
|
650 |
def strip(self, *pargs, **kargs): |
651 |
return self._str.strip(*pargs, **kargs) |
652 |
|
653 |
def rindex(self, *pargs, **kargs): |
654 |
return self._str.rindex(*pargs, **kargs) |
655 |
|
656 |
def rfind(self, *pargs, **kargs): |
657 |
return self._str.rfind(*pargs, **kargs) |
658 |
|
659 |
def rstrip(self, *pargs, **kargs): |
660 |
return self._str.rstrip(*pargs, **kargs) |
661 |
|
662 |
def __copy__(self): |
599 |
def __copy__(self): |
663 |
"""Immutable, so returns self.""" |
600 |
"""Immutable, so returns self.""" |
664 |
return self |
601 |
return self |