annotate hgext3rd/topic/common.py @ 6265:8b3cb6cae4a2

readme: add a section for python 2 support as well
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 23 Jun 2022 18:18:55 +0400
parents 945d27da146c
children 213db29a19e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4531
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
1 # Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net>
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
2 #
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
3 # This software may be used and distributed according to the terms of the
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
4 # GNU General Public License version 2 or any later version.
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
5
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
6 def hastopicext(repo):
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
7 """True if the repo use the topic extension"""
1d1f8f56daac topic: introduce a `hastopicext(repo)` function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
8 return getattr(repo, 'hastopicext', False)
6236
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
9
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
10 def parsefqbn(string):
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
11 """parse branch//namespace/topic string into branch, namespace and topic
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
12
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
13 >>> parsefqbn(b'branch//topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
14 ('branch', 'default', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
15 >>> parsefqbn(b'//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
16 ('default', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
17 >>> parsefqbn(b'branch//')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
18 ('branch', 'default', '')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
19 >>> parsefqbn(b'//namespace/')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
20 ('default', 'namespace', '')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
21 >>> parsefqbn(b'/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
22 ('/topic', 'default', '')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
23 >>> parsefqbn(b'//topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
24 ('default', 'default', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
25 >>> parsefqbn(b'branch//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
26 ('branch', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
27 >>> parsefqbn(b'file:///tmp/branch//')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
28 ('file:///tmp/branch', 'default', '')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
29 >>> parsefqbn(b'http://example.com/branch//namespace/topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
30 ('http://example.com/branch', 'namespace', 'topic')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
31 """
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
32 branch, sep, other = string.rpartition(b'//')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
33 if not sep:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
34 # when there's no // anywhere in the string, rpartition returns
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
35 # untouched string as the 3rd element, and the first two are empty
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
36 branch, other = other, b''
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
37 if not branch:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
38 branch = b'default'
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
39 tns, sep, topic = other.partition(b'/')
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
40 if not sep:
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
41 # when there's no / in the rest of the string, there can only be topic
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
42 tns, topic = b'default', tns
7ad8107d953a topic: introduce topic namespaces concept starting with simple parsing
Anton Shestakov <av6@dwimlabs.net>
parents: 4531
diff changeset
43 return branch, tns, topic
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
44
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
45 def formatfqbn(branch=b'', namespace=b'', topic=b''):
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
46 """format branch, namespace and topic into branch//namespace/topic string
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
47
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
48 >>> formatfqbn(branch=b'branch', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
49 'branch//topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
50 >>> formatfqbn(namespace=b'namespace', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
51 '//namespace/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
52 >>> formatfqbn(branch=b'branch')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
53 'branch'
6244
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
54 >>> formatfqbn(branch=b'branch//')
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
55 'branch////'
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
56 >>> formatfqbn(namespace=b'namespace')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
57 '//namespace/'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
58 >>> formatfqbn(branch=b'/topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
59 '/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
60 >>> formatfqbn(topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
61 '//topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
62 >>> formatfqbn(branch=b'branch', namespace=b'namespace', topic=b'topic')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
63 'branch//namespace/topic'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
64 >>> formatfqbn(branch=b'foo/bar', namespace=b'user26', topic=b'feature')
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
65 'foo/bar//user26/feature'
6244
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
66 >>> formatfqbn(branch=b'http://example.com/branch', namespace=b'namespace', topic=b'topic')
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
67 'http://example.com/branch//namespace/topic'
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
68 """
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
69 result = b''
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
70 if branch:
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
71 result += branch
6244
945d27da146c topic: format fqbn correctly from only a branch if it contains //
Anton Shestakov <av6@dwimlabs.net>
parents: 6237
diff changeset
72 if namespace or topic or b'//' in branch:
6237
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
73 result += b'//'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
74 if namespace:
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
75 result += namespace + b'/'
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
76 result += topic
0b9042408809 topic: formatting branch, namespace and topic into fully qualified branch name
Anton Shestakov <av6@dwimlabs.net>
parents: 6236
diff changeset
77 return result